parsing - Why would R use the "L" suffix to denote an integer? -
in r know convenient times want ensure dealing integer specify using "l"
suffix this:
1l # [1] 1
if don't explicitly tell r want integer assume meant utilize numeric
info type...
str( 1 * 1 ) # num 1 str( 1l * 1l ) # int 1
why "l" preferred suffix, why not "i" instance? there historical reason?
in addition, why r allow me (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 either homecoming error.
why "l" used suffix?
i've never seen written down, theorise in short 2 reasons:
beacuse r handles complex numbers may specified using suffix "i"
, simillar "i"
because r's integers 32-bit long integers , "l" hence appears sensible shorthand referring info type.
the value long integer can take depends on word size. r not natively back upwards integers word length of 64-bits. integers in r have word length of 32 bits , signed , hence have range of −2,147,483,648
2,147,483,647
. larger values stored double
.
this wiki page has more info on mutual info types, thier conventional names , ranges.
and ?integer
note current implementations of r utilize 32-bit integers integer vectors, range of representable integers restricted +/-2*10^9: doubles can hold much larger integers exactly.
why 1.0l , 1.1l homecoming different types?the reason 1.0l
, 1.1l
homecoming different info types because returning integer 1.1
result in loss of information, whilst 1.0
not (but might want know no longer have floating point numeric). buried deep lexical analyser (/src/main/gram.c:4463-4485
) code (part of function numericvalue()
) creates int
info type double
input suffixed ascii "l"
:
/* create things okay. */ if(c == 'l') { double = r_atof(yytext); int b = (int) a; /* asked create integer via l, check double , int values same. if not, problem , not lose info , utilize numeric value. */ if(a != (double) b) { if(generatecode) { if(seendot == 1 && seenexp == 0) warning(_("integer literal %s contains decimal; using numeric value"), yytext); else { /* hide l warning message */ *(yyp-2) = '\0'; warning(_("non-integer value %s qualified l; using numeric value"), yytext); *(yyp-2) = (char)c; } } asnumeric = 1; seenexp = 1; } }
r parsing integer semantics
No comments:
Post a Comment