I made this code in class to practice recursion. It is a sin and cos calculator using sin and cos definition. This code prints an exemple of the correct sin and cos from the <math.h> library and the sin and cos from the calculator between 0 and 360 degrees. I think, There is a mistake and some results give me #IND0. Anyone know where the problem is?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI 3.14159265358979323846
#define TOL 0.001
double my_sin(double num);
double my_cos(double num);
double a_radians(double num)
{
return num * M_PI / 180;
}
double my_sin(double num)
{
if (fabs(num) < TOL) {
num = 1.0;
return num;
} else {
num = 2.0 * (my_sin(num / 2.0)) * (my_cos(num / 2.0));
return num;
}
}
double my_cos(double num)
{
if (fabs(num) < TOL) {
num = 1.0;
return num;
} else {
num = ((my_cos(num / 2.0)) * (my_cos(num / 2.0))) -
((my_sin(num / 2.0)) * (my_sin(num / 2.0)));
return num;
}
}
int main()
{
int graus;
double radians, cos_r, sin_r;
graus = 0;
while (graus <= 360) {
printf("%d graus", graus);
radians = a_radians(graus);
printf("\n%f radiants", radians);
sin_r = radians;
sin_r = my_sin(sin_r);
cos_r = radians;
cos_r = my_cos(cos_r);
printf("\nEl sin es %f", sin(radians));
printf("\nEl sin de mysin es %.5f", sin_r);
printf("\nEl cos es %f", cos(radians));
printf("\nEl cos de mycos es %.5f\n\n", cos_r);
graus = graus + 45;
}
return 0;
}
I tried to do a sin and cos calculator and it doesn't work.
When the absolute value of the angle num is near to zero, the value of sin(num) can be approximated by num, not 1.0.
double my_sin (double num)
{
if (fabs(num)<TOL){
// remove this line
// num=1.0;
return num;
}
else{
num=2.0*(my_sin(num/2.0))*(my_cos(num/2.0));
return num;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With