4

Suppose I have two variables a and b, either both in type float, or both in type double, which hold some values. Do the following assertions always hold? I mean, do the existence of numerical errors alter the conclusions?

a > b is true if and only if a <= b is false

a < b is true if and only if a >= b is false

a >= b is necessarily true if a == b is true

a <= b is necessarily true if a == b is true

For the third and fourth, I mean for example, does "a == b is true" always give you "a >= b is true"?

EDIT:

Assume neither a or b is NaN or Inf.

EDIT 2:

After reading the IEEE 754 standard in the year 1985, I found the following.

First of all, it said the following

Comparisons are exact and never overflow nor underflow.

I understand it as when doing comparison, there is no consideration of numerical error, numbers are compared as-is. Since addition and subtraction such as a - b requires additional effort to define what the numerical error is, I assume the quote above is saying that comparisons like a > b is not done by judging whether a - b > 0 is true or not. Please let me know if I'm wrong.

Second, it listed the four canonical relations which are mutually exclusive.

Four mutually exclusive relations are possible: less than, equal, greater than, and unordered. The last case arises when at least one operand is NaN. Every NaN shall compare unordered with everything, including itself.

Then in Table 4, it defined the various kinds of operators such as ">" or ">=" in terms of the truth values under these four canonical relations. From the table we immediately have the following:

a >= b is true if and only if a < b is false

a <= b is true if and only if a > b is false

Both a >= b and a <= b are necessarily true if a == b is true

So the assertions in my questions can be concluded as true. However, I wasn't able to find anything in the standard defining whether symmetricity is true or not. In another way, from a > b I don't know if b < a is true or not. Thus I also have no way to derive a <= b is true from b < a is false. So I would be interested to know, in addition to the assertions in the OP, whether the following is always true or not

a < b is true if and only if b > a is true

a <= b is true if and only if b >= a is true

etc.

EDIT 3:

Regarding denormal numbers as mentioned by @Mark Ransom, I read the wikipedia page about it and my current understanding is that the existence of denormal numbers does not alter the conclusions above. In another way, if some hardware claims that it fully support denormal numbers, it also needs to ensure that the definitions of the comparison operators satisfy the above standards.

EDIT 4:

I just read the 2008 revision of IEEE 754, it doesn't say anything about symmetricity either. So I guess this is undefined.

(All the above discussion assumes no NaN or Inf in any of the operands).

Community
  • 1
  • 1
shaoyl85
  • 1,854
  • 18
  • 30
  • 2
    When you have `NaN`s the above assertions will fail. – Paul R Mar 06 '14 at 22:01
  • I see, thank you. How about the case without nan and inf? – shaoyl85 Mar 06 '14 at 22:02
  • 1
    see http://stackoverflow.com/questions/1565164 also – pmeerw Mar 06 '14 at 22:04
  • 1
    Given the restrictions you've added I think the only problem might be with denormalized numbers. See http://en.wikipedia.org/wiki/Denormal_number – Mark Ransom Mar 06 '14 at 22:13
  • Thank you all for the useful information. I was concerning cuda programming with GPUs, and I indeed found that they mentioned full denormal number support in GPUs. Furthermore, in the link @pmeerw provided, it says "less than, equal, greater than" are mutually exclusive. Altogether does this mean I can safely conclude that my assertions above, at least for the hardware that supports denormal numbers, hold true? – shaoyl85 Mar 06 '14 at 22:24

1 Answers1

2

If neither number is a NaN or infinity then your assertions hold, if you have an IEEE compatible system. If the standards don't mention it then I'm sure that is just because it was sufficiently obvious to not be worth mentioning. In particular, if "a < b" and "b > a" don't have the same value (even for NaNs and infinities) then we are in crazy town.

Denormals shouldn't affect the conclusions because if you are assuming IEEE compatibility then denormal support is a given.

The only risks I can think of are in cases involving the x87 FPU and it's odd 80-bit long double format. Rounding from 80-bit long double to double or float is expensive, so it is sometimes omitted, and that can lead to an assert like this firing:

assert(sqrt(val) == sqrt(val));

It could fire because the result of the first sqrt() call may be written to memory, and therefore rounded to float or double. It would then be compared against the result of the second sqrt() call, which might not be rounded. However, failing to round the result of the second sqrt() call before doing the comparison is, strictly speaking, not IEEE compliant if sqrt() returns float or double.

Infinity should only be a problem if both numbers are infinity.

Bruce Dawson
  • 3,284
  • 29
  • 38