Thread overview | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 18, 2003 NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Why is NaN the default value for floats and doubles? The most common desired behavior is to default to 0. If the point of initializers is to make sure you specify an initial value in user code then NaN is fine because you almost never want to start a computation with NaN but if the point is to make a convenient default value then I'd suggest going with 0. It would also improve ease-of-use because every language I can think of that has defaults uses 0. just a thought, -Ben |
January 18, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | "Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:b0c0n3$1jaj$1@digitaldaemon.com... > Why is NaN the default value for floats and doubles? The most common desired > behavior is to default to 0. If the point of initializers is to make sure you specify an initial value in user code then NaN is fine because you almost never want to start a computation with NaN but if the point is to make a convenient default value then I'd suggest going with 0. It would also > improve ease-of-use because every language I can think of that has defaults > uses 0. The reason is to cause the programmer to think about what the initial value should be. 0 is usually what is desired, but 1, pi, etc., are also candidates. Using NaN means that if you didn't specify one, and the result of the computation relies on it, the result would be NaN. If it were possible for NaNs for integers, I'd have used that too. |
January 18, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Yes, yes, I agree; it's been discussed to death already. Walter seems intent on forcing NaN's into our programs. Sean "Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:b0c0n3$1jaj$1@digitaldaemon.com... > Why is NaN the default value for floats and doubles? The most common desired > behavior is to default to 0. If the point of initializers is to make sure you specify an initial value in user code then NaN is fine because you almost never want to start a computation with NaN but if the point is to make a convenient default value then I'd suggest going with 0. It would also > improve ease-of-use because every language I can think of that has defaults > uses 0. > > just a thought, > -Ben |
January 18, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Yes the default should always be zero. M. |
January 18, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter here I would toss back at you the hoary old chestnut you toss at me when I suggest D features that don't feel like C.
Using a NaN initializer may be theoretically and pedagogically proper but it's dumb because nobody expects it. Zero is the value to use.
Mark
>
>The reason is to cause the programmer to think about what the initial value should be. 0 is usually what is desired, but 1, pi, etc., are also candidates. Using NaN means that if you didn't specify one, and the result of the computation relies on it, the result would be NaN. If it were possible for NaNs for integers, I'd have used that too.
>
>
|
January 18, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:b0c6pk$1n6b$1@digitaldaemon.com... > > "Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:b0c0n3$1jaj$1@digitaldaemon.com... > > Why is NaN the default value for floats and doubles? The most common > desired > > behavior is to default to 0. If the point of initializers is to make sure > > you specify an initial value in user code then NaN is fine because you almost never want to start a computation with NaN but if the point is to make a convenient default value then I'd suggest going with 0. It would > also > > improve ease-of-use because every language I can think of that has > defaults > > uses 0. > > The reason is to cause the programmer to think about what the initial value > should be. 0 is usually what is desired, but 1, pi, etc., are also candidates. Using NaN means that if you didn't specify one, and the result of the computation relies on it, the result would be NaN. If it were possible for NaNs for integers, I'd have used that too. Could you throw a compile-time warning when floats or doubles aren't explicitly initialized? Throwing a compile-time error would be harsh but a warning would save me from debugging the problem at run-time. At least a NaN isn't as nasty to debug as random uninitialized memory :) |
January 18, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark Evans | "Mark Evans" <Mark_member@pathlink.com> wrote in message news:b0cbcb$1q0i$1@digitaldaemon.com... > Walter here I would toss back at you the hoary old chestnut you toss at me when > I suggest D features that don't feel like C. Fair enough! > Using a NaN initializer may be theoretically and pedagogically proper but it's > dumb because nobody expects it. Zero is the value to use. The nice thing about NaN is it won't bite you if you don't expect it. 0 will. |
January 18, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | "Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:b0cc2q$1qh0$1@digitaldaemon.com... > Could you throw a compile-time warning when floats or doubles aren't explicitly initialized? I considered that, but my experience with C/C++ compilers that did that is rather negative. It results in lots of useless initializations put in just to shut the compiler up. I am philosophically opposed to requiring initializers when such values are never used - it confuses the person maintaining the code. > Throwing a compile-time error would be harsh but a > warning would save me from debugging the problem at run-time. At least a NaN > isn't as nasty to debug as random uninitialized memory :) NaNs are a lot easier to track down than an unintended 0. |
January 18, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <b0c6pk$1n6b$1@digitaldaemon.com>, walter@digitalmars.com says... > > "Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:b0c0n3$1jaj$1@digitaldaemon.com... > > Why is NaN the default value for floats and doubles? The most common > desired > > behavior is to default to 0. If the point of initializers is to make sure you specify an initial value in user code then NaN is fine because you almost never want to start a computation with NaN but if the point is to make a convenient default value then I'd suggest going with 0. It would > also > > improve ease-of-use because every language I can think of that has > defaults > > uses 0. > > The reason is to cause the programmer to think about what the initial value should be. 0 is usually what is desired, but 1, pi, etc., are also candidates. Using NaN means that if you didn't specify one, and the result of the computation relies on it, the result would be NaN. If it were possible for NaNs for integers, I'd have used that too. Hmm then why choose 0, which is a number which tends to hide and not cause much trouble, rather than something large that would screw up most calculations? - Factory |
January 19, 2003 Re: NaN initializers for floating point? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | "Ben Hinkle" wrote > Why is NaN the default value for floats and doubles? The most common desired > behavior is to default to 0. If the point of initializers is to make sure you specify an initial value in user code then NaN is fine because you almost never want to start a computation with NaN but if the point is to make a convenient default value then I'd suggest going with 0. It would also > improve ease-of-use because every language I can think of that has defaults > uses 0. I don't know. Usually NaN tells you that some numerics is out of control, division by zero or some other divergent situation. By setting the default value to NaN you are initially "out of control" which is basicly what you are if you don't assign some value to it. |
Copyright © 1999-2021 by the D Language Foundation