[M3devel] integer division rounding?
Rodney M. Bates
rodney_bates at lcwb.coop
Sun Jan 17 18:29:28 CET 2010
Tony Hosking wrote:
> What do Pascal and Modula-2 and Oberon do? M3 is in the same family...
For all of these, it is not so easy to decide what is the authoritative
definition. They all suffer from worse dialect proliferation than Modula-3.
----------------------------------------------------------------------------
Original Pascal report "The programming Language Pascal", Acta
Informatica, 1, 35-6 (1971) just says div is "division with truncation"
and the usual relationship: "m mod n = m-((m div n)*n)"
Does "truncate" mean towards zero or towards minus infinity? Read on.
----------------------------------------------------------------------------
"An Axiomatic Definition of the Programming Langauge Pascal",
by C. A. R. Hoare and N. Wirth (apparently a tech report from
Eidgenoessische Technische Hochschule Zuerich (November 1972) says only:
"3.10. (m>=0) ^ ((n>0) => m-n < (m div n)*n <= m"
leaving div undefined for either operand negative. It does give the
usual relationship.
----------------------------------------------------------------------------
"A Draft Proposal for Pascal", by A.M.Addyman (I have no citation
info for this, except the heading "technical contributions"--maybe
this was SIGPLAN?) says:
"It shall be an error if j is zero, otherwise the value of i div j
shall be such that
abs(i) - abs(j) < abs((i div j) * j) <= abs(i)
where the value shall be zero if abs(i)<abs(j), otherwise the sign
of the value shall be positive if i and j have the same sign and
negative if i and j have different signs.
This is truncating toward zero, which agrees with my memory about Pascal.
------------------------------------------------------------------------------
A "Draft ISO Standard for Pascal" that I have, dated 1982-08-12,
says the same thing about div, but adds:
"A term of the form i mod j shall be an error if j is zero or negative, otherwise the
value of i mod j shall be that value of (i-(k*j)) for integral k such that
0 <= i mod j < j."
disallowing negative j.
-----------------------------------------------------------------------------------
"Programming in Modula-2", Niklaus Wirth, 4th edition, Springer-Verlag, 1988, says:
"x DIV y is equal to the truncated quotient of x/y
x MOD y is equal to the remainder of the division x DIV y
x = (x DIV y) * y + (x MOD y), 0 <= (x MOD y) < y"
the last condition implying that MOD is either undefined or an error
for negative y.
-----------------------------------------------------------------------------------
I don't have any Oberon variant definitions quickly accessible.
I recall disliking the Pascal/M2 habit of truncating towards zero as both
inconsistent with traditional mathematics and not want you need as a programmer,
at least in the application of doing subscript arithmetic in circular buffers.
I was glad to see Modula-3 fix that.
>
> On 17 Jan 2010, at 04:36, Jay K wrote:
>
>> Modula-3 apparently specifies integer division as rounding down.
>> http://www.modula3.com/cm3/doc/reference/arithmetic.html
>>
>> C used to leave it unspecified. So it could be fast.
>> Fortran rounded to zero.
>> C's ldiv function rounds to zero.
>> C now rounds to zero with C99.
>> The rationale was that nobody using Fortran seemed to mind.
>> Java apparently rounds toward zero.
>> C# apparently rounds toward zero.
>>
>>
>> I'm assuming we are stuck being different here?
>>
>> - Jay
>>
>
More information about the M3devel
mailing list