Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

int

memory

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.

like image 235
Nexus Avatar asked Jul 02 '13 03:07

Nexus


People also ask

What happens when int exceeds max value?

(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).

How do you find the maximum value of an integer?

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 ).

What's bigger than an integer?

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 .

What is integer max value?

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.


2 Answers

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.

like image 184
Steve P. Avatar answered Sep 23 '22 04:09

Steve P.


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.

like image 42
djeikyb Avatar answered Sep 20 '22 04:09

djeikyb