From b7d1eee04768f5baf077f0ce36384748219ca063 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 16 Jun 2019 19:58:45 +0100 Subject: [PATCH] LibM: Add trigonometric approximations and misc mathematical functions --- LibM/math.cpp | 40 +++++++++++++++++++++++++++++++--------- LibM/math.h | 3 +++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/LibM/math.cpp b/LibM/math.cpp index dcc642396e..88fac357f8 100644 --- a/LibM/math.cpp +++ b/LibM/math.cpp @@ -2,15 +2,37 @@ #include extern "C" { - -double cos(double) +double trunc(double x) { - ASSERT_NOT_REACHED(); + return (int)x; } -double sin(double) +double cos(double angle) { - ASSERT_NOT_REACHED(); + return sin(angle + M_PI_2); +} + +double ampsin(double angle) +{ + double looped_angle = fmod(M_PI + angle, M_TAU); + double looped_angle_squared = looped_angle * looped_angle; + + 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; +} + +double sin(double angle) +{ + double vertical_scaling = M_PI_2 * M_PI_2; + return ampsin(angle) / vertical_scaling; } double pow(double x, double y) @@ -31,9 +53,9 @@ double tanh(double) ASSERT_NOT_REACHED(); } -double tan(double) +double tan(double angle) { - ASSERT_NOT_REACHED(); + return ampsin(angle) / ampsin(M_PI_2 + angle); } double sqrt(double) @@ -56,9 +78,9 @@ double log(double) ASSERT_NOT_REACHED(); } -double fmod(double, double) +double fmod(double index, double period) { - ASSERT_NOT_REACHED(); + return index - trunc(index / period) * period; } double exp(double) diff --git a/LibM/math.h b/LibM/math.h index ccbce35d54..720daa7747 100644 --- a/LibM/math.h +++ b/LibM/math.h @@ -5,6 +5,9 @@ __BEGIN_DECLS #define HUGE_VAL 1e10000 +#define M_PI 3.141592653589793 +#define M_PI_2 (M_PI / 2) +#define M_TAU (M_PI * 2) double acos(double); float acosf(float);