File indexing completed on 2025-08-05 08:19:07
0001
0002
0003
0004
0005 #include "../src/fast_exp.h"
0006
0007 #include "catch.hpp"
0008
0009 #include "../src/random.h"
0010
0011 using namespace trento;
0012
0013 TEST_CASE( "fast exponential" ) {
0014 double xmin = -3.2;
0015 double xmax = 2.7;
0016 std::size_t nsteps = 1000;
0017 double tolerance = .25*std::pow((xmax - xmin)/nsteps, 2);
0018
0019 FastExp<double> fast_exp{xmin, xmax, nsteps};
0020
0021 std::uniform_real_distribution<double> dist{xmin, xmax};
0022
0023 double worst_err = 0.;
0024 for (int i = 0; i < 1000; ++i) {
0025 auto x = dist(random::engine);
0026 auto approx = fast_exp(x);
0027 auto exact = std::exp(x);
0028 auto err = std::fabs(approx-exact)/exact;
0029 worst_err = std::max(worst_err, err);
0030 }
0031
0032 CHECK( worst_err < tolerance );
0033
0034 #ifndef NDEBUG
0035 CHECK_THROWS_AS( fast_exp(xmin - 1), std::out_of_range );
0036 #endif
0037 }