comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Zeller's Algorithm
Date: Wed, 31 Jul 2002 21:08:39 GMT
Date: 2002-07-31T21:08:39+00:00	[thread overview]
Message-ID: <wcck7nb1u7c.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 1028145549.13341@master.nyc.kbcfp.com

Hyman Rosen <hyrosen@mail.com> writes:

> Darren New wrote:
> > Robert A Duff wrote:
> >>Doesn't this make the % operator totally useless for negative numbers?
> > Yes.
> 
> No. It is required in C or C++ that for non-zero b,
> 	a / b * b + a % b == a
> 
> When division rounds towards zero, as is usually desired
> (the C++ standard claims that this is what the Fortran
> standard requires), this forces the remainder operation
> to return negative results. Eg., we want -3 / 2 == -1,
> so we must have -3 % 2 == -1 for the requirement to hold.

Hmm.  You're just "blaming" the fact that "%" is ill-defined on the fact
that "/" is ill-defined.  Wouldn't it be easier to understand that the
code is portable (and correct), if rounding were deterministic?
Can you give an example of an algorithm that takes advantage of the
above equivalence, but doesn't care which way rounding goes?

It's OK to blame this language flaw on history (C was implemented before
being standardized, unlike Ada -- we could have a nice thread discussing
the advantages and disadvantages of each approach), but to claim it's
not a flaw seems wrong.

By the way, how often do folks use "mod" or "rem" on negative numbers?
In *my* code, the numbers are (almost?) always nonnegative (and the
compiler knows that, because of subtype ranges, and can optimize
accordingly).

If I need to know the difference between "mod" and "rem", I have to look
it up every time, because I can't remember it.

- Bob



  parent reply	other threads:[~2002-07-31 21:08 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-23 23:56 Zeller's Algorithm Paul A Storm
2002-07-24  7:59 ` Thomas Wolf
2002-07-24 21:30   ` Paul A Storm
2002-07-29  8:29     ` Thomas Wolf
2002-07-30  4:35       ` Robert Dewar
2002-07-30  8:20         ` Thomas Wolf
2002-07-30 17:17 ` Rick Maffei
2002-07-30 22:10   ` Paul A Storm
2002-07-31 10:51     ` Thomas Wolf
2002-07-31 14:33       ` Frank J. Lhota
2002-07-31 19:13         ` Robert A Duff
2002-07-31 19:26           ` Darren New
2002-07-31 20:01             ` Hyman Rosen
2002-07-31 20:15               ` Frank J. Lhota
2002-07-31 21:08               ` Robert A Duff [this message]
2002-07-31 22:34                 ` Hyman Rosen
2002-07-31 22:57                   ` Robert A Duff
2002-08-01 15:17                     ` Hyman Rosen
2002-08-01 16:41                       ` Frank J. Lhota
2002-07-31 21:15               ` Dan Nagle
2002-07-31 22:36                 ` Hyman Rosen
2002-07-31 22:23         ` Matthew Woodcraft
2002-08-02  7:21         ` Thomas Wolf
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox