Re: Number comparison gotcha
"Mark S. Miller" <[email protected]>
| Newsgroups | gmane.comp.lang.e.general |
|---|---|
| Message-ID | <[email protected]> |
>
>
> if (value == -5000) { return }
>
> It didn't ever compare equal. (But the program ran with no
> thrown errors.) So I changed it to:
>
> if (value == -5000.0) { return }
>
> and it worked.
>
> The lack of any error indication that the first test was wrong
> is worrying for a language that was designed to be relatively "safe".
>
>
Hi Bill, this is because "==" in E means "is the same as", i.e., is
operationally equivalent to all overtly observable tests. Also, in E, ints
and float64s are distinct types, so
? 5000 == 5000.0
# value: false
is no stranger than
? 3 == "foo"
# value: false
This highlights the difference between computational equivalence (==) and
"same magnitude" (<=>). In E, the <=> operator is a peer with <, <=, >=, and
>. Unlike ==, for all five of the magnitude comparison operators, the
request is made to the left operand with the right operand as argument. The
answer is according to the left operand. For operands that satisfy the
contract, x <=> y iff x <= y && x >= y. So:
? 5000 <=> 5000.0
# value: true
? NaN == NaN
# value: true
? NaN <=> NaN
# value: false
? 0.0 == -0.0
# value: false
? 0.0 <=> -0.0
# value: true
--
Cheers,
--MarkM
_______________________________________________
e-lang mailing list
[email protected]
http://www.eros-os.org/mailman/listinfo/e-lang