6

I can't understand why this

float f = Integer.MAX_VALUE; System.out.println(Integer.MAX_VALUE); System.out.println((int)f); 

produces the same lines,

As well as why this does

Float f2 = (float) Integer.MAX_VALUE; System.out.println(Integer.MAX_VALUE); System.out.println(f2.intValue()); 

I mean, mantissa length for floating point number is 2^23-1. How does it manage to keep max_value of integer, which is 2^31 - 1?

3
  • BTW Don't use float is you can avoid it. double gives you half a trillion times the accuracy. Some suggest you should use BigDecimal (not my preference however) Commented May 15, 2013 at 6:57
  • Well, this sample is just for the sake of sample. Not a real life code. Commented May 15, 2013 at 7:00
  • True, You can make the same comparison with Long.MAX_VALUE and double. Commented May 15, 2013 at 7:15

1 Answer 1

8

How does it manage to keep max_value of integer, which is 2^31 - 1?

It actually doesn't. The value of f is 2147483648.

However, the narrowing primitive conversion from float to int clamps the value. It gets to this part:

  • Otherwise, one of the following two cases must be true:

    • The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

    • The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

You can see this easily by making the number even bigger:

float f = Integer.MAX_VALUE; f = f * 1000; System.out.println(Integer.MAX_VALUE); // 2147483647 System.out.println((int)f); // 2147483647 

Or by casting to long instead, which obviously doesn't need to be clamped at the same point:

float f = Integer.MAX_VALUE; System.out.println(Integer.MAX_VALUE); // 2147483647 System.out.println((long)f); // 2147483648 
Sign up to request clarification or add additional context in comments.

9 Comments

@dionadar: I like your idea of casting to long instead. Will add that to my answer, if that's okay with you.
Thank you for you answer, but I'm not sure I've understood it. :/ At first, f is 2147483648 on mine system it shows smth like 2.14748365E9 and the next explaination, I really don't get it.
@dhblah: If you print it out as a float it will show as 2.14748365E9 - but it really is exactly 2147483648. Does the casting to long not make this clearer?
Your float represents 1.0 (mantissa is rounded up towards an exact 1) * 2^32 (the exponent is 32, and the implied base is 2). The output 2.14748365E9 is another (rounding) conversion - this time to base 10, your float really is 1*2^32.
If you round 2.147483648E9 to one 9 significant digits (one before the dot, 8 after) you get 2.14748365E9. This is done to prevent irritating output, since your last digit often becomes useless anyway due to rounding in your computations.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.