[Rd] Strange Behavior in RNG
Jiefei Wang
@zwj|08 @end|ng |rom gm@||@com
Sat Aug 17 02:45:37 CEST 2024
Hi,
I just observed a strange behavior in R. The rnorm function does not
give me the numbers with a given length. I think it is somehow related
to the internal representation of double-type numbers but I am not
sure if this is supposed to happen. Below is a reproducible example
```
## Create a list, we will only take the forth value, which is 0.6
nList <- seq(0,1,0.2)
n <- nList[4]
n
# [1] 0.6
length(rnorm(1000*n))
# [1] 600
length(rnorm(1000-1000*n))
# [1] 399 <--- What happened here?
length(rnorm(1000-1000*0.6))
# [1] 400
1000-1000*n
# [1] 400 <- this looks good to me...
1000-1000*0.6
# [1] 400
identical(n, 0.6)
# [1] FALSE
.Internal(inspect(n))
# @0x00000217c75d79d0 14 REALSXP g0c1 [REF(1)] (len=1, tl=0) 0.6
.Internal(inspect(0.6))
# @0x00000217c791e0c8 14 REALSXP g0c1 [REF(2)] (len=1, tl=0) 0.6
```
As you can see, length(rnorm(1000-1000*n)) does not really give me the
result I want. This is somewhat surprising because it is hard to
imagine that a manually-typed 0.6 can behave differently than 0.6 from
a sequence. Furthermore, 0.6 is the only problematic number from
`nList`. The rest numbers work fine. I can guess it is due to the
rounding mechanism, but I think this should be treated as a bug: if
the print function can show the result of 1000-1000*n correctly, it
will be strange that rnorm behaves differently. Below is my session
info
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8
[2] LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
time zone: America/Chicago
tzcode source: internal
More information about the R-devel
mailing list