View mode: basic / threaded / horizontal-split · Log in · Help
June 08, 2012
Re: floats default to NaN... why?
hello List:

On 08/06/12 04:04, Kevin Cox wrote:
>
> On Jun 7, 2012 9:53 PM, "Minas" <minas_mina1990@hotmail.co.uk <mailto:minas_mina1990@hotmail.co.uk>> wrote:
>  >
>  > I agree that the default value for floats/doubles should be zero. It feels much more natural.

This highly depends on your perspective:
for numerical folks, NaN is _the_ natural default.

>  >
>  > I think the problem here is that people are thinking about some stuff too much. D is a rather new language that wants to be practical. Floats defaulting to NaN is NOT practical FOR MOST PEOPLE when at the same time I write:
>  >
>  > int sum;
>  >
>  > for(...)
>  >  sum += blah blah blah
>  >
>  > And it works.
>  >
>  > Having floats deaulting to a value that's un-natural for most people is, in my opinion, craziness. Even if that's "more" correct in a mathematical sense.
>  >

The ``most people'' argument in science and technical stuff is a very weak argument.

>  > Please excuse my English.
>
> The idea isn't being "practical" exactly.  The idea was to use invalid values as defaults. Unfortunately things like ints don't have invalid values, so they chose zero.  The idea is to make people initialize their variables.  It would have been better to pick 19472937 as the int default because not many people will use that value.
>
> The code you showed would be considered **bad** because you did not initialize your variables, however ints defaulting to zero is well defined so it isn't really a big deal.
>
> With floats there is this wonderful value that ensures that nothing reasonable comes out of a bad calculation, NaN.  Therefore this is used as a default because if you forget to initialize your vars it will jump out at you.
>
> This is the thought that was used and many people don't agree.  It is **very** unlikely to be changed now but __maybe__ D3 however far off that is.  I hope not though, I hope ints default to 8472927 instead, but everyone has different opinions.
>
I am totally agree.
I would step further by setting up as default for integers +infinite.
June 08, 2012
Re: floats default to NaN... why?
> The idea isn't being "practical" exactly.  The idea was to use 
> invalid
> values as defaults. Unfortunately things like ints don't have 
> invalid
> values, so they chose zero.  The idea is to make people 
> initialize their
> variables.

I understand the logic, but I still disagree. No offense :)
I don't like inconsistency though (that ints are 
zero-initialized).
June 09, 2012
Re: floats default to NaN... why?
On Thu, Jun 7, 2012 at 6:50 PM, Minas <minas_mina1990@hotmail.co.uk> wrote:

> I agree that the default value for floats/doubles should be zero. It feels
> much more natural.
>

The point is to make sure code is correct. Initializing your variables
should be what feels natural. Leaving then uninitialized is bad style, bad
practice, and generally a bad idea. If getting stung by a float
initializing to NaN is what it takes to make that happen, so be it.

>
> I think the problem here is that people are thinking about some stuff too
> much. D is a rather new language that wants to be practical. Floats
> defaulting to NaN is NOT practical FOR MOST PEOPLE when at the same time I
> write:


Actually, as far as I know, floats have been initialized to NaN since D
first appeared in 2001. This isn't news.

>


> int sum;
>
> for(...)
>  sum += blah blah blah
>
> And it works.
>
In any other systems language, this would be undefined behavior. D is
simply trying to make initialization bugs as obvious as possible. With
ints, the best we can do is 0. With floats, NaN makes it better.
June 09, 2012
Re: floats default to NaN... why?
> 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.

I think that if D wants people to initialize their variables, it 
should generate a compiler error when not doing so, like C# and 
Java. For me, having floats defaulting to NaN and ints to zero is 
somewhere in the middle... Which isn't good.

The current solution is not good for me (I still love D though).
June 09, 2012
Re: floats default to NaN... why?
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?  I think ints should
default to about half of their capacity (probably negative for signed). 
This way you are unlikely to get an off-by-one for an uninitialized values.

> I think that if D wants people to initialize their variables, it
> should generate a compiler error when not doing so, like C# and Java.
> For me, having floats defaulting to NaN and ints to zero is somewhere
> in the middle... Which isn't good.
I 100% agree.
June 09, 2012
Re: floats default to NaN... why?
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 think that if D wants people to initialize their variables, it
>> should generate a compiler error when not doing so, like C# and Java.
>> For me, having floats defaulting to NaN and ints to zero is somewhere
>> in the middle... Which isn't good.
> I 100% agree.
>
June 09, 2012
Re: floats default to NaN... why?
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.
June 09, 2012
Re: floats default to NaN... why?
On 09/06/12 20: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.
>

Do you ave in mind something as NaI (Nat an Integer) ?

> I think that if D wants people to initialize their variables, it should generate a compiler error when not doing so, like C# and Java. For me, having floats defaulting to NaN and ints to zero is somewhere in the middle... Which isn't good.
>
> The current solution is not good for me (I still love D though).
>
>

Jerome
June 09, 2012
Re: floats default to NaN... why?
On Sat, Jun 9, 2012 at 11:57 AM, Kevin <kevincox.ca@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.
June 09, 2012
Re: floats default to NaN... why?
On Sat, Jun 9, 2012 at 4:53 PM, Andrew Wiley <wiley.andrew.j@gmail.com> wrote:
>
> On Sat, Jun 9, 2012 at 11:57 AM, Kevin <kevincox.ca@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.

Crap, my apologies for responding with HTML.
2 3 4 5 6 7
Top | Discussion index | About this forum | D home