www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Strange behaviour of enums in for loops

reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
I've found what I believe to be a pretty serious bug in DMD (I've tried 
it with DMD 2.031):

    enum real ONE = 1.0;
    for (real x=0.0; x<=10.0; x+=ONE)  writeln(x);

The above loops only once, printing "0". And it gets weirder: Replace 
real by double, and the program starts a near-infinite loop, spitting 
out values on the order of 1e-312. Using float gives the same result as 
real.

Removing "enum" from the declaration of ONE makes the program behave as 
expected, as does writing "x+=1.0" in the for loop. The following works, 
which means it's probably not an issue with the += operator in general:

    enum real ONE = 1.0;
    real x = 1.0;
    x += ONE;
    assert (x == 2.0);

I can't seem to find an existing report on this issue in Bugzilla, but I 
find it hard to believe this hasn't been noticed before. That's why I 
thought I'd bring it up here before writing a bug report.

-Lars
Jul 17 2009
next sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Lars T. Kyllingstad wrote:
 I've found what I believe to be a pretty serious bug in DMD (I've tried 
 it with DMD 2.031):
 
    enum real ONE = 1.0;
    for (real x=0.0; x<=10.0; x+=ONE)  writeln(x);
 
 The above loops only once, printing "0".
By the way: x becomes NaN after the first iteration, which is why the loop ends. -Lars
Jul 17 2009
prev sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> wrote in message 
news:h3pooc$13ii$1 digitalmars.com...
 I can't seem to find an existing report on this issue in Bugzilla, but I 
 find it hard to believe this hasn't been noticed before. That's why I 
 thought I'd bring it up here before writing a bug report.
Enums have historically been so screwy (buggy) in D maybe people have been avoiding them. Or, maybe I'm not the only one who insists on using "const" for constants instead of "enum", even though D1's const doesn't actually create a true constant (It'd be rare to see an attempt to use a *real* enumeration value in that mannar).
Jul 17 2009
parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Nick Sabalausky wrote:
 "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> wrote in message 
 news:h3pooc$13ii$1 digitalmars.com...
 I can't seem to find an existing report on this issue in Bugzilla, but I 
 find it hard to believe this hasn't been noticed before. That's why I 
 thought I'd bring it up here before writing a bug report.
Enums have historically been so screwy (buggy) in D maybe people have been avoiding them. Or, maybe I'm not the only one who insists on using "const" for constants instead of "enum", even though D1's const doesn't actually create a true constant (It'd be rare to see an attempt to use a *real* enumeration value in that mannar).
I'm using D2, and if I'm not mistaken enum is the only way to define manifest constants. It would be nice to know if anyone can reproduce this bug. In that case I'll add it to Bugzilla. -Lars
Jul 18 2009
parent Max Samukha <spambox d-coding.com> writes:
On Sat, 18 Jul 2009 16:55:51 +0200, "Lars T. Kyllingstad"
<public kyllingen.NOSPAMnet> wrote:

Nick Sabalausky wrote:
 "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> wrote in message 
 news:h3pooc$13ii$1 digitalmars.com...
 I can't seem to find an existing report on this issue in Bugzilla, but I 
 find it hard to believe this hasn't been noticed before. That's why I 
 thought I'd bring it up here before writing a bug report.
Enums have historically been so screwy (buggy) in D maybe people have been avoiding them. Or, maybe I'm not the only one who insists on using "const" for constants instead of "enum", even though D1's const doesn't actually create a true constant (It'd be rare to see an attempt to use a *real* enumeration value in that mannar).
I'm using D2, and if I'm not mistaken enum is the only way to define manifest constants. It would be nice to know if anyone can reproduce this bug. In that case I'll add it to Bugzilla. -Lars
This is obviously a bug. Please add it. A possible workaround: enum { real ONE = 1.0 } for (real x=0.0; x<=10.0; x+=ONE) writeln(x);
Jul 18 2009