Cethric / pyeuclid

Automatically exported from code.google.com/p/pyeuclid
0 stars 0 forks source link

The angle method of vectors doesn't account for approximation errors. #16

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

>>> from lib.euclid import Vector2 as v
>>> vv = v(-35069.37850906927, -10011.874589043844)
>>> tt = v(-213.6847759229285, -61.004365320446894)
>>> vv.angle(tt)

Will triggere a math domain exception, because the fraction of which 
to calculate the acos of will return 1.0000000000000001 instead of 1.0

It's enough to enclose the fraction in a `round()` to solve the issue.

Original issue reported on code.google.com by quasipe...@gmail.com on 27 Sep 2011 at 9:30

GoogleCodeExporter commented 9 years ago
round() would destroy the fraction and turn all results into either 1 or 0, no? 
Better use something like `min(1.0, fraction)` to cap the fraction.

Original comment by rodrigo....@gmail.com on 6 Aug 2014 at 9:43

GoogleCodeExporter commented 9 years ago
You can also use round(x, n) to get n-digits of floating point precision. Using 
round(x, 12) would be another way to fix this assuming you only need 12 
significant digits.

Original comment by chris.fl...@gmail.com on 6 Aug 2014 at 10:27

GoogleCodeExporter commented 9 years ago
I don't think pyeuclid should set an arbitrary rounding precision: that's up 
for the user. The error is better handled by clamping fractions the [-1, 1] 
range prior to using trigonometric functions like acos. What do you think chris?

Original comment by rodrigo....@gmail.com on 6 Sep 2014 at 6:56