diff options
author | Jelle Raaijmakers <jelle@gmta.nl> | 2021-11-18 16:16:57 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-18 21:10:30 +0100 |
commit | dfbdd035dafe91188c7b3c1f7feda50788f120c8 (patch) | |
tree | 7e84459e85d8eddf3a917e4386789f25458c543b | |
parent | 9ea5a00e24076431fe197ec76f88009ef28d9bbb (diff) | |
download | serenity-dfbdd035dafe91188c7b3c1f7feda50788f120c8.zip |
AK: Implement `acos<T>` correctly
This is a naive implementation based on the symmetry with `asin`.
Before, I'm not really sure what we were doing, but it was returning
wildly incorrect results.
-rw-r--r-- | AK/Math.h | 2 | ||||
-rw-r--r-- | Tests/LibM/test-math.cpp | 8 |
2 files changed, 9 insertions, 1 deletions
@@ -250,7 +250,7 @@ constexpr T acos(T value) CONSTEXPR_STATE(acos, value); // FIXME: I am naive - return Pi<T> + asin(value); + return static_cast<T>(0.5) * Pi<T> - asin<T>(value); } template<FloatingPoint T> diff --git a/Tests/LibM/test-math.cpp b/Tests/LibM/test-math.cpp index 35bbab3bbe..6368aa293f 100644 --- a/Tests/LibM/test-math.cpp +++ b/Tests/LibM/test-math.cpp @@ -259,3 +259,11 @@ TEST_CASE(fmax_and_fmin) EXPECT(fmin(0, NAN) == 0); EXPECT(isnan(fmin(NAN, NAN))); } + +TEST_CASE(acos) +{ + EXPECT_APPROXIMATE(acos(-1), M_PI); + EXPECT_APPROXIMATE(acos(0), 0.5 * M_PI); + EXPECT_APPROXIMATE(acos(1), 0); + EXPECT(isnan(acos(1.1))); +} |