Related articles |
---|
Rounding with Div and Mod operators william.rayer@virgin.net (William Rayer) (1999-05-09) |
Re: Rounding with Div and Mod operators wclodius@aol.com (1999-05-16) |
Re: Rounding with Div and Mod operators ucapjab@ucl.ac.uk (Jonathan Barker) (1999-05-16) |
Re: Rounding with Div and Mod operators nr@labrador.cs.virginia.edu (Norman Ramsey) (1999-05-16) |
Re: Rounding with Div and Mod operators guerby@acm.org (Laurent Guerby) (1999-05-16) |
Re: Rounding with Div and Mod operators anton@mips.complang.tuwien.ac.at (1999-05-16) |
Re: Rounding with Div and Mod operators Scott.Daniels@Acm.Org (Scott.David.Daniels) (1999-05-16) |
Re: Rounding with Div and Mod operators cdg@nullstone.com (Christopher Glaeser) (1999-05-16) |
Re: Rounding with Div and Mod operators johan.persson@mbox319.swipnet.se (Johan Persson) (1999-05-16) |
Re: Rounding with Div and Mod operators genew@shuswap.net (1999-05-20) |
Re: Rounding with Div and Mod operators sofkam@rpi.edu (1999-05-20) |
Re: Rounding with Div and Mod operators drh@microsoft.com (Dave Hanson) (1999-05-20) |
[8 later articles] |
From: | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
Newsgroups: | comp.compilers |
Date: | 16 May 1999 15:14:31 -0400 |
Organization: | Institut fuer Computersprachen, Technische Universitaet Wien |
References: | 99-05-039 |
Keywords: | arithmetic, design |
"William Rayer" <william.rayer@virgin.net> writes:
> n = (n div d) * d + (n mod d)
>
> What is interesting about this rule is there seem to be two ways of
> rounding that satisfy it when n or d are negative - either we round
> integers to the next lowest value or we round towards zero.
The first is called floored division, the second symmetric division.
> My question is: which rounding system is preferred and does it matter?
It does matter in some applications. Which one is preferred depends
on the application, but in most cases that I have come across floored
division is preferred.
Therefore several languages have come up with operations for both
options: Ada has mod and rem (I don't know how it defines /), Forth
has fm/mod (floored) and sm/rem (symmetric).
And here's a little gem I have from Andrew Haley for doing floored
division of a double-precision integer by a single-precision integer
with single-precision results in terms of unsigned division; the
signed number representation is 2s complement:
denomsign=denom;
if (denom < 0) {
denom = -denom;
num = -num;
}
if (num < 0)
num.hi += denom; /* single-precision add to the most significant part of
the numerator. */
quot = num u/ denom;
rem = num u% denom;
if (denomsign<0)
rem = -rem;
- anton
--
M. Anton Ertl Some things have to be seen to be believed
anton@mips.complang.tuwien.ac.at Most things have to be believed to be seen
http://www.complang.tuwien.ac.at/anton/home.html
Return to the
comp.compilers page.
Search the
comp.compilers archives again.