readr
readr copied to clipboard
Strange rounding errors with signif and write_csv
I noticed that certain numbers cause write_csv, write_tsv, etc. to produce strange rounding errors. Here is an example:
library(readr)
df <- data.frame(x = 1.406148e-20)
df$rounded = signif(df$x, 4)
write_csv(df, "test1.csv")
write.csv(df, "test2.csv", row.names = FALSE)
The content of test1.csv file is
x,rounded
1.406148e-20,1.4060000000000002e-20
The problem seems to be only with readr. The test2.csv file is created as expected:
"x","rounded"
1.406148e-20,1.406e-20
I wonder if this is reproducible on other systems.
> sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.4.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Europe/London
tzcode source: internal
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] readr_2.1.4