Xref: utzoo comp.misc:8385 comp.lang.misc:4321 comp.arch:14370 Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!mcsun!ukc!edcastle!lfcs!db From: db@lfcs.ed.ac.uk (Dave Berry) Newsgroups: comp.misc,comp.lang.misc,comp.arch Subject: Re: Modulus Message-ID: <2573@castle.ed.ac.uk> Date: 2 Mar 90 13:37:47 GMT References: <981@m1.cs.man.ac.uk> <12115@goofy.megatest.UUCP> Reply-To: db@lfcs.ed.ac.uk (Dave Berry) Organization: Laboratory for the Foundations of Computer Science, Edinburgh U Lines: 52 In article <12115@goofy.megatest.UUCP> djones@megatest.UUCP (Dave Jones) writes: >Just now I did a little experimenting. On Sun-3, release 4.3. In both C >and Pascal, division is correct, but mod is wrong. > >In other words, they have it exactly backwards from the ANSI Pascal >Standard. Not right mathematically. Not right according to the standard. >You got to wonder, how come? Almost certainly becasue that's how the hardware does it. Almost all processors implements integer division and modulus that way.o Questions to comp.arch readers (who I've just tuned in): 1. Is the "normal" method for calculating integer division and modulus intrinsically faster than the mathematically expected definition (that rounds towards minus infinity), or is it just convention that it's implemented this way? 2. If everyone was suddenly to agree that the mathematically expected definition should be adopted as a standard, could it be done quicker in hardware than with software over the existing hardware? There is some weight to arguments that languages should support the round towards zero versions. Standard ML defines div and mod the mathematically expected way, and this has produced criticism from some users and implementers that use of these operators on positive arguments is unnecessarily penalised, since they can't use the versions provided by the hardware. It is possible that the definition might be amended to support both definitions. I note that the ISO draft proposal for standard computer arithmetic (published in SIGPLAN notices, Jan. 1990) allows either or both versions of div and mod. For reference, the Definition of Standard ML defines div and mod as follows: i mod d, i div d return integers r, q determined by the equation d * q + r = i, where either 0 <= r < d or d < r <= 0. Thus r has the same sign as d. [An exception is raised] if d = 0. [nb. the operators used in this definition are those of arithmetic, not of the language.] What sign i mod d should have when d < 0 (in an arbitrary language, not ML) was discussd in several other articles recently. Dave Berry, LFCS, Edinburgh Uni. db%lfcs.ed.ac.uk@nsfnet-relay.ac.uk "The thought of someone sharing one's own preferences without also sharing one's aversions strikes most people as utterly inconceivable." - C. A. Tripp.