9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Richard Miller <9fans@hamnavoe.com>
To: 9fans@9fans.net
Subject: [9fans] comparisons with NaN
Date: Wed, 21 Aug 2013 14:17:25 +0100	[thread overview]
Message-ID: <6ee41a996f876d895cfdb23a2fda5a06@hamnavoe.com> (raw)

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?




             reply	other threads:[~2013-08-21 13:17 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-21 13:17 Richard Miller [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6ee41a996f876d895cfdb23a2fda5a06@hamnavoe.com \
    --to=9fans@hamnavoe.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).