Thread overview
[Issue 350] New: Modulo doesn't work for negative values
Sep 16, 2006
d-bugmail
[Issue 350] Modulo for negative values is undefined
Sep 16, 2006
d-bugmail
Sep 17, 2006
d-bugmail
Nov 19, 2006
d-bugmail
September 16, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=350

           Summary: Modulo doesn't work for negative values
           Product: D
           Version: 0.166
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: deewiant@gmail.com


void main() {
   assert (-1 % 10 == 9); // fails
}

ANSI C leaves modulo undefined for negative values, which is fine, but D doesn't mention anything like that in the spec. If this is intentional, it deserves a mention.


-- 

September 16, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=350


smjg@iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com
           Keywords|                            |spec
            Summary|Modulo doesn't work for     |Modulo for negative values
                   |negative values             |is undefined




------- Comment #1 from smjg@iname.com  2006-09-16 17:33 -------
To claim simply that it "doesn't work" is nonsense.  Did you actually try experimenting to see what it does?

But see also

http://tinyurl.com/m4rms


-- 

September 17, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=350





------- Comment #2 from deewiant@gmail.com  2006-09-17 01:35 -------
import std.stdio : writefln;
import std.stdio : writefln;

void main() {
        writefln(-1 % 10);
        writefln(-1 % 5);
        writefln(-1 % 3);
        writefln(-1 % 2);
        writefln(-2 % 8);
        writefln(-28 % 7);
        writefln(-1 % -1);
        writefln(-2 % -2);
        writefln(-9 % -2);
        writefln(8 % -2);
        writefln(16 % -2);
        writefln(9 % -2);
        writefln(5 % -4);
}
--
Where both values are negative, it gives the correct answer.

Where only the dividend or divisor is negative, it appears to return (remainder-divisor) instead of just the remainder. Following your link, it seems that this is the so-called "trunc-mod", common in many other programming languages.

I'm sorry, I was in a bit of a hurry when I posted this report and couldn't be bothered to take a detailed look at the issue.

Anyhoo, a mention in the spec would be nice.


-- 

November 19, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=350


bugzilla@digitalmars.com changed:

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




------- Comment #3 from bugzilla@digitalmars.com  2006-11-19 04:16 -------
Documented DMD 0.173


--