0

Possible Duplicate:
Most effective way for float and double comparison

I have a function that calculates distance and then that distance is then later compared to the square root of 2 to see if two items are adjacent on a grid. In Java this works fine, apparently the Math.sqrt(Math.pow(x1-x2,2)....) produces the same as Math.sqrt(2) but in C++ cmath's sqrt(pow(x1-x2,2)...) is not equal.

I cout<<ed the doubles and to the precision shown (I will also output higher precision just to see) they looked equal but didn't evaluate as equal. On the other hand, if I test for values within +- 0.01 of sqrt(2) then this test for "approximate equality" works. Is this what I should do or is something else the matter? Is there some better way of doing this test if this is what I must do?

Thanks.

Community
  • 1
  • 1
Anonymous
  • 3,226
  • 2
  • 34
  • 49

0 Answers0