-1

Why this example will return 0.93

> trunc(0.94/100*100*100)/100
[1] 0.93

but this example:

> trunc(0.94*100)/100
[1] 0.94

returns 0.94. It is floating point error. How can I transform result of the calculations 0.94/100*100*100 in normal 94?

Andy
  • 263
  • 2
  • 5
  • 16
  • 3
    This is due to floating point error. Look at the result of `0.94/100*100*100 - 94`. So while R prints `94`, the number is stored as something ever so slightly less than `94`. see [this question](http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – Justin Feb 20 '14 at 20:25
  • how can I transform 0.94/100*100*100 in normal 94? – Andy Feb 20 '14 at 20:51
  • wrap it in `integer`! – Justin Feb 20 '14 at 20:52
  • trunc(as.integer(0.94/100*100*100))/100 not work – Andy Feb 20 '14 at 20:57

1 Answers1

1

As Justin said in the comments, floating point error.

Read this: What Every Computer Scientist Should Know About Floating-Point Arithmetic

(it applies to statisticians and scientists too, at least one who use computers!)

Harlan
  • 18,883
  • 8
  • 47
  • 56