r - Strange numeric behaviour with paste function -
can explain strange decimal behavior reproduced below? , how avoid it? use rounding suppose, don't see should need to.
x <- 200.10 y <- 200.96 paste("difference", x - y, sep = ":") # [1] "difference:-0.860000000000014" # not here! 200.10-200.96 # -0.86
there nothing strange going on. precision in both case same printed differently.
sprintf("difference: %.2f", x - y) # prints -0.86 in last output options(digits=15); 200.10-200.96 # prints -0.860000000000014 in first output
the precision in both case determined type (which double
in case). see https://stat.ethz.ch/r-manual/r-devel/library/base/html/double.html , https://stat.ethz.ch/r-manual/r-devel/library/base/html/zmachine.html
Comments
Post a Comment