[R] figure out whether 1 is between two numbers
(Ted Harding)
Ted.Harding at nessie.mcc.ac.uk
Sat Feb 18 15:39:52 CET 2006
On 18-Feb-06 Duncan Murdoch wrote:
> On 2/18/2006 1:13 AM, Robert W. Baer, Ph.D. wrote:
>> Try:
>> (X>1 & Y<1) | (X<1 & Y>1)
>
> This is a minor stylistic suggestion: when translating the mathematical
> expression (a < b < c), write it as (a < b) & (b < c). It has no
> effect
> on the outcome, but keeping the inequalities in the same direction
> makes
> the meaning more obvious.
>
> If you do this, your expression becomes
>
> (Y<1 & 1<X) | (X<1 & 1<Y)
>
> and it's clear at a glance what's going on.
>
> I think I originally read this suggestion in something written by
> Kernighan and/or Plauger, I can't claim credit for it.
>
> Duncan Murdoch
Mention of "Kernighan and/or Plauger" got me digging out K & P
"The Elements of Programming Style" (from 1974/78). The suggestion
is indeed in there, on pp 20-21, leading up to the rule "Use
the 'telephone test' for readability" ("If someone could
understand your code when read aloud over the telephone, it's
clear enough. If not, then it needs rewriting.")
The example is:
Consider the sequence
6 IF(X1.GE.ARRAY(I)) GO TO 2
IF(ARRAY(I).LT.X2) ICOUNT=ICOUNT+1
2 ...
It takes a while to realize that IVOUNT is incremented only
if ARRAY(I) lies between X1 and X2. .... Rewriting gives:
6 IF(ARRAY(I).GT.X1 .AND. ARRAY(I).LT.X2) ICOUNT = ICOUNT + 1
(Though I think it could be improved still further by the slight
change to "X1.LT.ARRAY(I)" to bring it into line with Duncan's
example).
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Feb-06 Time: 14:39:50
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list