Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min value of float in java is positive why? [duplicate]

Tags:

when we use MIN_VALUE function on either of primitive types in java it give us minimum value possible for that type. BUT in case of float and double it returned minimum positive value though float and double can have negative values also.

like image 760
ajaycoolfbd Avatar asked Mar 17 '12 02:03

ajaycoolfbd


People also ask

Why is double min value positive?

Double. MIN_VALUE is a constant holding the smallest POSITIVE nonzero value of type double, 2^(-1074). The mantissa, always a positive number, holds the significant digits of the floating-point number. The exponent indicates the positive or negative power of the radix that the mantissa and sign should be multiplied by.

Why the ID of float values are different when the same value is assigned to two different variables?

In that case the float value containing variable may contain same data but reason behind is to mathematical rules. The only two value are consider after the point & remaining are value get neglected. so the we get two different addresses of the same data containing variables.

What is float min value?

Wiki says this about float : The minimum positive normal value is 2^−126 ≈ 1.18 × 10^−38 and the minimum positive (denormal) value is 2^−149 ≈ 1.4 × 10^−45.

Can Java floats be negative?

when we use MIN_VALUE function on either of primitive types in java it give us minimum value possible for that type. BUT in case of float and double it returned minimum positive value though float and double can have negative values also.


2 Answers

MIN_VALUE tells you how precise a float can get, but not the mathematical minimum it can represent. They should have named it better...

Negative of MAX_VALUE is the mathematical minimum value for floats(same goes for double).

The reason you can assume this has to do with how numbers are represented in binary:

Java float and doubles use sign bit to represent negative/positive values as opposed to two's complement representation for integers. This means it's positive and negative value have the same magnitude, ie. -(positive max) = negative max. Therefore you don't need to define an actual mathematical minimum constant for floats because you can just use the positive max constant to figure out the what the negative bound is. For integers, you need 2 different constants defining max/min because of the way they represented in binary, i.e. -max != min.

For more info http://people.uncw.edu/tompkinsj/133/numbers/Reals.htm

like image 136
XiaoChuan Yu Avatar answered Oct 06 '22 18:10

XiaoChuan Yu


MIN_VALUE should be named EPSILON it's the smallest postive value a float can represent.

Because a float uses the sign-magnitude encoding, the lowest value a float can represent is -MAX_VALUE.

like image 36
Willem Van Onsem Avatar answered Oct 06 '22 18:10

Willem Van Onsem