Audacity 3.2.0
Functions
MathApproxTest.cpp File Reference
#include "MathApprox.h"
#include <catch2/catch.hpp>
#include <numeric>
Include dependency graph for MathApproxTest.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("FastLog2")
 

Function Documentation

◆ TEST_CASE()

TEST_CASE ( "FastLog2"  )

Audacity: A Digital Audio Editor

MathApproxTest.cpp

Matthieu Hodgkinson

Definition at line 16 of file MathApproxTest.cpp.

17{
18 constexpr auto size = 40;
19 const auto input = [&] {
20 std::vector<float> exponential(size);
21 std::iota(exponential.begin(), exponential.end(), -20.f);
22 std::vector<float> input(exponential.size());
23 std::transform(
24 exponential.begin(), exponential.end(), input.begin(),
25 [](float x) { return std::exp(x); });
26 return input;
27 }();
28 const auto expected = [&] {
29 std::vector<float> expected(size);
30 std::transform(input.begin(), input.end(), expected.begin(), [](float x) {
31 return std::log2(x);
32 });
33 return expected;
34 }();
35 const auto actual = [&] {
36 std::vector<float> actual(size);
37 std::transform(input.begin(), input.end(), actual.begin(), FastLog2);
38 return actual;
39 }();
40 const auto error = [&] {
41 std::vector<float> error(size);
42 std::transform(
43 expected.begin(), expected.end(), actual.begin(), error.begin(),
44 [](float e, float a) { return std::abs(e - a); });
45 return error;
46 }();
47 const auto maxError = *std::max_element(error.begin(), error.end());
48 REQUIRE(maxError < 1e-2);
49}
constexpr float FastLog2(float x)
Approximates the base-2 logarithm of a float to two decimal places, adapted from https://stackoverflo...
Definition: MathApprox.h:32

References FastLog2(), and size.

Here is the call graph for this function: