MCKL
Monte Carlo Kernel Library
constants.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/math/constants.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_MATH_CONSTANTS_HPP
33 #define MCKL_MATH_CONSTANTS_HPP
34 
35 #include <mckl/internal/config.h>
36 #include <limits>
37 #include <type_traits>
38 
39 #define MCKL_DEFINE_MATH_CONSTANTS(name, val) \
40  template <typename T> \
41  inline constexpr T const_##name() noexcept( \
42  std::is_floating_point<T>::value || std::is_integral<T>::value) \
43  { \
44  return static_cast<T>(val##l); \
45  } \
46  \
47  template <> \
48  inline constexpr float const_##name<float>() noexcept \
49  { \
50  return val##f; \
51  } \
52  \
53  template <> \
54  inline constexpr double const_##name<double>() noexcept \
55  { \
56  return val; \
57  } \
58  \
59  template <> \
60  inline constexpr long double const_##name<long double>() noexcept \
61  { \
62  return val##l; \
63  }
64 
65 namespace mckl {
66 
69 template <typename T>
70 inline constexpr T const_inf()
71 {
72  static_assert(std::is_floating_point<T>::value,
73  "**const_inf** used with T other than floating point types");
74 
75  return std::numeric_limits<T>::infinity();
76 }
77 
80 template <typename T>
81 inline constexpr T const_nan()
82 {
83  static_assert(std::is_floating_point<T>::value,
84  "**const_nan** used with T other than floating point types");
85 
86  return std::numeric_limits<T>::quiet_NaN();
87 }
88 
91 template <typename T>
92 inline constexpr T const_zero()
93 {
94  return 0;
95 }
96 
99 template <typename T>
100 inline constexpr T const_one()
101 {
102  return 1;
103 }
104 
107 MCKL_DEFINE_MATH_CONSTANTS(pi, 3.141592653589793238462643383279502884197)
108 
109 MCKL_DEFINE_MATH_CONSTANTS(pi_2, 6.283185307179586476925286766559005768394)
112 
115 MCKL_DEFINE_MATH_CONSTANTS(pi_inv, 0.3183098861837906715377675267450287240689)
116 
119 MCKL_DEFINE_MATH_CONSTANTS(pi_sqr, 9.869604401089358618834490999876151135314)
120 
123 MCKL_DEFINE_MATH_CONSTANTS(pi_by2, 1.570796326794896619231321691639751442099)
124 
127 MCKL_DEFINE_MATH_CONSTANTS(pi_by3, 1.047197551196597746154214461093167628066)
128 
131 MCKL_DEFINE_MATH_CONSTANTS(pi_by4, 0.7853981633974483096156608458198757210493)
132 
135 MCKL_DEFINE_MATH_CONSTANTS(pi_by6, 0.5235987755982988730771072305465838140329)
136 
139 MCKL_DEFINE_MATH_CONSTANTS(pi_2by3, 2.094395102393195492308428922186335256131)
140 
143 MCKL_DEFINE_MATH_CONSTANTS(pi_3by4, 2.356194490192344928846982537459627163148)
144 
147 MCKL_DEFINE_MATH_CONSTANTS(pi_4by3, 4.188790204786390984616857844372670512263)
148 
151 MCKL_DEFINE_MATH_CONSTANTS(sqrt_pi, 1.772453850905516027298167483341145182798)
152 
156  sqrt_pi_2, 2.506628274631000502415765284811045253007)
157 
161  sqrt_pi_inv, 0.5641895835477562869480794515607725858441)
162 
166  sqrt_pi_by2, 1.253314137315500251207882642405522626503)
167 
171  sqrt_pi_by3, 1.023326707946488488479551624889264860707)
172 
176  sqrt_pi_by4, 0.8862269254527580136490837416705725913988)
177 
181  sqrt_pi_by6, 0.7236012545582676593630146272907957678721)
182 
186  sqrt_pi_2by3, 1.447202509116535318726029254581591535744)
187 
191  sqrt_pi_3by4, 1.534990061919732732719327437333897291061)
192 
196  sqrt_pi_4by3, 2.046653415892976976959103249778529721415)
197 
200 MCKL_DEFINE_MATH_CONSTANTS(ln_pi, 1.144729885849400174143427351353058711647)
201 
204 MCKL_DEFINE_MATH_CONSTANTS(ln_pi_2, 1.837877066409345483560659472811235279723)
205 
209  ln_pi_by2, 0.4515827052894548647261952298948821435718)
210 
214  ln_pi_by3, 0.04611759718129048274818211443053300699980)
215 
219  ln_pi_by4, -0.2415644752704904446910368915632944245037)
220 
224  ln_pi_by6, -0.6470295833786548266690500070276435610757)
225 
229  ln_pi_2by3, 0.7392647777412357921654142358887095750753)
230 
234  ln_pi_3by4, 0.8570478133976192467042083453592312801438)
235 
239  ln_pi_4by3, 1.432411958301181101582646357346886143151)
240 
243 MCKL_DEFINE_MATH_CONSTANTS(e, 2.718281828459045235360287471352662497757)
244 
247 MCKL_DEFINE_MATH_CONSTANTS(e_inv, 0.3678794411714423215955237701614608674458)
248 
251 MCKL_DEFINE_MATH_CONSTANTS(sqrt_e, 1.648721270700128146848650787814163571654)
252 
256  sqrt_e_inv, 0.6065306597126334236037995349911804534419)
257 
260 MCKL_DEFINE_MATH_CONSTANTS(sqrt_2, 1.414213562373095048801688724209698078570)
261 
264 MCKL_DEFINE_MATH_CONSTANTS(sqrt_3, 1.732050807568877293527446341505872366943)
265 
268 MCKL_DEFINE_MATH_CONSTANTS(sqrt_5, 2.236067977499789696409173668731276235441)
269 
272 MCKL_DEFINE_MATH_CONSTANTS(sqrt_10, 3.162277660168379331998893544432718533720)
273 
277  sqrt_1by2, 0.7071067811865475244008443621048490392848)
278 
282  sqrt_1by3, 0.5773502691896257645091487805019574556476)
283 
287  sqrt_1by5, 0.4472135954999579392818347337462552470881)
288 
292  sqrt_1by10, 0.3162277660168379331998893544432718533720)
293 
296 MCKL_DEFINE_MATH_CONSTANTS(ln_2, 0.6931471805599453094172321214581765680755)
297 
300 MCKL_DEFINE_MATH_CONSTANTS(ln_3, 1.098612288668109691395245236922525704647)
301 
304 MCKL_DEFINE_MATH_CONSTANTS(ln_5, 1.609437912434100374600759333226187639526)
305 
308 MCKL_DEFINE_MATH_CONSTANTS(ln_10, 2.302585092994045684017991454684364207601)
309 
312 MCKL_DEFINE_MATH_CONSTANTS(ln_inv_2, 1.442695040888963407359924681001892137427)
313 
317  ln_inv_3, 0.9102392266268373936142401657361070006126)
318 
322  ln_inv_5, 0.6213349345596118107071993881805725841234)
323 
327  ln_inv_10, 0.4342944819032518276511289189166050822944)
328 
332  ln_ln_2, -0.3665129205816643270124391582326694694543)
333 
334 } // namespace mckl
335 
336 #endif // MCKL_MATH_CONSTANTS_HPP
constexpr T const_one()
One.
Definition: constants.hpp:100
constexpr T const_inf()
Infinity.
Definition: constants.hpp:70
#define MCKL_DEFINE_MATH_CONSTANTS(name, val)
Definition: constants.hpp:39
constexpr T const_nan()
Non-signalling NaN.
Definition: constants.hpp:81
constexpr T const_zero()
Zero.
Definition: constants.hpp:92
Definition: mcmc.hpp:40