From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,287e8ed2b0c0aabf X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-07-31 14:10:00 PST Newsgroups: comp.lang.ada Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!nntp.abs.net!uunet!dca.uu.net!ash.uu.net!world!news From: Robert A Duff Subject: Re: Zeller's Algorithm Sender: news@world.std.com (Mr Usenet Himself) Message-ID: Date: Wed, 31 Jul 2002 21:08:39 GMT References: <3D3DED1F.24DE3AC8@lmco.com> <3D46CA0E.BCD17D9C@none.provided> <3D470EEE.811F758D@lmco.com> <719a5d07.0207310251.8f1adef@posting.google.com> <3D483A0B.46EB2F7C@san.rr.com> <1028145549.13341@master.nyc.kbcfp.com> NNTP-Posting-Host: shell01.theworld.com Organization: The World Public Access UNIX, Brookline, MA X-Newsreader: Gnus v5.7/Emacs 20.7 Xref: archiver1.google.com comp.lang.ada:27536 Date: 2002-07-31T21:08:39+00:00 List-Id: Hyman Rosen 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