floats default to NaN... why?

Jonathan M Davis jmdavisProg at gmx.com
Sat Jun 9 17:49:58 PDT 2012


On Sunday, June 10, 2012 02:32:18 Jerome BENOIT wrote:
> I see. So the alternative, to get a kind of NaN effect, would be to set
> integers to their hardware extremum (INT_MAX,SIZE_MAX,...). But this option
> is hardware dependent, so zero as default for integers sounds the best
> option.

??? All integral types in D have fixed sizes. It's _not_ system or hardware 
dependent. There's no reason why would couldn't have defaulted int to int.max, 
long to long.max, etc. It would have been the same on all systems.

size_t does vary across systems, because it's an alias (uint on 32-bit, ulong 
on 64-bit), but that's pretty much the only integral type which varies.

There's no hardware stuff going on there like you get with NaN, so you wouldn't 
get stuff like dividing by zero results in int.max with ints. That would be the 
same as always if int defaulted to int.max - it's just the init value which 
would change. NaN can do more, because it has hardware support and floating 
point values are just plain different from integral values. But we could 
certainly have made integral values default to their max without costing 
performance.

Regardless, for better or worse, 0 was chosen as the init value for all of the 
integral types, and it would break tons of code to change it now. So, it's 
never going to change.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list