floats default to NaN... why?

Jerome BENOIT g6299304p at rezozer.net
Sat Apr 14 08:28:57 PDT 2012



On 14/04/12 16:47, F i L wrote:
> Jerome BENOIT wrote:
>> Why would a compiler set `real' to 0.0 rather then 1.0, Pi, .... ?
>
> Because 0.0 is the "lowest" (smallest, starting point, etc..)

quid -infinity ?

numerical value. Pi is the corner case and obviously has to be explicitly set.
>
> If you want to take this further, chars could even be initialized to spaces or newlines or something similar. Pointers/references need to be defaulted to null because they absolutely must equal an explicit value before use. Value types don't share this limitation.
>

CHAR set are bounded, `real' are not.


>> The more convenient default set certainly depends on the underlying mathematics,
>> and a compiler cannot (yet) understand the encoded mathematics.
>> NaN is certainly the certainly the very choice as whatever the involved mathematics,
>> they will blow up sooner or later. And, from a practical point of view, blowing up is easy to trace.
>
> Zero is just as easy for the runtime/compiler to default to;

Fortran age is over.
D compiler contains a lot of features that are not easy to set up by the compiler BUT meant for easing coding.


  and bugs can be introduce anywhere in the code, not just definition.

so the NaN approach discard one source of error.

  We have good ways of catching these bugs in D with unittests already.

Zero may give a value that sounds reasonable, NaN will give a NaN value ... which is not reasonable: this is NaN blow up and it can been seen right the way.

Note that the NaN approach is used in some numerical libraries as CLN.



More information about the Digitalmars-d-learn mailing list