This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes MIN_VALUE of Float & Double class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "MIN_VALUE of Float & Double class " Watch "MIN_VALUE of Float & Double class " New topic
Author

MIN_VALUE of Float & Double class

shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Is there any specific reason why the MIN_VALUE of Float & Double class are constants holding the smallest positive nonzero value of type float/double and not the negative number.
I would always expect that MIN_VALUE of a class means you cannot have a object having a value less than MIN_VALUE. But was amazed to see the output of the following Code.

o/p
fl1 = -1.23454
fl2 = 1.4E-45
fl is less than MIN_VALUE
fll is less than MIN_VALUE


--Shweta<br />SCJP 1.4 <br />SCWCD
Ben Ritchie
Ranch Hand

Joined: Nov 18, 2002
Posts: 98
It is due to the way in which floating point numbers are stored. Unlike integers, floats and doubles are stored in binary as an exponent and a mantissa (a Google search will show how these work). Simplified a bit, if you think of the number 2x10^3 (==2000), the 2 is the mantissa and the 3 is the exponent.
Both the exponent and the mantissa have a sign bit. For MAX_VALUE neither will be set, and the value is the largest that can be stored given the width of the type. The most negative number that can be stored in a Float or Double is simply equal to MAX_VALUE with the mantissa sign bit set, or in other words "-MAX_VALUE". As this is easy to work out there is not much point in storing it.
It is more useful to know the smallest number that can be stored - this will have the exponent sign bit set (so it will be very small - given the example above, 2x10^-3 == 0.002) but not the mantissa sign bit. That is what is stored in MIN_VALUE.
Hope that is clear - or at least clearer
Ben.


SCJP1.4, SCJD, SCEA (in progress)
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Thanks Ben,
That was really nice & interesting to know..!!
 
Don't get me started about those stupid light bulbs.
 
subject: MIN_VALUE of Float & Double class
 
Similar Threads
Compilation error
Wrapper
Float and ==
java.lang.Number
Usage of :- int comapareTo(float f)