floating point - nan initializers

Walter Bright newshound at digitalmars.com
Sat Feb 18 23:55:52 PST 2006


"Dave" <Dave_member at pathlink.com> wrote in message 
news:dt8o24$djh$1 at digitaldaemon.com...
> I've never seen a big stink raised among Java or C# users about 
> initializing fp types to 0 - I never hear anyone complaining "if doubles 
> were initialized to nans instead of 0, I wouldn't have all these bugs in 
> my arithmetic".

I'll go out on a limb here, and say that Java was designed by people who 
were unfamiliar with numerical analysis. Prof. W. Kahan points out in great 
detail how they missed the boat with floating point. I am not too familiar 
with C#, but haven't noticed its designers paid much attention to the needs 
of numerical analysts either.

Neither have the designers of C and C++, nor the vendors. Heck, look how 
most C++ compilers turned their backs on 80 bit floating point!!!

Any language that doesn't even fully support the floating point precision on 
the chip is not a precedent worth following, as nobody with understanding of 
numerical analysis has had any influence on its design. Such languages 
typically just copy what was done before without thinking about it.

In a previous engineering life, I *have* done numerical analysis engineering 
work. Not a great deal, but enough to be familiar with why floating point is 
the way it is, and what sorts of problems and tradeoffs it is set up to deal 
with. I've participated in the Numerical C Extensions Group, which tried to 
make C into a more reasonable language for numerical work. Prof. Kahan was 
instrumental in the design of IEEE 754 floating point, and its first 
hardware implementation on the 8087 numeric coprocessor. I've read some of 
his papers, and have corresponded with him. He makes a whole lot of sense.

I've also heard from people who do serious numerical work that, at last, D 
is a language that cares about numerical analysis and its needs. Default 
initializing to nan is part of that - it forces the user to *think* about 
what he wants the initial value to be. Initializing it by default to 0 means 
that it can easilly be overlooked, and 0.0 can introduce undetected, subtle 
errors in the result.

I know FORTRAN doesn't deal with nans. That's unsurprising, since FORTRAN 
had been around for 25 years before nans were invented.

There is a 'nan' value for pointers - null, a 'nan' value for UTF-8 chars - 
0xFF - which is an illegal UTF-8 character. If there was a 'nan' value for 
ints, D would use it as the default, too. 





More information about the Digitalmars-d mailing list