Bug 7774 – Floating point mod using %= does not properly update field while inside member function
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2012-03-25T12:33:00Z
Last change time
2012-03-26T04:24:47Z
Assigned to
nobody
Creator
vangelisforever
Comments
Comment #0 by vangelisforever — 2012-03-25T12:33:25Z
Try next code:
import std.stdio;
template TX(T)
{
struct A
{
T x;
T y;
public void opOpAssign(string op)(double d)
{
debug writeln(op);
if (op == "+")// +=
{
x += cast(T)d;
y += cast(T)d;
} else
if (op == "-") // -=
{
x -= cast(T)d;
y -= cast(T)d;
} else
if (op == "*") // *=
{
x *= cast(T)d;
y *= cast(T)d;
} else
if (op == "/") // /=
{
x /= cast(T)d;
y /= cast(T)d;
} else
if (op == "%") // %=
{
x %= cast(T)d;
y %= cast(T)d;
}
}
}
}
void main(string[] args)
{
TX!(double).A a;
a.x =10;
a.y = 10;
a%=8.0;
writeln(a.x, " ", a.y);
}
You'll see that the a.x won't change. It must be 2 after %= operation.
Thank you.
Comment #1 by dmitry.olsh — 2012-03-25T12:59:59Z
I'm using your exact code, it works fine here on dmd2.059head win7 and prints
2 2
Your version of dmd, system spec?
PS: I totally expected thouse ifs to be static ifs.
Comment #2 by bearophile_hugs — 2012-03-25T13:06:05Z
If confirmed this bug report needs a better name :-)
(In reply to comment #1)
> PS: I totally expected thouse ifs to be static ifs.
Those variables are known at compile-time, so a good compiler is supposed to remove the useless if branches (quite probably dmd does it).
Comment #3 by timon.gehr — 2012-03-25T13:48:49Z
You have found a bug in DMD 2.058, not in D2.
Smaller test case:
struct A{
double x;
void f(double d){x%=cast(double)d;}
}
void main(){
auto a=A(10);
a.f(8);
assert(a.x!=10.0);
}
Works with DMD 2.059head, fails with DMD 2.058.
Comment #4 by vangelisforever — 2012-03-25T23:46:44Z
(In reply to comment #3)
> You have found a bug in DMD 2.058, not in D2.
>
>
> Smaller test case:
> struct A{
> double x;
> void f(double d){x%=cast(double)d;}
> }
> void main(){
> auto a=A(10);
> a.f(8);
> assert(a.x!=10.0);
> }
>
> Works with DMD 2.059head, fails with DMD 2.058.
How can I get a DMD 2.059 linux version or DMD 2.0?
Comment #5 by timon.gehr — 2012-03-26T00:04:47Z
(In reply to comment #4)
>
> How can I get a DMD 2.059 linux version or DMD 2.0?
It hasn't been released yet, but you can build it from source:
https://github.com/D-Programming-Language/dmd
Comment #6 by schveiguy — 2012-03-26T04:24:47Z
The proper designation for this bug should be either "Fixed" or "Duplicate". I don't know which, because I don't know enough about the compiler to find out which code changed.
Correcting description. Please try and create a better description next time, thanks.