I've been trying to write a program that uses the trig functions in cmath
, sin()
and cos()
in particular. I understand that these functions use radians so I added * pi / 180
however the program still displays incorrect numbers. Here is some code.
#include<iostream>
#include<cmath>
using namespace std;
const double pi = 3.1415926;
int main()
{
int x = 0;
double d;
for(int forl=0;forl < 10;forl++)
{
d = sin(50 / pi * 180);
cout << d << endl;
x += 5;
}
cin >> x;
return 0;
}
WHy does this code display incorrect numbers?
sin(50 / pi * 180);
uses the exact same angle every time through the loop, so you'll print sin(50°) ten times. Perhaps you meant
sin(x * pi / 180.0);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments