9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] comparisons with NaN
@ 2013-08-21 13:17 Richard Miller
  2013-08-21 14:34 ` erik quanstrom
  2013-08-21 14:36 ` erik quanstrom
  0 siblings, 2 replies; 15+ messages in thread
From: Richard Miller @ 2013-08-21 13:17 UTC (permalink / raw)
  To: 9fans

The Plan 9 C compilers do not appear to be compliant with the IEEE floating
point standard when making comparisons with NaN (not a number) values.

The standard says a comparison with one or both operands NaN is "unordered",
ie all relations evaluate to false, except != which is always true.

Testing with this fragment of code:
	double a, b;
	setfcr(0);
	a = 0.0;
	b = sqrt(-1.0);
	if(a <  b) print(" (a < b)");
	if(a <= b) print(" (a <= b)");
	if(a == b) print(" (a == b)");
	if(a != b) print(" (a != b)");
	if(a >= b) print(" (a >= b)");
	if(a >  b) print(" (a > b)");
	if(b <  a) print(" (b < a)");
	if(b <= a) print(" (b <= a)");
	if(b == a) print(" (b == a)");
	if(b != a) print(" (b != a)");
	if(b >= a) print(" (b >= a)");
	if(b >  a) print(" (b > a)");
	print("\n");
on ARM the result is almost completely wrong:
 (a < b) (a <= b) (a != b) (b < a) (b <= a) (b != a)
and on x86 the result is even wronger:
 (a < b) (a <= b) (a == b) (b < a) (b <= a) (b == a)
compared to the IEEE expected result, for example on MacOS:
 (a != b) (b != a)

This was discovered by fgb; I've been looking into the cause -- which is
mainly the assumption, in the compiler and linker, that something like this:
	if (a < b) f();
can safely be transformed to this:
	if (a >= b) goto skip;
	f();
	skip:
Unfortunately if a or b is NaN, the conditional will be false in both cases.

So is this a feature, or a bug that needs fixing?




^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2013-09-20  9:05 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-21 13:17 [9fans] comparisons with NaN Richard Miller
2013-08-21 14:34 ` erik quanstrom
2013-08-21 16:08   ` Richard Miller
2013-08-21 16:55     ` erik quanstrom
2013-08-21 17:42       ` Bakul Shah
2013-08-21 17:47         ` erik quanstrom
2013-08-21 18:00           ` Bakul Shah
2013-08-21 18:00           ` Richard Miller
2013-08-21 18:24             ` Charles Forsyth
2013-08-22 14:05               ` Richard Miller
2013-08-22 14:25                 ` Charles Forsyth
2013-09-20  9:05                   ` Richard Miller
2013-08-21 14:36 ` erik quanstrom
2013-08-21 18:24   ` lucio
2013-08-21 18:27     ` erik quanstrom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).