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

## Recommended Posts

Muzzy A    737
[code]
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? */

[/code] Edited by Muzzy A

##### Share on other sites
taby    1265
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. Edited by taby

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

[/quote]

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:
[code]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));
[/code] Edited by alvaro

##### Share on other sites
alvaro    21246
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 &'.

##### Share on other sites
Cornstalks    7030
[url="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html"]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url]

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

##### Share on other sites
Muzzy A    737
[quote name='Cornstalks' timestamp='1337411593' post='4941373']
[url="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html"]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url]

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

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='alvaro' timestamp='1337410309' post='4941369']
[quote name='Muzzy A' timestamp='1337404899' post='4941354']
// The answer should be '(1,0)'
// But i ACTUALLY get '(-1,-4.37114e-008)'....

[/quote]

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

[/quote]

lol oops Edited by Muzzy A

##### Share on other sites
frob    44904
[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.
[/quote]
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. )

##### Share on other sites
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.

##### Share on other sites
Zoomulator    273
[quote name='Cornstalks' timestamp='1337411593' post='4941373']
-4.37114e-008 = -.0000000437114 (which is pretty dang close to zero)
[/quote]

Not if you're calculating the mass defect of an atom nucleus decaying by beta-radiation [img]http://public.gamedev.net//public/style_emoticons/default/cool.png[/img]
which I currently have to do for class... 4,014e-31 kg...

##### Share on other sites
mhagain    13430
[quote name='Zoomulator' timestamp='1337590478' post='4941864']
Not if you're calculating the mass defect of an atom nucleus decaying by beta-radiation [img]http://public.gamedev.net//public/style_emoticons/default/cool.png[/img]
[/quote]
In which case choosing stock float as the data type to use is quite obviously a baaaaaaaaaaaaaaaaaad idea.

##### Share on other sites
Zoomulator    273
[quote name='mhagain' timestamp='1337591469' post='4941868']
[quote name='Zoomulator' timestamp='1337590478' post='4941864']
Not if you're calculating the mass defect of an atom nucleus decaying by beta-radiation [img]http://public.gamedev.net//public/style_emoticons/default/cool.png[/img]
[/quote]
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.

##### Share on other sites
Cornstalks    7030
[quote name='Zoomulator' timestamp='1337590478' post='4941864']
Not if you're calculating the mass defect of an atom nucleus decaying by beta-radiation [img]http://public.gamedev.net//public/style_emoticons/default/cool.png[/img]
[/quote]
Which he isn't doing...

[quote name='Zoomulator' timestamp='1337593369' post='4941873']
[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.
[/quote]
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 [i]doing[/i] 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 [b][i]number[/i][/b] that's usually limiting you, it's usually the [b][i]number[u]s[/u][/i][/b] and what you're [b][i]doing[/i][/b] with the numbers. It's a subtle but significant difference of focus.

##### Share on other sites
frob    44904
[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 [b][i]number[/i][/b] that's usually limiting you, it's usually the [b][i]number[u]s[/u][/i][/b] and what you're [b][i]doing[/i][/b] with the numbers. It's a subtle but significant difference of focus.
[/quote]
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.

##### Share on other sites
taby    1265
[quote name='frob' timestamp='1337490764' post='4941587']
[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.
[/quote]
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.

##### Share on other sites
taby    1265
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.
[/quote]

Thanks for that. Edited by taby

##### Share on other sites
taby    1265
[quote name='Cornstalks' timestamp='1337411593' post='4941373']
[url="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html"]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url]

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

Add this to that list (which makes the ramifications of difference in order of magnitude a bit more transparent):
[url="http://www.drdobbs.com/cpp/184403224"]http://www.drdobbs.com/cpp/184403224[/url] Edited by taby

##### Share on other sites
alvaro    21246
[quote name='frob' timestamp='1337617427' post='4941946']
[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 [b][i]number[/i][/b] that's usually limiting you, it's usually the [b][i]number[u]s[/u][/i][/b] and what you're [b][i]doing[/i][/b] with the numbers. It's a subtle but significant difference of focus.
[/quote]
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. Edited by alvaro

##### Share on other sites
taby    1265
[quote name='alvaro' timestamp='1337623057' post='4941970']
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.
[/quote]

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. [img]http://public.gamedev.net//public/style_emoticons/default/mellow.png[/img]

I also mentioned digital computers to contrast against [url="http://www.wisegeek.com/what-is-an-analog-computer.htm"]analog[/url] [url="http://sf0.org/levitatingpotato/Analog-Computer-Creation/"]computers[/url], but again, I didn't explain very well what I meant. Edited by taby

##### Share on other sites
web383    804
Some very good information is explained on this [url="http://randomascii.wordpress.com/"]blog[/url]. 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.

##### Share on other sites
taby    1265
[quote name='web383' timestamp='1337634063' post='4942004']
Some very good information is explained on this [url="http://randomascii.wordpress.com/"]blog[/url]. 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.
[url="http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/"]http://randomascii.w...s-2012-edition/[/url]

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.
[/quote]

Yeah... because it's irrational... [img]http://public.gamedev.net//public/style_emoticons/default/rolleyes.gif[/img] (BTW, that's not an annoyed eyeroll, that's a "look up" eyeroll) Edited by taby

##### Share on other sites
alvaro    21246
The irrationality of pi is not exactly what the problem is. There are many rational numbers that cannot be expressed exactly in floating-point formats either, like 1/3 or 0.1.

##### Share on other sites
BruceDawson    109
[quote name='web383' timestamp='1337634063' post='4942004']
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.
[/quote]

That's correct, that he is not calculating cos(pi/2), he is calculating cos((float)(pi/2)). But what I think is really cool (and what my post on randomascii discusses) is exactly what *is* being calculated, because it turns out to be very meaningful.

When you calculate sin((float)pi)) or cos((float)(pi/2)) then a bit of calculus or geometric reasoning shows that what you are actually calculating is the error in the value that you are passing in! Thus:

sin((float)pi); equals pi-float(pi) -- the error in (float)pi
cos((float)(pi/2)); equals pi/2-(float)(pi/2) - the error in (float)(pi/2)

The Cos and sin functions are extremely accurate and the result is typically accurate to about seven significant digits, so the real meaning of the result is that (float)(pi/2) is about -4.37114e-008 away from pi/2. This happens to be close to FLT_EPSILON, but while that is probable it is not, in general, necessarily true.

Cool!

This claim is not generically true, It is true for cos/sin around these values because the derivative at those points is 1 and because the 'expected' value is zero. Edited by BruceDawson

##### Share on other sites
WiredCat    1452
mayby you are using degrees instead of radians if you use degrees you must multiply angle by pi/180

const float imopi = 0.017453292519943295769236907684886;

so then you should write this Cos = cos( angle* imopi);

##### Share on other sites
[quote name='___' timestamp='1337855254' post='4942856']const float imopi = 0.017453292519943295769236907684886;[/quote]

Not only has the problem been well-diagnosed to be [i]not this[/i], but (appropriately enough) that constant has far more digits of precision than are typically representable in a float.

##### Share on other sites
taby    1265
[quote name='alvaro' timestamp='1337643825' post='4942034']
The irrationality of pi is not exactly what the problem is. There are many rational numbers that cannot be expressed exactly in floating-point formats either, like 1/3 or 0.1.
[/quote]

Edit: sicrane brought me up to speed with your logic regarding 0.1. It's all obvious now that what does not possess infinite non-zero placeholders right of the decimal point in base 10 may have infinite non-zero placeholders right of the binary point in base 2. Thanks!

I vote for the next version of IEEE fp to be in base pi. Problem solved! I'm kidding.