public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Stupid off-topic question
@ 2000-08-02 12:09 Anjul Srivastava
  0 siblings, 0 replies; 8+ messages in thread
From: Anjul Srivastava @ 2000-08-02 12:09 UTC (permalink / raw)
  To: 'nbecker@fred.net', gcc

Maybe this will help you understand why gcc is correct.

main()
{
   signed   int x;
   unsigned int y;
   unsigned int z;

   x=-1;
   y=x;
   z=y%4083;
   printf("%u\n",y);
   printf("%u\n",z);
   printf("%u\n",4083*(y/4083)+z);
}

Output:

4294967295
2433
4294967295

-----Original Message-----
From: nbecker@fred.net [ mailto:nbecker@fred.net ]
Sent: Wednesday, August 02, 2000 2:00 PM
To: gcc@gcc.gnu.org
Subject: Stupid off-topic question


I can't understand this logic.

-1 % 4083 -> -1

But

 -1 % (unsigned)4083 -> 2433.

I would expect by any reasonable mathematical definition, that 

-1 % 4083 -> 4082.

This was gcc-2.95.2 i686-pc-linux-gnu.

If the generated code is really that stupid, maybe gcc should give a
warning?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Stupid off-topic question
  2000-08-03  5:03   ` nbecker
  2000-08-03  9:40     ` Michael Meissner
@ 2000-08-03 16:49     ` Geoff Keating
  1 sibling, 0 replies; 8+ messages in thread
From: Geoff Keating @ 2000-08-03 16:49 UTC (permalink / raw)
  To: nbecker; +Cc: gcc

> Cc: gcc@gcc.gnu.org
> From: nbecker@fred.net
> Date: 03 Aug 2000 08:02:52 -0400
> 
> Just to clarify, I understand how the result was obtained.  The
> problem is, I don't think this is reasonable behaviour.
> 
> In particular, the unwary user (me) writes something like:
> 
> i % j
> 
> where j happens to be size_t, an unsigned.  The result is very
> unexpected to the poor user.  Sure, it obeys the cited rule, but is
> this really the way you want your modulus operator to behave?

Well, you could try to propose your rule to the C standards
committee.  However, they've just done a major revision to the
standard, and the next one isn't due until 2009.

The behaviour on division might seem annoying, but this is certainly
what you want for +, -, and probably for multiplication.

-- 
- Geoffrey Keating <geoffk@cygnus.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Stupid off-topic question
  2000-08-03  5:03   ` nbecker
@ 2000-08-03  9:40     ` Michael Meissner
  2000-08-03 16:49     ` Geoff Keating
  1 sibling, 0 replies; 8+ messages in thread
From: Michael Meissner @ 2000-08-03  9:40 UTC (permalink / raw)
  To: nbecker; +Cc: Geoff Keating, gcc

On Thu, Aug 03, 2000 at 08:02:52AM -0400, nbecker@fred.net wrote:
> Just to clarify, I understand how the result was obtained.  The
> problem is, I don't think this is reasonable behaviour.
> 
> In particular, the unwary user (me) writes something like:
> 
> i % j
> 
> where j happens to be size_t, an unsigned.  The result is very
> unexpected to the poor user.  Sure, it obeys the cited rule, but is
> this really the way you want your modulus operator to behave?

Well given that it has behaved that way ever since Dennis Ritchie added
unsigned to the language, I would say yes that it is desired behavior (and even
if I thought it wasn't desired behavior, it is probably way too late to change
it).  Unsigned arithmetic has always meant modulo arithmetic using all of the
bits.  Patches to make the compiler warn about the possibly unintended
conversion semantics of divide/mod. if -Wconversion is used presumably would be
welcomed.

-- 
Michael Meissner, Red Hat, Inc.
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA
Work:	  meissner@redhat.com		phone: +1 978-486-9304
Non-work: meissner@spectacle-pond.org	fax:   +1 978-692-4482

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Stupid off-topic question
  2000-08-02 12:44 ` Geoff Keating
  2000-08-02 14:57   ` Toon Moene
@ 2000-08-03  5:03   ` nbecker
  2000-08-03  9:40     ` Michael Meissner
  2000-08-03 16:49     ` Geoff Keating
  1 sibling, 2 replies; 8+ messages in thread
From: nbecker @ 2000-08-03  5:03 UTC (permalink / raw)
  To: Geoff Keating; +Cc: gcc

Just to clarify, I understand how the result was obtained.  The
problem is, I don't think this is reasonable behaviour.

In particular, the unwary user (me) writes something like:

i % j

where j happens to be size_t, an unsigned.  The result is very
unexpected to the poor user.  Sure, it obeys the cited rule, but is
this really the way you want your modulus operator to behave?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Stupid off-topic question
  2000-08-02 14:57   ` Toon Moene
@ 2000-08-02 16:02     ` Geoff Keating
  0 siblings, 0 replies; 8+ messages in thread
From: Geoff Keating @ 2000-08-02 16:02 UTC (permalink / raw)
  To: toon; +Cc: nbecker, gcc

> Date: Wed, 02 Aug 2000 22:49:09 +0200
> From: Toon Moene <toon@moene.indiv.nluug.nl>

> > The rule in C99 is that (a/b)*b + a%b == a
> > and than / truncates towards 0.
>       ^^^^ that ?
> 
> Hmmm, has this changed with C99 ? 

Yes.  Before C99 it used to be machine-dependent whether it truncated
towards 0 or towards -MAX_INT.  The '(a/b)*b + a%b == a' rule was
always there, though.

-- 
- Geoffrey Keating <geoffk@cygnus.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Stupid off-topic question
  2000-08-02 12:44 ` Geoff Keating
@ 2000-08-02 14:57   ` Toon Moene
  2000-08-02 16:02     ` Geoff Keating
  2000-08-03  5:03   ` nbecker
  1 sibling, 1 reply; 8+ messages in thread
From: Toon Moene @ 2000-08-02 14:57 UTC (permalink / raw)
  To: Geoff Keating; +Cc: nbecker, gcc

Geoff Keating wrote:

> 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.
      ^^^^ that ?

Hmmm, has this changed with C99 ?  K&R II has (2.5 Arithmetic Operators,
page 41):

"The direction of truncation for / and the sign of the result for % are
machine-dependent for negative operands, ..."

Cheers,

-- 
Toon Moene - mailto:toon@moene.indiv.nluug.nl - phoneto: +31 346 214290
Saturnushof 14, 3738 XG  Maartensdijk, The Netherlands
GNU Fortran 77: http://gcc.gnu.org/onlinedocs/g77_news.html
GNU Fortran 95: http://g95.sourceforge.net/ (under construction)

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Stupid off-topic question
  2000-08-02 12:00 nbecker
@ 2000-08-02 12:44 ` Geoff Keating
  2000-08-02 14:57   ` Toon Moene
  2000-08-03  5:03   ` nbecker
  0 siblings, 2 replies; 8+ messages in thread
From: Geoff Keating @ 2000-08-02 12:44 UTC (permalink / raw)
  To: nbecker; +Cc: gcc

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>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Stupid off-topic question
@ 2000-08-02 12:00 nbecker
  2000-08-02 12:44 ` Geoff Keating
  0 siblings, 1 reply; 8+ messages in thread
From: nbecker @ 2000-08-02 12:00 UTC (permalink / raw)
  To: gcc

I can't understand this logic.

-1 % 4083 -> -1

But

 -1 % (unsigned)4083 -> 2433.

I would expect by any reasonable mathematical definition, that 

-1 % 4083 -> 4082.

This was gcc-2.95.2 i686-pc-linux-gnu.

If the generated code is really that stupid, maybe gcc should give a
warning?

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2000-08-03 16:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-02 12:09 Stupid off-topic question Anjul Srivastava
  -- strict thread matches above, loose matches on Subject: below --
2000-08-02 12:00 nbecker
2000-08-02 12:44 ` Geoff Keating
2000-08-02 14:57   ` Toon Moene
2000-08-02 16:02     ` Geoff Keating
2000-08-03  5:03   ` nbecker
2000-08-03  9:40     ` Michael Meissner
2000-08-03 16:49     ` Geoff Keating

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).