[R] a < b < c is alway TRUE

Duncan Murdoch dmurdoch at pair.com
Sat Jul 7 03:11:56 CEST 2001

On Fri, 6 Jul 2001 13:50:49 -0700, you wrote:

>The type conversion rule is nothing new--many other programming 
>languages also interpret 0 as false and 1 as true in various 
>contexts. For example, try this fortran program:
>       program foo
>       if (1) write(*,*) 'true'
>       if (0) write(*,*) 'false'
>       end

When I try that in g77, I get syntax errors:

>test.f: In program `foo':
>          if (1) write(*,*) 'true'
>              ^
>Type disagreement between expressions at (^) and (^)
>          if (0) write(*,*) 'false'
>              ^

I don't doubt there is some dialect of Fortran that accepts such
constructions, but it's not a dialect that I'd want to use.

As a general principle, code should be easy to read and understand by
the target audience.  If your audience is C programmers, then it's
fine to write junk like "if (1) ...", but if your audience is
statisticians, it's better if what you write is closer to standard
mathematical syntax.  Since 0 and 1 are numbers, not logical values,
it would be better if they were treated as such in R.  Since "3 < 2 <
1" has the interpretation "false" in standard mathematical notation,
if R is to accept it, it would be better if it had the same value in

HOWEVER, R is already old, and is essentially S, which is very old.  
"3 < 2 < 1" has a well-defined meaning in R.  Changing it now would be
a bad thing.  Adding warnings (or even a lint-like utility to check
through R source code) for constructions like this which are likely
sources of bugs would be a good thing, but there are a lot of good
things to do, and only a finite amount of time for people to do them.
Adding this wouldn't  be at the top of my priority list.

Duncan Murdoch
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list