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.
Per the documentation for Float.MIN_VALUE:
While the name is debatable as the “true minimum value” of a
floatis-Float.MAX_VALUE, I suspectMIN_VALUEwas chosen for consistency with the other numeric types. Using the namesMIN_RANGE_VALUEandMAX_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_VALUEis 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_VALUEbecauseFloat.MAX_VALUErepresents 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!