[e-lang] Number comparison gotcha

Mark S. Miller erights at google.com
Tue Jan 25 17:37:58 PST 2011


>
>
>   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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.eros-os.org/pipermail/e-lang/attachments/20110125/065b44f7/attachment.html 


More information about the e-lang mailing list