HP3000-L Archives

November 1997, Week 2

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Reply To:
Date:
Wed, 12 Nov 1997 13:39:22 CST
Content-Type:
text/plain
Parts/Attachments:
text/plain (46 lines)
On Wed, 12 Nov 1997 10:55:42 -0800, Art Bahrs <[log in to unmask]> wrote:
|Ok for those of us who don't know 'C'?
|
|     Why isn't it accurate Glenn?

|>>> Glenn Cole <[log in to unmask]> 11/12/97 10:44am
|>>>
|Klaus Franke writes:
|
|> a nice routine, i found in some of our old c-sources:
|>
|> #define leap_year(yy)    ((yy & 3) == 0 ? 1 : 0)
|
|Nice, just not accurate.

It should be something like:

   #define leap_year(yy)   ( (yy) %   4 ? 0       \
                           : (yy) % 100 ? 1       \
                           : (yy) % 400 ? 0       \
                                        : 1)

For the benefit of the non-C folks who are curious, "%" is the modulus
operator (aka mod or remainder).  And 0 is interpreted by C as FALSE,
any other numeric value is interpreted as TRUE.  The "? :" operator is
basicly an "if...then...else" construct in an expression.  So the
macro says "If the year is not divisible by 4, it's not; else if the year
is not divisible by 100, it is; else if the year is not divisible by 400,
it's not; else, it is."

It's not pretty, but I think it's accurate and succinct.

P.S.  I put all the references to yy in parentheses in case an expression is
      used as the parameter, to prevent precedence rules from becoming
      confused.  The C preprocessor operates on semantic tokens before the
      actual compiler ever sees the source.  I recommend all such formal
      parms to a C define macro always use parentheses for this reason.
      It can be an especially hard bug to detect when it shows up.
--
Jeff Woods
[log in to unmask] at Unison Software
[log in to unmask]   at home  [PGP key available here via finger]

"There is no sadder sight in the world than to see a beautiful
 theory killed by brutal fact."  --  Thomas Huxley (1825-1895)

ATOM RSS1 RSS2