On 20.04.20 02:57, 陈圣麒 wrote:
> 0.0 ? "foo" : "bar" returns bar
> That seems highly unreasonable
>
Hello,
Never do equality comparison on floating point numbers; floating point
is not an exact representation. It is also not unambiguous (i.e. there
is more than one way to store the same number; but == only compares the
bit patterns, so it may fail). This is independent of the programming
language, it's an inherent property of floats.
If you need to compare a float x to a specific value, you have to use
something like:
(abs(x-value) < 0.0001) ? "foo" : "bar";
i.e. check if x is within a small environment around the value.
cya
Invisible@Beutelland