From: corbett@lupa.eng.sun.com (Robert Corbett) Subject: Re: % on negative numbers Date: 04 Dec 1998 Newsgroups: comp.std.c In article <3669579e.3725172@news.dial.pipex.com>, Mathew Hendry <scampi@dial.pipex.com> wrote: > >I'm curious. Are most machines consistent in their implementation of >division/modulo operations involving negative numbers? Except in cases involving overflow, which presumably are not covered by the C standard, every architecture I have seen truncates toward zero when the mathematical result of an integer division is not an integer. The reason is simple. Unlike multiplication, there is no reasonable way to do signed division directly in hardware. (BTW, the direct hardware implementations of signed multiplication are very clever; they are well worth studying). The usual implementation of signed division does an unsigned division and then fixes up the result, negating if necessary. Since the result will already have been chopped, getting any result other than truncate toward zero would take extra time and circuitry. Hardware designers tend not to like to use either if they can avoid it. Most machines implement modulo using software, so there is greater variation. Sincerely, Bob Corbett

Index Home About Blog