`1L`和`1`之间有什么区别?

13 浏览
0 Comments

`1L`和`1`之间有什么区别?

在R代码中,我经常看到符号1L(或2L3L等)出现。 1L1之间有什么区别?1==1L的结果为TRUE。为什么在R代码中使用1L

0
0 Comments

L specifies an integer type, rather than a double that the standard numeric class is. When using the letter "L" after a number in R, it indicates that the number should be treated as an integer rather than a double.

For example, when we use the `str()` function in R to check the type of a number, we can see that without the "L" suffix, the number is considered as a double, while with the "L" suffix, it is considered as an integer.

> str(1)
 num 1
> str(1L)
 int 1

The reason for using the "L" suffix is to maintain consistency and precision when working with large or precise integer values. By explicitly specifying the number as an integer using the "L" suffix, we can avoid any potential loss of precision or rounding errors that could occur if the number was treated as a double.

To solve the potential issue of precision or rounding errors, we can simply append the "L" suffix to the number when we want to treat it as an integer. This ensures that the number is interpreted correctly as an integer data type and any calculations or operations involving that number will be performed with integer precision.

In conclusion, the "L" suffix in R is used to specify that a number should be treated as an integer rather than a double. By using the "L" suffix, we can ensure consistent and precise calculations when dealing with integer values.

0
0 Comments

在R语言中,我们可以使用`L`后缀来将数字限定为整数类型。例如,`0x10L`用十六进制表示创建整数值16。常量`1e3L`表示整数1000而不是数值,并且等价于`1000L`。(注意,`L`是限定术语`1e3`而不是`3`)。如果我们对非整数值进行限定,例如`1e-3L`,则会收到警告并创建数值型。如果数字中存在不必要的小数点,例如`1.L`,也会收到警告。

这个问题的出现是因为在R语言中,`1L`和`1`表示的是不同的数据类型。`1L`表示整数类型,而`1`表示数值类型。这可能会导致在某些情况下出现错误或不一致的结果。

为了解决这个问题,我们需要确保在需要整数类型的情况下使用`L`后缀来限定数字。这样可以避免在使用整数时出现错误或不一致的结果。同时,我们还需要注意不要在不需要小数点的数字中添加不必要的小数点,以免引发警告。

以下是一个示例,展示了`1L`和`1`在计算中的不同结果:

x <- 1L
y <- 1
# 整数运算
result1 <- x + 1L
result2 <- y + 1L
# 数值运算
result3 <- x + 1
result4 <- y + 1
print(result1) # 输出 2L
print(result2) # 输出 2
print(result3) # 输出 2
print(result4) # 输出 2

通过正确使用`1L`和`1`,我们可以确保在需要整数类型的情况下得到正确的结果,同时避免不必要的警告和错误。

0
0 Comments

`1L`和`1`之间的区别是什么?

在大多数情况下,它们没有什么区别 - 但有时你可以使用它们来使你的代码运行更快、占用更少的内存。一个双精度("numeric")向量每个元素使用8个字节,而一个整数向量每个元素只使用4个字节。对于大型向量来说,这意味着减少了浪费的内存,对于CPU来说也更容易处理(因此通常更快)。

大多数情况下,这适用于处理索引。

下面是一个例子,将1添加到一个整数向量中会将其转换为双精度向量:

x <- 1:100
typeof(x) # integer
y <- x+1
typeof(y) # double,两倍的内存大小
object.size(y) # 840字节(在win64上)
z <- x+1L
typeof(z) # 仍然是integer
object.size(z) # 440字节(在win64上)

...但是请注意,过多地使用整数可能会有危险:

1e9L * 2L # 正常工作,快速高效!
1e9L * 4L # 哎呀,溢出了!

...正如所指出的,整数的范围大约是-2e9到2e9。

需要注意的是,这适用于当前的R版本(2.13)。R可能会在某些时候更改这一点(64位整数将是很好的选择,这将使长度大于2e9的向量成为可能)。为了安全起见,您应该使用`.Machine$integer.max`,每当您需要最大整数值时(并且对最小值进行否定)。

我认为R的内存要求不论类型是否相同,至少根据`object.size`来看是一样的。它对于传递给Fortran或C代码可能需要特定类型的数据是有用的。

不,尝试一下`object.size(1:100)` vs. `object.size(1:100+0)`,前者是400字节+一些开销,后者是800字节+一些开销。我更新了上面的例子。

啊,是的,你是对的,尽管长度为1的对象都是32字节。

值得一提的是,整数溢出是由于使用32位有符号整数,因此限制在约+/-2*10^9,即使在64位的R上也是如此...

为什么要使用`1L`而不是`as.integer(1)`?我猜第二个语句会创建一个双精度数,然后转换为整数?

另外,用`1L`打字也要短得多 🙂

当然是辛普森。我真正想的是当你创建一个整个向量时的情况,比如`c(1L, 2L, 3L, 4L,...100L)` vs `as.integer(c(1, 2, 3, 4,...100))`。

Zach,是的,它会进行转换,但除非你要输入成千上万个值,否则这并不重要。此外,对于超过12个值,`as.integer()`打字更短 🙂

当将整数存储为浮点数时,当然避免了浮点误差。

0