floating point - nan initializers

Dave Dave_member at pathlink.com
Sun Feb 19 10:09:32 PST 2006


In article <dt98cn$1ord$4 at digitaldaemon.com>, Walter Bright says...
>
>"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'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.
>

Excepting nan init., I'm all for the great support that D gives to numerical
work, but not at the expense of the great majority of developers who don't do a
lot of "serious" numerical work day-in and day-out. Great numerics support won't
mean anything if the general developer community does not pick up the language
because they don't like (what they may well see as) inconsistencies in the
initialization of basic types. You mentioned that you recently got some good
feedback on D from the NWCUG - I wonder what they would think of initializing
with nan vs. 0 for integral types?

I know that internal to the machine, integral and fp types are worlds apart. But
a 'typical' developer uses floating point because they need precision past the
decimal point, but beyond that expects integrals and floats to pretty much act
the same.

IMO, the typical developer writing the typical program using some fp will end up
writing most of their code like this:

double foo(...)
{
int i,j,k;
double sum, dx, dy;
//...
for(...)
{
//...
sum += dx + dy;
//...
}
return sum;
}

[compile, run, oh shit]

double foo(...)
{
int i,j,k;
double sum = 0;
//...
for(...)
{
double dx = ..., dy = ...;
//...
sum += dx + dy;
//...
}
return sum;
}

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

Yes, but even Fortran 95 doesn't do nan initialization - why?

>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. 
>

As I mentioned previously, I wouldn't care about fp nans then because it would
be consistent for both integral and fp math operations (as seen from a 'typical'
developers perspective - they'd be use to initializing everything, integral and
fp alike).





More information about the Digitalmars-d mailing list