programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

MIN_VALUE of Float & Double class

shweta mathur
Ranch Hand
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

Ben Ritchie
Ranch Hand
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.

shweta mathur
Ranch Hand
Posts: 109
Thanks Ben,
That was really nice & interesting to know..!!