January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank Benoit (keinfarbton) | Frank Benoit (keinfarbton) wrote:
>>creates a new, uninitialized Vector3 and returns it. This should work:
>
>
> according to the spec, structs should be default initialized. Is this a bug?
The default initialization for float/double/real is nan.
Look under 'types'.
-DavidM
|
January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank Benoit (keinfarbton) | Am 11.01.2007, 15:23 Uhr, schrieb Frank Benoit (keinfarbton) <benoit@tionex.removethispart.de>: > >> creates a new, uninitialized Vector3 and returns it. This should work: > > according to the spec, structs should be default initialized. Is this a bug? No, since floats are initialized to NaN, at least AFAIR. -mike -- Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/ |
January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mike | mike wrote:
> Am 11.01.2007, 15:07 Uhr, schrieb Tim Fang <no@spam.com>:
>
>> I have called "a.set(1,1,1)" to init the members in main().
>
> Yeah, but
>
>>>> Vector3 opMul(float s)
>>>> {
>>>> Vector3 ret;
>>>> ret.x = x*s;
>>>> ret.y = y*s;
>>>> ret.z = z*s;
>>>> return ret;
>>>> }
>
> creates a new, uninitialized Vector3 and returns it. This should work:
It creates a new default-initialized Vector3, *initializes* it and returns it. I don't see anything wrong with the code. The result of the original code should be "222", not "nannannan".
As Bill Baxter said, this is a bug introduced in DMD 0.178.
/Oskar
|
January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oskar Linde | Am 11.01.2007, 15:31 Uhr, schrieb Oskar Linde <oskar.lindeREM@OVEgmail.com>: > It creates a new default-initialized Vector3, *initializes* it and returns it. I don't see anything wrong with the code. The result of the original code should be "222", not "nannannan". Yes, but the default initializer for float is NaN, so: ' Vector3 opMul(float s) ' { ' Vector3 ret; // <-- creates new, and ' ret.x = x*s; // <-- oh, wait a sec ... this x should be initialized ' ret.y = y*s; ' ret.z = z*s; ' return ret; ' } '} Now I'm confused ... :) Maybe it's really a bug. -mike -- Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/ |
January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mike | Every member of "Vector3 ret" assigned a new value in the opMul(), It seams need not to init "Vector3 ret". However if call ret.set(1,1,1), it works well; if call ret.set(0,0,0), it return (0,0,0). I think this should be a bug. "mike" <vertex@gmx.at> wrote:op.tlzubjapnxkcto@zimmermoos... Am 11.01.2007, 15:07 Uhr, schrieb Tim Fang <no@spam.com>: > I have called "a.set(1,1,1)" to init the members in main(). Yeah, but >>> Vector3 opMul(float s) >>> { >>> Vector3 ret; >>> ret.x = x*s; >>> ret.y = y*s; >>> ret.z = z*s; >>> return ret; >>> } creates a new, uninitialized Vector3 and returns it. This should work: ' Vector3 ret; ' ret.set(0., 0., 0.); ' // ... -mike -- Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/ |
January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to mike | ' Vector3 opMul(float s) ' { ' Vector3 ret; // <-- creates new, and ' ret.x = x*s; // <-- oh, wait a sec ... this x should be initialized -- x is the member of "Vector3 a", which is declared and initialized in "main()". ' ret.y = y*s; ' ret.z = z*s; ' return ret; ' } '} "mike" <vertex@gmx.at> wrote :op.tlzu9adrnxkcto@zimmermoos... Am 11.01.2007, 15:31 Uhr, schrieb Oskar Linde <oskar.lindeREM@OVEgmail.com>: > It creates a new default-initialized Vector3, *initializes* it and returns it. I don't see anything wrong with the code. The result of the original code should be "222", not "nannannan". Yes, but the default initializer for float is NaN, so: ' Vector3 opMul(float s) ' { ' Vector3 ret; // <-- creates new, and ' ret.x = x*s; // <-- oh, wait a sec ... this x should be initialized ' ret.y = y*s; ' ret.z = z*s; ' return ret; ' } '} Now I'm confused ... :) Maybe it's really a bug. -mike -- Erstellt mit Operas revolutionärem E-Mail-Modul: http://www.opera.com/mail/ |
January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | David Medlock escribió: > Its not a bug, you didn't initialize the x,y and z members of Vector3. > > try: > > struct Vector3 > { > float x =0, y =0, z =0; > .... > } Is there a logical explanation about why floats are initialized to NAN and not zero? -- Leandro Lucarella Integratech S.A. 4571-5252 |
January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Leandro Lucarella | Leandro Lucarella wrote: > David Medlock escribió: > > Its not a bug, you didn't initialize the x,y and z members of Vector3. > > > > try: > > > > struct Vector3 > > { > > float x =0, y =0, z =0; > > .... > > } > > Is there a logical explanation about why floats are initialized to NAN and not zero? http://www.digitalmars.com/d/faq.html#nan -- |
January 11, 2007 Re: Is this a operator overloading bug? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomas Lindquist Olsen | Tomas Lindquist Olsen escribió: > Leandro Lucarella wrote: > >> David Medlock escribió: >>> Its not a bug, you didn't initialize the x,y and z members of >>> Vector3. >>> >>> try: >>> >>> struct Vector3 >>> { >>> float x =0, y =0, z =0; >>> .... >>> } >> Is there a logical explanation about why floats are initialized to >> NAN and not zero? > > http://www.digitalmars.com/d/faq.html#nan My fault for not looking at the FAQ =) Even so, I think I read this before and I forgot it =P -- Leandro Lucarella Integratech S.A. 4571-5252 |
Copyright © 1999-2021 by the D Language Foundation