Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

Cos is not returning 0 for 90 degrees or PI/2

Started by Muzzy A 32 replies 9.2k views
Original Post
Muzzy A
Muzzy A

Vector2 Rotate( Vector2 &vec,float Angle )
{
/*
cos -sin
sin cos
*/

// When the Angle is PI/2 (90 degrees), it doesn't return 0, we get 4.37114e-008
float Cos = cos(Angle);
float Sin = sin(Angle);

Vector2 rot = Vector2( Cos*vec.x - Sin*vec.y , Sin*vec.x + Cos*vec.y );

return rot;
}

// If i pass this vector
Vector2 vec = Vector2(0,1);
vec = Rotate(vec,PI*.5f);

// The answer should be '(1,0)'
// But i ACTUALLY get '(-1,-4.37114e-008)'....

/* Anyone know what's going on here, is sin and cos incapable of returning 0? */

taby
taby
I get the same behaviour, where the answer is -4.37114e-008 for float and 6.12323e-017 for double. Consider that [eqn]\pi[/eqn] is irrational, and so you shouldn't really expect a perfect answer. When you use a digital computer / calculator and plug in [eqn]\cos(\pi / 2)[/eqn] and get an answer of precisely zero, it's likely because the person who coded the calculator put in a special conditional statement that says something similar to "if(answer < epsilon && answer > -epsilon) { answer = 0; }", where epsilon is some tiny number like 1e-7.

Don't lose sleep over it.
alvaro
alvaro

// The answer should be '(1,0)'
// But i ACTUALLY get '(-1,-4.37114e-008)'....



Well, the answer should be (-1,0), which it basically is... You can't expect infinite precision in floating-point operations.

EDIT: If instead of an angle you were to use a unit vector containing the angle's cosine and sine, you could just do this and not have the problem:
Vector2D rotate(Vector2D vec, Vector2D rot) {
return Vector2D(vec.x*rot.x - vec.y*rot.y, vec.x*rot.y + vec.y*rot.x); // Notice this is just multiplication of complex numbers!
}

Vector2 vec = Vector2(0,1);
vec = Rotate(vec,Vector2D(0,1));
alvaro
alvaro
Not that it has anything to do with your question, but vec' should not be a non-const reference: Either make it const or remove the &'.
Muzzy A
Muzzy A

What Every Computer Scientist Should Know About Floating-Point Arithmetic

-4.37114e-008 = -.0000000437114 (which is pretty dang close to zero)


oh, i assumed it was a garbage number that it spit out cause it didn't know what to do lol

thanks guys



[quote name='Muzzy A' timestamp='1337404899' post='4941354']
// The answer should be '(1,0)'
// But i ACTUALLY get '(-1,-4.37114e-008)'....



Well, the answer should be (-1,0)

[/quote]

lol oops
frob
frob

it's likely because the person who coded the calculator put in a special conditional statement that says something similar to "if(answer < epsilon && answer > -epsilon) { answer = 0; }", where epsilon is some tiny number like 1e-7.

It turns out the language has a constant for the actual epsilon that should be used for that. Proper use of FLT_EPSILON would apply here.

( In this case, the answer is ten times smaller than FLT_EPSILON, so ten times smaller than rounding error. )
Adam_42
Adam_42
Calculators generally work to a much higher precision than what they display. For example the Windows calculator (at least on recent versions of windows) has about 40 significant digits of precision (calculate sqrt(4) - 2 to see that). They then round the answer to fit the number of digits on the display.

Note that FLT_EPSILON is simply the minimum positive float such that 1.0f + FLT_EPSILON != 1.0f so the epsilon value you want to use depends on the magnitude of the expected answer. In the case of sin and cos it's probably the right epsilon to use, but in other cases it won't be.
Zoomulator
Zoomulator

-4.37114e-008 = -.0000000437114 (which is pretty dang close to zero)


Not if you're calculating the mass defect of an atom nucleus decaying by beta-radiation cool.png
which I currently have to do for class... 4,014e-31 kg...
21st Century Moose
21st Century Moose

Not if you're calculating the mass defect of an atom nucleus decaying by beta-radiation cool.png

In which case choosing stock float as the data type to use is quite obviously a baaaaaaaaaaaaaaaaaad idea.
Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls. 
Zoomulator
Zoomulator

[quote name='Zoomulator' timestamp='1337590478' post='4941864']
Not if you're calculating the mass defect of an atom nucleus decaying by beta-radiation cool.png

In which case choosing stock float as the data type to use is quite obviously a baaaaaaaaaaaaaaaaaad idea.
[/quote]
True enough.. though a double float would work fine.
Cornstalks
Cornstalks

Not if you're calculating the mass defect of an atom nucleus decaying by beta-radiation cool.png

Which he isn't doing...


[quote name='mhagain' timestamp='1337591469' post='4941868']
In which case choosing stock float as the data type to use is quite obviously a baaaaaaaaaaaaaaaaaad idea.

True enough.. though a double float would work fine.
[/quote]
A [font=courier new,courier,monospace]double[/font] only has about 15-17 decimal digits of significant precision. Yes, it's more than a [font=courier new,courier,monospace]float[/font], but it all depends on what you're doing with your data. Both [font=courier new,courier,monospace]float[/font]s and [font=courier new,courier,monospace]double[/font]s can approximate 4.014e-31, which isn't the problem (and subsequently, both can approximate 4.014e-31 + 4.014e-31 pretty well). Usually, the problem isn't "Can a [font=courier new,courier,monospace]float[/font]/[font=courier new,courier,monospace]double[/font] represent this number?" but instead "Can a [font=courier new,courier,monospace]float[/font]/[font=courier new,courier,monospace]double[/font] approximate the mathematical operation between these two (or more) numbers?" Just throwing out a small number doesn't mean that [font=courier new,courier,monospace]float[/font]s are ruled out; it all depends on what you're doing with that small number.

I'm not saying a [font=courier new,courier,monospace]double[/font] wouldn't work or that it wouldn't be the better choice in your case. I'm saying it isn't the number that's usually limiting you, it's usually the numbers and what you're doing with the numbers. It's a subtle but significant difference of focus.
frob
frob

I'm not saying a [font=courier new,courier,monospace]double[/font] wouldn't work or that it wouldn't be the better choice in your case. I'm saying it isn't the number that's usually limiting you, it's usually the numbers and what you're doing with the numbers. It's a subtle but significant difference of focus.

Yup, which is why the other discussion is pointless. The OP said what he was doing.

The OP is computing a cosine.

For the operation the scale is (0,1).

Considering the range of the result, the margin of error is based on 1.0*FLT_EPSILON, which in turn puts his stated result as within the error tolerance of 0.
taby
taby

[quote name='taby' timestamp='1337410191' post='4941368']
it's likely because the person who coded the calculator put in a special conditional statement that says something similar to "if(answer < epsilon && answer > -epsilon) { answer = 0; }", where epsilon is some tiny number like 1e-7.

It turns out the language has a constant for the actual epsilon that should be used for that. Proper use of FLT_EPSILON would apply here.

( In this case, the answer is ten times smaller than FLT_EPSILON, so ten times smaller than rounding error. )
[/quote]

There's always numeric_limit's epsilon(), and that would technically be the proper use of the language, but whatever you say.
taby
taby

Calculators generally work to a much higher precision than what they display. For example the Windows calculator (at least on recent versions of windows) has about 40 significant digits of precision (calculate sqrt(4) - 2 to see that). They then round the answer to fit the number of digits on the display.

Note that FLT_EPSILON is simply the minimum positive float such that 1.0f + FLT_EPSILON != 1.0f so the epsilon value you want to use depends on the magnitude of the expected answer. In the case of sin and cos it's probably the right epsilon to use, but in other cases it won't be.


Thanks for that.
alvaro
alvaro

[quote name='Cornstalks' timestamp='1337608410' post='4941910']
I'm not saying a [font=courier new,courier,monospace]double[/font] wouldn't work or that it wouldn't be the better choice in your case. I'm saying it isn't the number that's usually limiting you, it's usually the numbers and what you're doing with the numbers. It's a subtle but significant difference of focus.

Yup, which is why the other discussion is pointless. The OP said what he was doing.

The OP is computing a cosine.

For the operation the scale is (0,1).

Considering the range of the result, the margin of error is based on 1.0*FLT_EPSILON, which in turn puts his stated result as within the error tolerance of 0.
[/quote]

Not to be nit-picky or anything, but it's not computing the cosine that introduces an error here: The error in this situation comes from not being able to represent pi/2 precisely as a float. We are computing the cosine of pi/2+epsilon, which should be very close to -epsilon.

The smallest value of epsilon for which pi/2+epsilon is representable is .00000004371139000186... , so the number the OP was getting is very well explained by my story, and the error introduced by the computation of the cosine is a second-order effect.
taby
taby

Not to be nit-picky or anything, but it's not computing the cosine that introduces an error here: The error in this situation comes from not being able to represent pi/2 precisely as a float. We are computing the cosine of pi/2+epsilon, which should be very close to -epsilon.

The smallest value of epsilon for which pi/2+epsilon is representable is .00000004371139000186... , so the number the OP was getting is very well explained by my story, and the error introduced by the computation of the cosine is a second-order effect.


I don't think that you're being too nitpicky, because while I do think that both sides of the coin are applicable here, the error does technically spring up from the less-than-perfect definition of pi. I should have thrown in a mention of float pi_half = acos(0.0f); cout << cos(pi_half) << endl; and of epsilon() alongside the mention of irrational numbers to explain what I meant a little better, because it's how I indirectly double-checked the OP's definition of pi. mellow.png

I also mentioned digital computers to contrast against analog computers, but again, I didn't explain very well what I meant.
web383
web383
Some very good information is explained on this blog. Actually most (if not all) of his recent blog posts refer to floating point calculations and the mystery behind them.

Scroll down to the 'Catastrophic cancellation, hiding in plain sight' section.
http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Alvaro basically hit it spot on. i.e. You're not calculating the sin of PI... you're calculating the sin of (float)pi, or (double)pi. PI, as a whole, can't be represented as a float or even a double. So there is error in the calculations.
taby
taby

Some very good information is explained on this blog. Actually most (if not all) of his recent blog posts refer to floating point calculations and the mystery behind them.

Scroll down to the 'Catastrophic cancellation, hiding in plain sight' section.
http://randomascii.w...s-2012-edition/

Alvaro basically hit it spot on. i.e. You're not calculating the sin of PI... you're calculating the sin of (float)pi, or (double)pi. PI, as a whole, can't be represented as a float or even a double. So there is error in the calculations.


Yeah... because it's irrational... rolleyes.gif (BTW, that's not an annoyed eyeroll, that's a "look up" eyeroll)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.