File: test_threaded_precision.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (153 lines) | stat: -rw-r--r-- 5,346 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
///////////////////////////////////////////////////////////////
//  Copyright 2021 John Maddock. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
//

#ifdef _MSC_VER
#define _SCL_SECURE_NO_WARNINGS
#endif

#include <thread>
#include <boost/detail/lightweight_test.hpp>
#include <array>
#include <boost/math/special_functions/relative_difference.hpp>
#include <boost/math/special_functions/gamma.hpp>
#include <boost/math/quadrature/tanh_sinh.hpp>
#include "test.hpp"

#if !defined(TEST_MPF_50) && !defined(TEST_MPFR_50)
#define TEST_MPF_50
#define TEST_MPFR_50

#ifdef _MSC_VER
#pragma message("CAUTION!!: No backend type specified so testing everything.... this will take some time!!")
#endif
#ifdef __GNUC__
#pragma warning "CAUTION!!: No backend type specified so testing everything.... this will take some time!!"
#endif

#endif

#include <boost/multiprecision/mpfr.hpp>

#if defined(TEST_MPF_50)
#include <boost/multiprecision/gmp.hpp>
#endif

template <class T>
void thread_test_proc(unsigned digits)
{
   typedef boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<1000> > mpfr_float_1000;

   if (!mpfr_buildopt_tls_p())
      return;

   T::thread_default_precision(digits);

   T result, value;

   value = boost::math::constants::pi<T>();
   result.assign(boost::math::constants::pi<mpfr_float_1000>().str());
   BOOST_CHECK_LE(boost::math::epsilon_difference(value, result), 20);
   BOOST_CHECK_EQUAL(value.precision(), digits);

   for (unsigned i = 20; i < 500; ++i)
   {
      T arg(i);
      arg /= 3;
      value = boost::math::tgamma(arg);
      BOOST_CHECK_EQUAL(value.precision(), digits);
      result.assign(boost::math::tgamma(mpfr_float_1000(arg)).str());
      BOOST_CHECK_LE(boost::math::epsilon_difference(value, result), 800);
   }

   //
   // Try tanh_sinh integration:
   //
   using namespace boost::math::constants;
   boost::math::quadrature::tanh_sinh<T> integrator;
   T                                     tol = 500;
   // Example 1:
   auto f1         = [](const T& t) { return static_cast<T>(t * boost::math::log1p(t)); };
   T Q          = integrator.integrate(f1, (T)0, (T)1);
   T Q_expected = half<T>() * half<T>();
   BOOST_CHECK_EQUAL(Q.precision(), digits);
   BOOST_CHECK_EQUAL(Q_expected.precision(), digits);
   BOOST_CHECK_LE(boost::math::epsilon_difference(Q, Q_expected), tol);

   // Example 2:
   auto f2    = [](const T& t) { return static_cast<T>(t * t * atan(t)); };
   Q          = integrator.integrate(f2, (T)0, (T)1);
   Q_expected = (pi<T>() - 2 + 2 * ln_two<T>()) / 12;
   BOOST_CHECK_EQUAL(Q.precision(), digits);
   BOOST_CHECK_EQUAL(Q_expected.precision(), digits);
   BOOST_CHECK_LE(boost::math::epsilon_difference(Q, Q_expected), tol);

   // Example 3:
   auto f3    = [](const T& t) { return static_cast<T>(exp(t) * cos(t)); };
   Q          = integrator.integrate(f3, (T)0, half_pi<T>());
   Q_expected = boost::math::expm1(half_pi<T>()) * half<T>();
   BOOST_CHECK_EQUAL(Q.precision(), digits);
   BOOST_CHECK_EQUAL(Q_expected.precision(), digits);
   BOOST_CHECK_LE(boost::math::epsilon_difference(Q, Q_expected), tol);

   // Example 4:
   auto f4    = [](T x) -> T { T t0 = sqrt(x*x + 2); return atan(t0)/(t0*(x*x+1)); };
   Q          = integrator.integrate(f4, (T)0, (T)1);
   Q_expected = 5 * pi<T>() * pi<T>() / 96;
   BOOST_CHECK_EQUAL(Q.precision(), digits);
   BOOST_CHECK_EQUAL(Q_expected.precision(), digits);
   BOOST_CHECK_LE(boost::math::epsilon_difference(Q, Q_expected), tol);

   // Example 5:
   auto f5    = [](const T& t)->T { return sqrt(t) * log(t); };
   Q          = integrator.integrate(f5, (T)0, (T)1);
   Q_expected = -4 / (T)9;
   BOOST_CHECK_EQUAL(Q.precision(), digits);
   BOOST_CHECK_EQUAL(Q_expected.precision(), digits);
   BOOST_CHECK_LE(boost::math::epsilon_difference(Q, Q_expected), tol);

   // Example 6:
   auto f6    = [](const T& t)->T { return sqrt(1 - t * t); };
   Q          = integrator.integrate(f6, (T)0, (T)1);
   Q_expected = pi<T>() / 4;
   BOOST_CHECK_EQUAL(Q.precision(), digits);
   BOOST_CHECK_EQUAL(Q_expected.precision(), digits);
   BOOST_CHECK_LE(boost::math::epsilon_difference(Q, Q_expected), tol);
}

int main()
{
#ifdef TEST_MPF_50
   {
      std::thread t1(thread_test_proc<boost::multiprecision::mpf_float>, 35);
      std::thread t2(thread_test_proc<boost::multiprecision::mpf_float>, 55);
      std::thread t3(thread_test_proc<boost::multiprecision::mpf_float>, 75);
      std::thread t4(thread_test_proc<boost::multiprecision::mpf_float>, 105);
      std::thread t5(thread_test_proc<boost::multiprecision::mpf_float>, 305);

      t1.join();
      t2.join();
      t3.join();
      t4.join();
      t5.join();
   }
#endif
#ifdef TEST_MPFR_50
   {
      std::thread t1(thread_test_proc<boost::multiprecision::mpfr_float>, 35);
      std::thread t2(thread_test_proc<boost::multiprecision::mpfr_float>, 55);
      std::thread t3(thread_test_proc<boost::multiprecision::mpfr_float>, 75);
      std::thread t4(thread_test_proc<boost::multiprecision::mpfr_float>, 105);
      std::thread t5(thread_test_proc<boost::multiprecision::mpfr_float>, 305);

      t1.join();
      t2.join();
      t3.join();
      t4.join();
      t5.join();
   }
#endif
   return boost::report_errors();
}