LibM: Rewrite trigonometric functions.

This way they seem to actually work as expected ;)
This commit is contained in:
Sergey Bugaev 2019-06-17 16:44:47 +03:00 committed by Andreas Kling
parent 502c54e39a
commit 9fa4e779ff
Notes: sideshowbarker 2024-07-19 13:33:44 +09:00

View file

@ -12,27 +12,34 @@ double cos(double angle)
return sin(angle + M_PI_2);
}
double ampsin(double angle)
static double sin_helper(double a)
{
double looped_angle = fmod(M_PI + angle, M_TAU);
double looped_angle_squared = looped_angle * looped_angle;
double a3 = a * a * a;
double a5 = a3 * a * a;
return a - a3 / 6.0 + a5 / 120.0;
}
double quadratic_term;
if (looped_angle_squared > 0) {
quadratic_term = -looped_angle_squared;
} else {
quadratic_term = looped_angle_squared;
}
double linear_term = M_PI * looped_angle;
return quadratic_term * linear_term;
static double cos_helper(double a)
{
double a2 = a * a;
double a4 = a2 * a2;
return 1 - a2 / 2.0 + a4 / 24.0;
}
double sin(double angle)
{
double vertical_scaling = M_PI_2 * M_PI_2;
return ampsin(angle) / vertical_scaling;
double a = fmod(angle, M_TAU);
if (a < 0)
a += M_TAU;
if (a <= M_PI_2 * 0.5)
return sin_helper(a);
if (a <= M_PI_2)
return cos_helper(M_PI_2 - a);
if (a <= M_PI_2 * 1.5)
return cos_helper(a - M_PI_2);
if (a <= M_PI)
return sin_helper(M_PI - a);
return -sin(angle - M_PI);
}
double pow(double x, double y)
@ -55,7 +62,7 @@ double tanh(double)
double tan(double angle)
{
return ampsin(angle) / ampsin(M_PI_2 + angle);
return sin(angle) / cos(angle);
}
double sqrt(double)