Public Group

This topic is 4161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I want to do some 90 degree vector rotations but it seems I cant use any trig functions that divid by 0 in C++. I just get massive weird numbers. So if im working with these trig functions for example cos(90) or sin(180) How do I work around this? Do I have to put checks in for 0? What am I doing wrong.

##### Share on other sites
The trig functions use radians. There are 2*Pi radians in a circle, rather than 360 degrees.

EDIT: Ooops, ignore this. It looks like I misunderstood. However, if you didn't know this, it's still worth remembering.

##### Share on other sites
Without more information on what exactly you're trying to rotate, it's hard to give good suggestions. However, you may want to look into atan2( ). It's not fooled by parameters that are 0, as long as both of them aren't.

Can you offer some clarification, and possibly a snippet or few of code?
-jouley

##### Share on other sites
Quote:
 Original post by TheUnbelieverThe trig functions use radians. There are 2*Pi radians in a circle, rather than 360 degrees.

Ah maybe i should clarify my post...

I use this code

cos(90*PI/180)

Which should = 0? But instead I get some crazy double number.

sin(90*PI/180)

sin 90 works as intended though as it equals 1. Even if I plug the radian number directly into the cos function I still get something that defiently does not equal 0. I have the same problem with for instance...

sin(180*PI/180)

So it is clearly a problem with 0.

Quote:
 Original post by jouleyWithout more information on what exactly you're trying to rotate, it's hard to give good suggestions. However, you may want to look into atan2( ). It's not fooled by parameters that are 0, as long as both of them aren't.Can you offer some clarification, and possibly a snippet or few of code?-jouley

Im using something like this to rotate shapes

new x = x * cos(theta) + y * sin(theta)
new y = x * sin(theta) - y * cos(theta)

in my code it is impletemented as such:

int x = game_piece::block_x_pos - game_piece::axis_x;int y = game_piece::block_y_pos - game_piece::axis_y;float x_new = x * cos(180*PI/180) + y * sin(180*PI/180); float y_new = y * sin(180*PI/180) - x * cos(180*PI/180);

My problem is not with my method as it works on paper but applying it to C++. You suggest using atan2. I havent worked out a method using this yet.

##### Share on other sites
180 * PI / 180

Those 180s are integers. You're trying to do intger math with pi, and that's going to be a mess.

Try using 180.0f or 180.0 instead.

##### Share on other sites
That code

float PI = 3.1415926535897;cout << cos (90.0*PI/180.0) << endl;cout << sin (90.0*PI/180.0) << endl;

produces an output of -4.37114e-008 and 1.

Note that the cos value has an exponent that makes the value almost 0. Mabe you misunderstood that? Or I didn't catch what do you mean.

##### Share on other sites
Don't know if this is relevant, but the following code with VS:

#include <cstdlib>#include <cstdio>#include <iostream>#define _USE_MATH_DEFINES#include <cmath>int main(){	float A=cos(90*M_PI/180);	std::cout << "cout: " << A << std::endl;	std::printf("printf: %f\n",A);		return 0;}

produces the output:

cout: 6.12323e-017
printf: 0.000000

Never really understood why that was.

[EDIT] surfi's comment above about the exponent goes a long way towards explaining it.

##### Share on other sites
Quote:
 Original post by parawizardWhich should = 0? But instead I get some crazy double number.

What is the number you're getting? Close to 0?

PI is approximated when expressed as a variable in C++. For instance, one can use either 3.141592653589793 or 3.14 to express its true value. They're both approximations, though the first one is obviously the better of the two in terms of precision.

The better the approximation of PI, the more precise the final approximation of degrees being converted into radians.

Because the radian value is inherently imprecise, so is the input into cos(), and so is the answer it returns.

The value, 6.12323e-017, as quoted by EasilyConfused, is very close to 0 but not quite. It's approximate.

##### Share on other sites
Quote:
 Original post by Ra180 * PI / 180Those 180s are integers. You're trying to do intger math with pi, and that's going to be a mess.Try using 180.0f or 180.0 instead.

Since PI is not an integer the compiler should be clever enough to upgrade the ints to floats before doing any maths.

You'd have to worry about that if you were writing "int x = 3 / 2;" for example. Even then you'd only have to change one of the values "int x = 3 / 2.0f;"

How about declaring a const float rad = PI/180.0; though? Or if you truly are using the same sin() and cos() values all the time, cache those.

##### Share on other sites
Well having a number close to 0 doesnt really help at all ;) My calculator shows it is equal to zero. Is this because the approximation of PI is longer or as long as the calculators decimal places so that it rounds to a perfect 0? I dont really want to be shown how to fix this problem but rather why it is a problem ;) What is the difference between this and my calculator.

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 10
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631420
• Total Posts
2999987
×