diff options
author | Linus Groh <mail@linusgroh.de> | 2020-04-06 18:56:04 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-06 20:31:48 +0200 |
commit | 220ecde626faef8780b4a7a3372b8f2499aaf564 (patch) | |
tree | 774a2cfe8716b8bcd3e43aec3a654ca40d591f37 /Libraries/LibM | |
parent | 4fe14aab3b9a9cd190d299bdfc77a79eb8938fae (diff) | |
download | serenity-220ecde626faef8780b4a7a3372b8f2499aaf564.zip |
LibM: Improve pow() and powf()
Diffstat (limited to 'Libraries/LibM')
-rw-r--r-- | Libraries/LibM/math.cpp | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/Libraries/LibM/math.cpp b/Libraries/LibM/math.cpp index b3c89e4a26..bc4a39bffa 100644 --- a/Libraries/LibM/math.cpp +++ b/Libraries/LibM/math.cpp @@ -101,12 +101,39 @@ float sinf(float angle) double pow(double x, double y) { - //FIXME: Extremely unlikely to be standards compliant. + // FIXME: Please fix me. I am naive. + if (y == 0) + return 1; + if (y == 1) + return x; + int y_as_int = (int)y; + if (y == (double)y_as_int) { + double result = x; + for (int i = 0; i < abs(y) - 1; ++i) + result *= x; + if (y < 0) + result = 1.0 / result; + return result; + } return exp(y * log(x)); } float powf(float x, float y) { + // FIXME: Please fix me. I am naive. + if (y == 0) + return 1; + if (y == 1) + return x; + int y_as_int = (int)y; + if (y == (float)y_as_int) { + float result = x; + for (int i = 0; i < abs(y) - 1; ++i) + result *= x; + if (y < 0) + result = 1.0 / result; + return result; + } return (float)exp((double)y * log((double)x)); } |