This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Stupid off-topic question



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>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]