floats default to NaN... why?

Andrew Wiley wiley.andrew.j at gmail.com
Sat Jun 9 16:53:06 PDT 2012


On Sat, Jun 9, 2012 at 11:57 AM, Kevin <kevincox.ca at gmail.com> wrote:

> On Sat 09 Jun 2012 14:59:21 EDT, Jerome BENOIT wrote:
> >
> >
> > On 09/06/12 20:48, Kevin wrote:
> >> On 09/06/12 14:42, Minas wrote:
> >>>> With
> >>>> ints, the best we can do is 0. With floats, NaN makes it better.
> >>>
> >>> With the logic that NaN is the default for floats, 0 is a very bad
> >>> choice for ints. It the worst we could do. Altough I understand that
> >>> setting it to something else like -infinity is still not a good choice.
> >> Is it just me but do ints not have infinity values?
> >
> > in Mathematics yes, but not in D.
> >
> >  I think ints should
> >> default to about half of their capacity (probably negative for signed).
> >
> > This would be machine depends, as such it should be avoided.
> >
> >> This way you are unlikely to get an off-by-one for an uninitialized
> >> values.
> >
> > something as a Not an Integer NaI should be better.
>
> I just don't think it is a good idea to add more metadata to ints.
>
>
I agree. With floats, NaN is implemented in hardware. The compiler doesn't
have to check for NaN when emitting assembly, it just emits operations
normally and the hardware handles NaN like you'd expect.
If we tried to add a NaN-like value for integers, we would have to check
for it before performing integer math. Even with value range propagation, I
think that would injure integer math performance significantly.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120609/9cbd5e43/attachment.html>


More information about the Digitalmars-d-learn mailing list