[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Bug modulo operator on negative numbers
- From: Viacheslav Usov <via.usov@...>
- Date: Wed, 18 Aug 2021 10:37:43 +0200
On Tue, Aug 17, 2021 at 9:17 PM Viacheslav Usov <via.usov@gmail.com> wrote:
> Fortran was made for IBM 704, whose fixed point division had a
> different rule, viz., "the sign of the remainder always agrees with
> the sign of the dividend"
This is wrong, and it is actually the same rule. I made a mistake here:
> 3 div -2 = -2 (rem 2)
Obviously (-2) * (-2) + 2 = 6, not 3. The correct operation is
3 div -2 = -1 (rem 1)
so that (-1) * (-2) + 1 = 3.
Thus the Fortran rule followed the IBM 701/704 CPU rule. And so did C,
half a century later.
Cheers,
V.