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:
Glenn Cole <[log in to unmask]>
Reply To:
Date:
Wed, 12 Nov 1997 11:59:10 -0800
Content-Type:
text/plain
Parts/Attachments:
text/plain (108 lines)
Klaus Franke writes:

> a nice routine, i found in some of our old c-sources:
>
> #define leap_year(yy)    ((yy & 3) == 0 ? 1 : 0)


Glenn responds:

> Nice, just not accurate.


Art asks:

> Ok for those of us who don't know 'C'?
>
>     Why isn't it accurate Glenn?


The short answer is, it tests only for divisibility by 4,
not the "and not by 100, or it is divisible by 400" parts
of the rule. As such, since 2000 happens to be a leap year,
it's probably fine for most apps.


Here's how it works (if you're interested):

- the "yy & 3" is a bitwise AND between the year and 3
  (thus examining only the two low-order bits). If either
  of these two bits are set in the year, then it is NOT
  divisible by 4; if both bits are clear, then it IS divisible
  by 4.

  Note also that this works whether the year has 2 or 4 digits.


- "==" is C's test for equality (numeric items only).

  This can be the source of a bug, as a quick typist may INTEND

        if( i == 3 )

  but ACTUALLY type

        if( i = 3 )

  The latter is allowable, but it carries a very different meaning.
  Instead of TESTING i, it first assigns (!) 3 to i, then tests to
  see if the result is non-zero (like BASIC/3000).

  Some try to ward against this by placing the constant first

        if( 3 == i )

  since a slip of the fingers results in the illegal construct

        if( 3 = i )


- "?:" is the only ternary operator in C, and works similar to
  the IF statement in Excel, or to one form of the IF in SPL.
  Here's the deal:

        expression ? result_if_true : result_if_false

  As with BASIC/3000, zero is "false;" non-zero is "true."


- "#define" serves many purposes:

     1. it defines flags that can be tested later
                #define __GNUC
                #ifdef  __GNUC  (yep, defined above)

     2. it defines straight text substitution, as in COBOL II:
                #define ARRAY_SIZE  30
                int a[ ARRAY_SIZE ];
                int b[ ARRAY_SIZE ];

     3. it defines macros, again as in COBOL II, that also
        work by text substitution at compile time. For example,

                #define SQUARE_ME(x)  ((x)*(x))
                y = SQUARE_ME(i+3);

        is compiled as
                y = (i+3)*(i+3)

        This is why the parentheses are necessary around (x).
        Otherwise, it would compile
                y = i+3*i+3

        which is not what is intended.


Aren't you glad you asked? ;)


> Art "hmm... I know Snobol?  Does that count?hehehe" Bahrs

EVERY language counts, especially those that do not follow
the ALGOL-style mold. SNOBOL, LISP, APL, Prolog, and the like
all help us to "think different." ;)

--Glenn Cole
  Software al dente, Inc.
  [log in to unmask]

ATOM RSS1 RSS2