Friday, June 10, 2011

Float.MIN_VALUE Craziness

Did I miss a memo? How can this be right:-

float f = Math.max(Float.MIN_VALUE, 0f); // Returns "1.4E-45" (i.e. Float.MIN_VALUE I think)

What? Maybe I missed something in the documentation that said "Please note that this function is crazy." Unless the logic of "closest to positive infinity" means that Float.MIN_VALUE is actually closer since it loops round backwards. I hope not.

2 comments:

Evil Engel Sanchez said...

Steve, MIN_VALUE is the "smallest positive nonzero value" according to the javadoc, not the lowest negative value like it is for Integer (which is very confusing).

Steve said...

Goddam! Thanks for letting me know. They could have used a different name like Float.SMALLEST_VALUE. I guess the way to get the "most negative" number is just multiply Float.MAX_VALUE by -1. I'll give it a try... Thanks again.