Why is Float.MIN_VALUE in Java a positive value?
floating-point, java
Solution
Per the documentation for Float.MIN_VALUE:
A constant holding the smallest positive nonzero value of type float, 2-149. It is equal to the hexadecimal floating-point literal 0x0.000002P-126f and also equal to Float.intBitsToFloat(0x1).
While the name is debatable as the "true minimum value" of a `float` is `-Float.MAX_VALUE`, I suspect `MIN_VALUE` was chosen for consistency with the other numeric types. Using the names `MIN_RANGE_VALUE` and `MAX_RANGE_VALUE` (or similar) might have made the difference more clear.
To understand why this is the "minimum value" requires understanding a little bit about how Java (or IEEE-754) floating point values work. With this insight, after reading the documentation, it is clear that `Float.MIN_VALUE` is the minimum non-zero value representable by the mantissa and exponent components of a float. Or, the smallest positive value a float can represent.
The "true minimum value" is `-Float.MAX_VALUE` because `Float.MAX_VALUE` represents the maximum value that the mantissa and exponent components of a float can represent. Since the sign for a float is stored as a discrete bit, this range limit is the same for both positive and negative numbers.
This differs from how integers work in Java (and on most CPUs): they are encoded using two's complement. (Some computer systems used a discrete sign bit, which is called "one's complement", which then has two integer values of zero: 0 and -0!)
Happy researching!
Problem
What do you think Float.MIN_VALUE equals to? The next code explains where my last 5 hours went to, trying solving a bug. ``` public static void main(String[] args) { compareToZero(Float.MIN_VALUE); // Out = true false false compareToZero(Float.MAX_VALUE); // Out = true false false System.out.println("Float minimum " + Float.MIN_VALUE); // Out = 1.4E-45 System.out.println("Float maximum " + Float.MAX_VALUE); // Out = 3.4028235E38 } private static void compareToZero(float value1) { System.out.print((value1 > 0) + " "); System.out.print((value1 < 0) + " "); System.out.print((value1 == 0) + "\n"); } ``` I didn't imagined that minimum value of float will be a positive value... Can't find any use for it.