This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Dear Mike If your Logic is fitting in this case tehn, why do the same thing is not happening for Long.MAX_VALUE
Joined: Mar 13, 2004
Thanks for your excellent question. And I thought young'uns today didn't care about bits and bytes.
Please first run the following enhancement to Vishnu's program and examine the results:
Integer.MAX_VALUE comes out differently when it is first converted to a float and then to a double than when it is converted directly to a double. This is because it must be rounded from 2147483647 to 2147483648 to fit in a float and the rounded value is then converted exactly to a double. However, any int can be exactly converted into a double without rounding. So we are comparing 2147483648 to 2147483647.
Long.MAX_VALUE is rounded from 9223372036854775807 to 9223372036854775808 when converted to either a float or a double. The rounded value converts exactly from a float to a double, without further rounding. So Long.MAX_VALUE comes out the same, whether converted directly to a double or converted to a float and then to a double.
Please feel free to study the bit representations and compare them to whichever write-up on IEEE 754 floating point format you prefer. Or ask another question - someone is always in the Big Moose Saloon.
Joined: Oct 10, 2004
Dear Mike Thanks very much for you reply. That was real beauty to discover the bit n byte stuffs. Rituparno