Tag Archives: programming

Floating point precision

float

A floating point. Well, a floating doughnut.

How do you compare two numbers for equality? Use the equality operator of your language-of-choice, right? Well, not exactly. For example, consider the following code (in Java):

int i = 2 * 5;
if (i == 10) {
...do something
} else {
...do something else
}

and this one:

float f = 2 * 5;
if (f == 10) {
...do something
} else {
...do something else
}

What would be the end result? The correct answer is that the first if block always returns true, while we cannot be so sure about the second one. But why?

Continue reading