我知道在 Java 中一个 int 可以得到 2,147,483,647 的值。但我想要更多的价值。例如,我有一个公式:
double x = a/(b*c);
所以分母 (b*c) 可以达到 10^10 甚至更高。但每当我执行公式时,该值始终限制为 2,147,483,647。我知道,因为 x 必须始终小于 1.0。P/S:如果满足某些条件,即使变量“a”也可以达到 10^10。a,b,c 都是整数。
既然你要求它,这里有一个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"
是可选的减号和数字(没有空格或逗号)。例如BigInteger z = new BigIntger("-3463634");
NB:如果您的号码在其范围内, BigInteger
实际上会为您返回 a 。以 结尾,如:long
longs
L
long num = 372036854775807L;
a 的最大长度为long
:9,223,372,036,854,775,807。如果你的数字比这个少,它会让你的生活更容易使用,long
或者Long
它的包装,结束BigInteger
。由于 with long
,您不必使用除法/乘法等方法。
The max int value is a java language constant. If you want real numbers larger than what int
provides, use long. If you need integers larger than what int
and long
provide, you can use 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. Instead use the methods provided by the class.
I also notice you're ending up with a double
. If it's okay to use doubles throughout your formula, it's worth noting that their max value is: 1.7976931348623157 E 308
Use a long type, still an integer type but its maximum value is higher than int
尝试这个
double x = a / ((double) b * c);
a 和 c 将被 java 自动转换为 double。请参阅http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 如果任一操作数是双精度类型,则另一个将转换为双精度。