Math Weirdness in Java

July 5th, 2009

MathDealing with infinity is always an interesting proposition. For instance, if you have a line that extends to infinity in either direction, and you cut the line at an arbitrary location, then you have cut the line in half. And what is infinity divided by infinity? If I recall my college math classes correctly, it can be zero, infinity, or a real number, depending upon the relative size of the infinite values. And did you know there are different types of infinity? I never got very far into this, but mathematicians have defined at least two that I know of: countably infinite and uncountably infinite. And they’ve also proven that the set of real numbers between zero and one is uncountable.

The last paragraph is just a lead up to math in the Java programming language. Not only does it have a value that represents infinity, it has one that indicates the value is not a number (NaN), and it also allows for negative zero. (Here I thought the last I’d heard of negative zero was in ones’ complement arithmetic.) It gets weirder. For example, If two variables are equal to NaN, comparing them will return false. And -0.0 is equal to 0.0, except for a few operations where it has unexpected consequences. The linked article has all of the nuances, although it probably won’t mean much to non-programmers.

Thanks to Josh for this topic.


Leave a Reply


HTML: You can use these tags.