1

Sometimes, when I save a columns of double precision numbers to a csv using write_csv from readr (part of the tidyverse), the following happens:a double like 285121.15 is written as 285121.14999999997. The original value has only two decimals and this is not an artifact of printing it on screen. Numerically, they are pretty much the same thing, but it is annoying to share files with so many (unneeded decimals). The documentation of write_csv says that the grisu3 algorithm is used. At the same time, I would like to avoid rounding up the values myself, since in general the number of decimals may vary. According to what I found here

http://www.serpentine.com/blog/2011/06/29/here-be-dragons-advances-in-problems-you-didnt-even-know-you-had/

florian.loitsch.com/publications/dtoa-pldi2010.pdf?attredirects=0

it is a known shortcoming of grisu3. Seen that I am now dealing with large data sets (hence writing to disk is not a big issue), I came up with the following

############ to avoid troubles when saving numbers



 num_to_char <-  function(df){

    res <- df %>% mutate_if(is.numeric, as.character )

    return(res)

    }



   to_csv <- function(df, ...){

    df <- num_to_char(df)

    write_csv(df, ...)
   }

i.e. I essentially convert the numbers to strings prior to saving the file. I ran some tests, and it seems to me my problem has been solved, but are there any caveats I should be aware of? Many thanks!

Nisarg
  • 1,631
  • 6
  • 19
  • 31
larry77
  • 1,309
  • 14
  • 29

1 Answers1

0

My suggestion is to using following code:

#removing unlike precision (double precision)
A <- floor(A*100)
#then converting to the real number
A <- A/100

A simple example in R area;)

A <-9.12234353423242
A<-A*100
A
#[1] 912.2344
A<- floor(A)
A
#[1] 912
A <- A/100
A
#[1] 9.12