From dfbdd035dafe91188c7b3c1f7feda50788f120c8 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Thu, 18 Nov 2021 16:16:57 +0100 Subject: [PATCH] AK: Implement `acos` 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. --- AK/Math.h | 2 +- Tests/LibM/test-math.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/AK/Math.h b/AK/Math.h index 8424ec8f2f1..d7aaf9ed111 100644 --- a/AK/Math.h +++ b/AK/Math.h @@ -250,7 +250,7 @@ constexpr T acos(T value) CONSTEXPR_STATE(acos, value); // FIXME: I am naive - return Pi + asin(value); + return static_cast(0.5) * Pi - asin(value); } template diff --git a/Tests/LibM/test-math.cpp b/Tests/LibM/test-math.cpp index 35bbab3bbe2..6368aa293f7 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))); +}