简体   繁体   English

如何获得大于最大整数值的整数值

[英]How to get an integer value that is greater than the maximum integer value

I know that in Java an int can get a value of 2,147,483,647. 我知道在Java中int可以得到2,147,483,647。 But I want more value than that. 但是我想要更多的价值。 I have a formula for example: 我有一个公式,例如:

double x = a/(b*c); 

So the denominator (b*c) can reach to 10^10 or maybe even higher than that. 因此分母(b * c)可以达到10 ^ 10甚至更​​高。 But whenever I execute the formula, the value is always limited to 2,147,483,647. 但是无论何时执行公式,该值始终限制为2,147,483,647。 I know because x must always be smaller than 1.0. 我知道,因为x必须始终小于1.0。 P/S: Even variable "a" can also reach 10^10 if some conditions are satisfied. P / S:如果满足某些条件,即使变量“ a”也可以达到10 ^ 10。 a,b,c are all integer numbers. a,b,c都是整数。

Since you asked for it, here's a BigInteger example: 自您提出要求以来,这里有一个BigInteger示例:

BigInteger a = new BigInteger("yourNumberInStringFormA");
BigInteger b = new BigInteger("yourNumberInStringFormB");
BigInteger c = new BigInteger("yourNumberInStringFormC");

BigInteger x = a.divide(b.multiply(c));

Where "yourNumberInStringForm" is an optional minus sign and numbers only (no whitespace or commas). 其中"yourNumberInStringForm"是可选的减号和仅数字(无空格或逗号)。 For example BigInteger z = new BigIntger("-3463634"); 例如, BigInteger z = new BigIntger("-3463634"); NB: BigInteger will actually return a long for you if your number is in its range. 注意:如果您的数字在其范围内,则BigInteger实际上会为您返回一个long longs end in L , as in: longsL ,如:

long num = 372036854775807L;

The max length for a long is: 9,223,372,036,854,775,807. 一个最大长度long是:9,223,372,036,854,775,807。 If your numbers are going to be less than that, it'll make your life a ton easier to use long or Long , its wrapper, over BigInteger . 如果您的数字要少于这个数字,那么使用BigInteger longLong可以使您的生活更加轻松。 Since with long , you don't have to use methods for dividing/multiplying, etc. long ,您不必使用除法/乘法等方法。

The max int value is a java language constant. max int值是Java语言常数。 If you want real numbers larger than what int provides, use long . 如果要使实数大于int提供的实数,请使用long If you need integers larger than what int and long provide, you can use BigInteger : 如果需要大于intlong提供的整数,则可以使用BigInteger

BigInteger a = new BigInteger("9");
BigInteger b = new BigInteger("3");
BigInteger c = a.add(b); // c.equals(new BigInteger("12"), a and b are unchanged

BigInteger is immutable just like Long and Integer , but you can't use the usual operator symbols. 就像LongIntegerBigInteger是不可变的,但是您不能使用通常的运算符。 Instead use the methods provided by the class. 而是使用类提供方法

I also notice you're ending up with a double . 我还注意到您最终获得了double If it's okay to use doubles throughout your formula, it's worth noting that their max value is: 1.7976931348623157 E 308 如果可以在公式中使用双精度数,则值得注意的是它们的最大值是: 1.7976931348623157 E 308

Read more about java language constants . 阅读有关Java语言常量的更多信息

使用long类型,仍然是整数类型,但其最大值大于int

try this 尝试这个

double x = a / ((double) b * c);

a and c will be cast to double by java automatically. java将自动将a和c强制转换为double。 See http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 If either operand is of type double, the other is converted to double. 请参见http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 如果一个操作数的类型为double,则另一个将转换为double。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道float值是否大于Integer.MAX_VALUE? - How to know if the float value is greater than Integer.MAX_VALUE? 对于真正大于整数最大值的巨大数,求解递归关系的最佳方法应该是什么 - What should be the optimal way of solving Recurrence relation for really Huge number greater than Integer maximum value 最大整数值java - Maximum Integer Value java Ruby获得最大整数大小值 - Ruby get maximum integer size value 如何在列表中找到特定键的最大值<TreeMap<Integer,Integer> &gt;? - How to find the Maximum value for specific key in List<TreeMap<Integer,Integer>>? 如果输入的值大于 int 的范围,如何验证请求 bean 中的整数字段? - How do I validate an integer field in a Request bean, if the entered value is greater than the range of an int? 请任何人参考我如何检查哈希图中的布尔值是否大于用户定义的整数 - Please anyone refer me how can i check the boolean value in hashmap is greater than user defined integer 如果实际大小大于 Integer.MAX_VALUE,如何找出 java.util.List 的大小? - How can I find out the size of a java.util.List if the actual size is greater than the Integer.MAX_VALUE? 如何获取和比较地图的价值结果<Integer, Integer> - How to get and compare the value results of a map<Integer, Integer> 如何从哈希表中获取价值<integer,hashtable<string,integer> &gt; 小时</integer,hashtable<string,integer> - How to get value from Hashtable<Integer,Hashtable<String,Integer>> h
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM