20 May 1999 01:44:19 -0400

Related articles |
---|

[3 earlier articles] |

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) |

Re: Rounding with Div and Mod operators peter.r.wilson@boeing.com (Peter Wilson) (1999-05-20) |

Re: Rounding with Div and Mod operators mfinney@lynchburg.net (1999-05-21) |

Re: Rounding with Div and Mod operators jmoore@softmoore.com (John I. Moore, Jr.) (1999-05-22) |

[4 later articles] |

From: | genew@shuswap.net (Gene Wirchenko) |

Newsgroups: | comp.compilers |

Date: | 20 May 1999 01:44:19 -0400 |

Organization: | Okanagan Internet Junction |

References: | 99-05-039 99-05-060 |

Keywords: | arithmetic, design |

Johan Persson <johan.persson@mbox319.swipnet.se> wrote:

*>William Rayer wrote:*

*>> My question is: which rounding system is preferred and does it matter?*

*>*

*>My experience of using int, mod and div operators are that if they round*

*>towards zero they are useless if you don't know that you only are*

*>dealing with positive OR negative numbers. I usually write my own*

*>functions that round downwards and use them instead of the built-in*

*>operators. I have never come across a case where rounding towards zero*

*>is the preferred choice.*

There is a neat hack for determining digital sums that would be

cleaner if modulo rounded down to 0. (That's assuming that the

digital sum of a number is always the same as that of its abs().)

int ds(int n)

{

int baseds;

if (n==0)

return 0;

/* Discussion Point */

baseds=n%9;

if (baseds>0)

return baseds;

else

return 9;

}

This won't work for negative numbers as you have to abs() first,

but that doesn't work either because in a two's complement system,

abs(INT_MIN) could overflow (e.g. -32768 vs. 32767). At the

discussion point, you need something like

if (n<-9)

n+=9;

n=abs(n);

Sincerely,

Gene Wirchenko

Post a followup to this message

Return to the
comp.compilers page.

Search the
comp.compilers archives again.