Re: Rounding with Div and Mod operators

"Scott.David.Daniels" <Scott.Daniels@Acm.Org>
16 May 1999 15:24:34 -0400

          From comp.compilers

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)
Re: Rounding with Div and Mod operators anton@mips.complang.tuwien.ac.at (1999-05-20)
[7 later articles]
| List of all articles for this month |

From: "Scott.David.Daniels" <Scott.Daniels@Acm.Org>
Newsgroups: comp.compilers
Date: 16 May 1999 15:24:34 -0400
Organization: Compilers Central
References: 99-05-039
Keywords: arithmetic, design

One thing I'd mention is that each CPU manufacturer builds one of the
definitions in hardware. If you don't have a definition that matches
the CPU, your definition will penalize code where both n and d are
non- negative, but are not provably (in the compiler) so. This
difference is the only justification for a rule such as C has (not
well defined), but it is, at least to me, a powerful justification.
You might try having several operators yourself: towards zero, down,
and fastest. This would allow you to irritate most every partisan
equally.


Here's an interesting question assuming a two's complement machine:
Let MinInf = <sign bit on, all others off = most negative int = -1Q0>
(points to those other old fogeys who know what -1Q0 means). What is
-1 rem MinInf ?


William Rayer wrote:
> Dear Newsgroup
.... what to do about div and mod ...
> n = (n div d) * d + (n mod d)
.... nice table showing the problem at zero but not at integer limits.
> I think this is because on the PC the default rounding of the Intel
> IDIV opcode is towards zero.


Actually, I think the rounding of the IDIV opcode is is behavior, not
a default; I don't know how to change it to do anything else. This
could simply be lack of data on my part, but I think not.


....
> (3) The language I'm developing also includes an "int" operator which
> takes a real number operand and returns an integer which is the
> operand rounded down to the next lowest integer (if the operand is an
> exact integer value it is unaltered). Thus if I define div using next
> lowest integer rounding, it is redundant as (n div d = int(n/d))
> always. Thus rounding to the lower integer is redundant...


You might want to call this thing "Floor" (or some such name).
There is also Ceiling, (which is -Floor(-X) for non-boundary cases).
You might also want to have a towards zero version so you can have the
function with the largest range of input values.


Be aware your users may feel cheated when Int( 10 * 0.1 ) = 9.


-Scott David Daniels
Scott.Daniels@Acm.Org


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.