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

I know that in Java int can get the value 2,147,483,647. But I want more value than that. I have a formula, for example:

double x = a/(b*c); 

Hence, the denominator (b * c) can reach 10 ^ 10 or, perhaps, even higher than this. But whenever I execute the formula, the value is always limited to 2,147,483,647. I know, because x should always be less than 1.0. P / S: Even the variable "a" can also reach 10 ^ 10 if certain conditions are met. a, b, c are all integers.

+4
source share
4 answers

Since you asked for it, here is a BigInteger example:

 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 number (without spaces or commas). For example BigInteger z = new BigIntger("-3463634"); NB: BigInteger will actually return long for you if your number is in its range. longs end in L , as in:

 long num = 372036854775807L; 

The maximum length for a long is: 9,223,372,036,854,775,807. If your numbers are less than this, then your life will become easier to use long or long , its wrapper, on top of BigInteger . Since with long you do not need to use methods for division / multiplication, etc.

+5
source

The value of max int is a java language constant. If you want real numbers to be larger than int , use long . If you need integers greater than int and long , 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 immutable just like long and Integer , but you cannot use normal operator characters. Instead, use the methods provided by the class.

I also notice that you end up with double . If doubling can be used in your formula, it is worth noting that their maximum value is: 1.7976931348623157 E 308

Learn more about java language constants .

+3
source

Use the long type, still an integer type, but its maximum value is higher than int

+2
source

try it

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

a and c will be automatically reset using java. See http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 If one of the operands is of type double, the other is converted to double.

0
source

Source: https://habr.com/ru/post/1489195/


All Articles