What are the limits of BigDecimal and BigInteger? [duplicate] - java

This question already has answers here:
Is there an upper bound to BigInteger? [duplicate]
(3 answers)
What does BigInteger having no limit mean?
(4 answers)
Closed 9 years ago.
I was multiplying very two huge BigIntegervalues in a program. It failed. What are the limits of BigInteger and BigDecimal ?

You won't get NumberFormatException multiplying large numbers. If the number produced is too large, you will get a cryptic NegativeArraySizeException as the size of the array overflows.
You are more likely to get an out of memory error.
The limit is 32 * 2^32-1 bits for BigInteger or about 2^(4 billion).
You can get a NumberFormatException if you
create a BigInteger from an empty byte[]
use a signum < -1 or > +1
try to parse a number in base >36 or < 2
have a string with illegal digits.
When you get an exception you should also look at the message and the stack trace as this usually gives you the real cause.

there shouldn't be a limit, except for memory, but maybe there is, according to the implementation of the class (for example, some fields there might be int or long).

Related

Does Java support array size more than 2^31 -1? [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Do Java arrays have a maximum size?
(10 answers)
Closed last month.
I was trying to create a int[] type array in Java with a user input based length. Below is the code for the same :
int[] change = new int[max+1];
When the value of max = 2147483647, the compiler shows the below error code :
type here
java.lang.NegativeArraySizeException: -2147483648
I know that the upper limit for int in Java is -2147483648 to 2147483647 (-231 to 231-1), but why is the size of array going negative with a addition (is it looping back to the lower limit?).
The problem remains the same with long[] type array. So, does Java not support array size above 231-1 ?

integer overflow in java [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Closed 3 years ago.
I though that the following pieces of code does not compile. However after running it I got unexpected result, I don't understand how it is printing -2 ? can you explain how addition is done here?
int x = 2147483647+2147483647; // it compiles
System.out.print(x); // prints -2
any explain is welcome
Short answer: When Java integers reach their maximum value plus one, they start at their minimum again. It's like going in a circle.
It's like that due to the technical representation of integers as bits. Imagine having 3 bits available to represent a number. You could have the number 111. If you add 1 to it, you'll end up at 1000 but since you only have 3 bits available, it cuts off the first and you end up with 000 which is why you're at the minimum value again after adding 1 to the maximum.

I am having different value from compiler and calculator [duplicate]

This question already has answers here:
Best way to make Java's modulus behave like it should with negative numbers?
(6 answers)
Closed 5 years ago.
I was writing a code (I knew the output,In Java) , after compiling it giving the input i got a weird value.
Code Snippet -
long a,b,c;
a=-245436499;
b=992;
c=(a+b);
System.out.print(c%b);
And the Output it gave me was
-819
But when i calculated it on calculator, it was
173 \\why?
Proof:
Calculators output
Compiler's output
There are two ways of calculating modulo of a negative number:
Java (as per the JLS) uses the "preserve sign" method; ie if the first operand is negative, non-zero results are negative
Results are (made) positive by adding (if necessary) the second operand to the result of method 1
Your calculator clearly uses method 2 (-819 + 992 = 173)

A value over size of integer [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Closed 5 years ago.
I'm a Vietnamese so my English's not good, please sympathize me. Thanks in advance.
I have a question.
I input a integer value but if it would been out of the size of int.
Ex: I input a = 1323544875154846543513521
So how to catch the error?
I must input in and then check the value?
Simple: don't use int, use Use BigInteger.
You might also wanna look other JVM languages that have number auto-casting, that is, automatically changing the number type according to the value at runtime (e.g. Clojure).
I input a integer value but if it would been out of the size of int.
Ex: I input a = 1323544875154846543513521 So how to catch the error?
System.out.println(Integer.MAX_VALUE+1); // anymore than the max will wrap around, value printed = -2147483648
System.out.println(Integer.MIN_VALUE-1); // if less than the min it will also wrap around, value printed = 2147483647
you could use Math.toIntExact(long value) if you want to receive an exception on overflow/underflow.
Alternatively, you could use BigInteger or BigDecimal which have no limit in size (your RAM is the limit).

Java Long primitive type maximum limit [duplicate]

This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Closed 9 years ago.
I am using the Long primitive type which increments by 1 whenever my 'generateNumber'method called. What happens if Long reaches to his maximum limit? will throw any exception or will reset to minimum value?
here is my sample code:
class LongTest {
private static long increment;
public static long generateNumber(){
++increment;
return increment;
}
}
Long.MAX_VALUE is 9,223,372,036,854,775,807.
If you were executing your function once per nanosecond, it would still take over 292 years to encounter this situation according to this source.
When that happens, it'll just wrap around to Long.MIN_VALUE, or -9,223,372,036,854,775,808 as others have said.
It will overflow and wrap around to Long.MIN_VALUE.
Its not too likely though. Even if you increment 1,000,000 times per second it will take about 300,000 years to overflow.
Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
It will start from -9,223,372,036,854,775,808
Long.MIN_VALUE.
Exceding the maximum value of a long doesnt throw an exception, instead it cicles back. If you do this:
Long.MAX_VALUE + 1
you will notice that the result is the equivalent to Long.MIN_VALUE.
From here: java number exceeds long.max_value - how to detect?

Categories