为什么R会使用“L”?后缀表示整数? [英] Why would R use the "L" suffix to denote an integer?

查看:179
本文介绍了为什么R会使用“L”?后缀表示整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,我们都知道,我们希望确保处理整数以使用L后缀来指定它,这样很方便:

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

如果我们没有明确告诉R我们想要一个整数,它会假设我们打算使用数字数据类型。 。

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

为什么L是首选后缀,为什么不是I例如?有历史原因吗?

Why is "L" the preferred suffix, why not "I" for instance? Is there a historical reason?

此外,为什么R允许我这样做(有警告):

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 

但不是..

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.

推荐答案

为什么L用作后缀?



我从未见过写过但是我理所当然地理由有两个原因:

Why is "L" used as a suffix?

I've never seen it written down, but I theorise in short for two reasons:


  1. 因为R处理可以使用$ b $指定的复数b后缀i这对于我而言太过分了

因为R的整数是32位长整数,因此L似乎是引用此数据类型的合理简写。

Because R's integers are 32-bit long integers and "L" therefore appears to be sensible shorthand for referring to this data type.

长整数可以取的值取决于字大小。 R本身不支持字长为64位的整数。 R中的整数具有32位的字长并且是有符号的,因此其范围 -2,147,483,648 2,147,483,647 。较大的值存储为 double

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 to 2,147,483,647. Larger values are stored as double.

此Wiki页面提供了有关常见数据类型,常规名称和范围的更多信息。

This wiki page has more information on common data types, their conventional names and ranges.

还来自?整数


注意R的当前实现使用32整数向量的位整数,因此可表示整数的范围被限制在大约+/- 2 * 10 ^ 9:双精度可以精确地保持更大的整数。

Note that current implementations of R use 32-bit integers for integer vectors, so the range of representable integers is restricted to about +/-2*10^9: doubles can hold much larger integers exactly.






为什么1.0L和1.1L会返回不同的类型?



1.0L 1.1L 将返回不同数据类型的原因是因为为 1.1 返回一个整数会导致信息丢失,而对于 1.0 则不会(但你可能想要)要知道你不再有浮点数字)。使用词法分析器深深埋藏( /src/main/gram.c:4463-4485 )是这段代码(函数 NumericValue()的一部分) )实际上从 double 输入创建 int 数据类型,后缀为ascii L


Why do 1.0L and 1.1L return different types?

The reason that 1.0L and 1.1L will return different data types is because returning an integer for 1.1 will result in loss of information, whilst for 1.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 function NumericValue()) which actually creates a int data type from a double input that is suffixed by an ascii "L":

/* Make certain that things are okay. */
if(c == 'L') {
double a = R_atof(yytext);
int b = (int) a;
/* We are asked to create an integer via the L, so we check that the
   double and int values are the same. If not, this is a problem and we
   will not lose information and so use the 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 the L for the warning message */
        *(yyp-2) = '\0';
        warning(_("non-integer value %s qualified with L; using numeric value"), yytext);
        *(yyp-2) = (char)c;
    }
    }
    asNumeric = 1;
    seenexp = 1;
}
}

这篇关于为什么R会使用“L”?后缀表示整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆