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

查看:21
本文介绍了为什么 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 我们想要一个整数,它会假设我们打算使用 numeric 数据类型...

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 处理可以使用后缀"i",这与"I"

因为 R 的整数是 32 位长整数,而L"是 32 位长整数.因此,引用这种数据类型似乎是明智的速记.

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,6482,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.

这个维基页面有更多关于常见数据类型的信息,它们的常规数据类型名称和范围.

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

也来自 ?integer

请注意,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.0L1.1L 返回不同数据类型的原因是因为1.1 返回一个整数会导致信息丢失, 而对于 1.0 它不会(但您可能想知道您不再有浮点数字).与词法分析器 (/src/main/gram.c:4463-4485) 深埋在一起的是这段代码(函数 NumericValue() 的一部分),它实际上创建了一个int 来自 double 输入的数据类型,后缀为 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) = '';
        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天全站免登陆