Is D floating point semantics too advanced?

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Aug 23 09:41:35 PDT 2007


Hans-Eric Grönlund skrev:
> Hello!
> 
> I've written a post on my blog wondering if the floating point semantics is too advanced. The problem I have with it is that I can't write something like this:
> 
> if (someValue == real.nan) {...}
> 
> It will never be true, which - for me - is unintuitive.

And for others, the opposite would be unintuitive. A classic test for 
NaN is

if (x != x) {...}

There are several reasons for the current behavior, and one of them is 
that this behavior is what a ieee 754 allows. There are a very large 
number of different binary representations of floating point numbers 
that represent NaN.

Out of habit, I usually use isnan(x) to explicitly check for NaN, but x 
!<>= 0 should work as well.

I agree that the multitude of floating point comparison operators can 
feel overwhelming, and I often have to check the table to be sure, but 
they are very logical if you remember that NaN always compare false with 
the non-negated operators: <, <=, ==, >=, >, <>, <>=, and therefore 
compare true with their opposites: !<, !<=, !=, !>=, !>, !<>, !<>=.

-- 
Oskar



More information about the Digitalmars-d mailing list