aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper-Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper-Q" Watch "Wrapper-Q" New topic
Author

Wrapper-Q

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

Q: Why Integer.MAX_VALUE is not returning equal.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Integer.MAX_VALUE has 31 bits of precision plus a sign.

A 32 bit float has 8 bits for the exponent, 23 bits for the fraction, and a sign bit. So a float loses precision (not magnitude) in the conversion from a 31 bit value.

A 64 bit double has 11 bits for the exponent, 52 bits for the fraction, and a sign bit. So double can hold a 31 bit value with no loss of precision.

Comparing an approximate number to an exact number, we get an unequal result.


Mike Gershman
SCJP 1.4, SCWCD in process
Rituparno Pal
Greenhorn

Joined: Oct 10, 2004
Posts: 12
Dear Mike
If your Logic is fitting in this case tehn, why do the same thing is not happening for Long.MAX_VALUE
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Dear Rituparno:

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.

Mike
Rituparno Pal
Greenhorn

Joined: Oct 10, 2004
Posts: 12
Dear Mike
Thanks very much for you reply. That was real beauty to discover the bit n byte stuffs.
Rituparno
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Mike the first post is clear but second one ( long one ) is not clear .
can you put some more light on this .

thanks .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Please run the code I provided, examine the output, then ask me a question and I'll do my best to explain.
Mohammed Al-Qaimari
Greenhorn

Joined: Jan 05, 2005
Posts: 7
I beleive you know that it's better to take 311-055 exam if you have 6-12 months of experience in Tiger.

I've been working with tiger since it's first beta release and I find the exam objectives challanging.
[ January 05, 2005: Message edited by: Mohammed Al-Qaimari ]

There's No Knowledge That Is NOT Power.
 
 
subject: Wrapper-Q