On 19.02.2011 13:06, spir wrote:
Hello,
What do you think of this?
unittest {
assert(-1.1 + 2.2 == 1.1); // pass
assert(-1.1 + 2.2 + 3.3 == 4.4); // pass
assert(-1.1 + 3.3 + 2.2 == 4.4); // fail
assert(-1.1 + 3.3 == 2.2); // fail
}
There is approxEquals in stdlib, right; but shouldn't builtin "==" be
consistent anyway?
Denis
It is generally a bad idea to use "==" with floats, as most decimals
cant be represented exact in binary floating point. That means there
is no float for the value "1.1". Though there are exact floats for
0.25, 0.5, 42.125 and so on. The only reason the first two testcases
work is, that it is rounded the same way both sides of the "==" but
you should not rely on that.
Also note, that these calculations are probably done at compile-time,
and the compiler is allowed to use a higher precision than at
run-time, so you might get different result when you let the user
input the numbers.
- Krox