Jump to page: 1 2 3
Thread overview
default float initialiser
Feb 02, 2004
imr1984
Feb 02, 2004
der_held
Feb 02, 2004
yaneurao
Feb 02, 2004
Olaf Rogalsky
Feb 02, 2004
Sebastian Beschke
Feb 02, 2004
davepermen
Feb 02, 2004
Sean L. Palmer
Feb 03, 2004
Matthew
Feb 03, 2004
Walter
Feb 03, 2004
Sean L. Palmer
Feb 03, 2004
Matthew
Feb 03, 2004
Walter
Feb 04, 2004
Sean L. Palmer
Feb 07, 2004
Walter
Feb 05, 2004
Manfred Nowak
[bug?] Re: default float initialiser
Feb 06, 2004
Ilya Minkov
Feb 07, 2004
Georg Wrede
Feb 07, 2004
Manfred Nowak
Feb 07, 2004
Georg Wrede
Feb 16, 2004
Jan-Eric Duden
Feb 07, 2004
Walter
Feb 16, 2004
Jan-Eric Duden
Feb 17, 2004
Ilya Minkov
Feb 03, 2004
J C Calvarese
Feb 03, 2004
Olaf Rogalsky
February 02, 2004
ok so the default initialiser for an integer is 0, so why is it nan for floats? Surely 0.0f would be more logical & useful.


February 02, 2004
>ok so the default initialiser for an integer is 0, so why is it nan for floats? Surely 0.0f would be more logical & useful.
>
No! If it's not initialized it should have a value which says that it's not initialized. nan is just perfect for that use.


February 02, 2004
In article <bvl7nc$abm$1@digitaldaemon.com>, imr1984 says...
>ok so the default initialiser for an integer is 0, so why is it nan for floats? Surely 0.0f would be more logical & useful.

I agree with you. sometimes I am annoyed with it.
for example , in the following two case , the results are different.

struct foo {
union {
float a; // initialized to nan
int   b; // initialized to 2143289344
}}

struct foo {
union {
int   b; // initialized to 0
float a; // initialized to 0.0
}}

incidentally , it is better to be capable of initalizing by expression.

eg.
class X {
Y y = new Y;
int a[3] = [ 1,2,3 ];
}

yaneurao.


February 02, 2004
imr1984 wrote:
> ok so the default initialiser for an integer is 0,
Can't find this is in the manual, can anybody advise
me where to find it. Are automatic variables (of simple type)
beeing initialized on entry of a function?

> so why is it nan for floats?
> Surely 0.0f would be more logical & useful.
This seem to be a matter of personal taste. I prefer to initialize variables explicitly, when needed. If I forget to initialize, a NaN is more usesfull than 0.0.

Olaf

-- 
+-------------------------------------------------------------------+ I Dr. rer. nat. Olaf Rogalsky     Institut fuer Theoretische Physik I I                                 Universitaet Erlangen-Nuernberg   I I Tel.: 09131 8528440             Staudtstr. 7 B3                   I I Fax.: 09131 8528444             D-91058 Erlangen                  I | rogalsky@theorie1.physik.uni-erlangen.de                          I +-------------------------------------------------------------------+
February 02, 2004
Olaf Rogalsky wrote:
> This seem to be a matter of personal taste. I prefer to initialize
> variables explicitly, when needed. If I forget to initialize, a NaN
> is more usesfull than 0.0.

Exactly. I prefer to go the C-way: Never use a variable without initializing it. For that matter, I don't think even ints need to be initialized. Explicitly initializing variables makes things somewhat clearer to read.

Sebastian
February 02, 2004
the reason they have a default value is only the ability to repeat situations. useful for debuging, as situations are 100% determinable, only depending on external functions (user input, timers, etc).

helps a bunch.

they don't need a rather meaningful value for this. NaN is perfect.

"Sebastian Beschke" <s.beschke@gmx.de> schrieb im Newsbeitrag news:bvll7k$vh2$1@digitaldaemon.com...
> Olaf Rogalsky wrote:
> > This seem to be a matter of personal taste. I prefer to initialize variables explicitly, when needed. If I forget to initialize, a NaN is more usesfull than 0.0.
>
> Exactly. I prefer to go the C-way: Never use a variable without initializing it. For that matter, I don't think even ints need to be initialized. Explicitly initializing variables makes things somewhat clearer to read.
>
> Sebastian


February 02, 2004
This has been talked to death already.  Some of us would rather have uninitialized variables be an error, but Walter doesn't like that because you'd have to initialize some variables twice to get the compiler to shut up.  I'm in the camp that defaults should be useful rather than pitfalls you have to avoid anyway.  I find NaN default initialization just one teensy bit nicer than garbage, but it would be far superior to initialize to something that is potentially useful.

I find that I *want* to initialize to 0.0 far far far more often than I want to initialize to NaN.  The way it is now, uninitialized variables are just guaranteed to bite you in the ass at runtime, instead of randomly, which is good, but I'd rather it bite you at compile time.

Sean

Olaf Rogalsky wrote:
| imr1984 wrote:
|| ok so the default initialiser for an integer is 0,
| Can't find this is in the manual, can anybody advise
| me where to find it. Are automatic variables (of simple type)
| beeing initialized on entry of a function?
|
|| so why is it nan for floats?
|| Surely 0.0f would be more logical & useful.
| This seem to be a matter of personal taste. I prefer to initialize
| variables explicitly, when needed. If I forget to initialize, a NaN
| is more usesfull than 0.0.
|
| Olaf


February 03, 2004
Olaf Rogalsky wrote:
> imr1984 wrote:
> 
>>ok so the default initialiser for an integer is 0, 
> 
> Can't find this is in the manual, can anybody advise
> me where to find it. 
http://www.digitalmars.com/d/property.html

It says it twice if you look hard enough...

.init		initializer (0)


int.init	// is 0


-- 
Justin
http://jcc_7.tripod.com/d/
February 03, 2004
NaN seems arbitrarily inconsistent.

I agree that one will much more often want to rely on it being 0 than NaN.

Is the rationale for NaN documented anywhere? I'd like to read it.

"Sean L. Palmer" <palmer.sean@verizon.net> wrote in message news:bvmk0p$2jip$1@digitaldaemon.com...
> This has been talked to death already.  Some of us would rather have uninitialized variables be an error, but Walter doesn't like that because you'd have to initialize some variables twice to get the compiler to shut up.  I'm in the camp that defaults should be useful rather than pitfalls
you
> have to avoid anyway.  I find NaN default initialization just one teensy
bit
> nicer than garbage, but it would be far superior to initialize to
something
> that is potentially useful.
>
> I find that I *want* to initialize to 0.0 far far far more often than I
want
> to initialize to NaN.  The way it is now, uninitialized variables are just guaranteed to bite you in the ass at runtime, instead of randomly, which
is
> good, but I'd rather it bite you at compile time.
>
> Sean
>
> Olaf Rogalsky wrote:
> | imr1984 wrote:
> || ok so the default initialiser for an integer is 0,
> | Can't find this is in the manual, can anybody advise
> | me where to find it. Are automatic variables (of simple type)
> | beeing initialized on entry of a function?
> |
> || so why is it nan for floats?
> || Surely 0.0f would be more logical & useful.
> | This seem to be a matter of personal taste. I prefer to initialize
> | variables explicitly, when needed. If I forget to initialize, a NaN
> | is more usesfull than 0.0.
> |
> | Olaf
>
>


February 03, 2004
"Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:bvmvui$643$1@digitaldaemon.com...
> NaN seems arbitrarily inconsistent.

That's only because there is no NaN value for integers. null is the NaN value for pointers and references, and that is used, too. Even better would be if the CPU offered some way to tag a location as 'empty', but so far, this does not exist outside of the 8087 registers.

> I agree that one will much more often want to rely on it being 0 than NaN.

The point is to provide a useless value so 'uninitialized' variable problems will be exposed, and a consistent value to aid in tracking the problem down.

> Is the rationale for NaN documented anywhere? I'd like to read it.

The idea is if you want to use it, you should set it to a value first. The NaN is to catch paths where you used it but failed to initialize it.


« First   ‹ Prev
1 2 3