diff --git a/CMakeLists.txt b/CMakeLists.txt index 016677d..8dd3c15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,7 @@ if(NOT CMAKE_CROSSCOMPILING) "lib/grit/math/power_test.cpp" "lib/grit/math/normalizable_range_test.cpp" "lib/grit/math/static_lookup_table_test.cpp" + "lib/grit/math/trigonometry_test.cpp" "lib/grit/unit/decibel_test.cpp" ) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 572990e..b86e04f 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -78,6 +78,7 @@ target_sources(gritwave-grit INTERFACE "grit/math/sign.hpp" "grit/math/static_lookup_table.hpp" "grit/math/static_lookup_table_transform.hpp" + "grit/math/trigonometry.hpp" "grit/unit.hpp" "grit/unit/decibel.hpp" diff --git a/lib/grit/math.hpp b/lib/grit/math.hpp index 27015f9..462e224 100644 --- a/lib/grit/math.hpp +++ b/lib/grit/math.hpp @@ -13,3 +13,4 @@ #include #include #include +#include diff --git a/lib/grit/math/trigonometry.hpp b/lib/grit/math/trigonometry.hpp new file mode 100644 index 0000000..9f7eae0 --- /dev/null +++ b/lib/grit/math/trigonometry.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +namespace grit { + +/// \ingroup grit-math +template +[[nodiscard]] constexpr auto bhaskara(Float x) -> Float +{ + auto const pi = static_cast(etl::numbers::pi); + auto const num = Float(16) * x * (pi - x); + auto const denom = Float(5) * pi * pi - Float(4) * x * (pi - x); + return num / denom; +} + +} // namespace grit diff --git a/lib/grit/math/trigonometry_test.cpp b/lib/grit/math/trigonometry_test.cpp new file mode 100644 index 0000000..14535ce --- /dev/null +++ b/lib/grit/math/trigonometry_test.cpp @@ -0,0 +1,15 @@ +#include "trigonometry.hpp" + +#include +#include + +TEMPLATE_TEST_CASE("math: bhaskara", "", float, double) +{ + using Float = TestType; + + REQUIRE_THAT(grit::bhaskara(Float(0.0)), Catch::Matchers::WithinAbs(0.0, 1e-8)); + REQUIRE_THAT(grit::bhaskara(Float(0.5)), Catch::Matchers::WithinAbs(0.479426, 1e-2)); + REQUIRE_THAT(grit::bhaskara(Float(1.0)), Catch::Matchers::WithinAbs(0.841471, 1e-2)); + REQUIRE_THAT(grit::bhaskara(Float(etl::numbers::pi / 3.0)), Catch::Matchers::WithinAbs(0.866025, 1e-2)); + REQUIRE_THAT(grit::bhaskara(Float(etl::numbers::pi / 2.0)), Catch::Matchers::WithinAbs(1.0, 1e-8)); +}