Skip to content

Commit

Permalink
More complete log implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Nov 23, 2023
1 parent 7796ba7 commit f0ca995
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/math_approx/src/log_approx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ T log (T x)
{
return log<pow_detail::BaseE<scalar_of_t<T>>, order> (x);
}

template <int order, typename T>
T log2 (T x)
{
return log<pow_detail::Base2<scalar_of_t<T>>, order> (x);
}

template <int order, typename T>
T log10 (T x)
{
return log<pow_detail::Base10<scalar_of_t<T>>, order> (x);
}
}
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ setup_catch_test(tanh_approx_test)
setup_catch_test(sigmoid_approx_test)
setup_catch_test(trig_approx_test)
setup_catch_test(pow_approx_test)
setup_catch_test(log_approx_test)
115 changes: 115 additions & 0 deletions test/src/log_approx_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "test_helpers.hpp"
#include "catch2/catch_template_test_macros.hpp"

#include <catch2/catch_test_macros.hpp>
#include <iostream>

#include <math_approx/math_approx.hpp>

template <typename T = float>
void test_approx (const auto& all_floats, const auto& y_exact, auto&& f_approx, float err_bound)
{
const auto y_approx = test_helpers::compute_all<T> (all_floats, f_approx);
const auto error = test_helpers::compute_error<T> (y_exact, y_approx);
const auto max_error = test_helpers::abs_max<T> (error);

std::cout << max_error << std::endl;
REQUIRE (std::abs (max_error) < err_bound);
}


TEMPLATE_TEST_CASE ("Log Approx Test", "", float, double)
{
const auto all_floats = test_helpers::all_32_bit_floats<TestType> (0.01f, 10.0f, 1.0e-3f);
const auto y_exact = test_helpers::compute_all<TestType> (all_floats, [] (auto x)
{ return std::log (x); });

SECTION ("6th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log<6> (x); },
4.5e-6f);
}
SECTION ("5th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log<5> (x); },
1.5e-5f);
}
SECTION ("4th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log<4> (x); },
8.5e-5f);
}
SECTION ("3th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log<3> (x); },
6.5e-4f);
}
}

TEMPLATE_TEST_CASE ("Log2 Approx Test", "", float, double)
{
const auto all_floats = test_helpers::all_32_bit_floats<TestType> (0.01f, 10.0f, 1.0e-3f);
const auto y_exact = test_helpers::compute_all<TestType> (all_floats, [] (auto x)
{ return std::log2 (x); });

SECTION ("6th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log2<6> (x); },
6.0e-6f);
}
SECTION ("5th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log2<5> (x); },
2.0e-5f);
}
SECTION ("4th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log2<4> (x); },
1.5e-4f);
}
SECTION ("3th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log2<3> (x); },
9.0e-4f);
}
}

TEMPLATE_TEST_CASE ("Log10 Approx Test", "", float, double)
{
const auto all_floats = test_helpers::all_32_bit_floats<TestType> (0.01f, 10.0f, 1.0e-3f);
const auto y_exact = test_helpers::compute_all<TestType> (all_floats, [] (auto x)
{ return std::log10 (x); });

SECTION ("6th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log10<6> (x); },
2.0e-6f);
}
SECTION ("5th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log10<5> (x); },
6.0e-6f);
}
SECTION ("4th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log10<4> (x); },
4.0e-5f);
}
SECTION ("3th-Order")
{
test_approx<TestType> (all_floats, y_exact, [] (auto x)
{ return math_approx::log10<3> (x); },
3.0e-4f);
}
}

0 comments on commit f0ca995

Please sign in to comment.