digitalmars.D - Is this a operator overloading bug?
- Tim Fang (30/30) Jan 10 2007 I am very new to D and I am trying to implement a Vector3 struct, pl...
- Bill Baxter (5/43) Jan 10 2007 Yikes. That looks to me like it might be a bug introduced by the Named
- Bill Baxter (5/43) Jan 10 2007 Btw, you might also want to check out Helix before you spend a bunch of
- Bill Baxter (13/51) Jan 11 2007 I have confirmed that the code below works properly in DMD 0.177 but
- Tim Fang (6/57) Jan 11 2007 Thank you for you reply.
- David Medlock (9/47) Jan 11 2007 Its not a bug, you didn't initialize the x,y and z members of Vector3.
- Tim Fang (3/49) Jan 11 2007 I have called "a.set(1,1,1)" to init the members in main().
- mike (10/19) Jan 11 2007 creates a new, uninitialized Vector3 and returns it. This should work:
- mike (6/9) Jan 11 2007 Ah ... of course you need to initialize with 1, not 0. :)
- Frank Benoit (keinfarbton) (1/2) Jan 11 2007 according to the spec, structs should be default initialized. Is this a ...
- David Medlock (4/8) Jan 11 2007 The default initialization for float/double/real is nan.
- mike (9/12) Jan 11 2007 :
- Oskar Linde (6/22) Jan 11 2007 It creates a new default-initialized Vector3, *initializes* it and
- Tim Fang (14/23) Jan 11 2007 Every member of "Vector3 ret" assigned a new value in the opMul(), It se...
- Leandro Lucarella (7/16) Jan 11 2007 Is there a logical explanation about why floats are initialized to NAN
- Tomas Lindquist Olsen (3/17) Jan 11 2007 http://www.digitalmars.com/d/faq.html#nan
- Leandro Lucarella (7/24) Jan 11 2007 My fault for not looking at the FAQ =)
I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00. --code--------------------- import std.stdio; void main() { Vector3 a; a.set(1,1,1); a = a*2; writefln(a.x, a.y, a.z); } struct Vector3 { float x,y,z; // constructor void set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; } Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; } }
Jan 10 2007
Yikes. That looks to me like it might be a bug introduced by the Named Return Value Optimization implemented in DMD 0.178. http://www.digitalmars.com/d/glossary.html#nrvo --bb Tim Fang wrote:I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00. --code--------------------- import std.stdio; void main() { Vector3 a; a.set(1,1,1); a = a*2; writefln(a.x, a.y, a.z); } struct Vector3 { float x,y,z; // constructor void set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; } Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; } }
Jan 10 2007
Btw, you might also want to check out Helix before you spend a bunch of time writing your own Vector3 type classes. http://www.dsource.org/projects/helix --bb Tim Fang wrote:I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00. --code--------------------- import std.stdio; void main() { Vector3 a; a.set(1,1,1); a = a*2; writefln(a.x, a.y, a.z); } struct Vector3 { float x,y,z; // constructor void set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; } Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; } }
Jan 10 2007
I have confirmed that the code below works properly in DMD 0.177 but fails in DMD 0.178 (in the same way it does in 1.00), so it does look very much like the NRVO is indeed the culprit. Can you file a bug about it with bugzilla? http://d.puremagic.com/issues/ You can get around it for now by making a constructor function (using static opCall is popular), and writing your opMul as return the_constructor_func(x*s, y*s, z*s); That seems to work, although I suspect that just means that it's disabling the NRVO, which I guess isn't smart enough to do the optimization when the return value is passed through several functions. --bb Tim Fang wrote:I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00. --code--------------------- import std.stdio; void main() { Vector3 a; a.set(1,1,1); a = a*2; writefln(a.x, a.y, a.z); } struct Vector3 { float x,y,z; // constructor void set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; } Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; } }
Jan 11 2007
Thank you for you reply. static opCall() works well. I have downloaded helix lib, it looks pretty good! "Bill Baxter" <dnewsgroup billbaxter.com> wrote:eo4r0c$25tg$1 digitaldaemon.com...I have confirmed that the code below works properly in DMD 0.177 but fails in DMD 0.178 (in the same way it does in 1.00), so it does look very much like the NRVO is indeed the culprit. Can you file a bug about it with bugzilla? http://d.puremagic.com/issues/ You can get around it for now by making a constructor function (using static opCall is popular), and writing your opMul as return the_constructor_func(x*s, y*s, z*s); That seems to work, although I suspect that just means that it's disabling the NRVO, which I guess isn't smart enough to do the optimization when the return value is passed through several functions. --bb Tim Fang wrote:I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00. --code--------------------- import std.stdio; void main() { Vector3 a; a.set(1,1,1); a = a*2; writefln(a.x, a.y, a.z); } struct Vector3 { float x,y,z; // constructor void set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; } Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; } }
Jan 11 2007
Tim Fang wrote:I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00. --code--------------------- import std.stdio; void main() { Vector3 a; a.set(1,1,1); a = a*2; writefln(a.x, a.y, a.z); } struct Vector3 { float x,y,z; // constructor void set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; } Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; } }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; ... } -DavidM
Jan 11 2007
I have called "a.set(1,1,1)" to init the members in main(). "David Medlock" <noone nowhere.com> Wrote :eo5bif$2sr4$1 digitaldaemon.com...Tim Fang wrote:I am very new to D and I am trying to implement a Vector3 struct, please take a look at the code below, why the output is "nananan"? I use dmd 1.00. --code--------------------- import std.stdio; void main() { Vector3 a; a.set(1,1,1); a = a*2; writefln(a.x, a.y, a.z); } struct Vector3 { float x,y,z; // constructor void set(float _x, float _y, float _z) { x = _x; y = _y; z = _z; } Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; } }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; ... } -DavidM
Jan 11 2007
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, butcreates a new, uninitialized Vector3 and returns it. This should work: ' Vector3 ret; ' ret.set(0., 0., 0.); ' // ... -mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/Vector3 opMul(float s) { Vector3 ret; ret.x =3D x*s; ret.y =3D y*s; ret.z =3D z*s; return ret; }
Jan 11 2007
Am 11.01.2007, 15:24 Uhr, schrieb mike <vertex gmx.at>:' Vector3 ret; ' ret.set(0., 0., 0.); ' // ...Ah ... of course you need to initialize with 1, not 0. :) -mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Jan 11 2007
creates a new, uninitialized Vector3 and returns it. This should work:according to the spec, structs should be default initialized. Is this a bug?
Jan 11 2007
Frank Benoit (keinfarbton) wrote:The default initialization for float/double/real is nan. Look under 'types'. -DavidMcreates a new, uninitialized Vector3 and returns it. This should work:according to the spec, structs should be default initialized. Is this a bug?
Jan 11 2007
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=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Jan 11 2007
mike wrote:Am 11.01.2007, 15:07 Uhr, schrieb Tim Fang <no spam.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". As Bill Baxter said, this is a bug introduced in DMD 0.178. /OskarI have called "a.set(1,1,1)" to init the members in main().Yeah, butcreates a new, uninitialized Vector3 and returns it. This should work:Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; }
Jan 11 2007
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 th=e =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 =3D x*s; // <-- oh, wait a sec ... this x should be initialized= ' ret.y =3D y*s; ' ret.z =3D z*s; ' return ret; ' } '} Now I'm confused ... :) Maybe it's really a bug. -mike -- = Erstellt mit Operas revolution=E4rem E-Mail-Modul: http://www.opera.com/= mail/
Jan 11 2007
' 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/
Jan 11 2007
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, butcreates 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/Vector3 opMul(float s) { Vector3 ret; ret.x = x*s; ret.y = y*s; ret.z = z*s; return ret; }
Jan 11 2007
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
Jan 11 2007
Leandro Lucarella wrote:David Medlock escribió:http://www.digitalmars.com/d/faq.html#nan --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?
Jan 11 2007
Tomas Lindquist Olsen escribió:Leandro Lucarella wrote: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-5252David Medlock escribió:http://www.digitalmars.com/d/faq.html#nanIts 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?
Jan 11 2007