Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare doubles in c++

I want to determine whether a point is inside a circle or not. So I do this :

(x - center_x)^2 + (y - center_y)^2 < radius^2

But my coordinates are double and I think I should do it with epsilon, so is fabs ((x - center_x)^2 + (y - center_y)^2 - radius^2 ) < EPS better?

like image 337
Gigata Avatar asked Dec 02 '22 14:12

Gigata


1 Answers

You don't need the epsilon when you're comparing using < or >, those are perfectly fine. You need it instead of ==. In your case, you've just added a small amount to radius, which is probably undesirable. Also note that ^ is not the same as pow(a, b).

like image 170
lorro Avatar answered Dec 27 '22 09:12

lorro