Handle cos, sin function.

I dont believe i get the correct results. Is something wrong with the way i use the cos and sin function? When the angle i 45 degree the x y velocity should be the same, since cos(45) and sin(45) is the same value.

Now i get the vX to 5.25 and vY to 8.5. I guess i have done some simple error but i have tried for some time now, cant seem to find the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <iostream>
#include <vector>
#include <cmath>

double positionX();
double positionY();


int main()
{
    const int frameX{100}, frameY{100};
    double t{0}, g{9.82}, v0{10}, vX{0}, vY{0}, cosinusAng{0}, sinusAng{0}; 
    double pi = 2*std::acos(0.0);
    int angle{45}, posX{0}, posY{0};
    
    cosinusAng = std::cos(angle);
    sinusAng = std::sin(angle);

    vX = v0 * cosinusAng;
    vY = v0 * sinusAng;
    
    
    std::cout << std::cos(angle)*(180/pi) << "\n";
    std::cout << "vX: " << vX << "\t" << "vY: " << vY << "\t" << pi;
    return 0;
}
cos(), sin() (and tan etc) work in radians - not degrees. If your angle is in degrees then you need to convert them to radians.

180 degrees is pi radians

so to convert degree to radians:

angle * pi / 180
Thanks, but something is strange anyway. When I add vX=v0*cosinusAng*(pi/180) I get 0.091686. It’s way to small, I mean v0 = 10.
When I add vX=v0*cosinusAng*(pi/180)

The "*(pi/180)" should have been done when evaluating the cosine, NOT at this point.


It doesn't make sense that the angle should be an int.


cosinusAng = std::cos(angle);
sinusAng = std::sin(angle);

These are wrong if angle is in degrees (as you have at the moment).

If angle is in degrees then
1
2
    cosinusAng = std::cos(angle*pi/180);
    sinusAng = std::sin(angle*pi/180);



Given the opportunities for getting angular units wrong, it's quite a good idea to include them in the name: angle_degrees, or angle_radians. Just wait until you have to deal with rotating machinery and RPM!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>

int main() {
	const double v0 { 10 };
	const int angle { 45 };

	const auto cosinusAng { std::cos(angle * M_PI / 180.0) };
	const auto sinusAng { std::sin(angle * M_PI / 180.0) };

	const auto vX { v0 * cosinusAng };
	const auto vY { v0 * sinusAng };

	std::cout << cosinusAng << "\n";
	std::cout << "vX: " << vX << "\t" << "vY: " << vY << "\t" << M_PI << '\n';
}


displays:


0.707107
vX: 7.07107     vY: 7.07107     3.14159


as expected.

Note. Depending upon the compiler you may need #define _USE_MATH_DEFINES
before the #includes for M_PI.

If you're using C++20, then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
#include <numbers>

int main() {
	const double v0 { 10 };
	const int angle { 45 };
	const double angrad { angle * std::numbers::pi / 180.0 };

	const auto cosinusAng { std::cos(angrad) };
	const auto sinusAng { std::sin(angrad) };

	const auto vX { v0 * cosinusAng };
	const auto vY { v0 * sinusAng };

	std::cout << cosinusAng << "\n";
	std::cout << "vX: " << vX << "\t" << "vY: " << vY << "\t" << M_PI << '\n';
}


See:
https://en.cppreference.com/w/cpp/numeric/constants
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
#include <cmath>

//double positionX();
//double positionY();


int main()
{
    const int frameX{100}, frameY{100};
    double t{0}, g{9.82}, v0{10}, vX{0}, vY{0}, cosinusAng{0}, sinusAng{0}; 
    double pi = 2*std::acos(0.0);
    double angle{45}, posX{0}, posY{0};              // <==== double
    
    cosinusAng = std::cos(angle*pi/180);
    sinusAng = std::sin(angle*pi/180);

    vX = v0 * cosinusAng;
    vY = v0 * sinusAng;
    
    
    std::cout << cosinusAng << "\n";                 // <===== should be 1/sqrt(2), or 0.7071...
    std::cout << "vX: " << vX << "\t" << "vY: " << vY << "\t" << pi;
}
0.707107
vX: 7.07107	vY: 7.07107	3.14159 



There's not much point in initialising some of variables to values that you are about to overwrite.

What latitude are you at with g=9.82 m/s^2?
Last edited on
Topic archived. No new replies allowed.