Skip to content

Commit

Permalink
Add bhaskara sine approximation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Feb 17, 2024
1 parent be6f01f commit 6741597
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/grit/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
#include <grit/math/sign.hpp>
#include <grit/math/static_lookup_table.hpp>
#include <grit/math/static_lookup_table_transform.hpp>
#include <grit/math/trigonometry.hpp>
18 changes: 18 additions & 0 deletions lib/grit/math/trigonometry.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <etl/cmath.hpp>
#include <etl/numbers.hpp>

namespace grit {

/// \ingroup grit-math
template<etl::floating_point Float>
[[nodiscard]] constexpr auto bhaskara(Float x) -> Float
{
auto const pi = static_cast<Float>(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
15 changes: 15 additions & 0 deletions lib/grit/math/trigonometry_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "trigonometry.hpp"

#include <catch2/catch_template_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>

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));
}

0 comments on commit 6741597

Please sign in to comment.