Precision issues with acos/cos/sin

Hi,

The issue at hand is simple:
I have two vectors (a1, a2) that define the principal axes of an ellipse like this
1
2
___|___
   |

Now I have a third vector (v) that somewhere lies within the plane of the ellipse, like this
1
2
___|/__
   |

All right. I need to know if the vector is within the ellipse itself.

So I do the dotproduct a1.v/|a1|/|v| to find cos(theta), because by definition a1.v = |a1||v|cos(theta).

Now here the trouble starts.This cos(theta) can be extracted pretty precise. However I need sin(theta) as well, which I do by sin(acos(cos(theta))). This sin(theta) is disgustingly inaccurate, even when using long doubles.

I need it because the check I want to perform is |a1|*cos(theta)+|a2|*sin(theta) > |v|, namely that v is within the ellipse.

I get results like for |a1|=|a2|=0.5 that 0.47+0.14 > |v|, which anyone can see is not the correct result for any angle applied to the above formula. So I can only guess that severe rounding errors were made.

The questions are:
-Why is a standard math.h function so poor?
-Is there any other way to perform a cos(theta) to sin(theta) switch?
-Is there any other way in general?
Last edited on
All right, I tried sin(theta) = sqrt(1-cos(theta)*cos(theta)). It gave the same result.

Made me notice that 0.14 is indeed a logical value for 0.47 given the above formula. I was just wrong with my check in the first place... Gonna look for the correct check and post it here later.

Correct check: |a1|*cos(theta)^2+|a2|*sin(theta)^2 > |v|
Last edited on
Just a sidequestion, why does
if(section1->d1 == section2->d1 == section1->d2 == section2->d2)
fail when all d's are 0.5? Precision or bad syntax?
_Cross product |v x u| = |v| |u| sin \theta
_Another sin \theta = cos( \theta - \pi/2 ) (you've got orthogonal axis)
_You could use Pithagoras theorem too.
_By definition: an ellipse is the set of points such that the sum of the distances to two fixed points (focus) is constant.
(I guess that you can't avoid the square root)
_Maybe threat the ellipse as a NURBS, (I don't remember it, xP)
Last edited on
About the if:
Both.
When comparing floating points for equallity you will need to use a tolerance. fabs( a-b ) < TOL
(round-off error, arithmetic precision, machine epsilon)

a==b==cis associated as (a==b) == c. So you are comparing a boolean value (true, false) against the other number.
You will need to do a==b and b==c
There is no way standard math is that inaccurate.

Fix your if, always use a tolerance when comparing floating point numbers.

If you are still getting that degree of inaccuracy then you must throw your computer away.

What about calculating sin(theta) as the height / hypotenuse of your vector v?

http://www.csgnetwork.com/trigtriformulatables.html

Topic archived. No new replies allowed.