Bug 350 – Modulo for negative values is undefined

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-09-16T12:31:00Z
Last change time
2014-02-15T13:19:24Z
Keywords
spec
Assigned to
bugzilla
Creator
matti.niemenmaa+dbugzilla

Comments

Comment #0 by matti.niemenmaa+dbugzilla — 2006-09-16T12:31:41Z
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.
Comment #1 by smjg — 2006-09-16T17:33:43Z
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
Comment #2 by matti.niemenmaa+dbugzilla — 2006-09-17T01:35:50Z
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.
Comment #3 by bugzilla — 2006-11-19T04:16:58Z
Documented DMD 0.173