in reply to Why is Zero not 0?
$x = 0; $x += 0.1 for 1..10; $x_eq_1 = $x == 1 ? 'yes' : 'no'; print("$x = 1? $x_eq_1\n");
1 = 1? no
Some numbers can't be represented by floats. Your print statement is rounding the number. This include numbers that are periodic in binary. 0.1 is one of them.
... printf("\$x is really %.16e\n", $x);
... $x is really 9.9999999999999989e-001
If you print your numbers using %.16e, you'll find at least one isn't what you think it is. You should never check floats for equality.
|
|---|