Possible Duplicate:
Is JavaScript's Math broken?
Why can't decimal numbers be represented exactly in binary?
What will be result of next code:
if(0.3 == ( 0.1 + 0.1 + 0.1 ))
{
alert(true);
}
else
{
alert(false);
}
It is strange, but result will be false.
Reason is that result of
0.1+0.1+0.1
will be
0.30000000000000004
How can be explained this behavior?
It's the same reason 1/3 + 1/3 + 1/3 may not give you exactly 1 in decimal. If you use 1/3 as .33333333, then 1/3 + 1/3 + 1/3
will give you .9999999
which isn't exactly one.
Unless you know exactly what you're doing, don't compare non-integer numeric types for equality.
The explanation is pretty simple - read about Floating Point Numbers Problems
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With