-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
write_csv adds 15 decimal digits to numbers obtained by subtracting from 1 #1516
Comments
This is merely an artifact of floating point arithmetic as implemented on all computers and is not specific to R. Not all values can be represented exactly in floating point arithmetic. Run |
Hi @joranE. If you do Other analog functions I work with, like readr::write_csv(df, "test_write_csv.csv")
# first_value,second_value,third_value,fourth_value,fifth_value,sixth_value,seventh_value
# 0.050000000000000044,0.09999999999999998,0.9999999999999998,0.09999999999999998,0.9999999999999998,3.1,310
readr::write_csv2(df, "test_write_csv2.csv")
# first_value;second_value;third_value;fourth_value;fifth_value;sixth_value;seventh_value
# 0,05;0,1;1;0,1;1;3,1;310
utils::write.csv(df, "data.utils.csv", row.names = FALSE)
# "first_value","second_value","third_value","fourth_value","fifth_value","sixth_value","seventh_value"
# 0.05,0.1,1,0.1,1,3.1,310
data.table::fwrite(df, "data.fwrite.csv")
# first_value,second_value,third_value,fourth_value,fifth_value,sixth_value,seventh_value
# 0.05,0.1,1,0.1,1,3.1,310 On a different note, I noticed another odd bahaviour. I created a column where I wrote in each row a number made of increasing number of digits (up to 20 digits after the decimal separator) of the following number df2 <- data.frame("numbers" = c(0.2, 0.23, 0.234, 0.2347, 0.23472, 0.234723, 0.2347235, 0.23472354, 0.234723542, 0.2347235423, 0.23472354234, 0.234723542349, 0.2347235423492, 0.23472354234923, 0.234723542349237, 0.2347235423492378, 0.23472354234923784, 0.234723542349237840, 0.2347235423492378402, 0.23472354234923784023)) If you take a look at how rows 15 to 20 are being written, you'll see the following, showing an odd behaviour between 17 and the subsequent values: readr::write_csv(df2, "data2.readr.csv")
# 15 0.234723542349237
# 16 0.2347235423492378
# 17 0.23472354234923784
# 18 0.2347235423492378
# 19 0.2347235423492378
# 20 0.2347235423492378
data.table::fwrite(df2, "data2.fwrite.csv")
# ...
# 15 0.234723542349237
# 16 0.234723542349238
# 17 0.234723542349238
# 18 0.234723542349238
# 19 0.234723542349238
# 20 0.234723542349238 Finally, I read the documentation about Sorry for the lengthy reply. And thanks for the feedback. |
First, I think you have mistaken me for an author of this package, which I am not, nor am I even a contributor. I'm sure one of the authors will weigh in eventually. In general, due to the nuances involved in floating point arithmetic, if you want complete control over the decimal precision of data written out to a file, you will need to use something like Finally, |
Also not a maintainer or contributor here, but this is a rather succinct explanation of why the behavior you're seeing has little to do with {readr}... print(1 - 0.95, digits = 16)
#> [1] 0.05000000000000004
print(0.05, digits = 16)
#> [1] 0.05
1 - 0.95 == 0.05
#> [1] FALSE |
Hi there.
I'm puzzled with an issue when writing some values using
write_csv
. If you take a look at the example below, variables first to fifth, which were obtained by subtracting from1
, when written to a CSV file usingwrite_csv
, they look like:When subtracting from a number other than
1
, it's being written fine (see sixth and seventh variables).Note 1: All of the variables are
numeric
.Note 2: if you pay attention to variable fifth, you will notice that every value has a decimal digit, and still fails.
Here's the code to reproduce the issue:
Below is the
sessionInfo()
output from a laptop with MacOS, although I have tried it as well from a similar install on Debian:I will really appreciate any feedback on this matter.
Best,
Santiago
The text was updated successfully, but these errors were encountered: