O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Comparing Floating Point Numbers

Float a = new Float(3.0f);
											Float b = new Float(3.0f);
											if (a.equals(b)) {
											// they are equal
											}

Because of rounding errors that can occur when working with floating point numbers, you have to be a bit more careful when trying to compare them. Therefore, instead of comparing the basic Java floating point types of float and double using the == operator; you should instead compare their object equivalents. The equals() method on Float and Double will return true only if the two values are exactly the same, bit for bit, or if they are both the value NaN. The value NaN stands for not a number. This value indicates a value that is not a valid number.

In the real world, when comparing floating point numbers, you might not ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required