0

I need to compare numbers. But there are many numerical errors in some comparisons. I know they are errors because analytical results are known. Of course when analytical results are known numerical evaluations are not needed, but I want to know how to resolve these problems numerically. Examples are shown below...

> x <- seq(0,1,length=11)
> x[8]
[1] 0.7
> x[8]==0.7
[1] FALSE
> 
> x <- round(seq(1,0,length=21),2)
> x[3]==0.9
[1] TRUE
> x[5]==0.8
[1] TRUE
> (x[3]+x[5])/2
[1] 0.85
> (x[3]+x[5])/2==0.85
[1] FALSE
quibble
  • 303
  • 2
  • 13
  • 1
    See https://stackoverflow.com/questions/588004/is-floating-point-math-broken – jakevdp Aug 03 '18 at 05:11
  • similar to this: https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal – PKumar Aug 03 '18 at 05:16
  • thank you. the suggested links seem to provide possible approaches that may be helpful. i think this question can be deleted (but I can't do so by myself). – quibble Aug 03 '18 at 05:22

0 Answers0