Tuesday, September 17, 2013

Java Math.min() Error on Integer.Max_Value

Java Math.min() Error on Integer.Max_Value

This code, which should return the smaller of the two numbers, returns a
negative number similar to the greater number:
Math.min(15, 21474836477) --> returns -2147483642
I suspected this had something to do with the range of int's, so I changed
the values to long and the program worked fine.
I don't quite understand the seemingly random number it returns- why is it
-2147483643 and not the actual number I put in, -21474836477? Is the
difference caused by the amount it overflowed, or is it influenced by the
other parameter of Math.min in some way?

No comments:

Post a Comment