www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10215] New: const causes false float calculation

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215

           Summary: const causes false float calculation
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: rswhite4 googlemail.com


--- Comment #0 from rswhite4 googlemail.com 2013-05-30 12:38:47 PDT ---
Example #1:
----
import std.stdio;

class Foo {
public:
    float a = 3.14 / 2 / 0.5;
    const float b = 3.14 / 2 / 0.5;
}

void main()
{
    float a = 3.14 / 2 / 0.5;
    const float b = 3.14 / 2 / 0.5;

    writefln("a = %f, b = %f", a, b);

    Foo f = new Foo();
    writefln("a = %f, b = %f", f.a, f.b);
}
----
Expected:
a = 3.140000, b = 3.140000
a = 3.140000, b = 3.140000

I get:
a = 3.140000, b = 3.140000
a = 3.140000, b = 0.000000

Example #2:
----
import std.math;
import std.stdio;

class Foo {
public:
    const float airTime = 0.5;

    //Speed to progress the jump
    const float jumpSinWaveSpeed = PI_2 / airTime;
    float jumpSinWaveSpeed2 = PI_2 / airTime;
}

void main() {
    Foo f = new Foo();
    writeln(f.jumpSinWaveSpeed);
    writeln(f.jumpSinWaveSpeed2);
}
----

Expected:
3.14159
3.14159

I get:
6.39384e-39
3.14159

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-05-30
14:18:08 PDT ---
Compiler options, OS, 32/64bit, and compiler version please. I can't reproduce
this locally.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215



--- Comment #2 from rswhite4 googlemail.com 2013-05-30 14:25:36 PDT ---
No compiler options and on Linux / Windows with dmd 2.063.
"Works" also on Dpaste:
http://dpaste.1azy.net/e6e3bb7b
and
http://dpaste.1azy.net/e1975814

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-05-30
14:28:04 PDT ---
(In reply to comment #2)
 No compiler options and on Linux / Windows with dmd 2.063.
 "Works" also on Dpaste:
 http://dpaste.1azy.net/e6e3bb7b
 and
 http://dpaste.1azy.net/e1975814
Ok I can recreate it in 2.063, but not in git-head. This seems like a pretty huge bug to have in a release version. Whatever it was it seems to be fixed now. Anyone know what happened? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215



--- Comment #4 from rswhite4 googlemail.com 2013-05-30 14:32:50 PDT ---
Maybe we should offer a patch for this bug? It's a bit annoying. ;)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|const causes false float    |Regression (2.063 release):
                   |calculation                 |const causes wrong float
                   |                            |calculation
           Severity|major                       |regression


--- Comment #5 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-05-30
15:00:22 PDT ---
I can't seem to bisect this. Does anyone know which specific commit the 2.063
release is based on?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215


Tiberiu Gal <galtiberiu gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |galtiberiu gmail.com


--- Comment #6 from Tiberiu Gal <galtiberiu gmail.com> 2013-05-31 01:04:56 PDT
---
import std.stdio, std.math;


class Foo {
public:

    const int i = 3;
    auto getI(){ return i ; }    
    int j = 3;
    auto getJ(){ return j;}
    const float x = 0.4;
    auto getX(){ return x; }
    float y = 0.4;
    auto getY(){ return y; }    


}


struct Soo {

    const int i = 3;
    auto getI(){ return i ; }    
    int j = 3;
    auto getJ(){ return j;}
    const float x = 0.4;
    auto getX(){ return x; }
    float y = 0.4;
    auto getY(){ return y; }    
}

void main() {



    writeln("with local vars");
    const float x = 3.12;
    const int y = 3;
    writeln(x , " / " , x == 3.12);
    writeln(y, " / ", y == 3);

    writeln("with classes");
    Foo f = new Foo();
    writeln(f.x, " ", f.getX(), " / ", f.x == f.getX() );
    writeln(f.y, " ", f.getY(), " / ", f.y == f.getY() );
    writeln(f.i, " ", f.getI(), " / ", f.i == f.getI() );
    writeln(f.j, " ", f.getJ(), " / ", f.j == f.getJ() );


    writeln("now with structs");
    Soo s;
    writeln(s.x, " ", s.getX(), " / ", s.x == s.getX() );
    writeln(s.y, " ", s.getY(), " / ", s.y == s.getY() );
    writeln(s.i, " ", s.getI(), " / ", s.i == s.getI() );
    writeln(s.j, " ", s.getJ(), " / ", s.j == s.getJ() );


}
// outputs 
/**

with local vars
3.12 / true
3 / true
with classes
6.09598e-39 0.4 / false
0.4 0.4 / true
4350240 3 / false
3 3 / true
now with structs
4.2039e-45 0.4 / false
0.4 0.4 / true
3 3 / true
3 3 / true

*/

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215



--- Comment #7 from Tiberiu Gal <galtiberiu gmail.com> 2013-05-31 01:17:04 PDT
---
it's not about calculations, it's about accessing const fields in classes and
structs. 
it works perfectly with local variables, it works when accessing the them from
within the class itself.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215



--- Comment #8 from rswhite4 googlemail.com 2013-05-31 03:27:14 PDT ---
Does anyone knows in the meanwhile, which commit solved the problem?
The workaround is to use enum but it would be nice if const would work in dmd
2.063...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code


--- Comment #9 from Kenji Hara <k.hara.pg gmail.com> 2013-06-01 08:10:52 PDT ---
(In reply to comment #8)
 Does anyone knows in the meanwhile, which commit solved the problem?
 The workaround is to use enum but it would be nice if const would work in dmd
 2.063...
This bug reproduces just only with dmd2.063 "release". Current 2.063 branch in github http://github.com/D-Programming-Language/dmd/branches/2.063 and released zip does not contain exactly same source code. I found a difference between them, in expression.c DotVarExp::semantic. https://github.com/D-Programming-Language/dmd/blob/2.063/src/expression.c#L7501 The line "#if PULL93" and "#endif" does not exist in released src/expression.c, and the bug reproduces 2.063 branch + removing the #if and #endif lines. Therefore, I can say that the root cause was exists in the mis-operation for building and packaging release files. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 01 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215



--- Comment #10 from Andrej Mitrovic <andrej.mitrovich gmail.com> 2013-06-01
08:21:02 PDT ---
(In reply to comment #9)
 Therefore, I can say that the root cause was exists in the mis-operation for
 building and packaging release files.
I figured that might be the problem. This is another reason why we have to have an open release process, meaning we know exactly how and when a build is made. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 01 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10215


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #11 from Kenji Hara <k.hara.pg gmail.com> 2013-06-27 21:06:15 PDT
---
Fixed in 2.063.2 release.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 27 2013