In R we all know it is convenient for those times we want to ensure we are dealing with an integer to specify it using the "L"
suffix like this:
1L
# [1] 1
If we don't explicitly tell R we want an integer it will assume we meant to use a numeric
data type...
str( 1 * 1 )
# num 1
str( 1L * 1L )
# int 1
Why is "L" the preferred suffix, why not "I" for instance? Is there a historical reason?
In addition, why does R allow me to do (with warnings):
str(1.0L)
# int 1
# Warning message:
# integer literal 1.0L contains unnecessary decimal point
But not..
str(1.1L)
# num 1.1
#Warning message:
#integer literal 1.1L contains decimal; using numeric value
I'd expect both to either return an error.
Why is "L" used as a suffix?
I've never seen it written down, but I theorise in short for two reasons:
Because R handles complex numbers which may be specified using the suffix
"i"
and this would be too simillar to"I"
Because R's integers are 32-bit long integers and "L" therefore appears to be sensible shorthand for referring to this data type.
The value a long integer can take depends on the word size. R does not natively support integers with a word length of 64-bits. Integers in R have a word length of 32 bits and are signed and therefore have a range of
−2,147,483,648
to2,147,483,647
. Larger values are stored asdouble
.This wiki page has more information on common data types, their conventional names and ranges.
And also from
?integer
Why do 1.0L and 1.1L return different types?
The reason that
1.0L
and1.1L
will return different data types is because returning an integer for1.1
will result in loss of information, whilst for1.0
it will not (but you might want to know you no longer have a floating point numeric). Buried deep with the lexical analyser (/src/main/gram.c:4463-4485
) is this code (part of the functionNumericValue()
) which actually creates aint
data type from adouble
input that is suffixed by an ascii"L"
: