3165 – What kind of integer division does D use?

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3165 - What kind of integer division does D use?
Summary: What kind of integer division does D use?
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL: http://www.digitalmars.com/d/1.0/expr...
Keywords: spec
Depends on:
Blocks: 677
Show dependency tree / graph
Reported: 2009年07月12日 01:33 UTC by Michiel Helvensteijn
Modified: 2015年06月09日 05:15 UTC (History)
2 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Michiel Helvensteijn 2009年07月12日 01:33:00 UTC
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 Michiel Helvensteijn 2009年07月12日 14:11:52 UTC
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 Stewart Gordon 2009年07月14日 16:30:04 UTC
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 Walter Bright 2009年09月03日 13:25:32 UTC
Fixed dmd 1.047 and 2.032


AltStyle によって変換されたページ (->オリジナル) /