Bug 3165 – What kind of integer division does D use?

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2009-07-12T01:33:00Z
Last change time
2015-06-09T05:15:16Z
Keywords
spec
Assigned to
nobody
Creator
m.helvensteijn
Blocks
677

Comments

Comment #0 by m.helvensteijn — 2009-07-12T01:33:00Z
Specify what kind of integer division D uses. The description is ambiguous when one or both operands of / are negative. So, in other words, what does D return from: 3 / 2 3 / -2 -3 / 2 -3 / -2 Such information should be part of the language specification.
Comment #1 by m.helvensteijn — 2009-07-12T14:11:52Z
I've ran the following test: ---------------------------------- import std.stdio; int main() { writefln(8/3); writefln(8/(-3)); writefln((-8)/3); writefln((-8)/(-3)); writefln(8%3); writefln(8%(-3)); writefln((-8)%3); writefln((-8)%(-3)); return 0; } ---------------------------------- outputs the following ---------- 2 -2 -2 2 2 2 -2 -2 ---------- So DMD uses truncated division. The quotient rounds towards zero and the remainder has the same sign as the dividend.
Comment #2 by smjg — 2009-07-14T16:30:04Z
Changing URL since this is a D1 spec issue just the same. But you could well ask why it defines it as undefined for % but makes no comment at all (even on whether it's rounded up, down or off for +ve operands) for /. I've felt for ages that the programmer ought to have the choice between trunc and floor versions for both http://www.digitalmars.com/d/archives/digitalmars/D/13125.html but that's another matter.
Comment #3 by bugzilla — 2009-09-03T13:25:32Z
Fixed dmd 1.047 and 2.032