Re: Sign conventions for remainder
From: mike.staniforth (mike.staniforth_at_btinternet.com)
Date: 06/18/04
- Next message: David C. Ullrich: "Re: Facts That Make Walz Run."
- Previous message: ošin: "Re: JSH: The Hammer is coming"
- In reply to: Michael Stemper: "Sign conventions for remainder"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 18 Jun 2004 23:34:49 +0100
"Michael Stemper" <mstemper@siemens-emis.com> wrote in message
news:200406171732.i5HHWRG37280@mickey.empros.com...
> Just for fun, I've been implementing a package to perform arithmetic on
> integers of arbitrary size. Addition, subtraction, and multiplication
> were all pretty straight-forward. (Subtraction was the easiest!) But,
> when I prepared to implement division, I realized that I'm not aware of
> the conventions for remainders.
>
There is no universally agreed convention. I use Visual Basic in Excel for a
lot of my mathematical programming, and Microsoft manage to use two
different conventions in the same package. Visual Basic has an integer
divide function and a mod (remainder) function. The results obtained for
different sign combinations are:
39/5 = 7 rem 4
39/(-5) = -7 rem 4
(-39)/5 = -7 rem -4
(-39)/(-5) = 7 rem -4
Excel also has a MOD function. This returns the following results:
39/5 = x rem 4
39/(-5) = x rem -1
(-39)/5 = x rem -1
(-39)/(-5) = x rem -4
Excel does not have an integer divide function, so the value of x in the
above list cannot be generated directly. Instead, it is necessary to perform
the division using normal floating point division (giving the answer 7.8
or -7.8 as appropriate) and then convert it to an integer using either the
INT or TRUNC function. INT rounds to the integer below, so INT(-7.8) = -8,
whereas TRUNC discards the fractional part, so TRUNC(-7.8) = -7. It is
necessary to choose the INT function for consistency with the results
returned by MOD (eg (-39)/5 must be -8 rem 1).
- Next message: David C. Ullrich: "Re: Facts That Make Walz Run."
- Previous message: ošin: "Re: JSH: The Hammer is coming"
- In reply to: Michael Stemper: "Sign conventions for remainder"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|