# MIN_VALUE of Float & Double class

shweta mathur

Ranch Hand

Posts: 109

posted 13 years ago

- 0

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

o/p

fl1 = -1.23454

fl2 = 1.4E-45

fl is less than MIN_VALUE

fll is less than MIN_VALUE

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

Posts: 98

posted 13 years ago

- 0

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.

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)

I agree. Here's the link: http://aspose.com/file-tools |