1

I would like to know, for the following cases, if the IEEE standard guarantees every possible case (excluding NaN and infinities) using any cpu adhering to the standard:

  • Commutativity: x # y = y # x
  • Associativity: (x # y) # z = x # (y # z)
  • x - x = 0 (does x - x == 0.0f always return true?)
  • x * 0 = 0 (does x * 0.0f == 0.0f always return true?)
  • x * 1 = x (does x * 1.0f == x always return true?)
  • x / x = 1.0f (does x / x == 1.0f always return true? Except for x = 0 of course)

(# means all operations: + - * /)

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
ZeroZ30o
  • 375
  • 2
  • 18

1 Answers1

6
  1. Commutativity: + and * are guaranteed unless either argument is NaN. - and / are not commutative and division by 0.0 gives you +Inf, -Inf, or NaN according to the numerator. Here I'm not giving any consideration to signed zeros.

  2. Associativity. Absolutely not. The addition of two small numbers followed by a large number is a counter-example.

  3. x - x is 0 unless x is NaN, +Inf, or -Inf in which case it is NaN.

  4. x * 0 is 0 unless x is NaN, +Inf, or -Inf in which case it is NaN.

  5. x * 1 is x unless x is NaN in which case it is NaN.

  6. x / x is 1 unless x is 0.0, +Inf, -Inf, or NaN in which case it is NaN.

Note the subtle difference of (5).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Commutativity is a little bit subtle if you care about NaN signs and/or payloads: if both `x` and `y` are NaNs, with different payloads, then `x + y` and `y + x` may not be identical. It's not unusual for `x + y` to be identical to `x`, and `y + x` to be identical to `y`. (The standard doesn't specify, though: it just says "the result shall be a quiet NaN which should be one of the input NaNs") – Mark Dickinson Jul 27 '17 at 14:44
  • Hmm. Actually, on my macOS machine, it looks as though `x + y` where `x` and `y` are qNaNs with different payloads returns the _second_ operand `y`. – Mark Dickinson Jul 27 '17 at 14:48
  • (1) was only partially correct. Commutativity is not guaranteed for NaN. (I think a processor will probably treat NaN in the same way as a signed zero.) – Bathsheba Jul 27 '17 at 14:50
  • For signed zeros, though, the rules about the result are completely specified by the standard, and commutativity will hold. It's only NaNs where there's a possibility of non-commutativity (and even then, only if you care about payloads and signs; for many uses of NaNs, they don't matter). – Mark Dickinson Jul 27 '17 at 14:51
  • I think I'm going to wiki this: perhaps it does need more detail. I think I'm getting confused with `std::max` &c. for signed zero and zero arguments. My disclaiming consideration for signed zeros is a little indolent. – Bathsheba Jul 27 '17 at 14:54