Everything is fine until I got the result of cos x at 90

Everything is fine until I got the result of cos x at 90. Can anyone help me to find whats wrong with it? :'(

I got the result for cos(90) like this
6,08736e-0,05


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
27
28
  #include <iostream>
#include <iomanip>
#include<math.h>
using namespace std;
int main()

{
cout << "***TABEL SINUS DAN COSINUS DALAM SATUAN DERAJAD\n";
using std::setw;
cout << std::left;
	cout <<setw(7) <<"X" << setw(15) <<"Sin(x)"<< setw(15) << "Cos(x)"<<'\n'<<endl;
	double x,y,a,b;
cout << setw(22)<<"_______________________________________________________________\n"<<endl;
for(x=0; x<=360; x+=30)


     {
     
     	y=x/57.298;
     	double a =sin(double(y));
     	double b = cos(double(y));
    
	using std::setw;
	std::cout << std::left;
	std::cout <<
	setw(7) <<x << setw(15) <<a<< setw(15) <<b<<'\n';}
	cout<<endl;
}
6.08736e-005 means 6.08736 × 10-5 which is close to zero.

You can get much closer by using a more accurate conversion from degrees to radians but you will probably still get some rounding error. This is something that you should expect when working with floating point numbers.

You might want to use std::fixed (to disable scientific notation) and std::setprecision (to limit the number of digits after the decimal point). That way the number will appear as zero even though there is slight rounding errors in your calculations.

 
std::cout << std::fixed << std::setprecision(2);

http://www.cplusplus.com/reference/ios/fixed/
http://www.cplusplus.com/reference/iomanip/setprecision/
Last edited on
> I got the result for cos(90) like this
> 6,08736e-0,05
the result is 6,08736e-05
that's pretty close to 0
¿what's the problem?
The value of 12 * pi needs to be more precise.

Before you define main() add this:

double my_pi(void); //declaration
double my_pi(void){return (4*atan2f(1,1));}; //definition of pi/4 * 4


Then in your main definition:
{

y=x/(12.0*my_pi());
float a =sin((y));
float b = cos((y));
...

}
I prefer floats to doubles.

I would keep the precision at 7 instead of using 7 and 15.




-I prefer floats to doubles.

I advise to prefer doubles to floats; floats are smaller but much more imprecise. floats are better in the uncommon situation where you have to transmit or store a very large # of them (large varies depending on your bandwidth/space/needs) AND the data is of acceptable precision in that format. For almost anything else, doubles are probably the right choice.
John56432 wrote:
The value of 12 * pi needs to be more precise.

The value of 12 * pi needs to be replaced by 180.0 / pi.
Just kinda summarizing advice from Peter87 , John56432, lastchance into working code. I agree with Peter87's findings that scientific should be removed -- with just setprecision, the scientific would still show values very close to zero but not zero for sin(180). Still one funny point here is sin(360) is giving me negative 0!
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
27
28
29
#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

double pi() 
{ 
  return std::atan(1)*4; 
}

int main()
{
  cout << "***TABEL SINUS DAN COSINUS DALAM SATUAN DERAJAD\n";
  cout << left;
  cout << setw(7) <<"X" << setw(15) <<"Sin(x)"<< setw(15) << "Cos(x)"<<'\n'<<endl;
  cout << setw(22)<<"_______________________________________________________________\n"<<endl;
  
  double a,b;
  cout << fixed << setprecision(3);

  for (int degrees=0; degrees<=360; degrees+=30)
  {  
    a = sin(degrees * pi()/180);
    b = cos(degrees * pi()/180);
    cout << setw(7) << degrees << setw(15) << a << setw(15) << b <<'\n';
  }
  cout<<endl;
}


In action: https://repl.it/repls/DutifulPrestigiousBotany
Topic archived. No new replies allowed.