I know that in Java an int can get a value of 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. But whenever I execute the formula, the value is always limited to 2,147,483,647. I know because x must always be smaller than 1.0. P/S: Even variable "a" can also reach 10^10 if some conditions are satisfied. a,b,c are all integer numbers.
(Arithmetic) Integer Overflows An integer overflow occurs when you attempt to store inside an integer variable a value that is larger than the maximum value the variable can hold. The C standard defines this situation as undefined behavior (meaning that anything might happen).
If the first bit is 1 and all the other 31 bits are 0 then the number will be the maximum negative number . Integer. MAX_VALUE represents the maximum positive integer value that can be represented in 32 bits (i.e., 2147483647 ).
If you need to hold an integer larger than the Integer data type can hold, you can use the Long data type instead. Long variables can hold numbers from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Operations with Long are slightly slower than with Integer .
The number 2,147,483,647 (or hexadecimal 7FFFFFFF16) is the maximum positive value for a 32-bit signed binary integer in computing. It is therefore the maximum value for variables declared as integers (e.g., as int ) in many programming languages.
Since you asked for it, here's 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 numbers only (no whitespace or commas). For example BigInteger z = new BigIntger("-3463634");
NB: BigInteger
will actually return a long
for you if your number is in its range. longs
end in L
, as in:
long num = 372036854775807L;
The max length for a long
is: 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
. Since with long
, you don't have to use methods for dividing/multiplying, etc.
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
Read more about java language constants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With