#include <stdio.h>
float sinus(float x) {
x = (x*3.14159265)/180;
float sum = x;
float term = x, powx = x, factx=1;
int n = 1, plus = +1;
while (term >= 0.001 || term <= -0.001)
{
n = n + 2;
factx *= n * (n - 1);
powx *= x*x;
plus = -plus;
term = plus * powx / factx;
sum = sum + term;
}
return sum;
}
int main() {
float degree;
scanf("%f", °ree);
float radian = degree * 3.14159265 / 180;
printf("%.3f\n", sinus(radian));
return 0;
}
This code attempts to compute the sine of an angle using a Taylor series expansion. The `sinus` function converts degrees to radians and then approximates the sine value by summing terms of the series. However, there is a logical error in how the function is called and how the conversion is handled.
- The `sinus` function already converts its input from degrees to radians, but in `main` you also convert the input to radians before passing it. This means the conversion is applied twice, leading to an incorrect result. You should only convert once, either inside `sinus` or in `main`, but not both.
- The while loop condition `term >= 0.001 || term <= -0.001` will continue as long as the term is outside the range -0.001 to 0.001. This is correct for stopping when the term becomes small enough, but make sure the initial term value is set properly before the loop starts.