I read data stored in the format DECIMAL
from a MySQL-Table. I want to do calculations on those numbers within R.
I used to cast them to a numeri represaentation using as.numeric()
, but the documentation says:
numeric is identical to double (and real).
But is there also a datatype Decimal in R? (Datatype without rounding errors,...)
Here a simple example for the problem with rounding errors:
numbersStrings = c("0.1", "0.9")
numbersNumeric = as.numeric(numbersStrings)
numbersMirror = c(numbersNumeric, 1-numbersNumeric)
str(numbersMirror)
numbersMirror
unique(numbersMirror) # has two times 0.1 ...
sprintf("%.25f", numbersMirror)
sprintf("%.25f", unique(numbersMirror)) # ... because there was a rounding error