Hacker News new | past | comments | ask | show | jobs | submit login

It's not commutative: -0 + 0 = -0, but 0 + -0 = 0.



But doesn't IEEE-754 consider -0 and 0 equal? From https://en.wikipedia.org/wiki/IEEE_floating_point#Total-orde... :

"The normal comparison operations however treat NaNs as unordered and compare −0 and +0 as equal."


Right. -0 == 0. The existence of a "-0" is a property of the underlying representation. The mathematical properties don't know about the sign bit, per se.


Ah, that's news to me - can you provide a reference? I'm trying to look it up but my Google-fu is failing to find a specific link. Thanks.


That's not the behaviour I'm seeing on my machine. (http://codepad.org/rlLuB5VF) ... but, if you subtract zero from zero the sign depends on the order: http://codepad.org/g9LzZWPd .



So quoting from the discussion:

    Oh, of course: for non-NaNs, correctly implemented IEEE
    arithmetic is "exactly rounded". So addition of non-NaNs
    must be commutative for any given rounding rule, ...


(-0.) + (+0.) and (+0.) + (-0.) both evaluate to +0., as any compiler should readily confirm.


N.B.: Except in round-to-minus-infinity, in which case they evaluate to -0.


Is the compiler calculating that at compile time based on the lexical tokens, or at runtime with native CPU floating point instructions? That absolutely matters.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: