Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test_fast_exp.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file test_fast_exp.cxx
1 // TRENTO: Reduced Thickness Event-by-event Nuclear Topology
2 // Copyright 2015 Jonah E. Bernhard, J. Scott Moreland
3 // MIT License
4 
5 #include "../src/fast_exp.h"
6 
7 #include "catch.hpp"
8 
9 #include "../src/random.h"
10 
11 using namespace trento;
12 
13 TEST_CASE( "fast exponential" ) {
14  double xmin = -3.2;
15  double xmax = 2.7;
16  std::size_t nsteps = 1000;
17  double tolerance = .25*std::pow((xmax - xmin)/nsteps, 2);
18 
19  FastExp<double> fast_exp{xmin, xmax, nsteps};
20 
21  std::uniform_real_distribution<double> dist{xmin, xmax};
22 
23  double worst_err = 0.;
24  for (int i = 0; i < 1000; ++i) {
25  auto x = dist(random::engine);
26  auto approx = fast_exp(x);
27  auto exact = std::exp(x);
28  auto err = std::fabs(approx-exact)/exact;
29  worst_err = std::max(worst_err, err);
30  }
31 
32  CHECK( worst_err < tolerance );
33 
34 #ifndef NDEBUG
35  CHECK_THROWS_AS( fast_exp(xmin - 1), std::out_of_range );
36 #endif
37 }