diff options
author | stelar7 <dudedbz@gmail.com> | 2020-06-21 09:45:48 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-22 10:33:50 +0200 |
commit | b97da17b904873b8af339894213ccc6d4e787688 (patch) | |
tree | 631ae17c0eeb6f05fd2d6eb1511e161bf3010dbb | |
parent | 07d976716fdb323de1087ac0378060a7e20eb18c (diff) | |
download | serenity-b97da17b904873b8af339894213ccc6d4e787688.zip |
LibM: Add some more math functions
-rw-r--r-- | Libraries/LibM/math.cpp | 62 | ||||
-rw-r--r-- | Libraries/LibM/math.h | 11 |
2 files changed, 73 insertions, 0 deletions
diff --git a/Libraries/LibM/math.cpp b/Libraries/LibM/math.cpp index f1de325ebd..ffa6483ccb 100644 --- a/Libraries/LibM/math.cpp +++ b/Libraries/LibM/math.cpp @@ -474,4 +474,66 @@ double modf(double x, double* intpart) *intpart = (double)((int)(x)); return x - (int)x; } + +double gamma(double x) +{ + // Stirling approximation + return sqrt(2.0 * M_PI / x) * pow(x / M_E, x); +} + +double expm1(double x) +{ + return pow(M_E, x) - 1; +} + +double cbrt(double x) +{ + if (x > 0) { + return pow(x, 1.0 / 3.0); + } + + return -pow(-x, 1.0 / 3.0); +} + +double log1p(double x) +{ + return log(1 + x); +} + +double acosh(double x) +{ + return log(x + sqrt(x * x - 1)); +} + +double asinh(double x) +{ + return log(x + sqrt(x * x + 1)); +} + +double atanh(double x) +{ + return log((1 + x) / (1 - x)) / 2.0; +} + +double hypot(double x, double y) +{ + return sqrt(x * x + y * y); +} + +double erf(double x) +{ + // algorithm taken from Abramowitz and Stegun (no. 26.2.17) + double t = 1 / (1 + 0.47047 * abs(x)); + double poly = t * (0.3480242 + t * (-0.958798 + t * 0.7478556)); + double answer = 1 - poly * exp(-x * x); + if (x < 0) + return -answer; + + return answer; +} + +double erfc(double x) +{ + return 1 - erf(x); +} } diff --git a/Libraries/LibM/math.h b/Libraries/LibM/math.h index c8177fded8..36b971560f 100644 --- a/Libraries/LibM/math.h +++ b/Libraries/LibM/math.h @@ -99,4 +99,15 @@ double frexp(double, int*); float frexpf(float, int*); long double frexpl(long double, int*); +double gamma(double); +double expm1(double); +double cbrt(double); +double log1p(double); +double acosh(double); +double asinh(double); +double atanh(double); +double hypot(double, double); +double erf(double); +double erfc(double); + __END_DECLS |