This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Stupid off-topic question
- To: nbecker at fred dot net
- Subject: Re: Stupid off-topic question
- From: Geoff Keating <geoffk at cygnus dot com>
- Date: 02 Aug 2000 12:44:48 -0700
- CC: gcc at gcc dot gnu dot org
- References: <x88r987pjy9.fsf@adglinux1.hns.com>
nbecker@fred.net writes:
> I can't understand this logic.
>
> -1 % 4083 -> -1
The rule in C99 is that (a/b)*b + a%b == a
and than / truncates towards 0.
Thus,
-1 / 4083 == 0
-1 % 4083 == -1
0 * 4083 + -1 == -1
as required.
> But
>
> -1 % (unsigned)4083 -> 2433.
This turns into
(unsigned)-1 % 4083
on your machine, (unsigned)-1 is 4294967295, under the rules for
converting signed integers to unsigned. Thus the result you saw.
> I would expect by any reasonable mathematical definition, that
>
> -1 % 4083 -> 4082.
I think you would probably prefer truncate-towards-zero for / than
have % work like this.
--
- Geoffrey Keating <geoffk@cygnus.com>