Originally Posted by
InvariantLoop
Code:
x *= 3.141593 /180.0; /* convert to radians*/
If you are going to be doing math, you might as well get the best precision that you can. Doubles are good for 15 or 16 significant decimal digits, but not if your approximation of pi is only 8 digits:
Code:
#include <stdio.h>
#include <math.h>
int main()
{
double x;
double pi;
x =45.0; /* 45 degree */
x *= 3.141593 /180.0; /* convert to radians*/
printf("First with pi = 3.141593:\n");
printf(" The sine of 45 degrees is: %.15f\n", sin(x));
printf(" The cosine of 45 degrees is: %.15f\n", cos(x));
printf(" The tangent of 45 degrees is: %.15f\n", tan(x));
/*
* Here's a way to get the best precision pi that your library can
* calculate, without typing in a bunch of digits:
*
*/
pi = 4.0 * atan(1.0);
x = 45.0;
x *= pi / 180.0;
printf("\n\n");
printf("Now, with pi = %.15f:\n", pi);
printf(" The sine of 45 degrees is: %.15f\n", sin(x));
printf(" The cosine of 45 degrees is: %.15f\n", cos(x));
printf(" The tangent of 45 degrees is: %.15f\n", tan(x));
return 0;
}
Regards,
Dave