Skip to content

Where are usages like "== 2L" documented?

2 messages · Bryan Hanson, Brian Ripley

#
Patrick reminded me that it's also in The R Inferno, another document that I
need to read again.

So now, a more particular semi-question:

x <- 1L
y <- 1
z <- 1.0

class(x) # integer
class(y) # numeric
class(z) # numeric

x == 1
x == 1L
y == 1
z == 1 # all test TRUE

Just to clarify, I think the steps above prove it, but *in a test* like x ==
1 the test is for the contents, not the storage mode, not for a combination
of storage mode and contents.

So... The reason for defining the notion of "L" is smaller storage space,
and more generally, for use anytime one wants explictly an integer for
whatever reason?  Are there other reasons, for instance, ways it saves lines
of code?

Bryan
*************
Bryan Hanson
Acting Chair
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA
On 11/17/09 4:20 AM, "Patrick Burns" <pburns at pburns.seanet.com> wrote:

            
#
On Tue, 17 Nov 2009, Bryan Hanson wrote:

            
NB: identical(x, y) is TRUE, so they behave the same.
Not really: the operands are coerced to a common type and then tested 
for equality.  From the help page for "=="

      If the two arguments are atomic vectors of different types, one is
      coerced to the type of the other, the (decreasing) order of
      precedence being character, complex, numeric, integer, logical and
      raw.
It avoids coercion.  The test x == 1 requires x to be coerced from 
integer to double, and foo[1] requires '1' to be coerced from double 
to integer.  There is little if any gain in storage space for small 
integer vectors over small double vectors, but all those coercions can 
create a lot of new objects which need to be garbage-collected.

Also, 2L is simpler to write and to read than as.integer(2) (and 
avoids a funtion call and a coercion).