1 #ifndef STAN_MATH_PRIM_FUN_CONSTANTS_HPP
2 #define STAN_MATH_PRIM_FUN_CONSTANTS_HPP
3
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/inv.hpp>
6 #include <boost/math/constants/constants.hpp>
7 #include <cmath>
8 #include <limits>
9
10 namespace stan {
11 namespace math {
12
13 // TODO(anyone) Use constexpr when moving to C++17
14
15 /**
16 * Return the base of the natural logarithm.
17 *
18 * @return Base of natural logarithm.
19 */
e()20 inline double e() { return boost::math::constants::e<double>(); }
21
22 /**
23 * Return the value of pi.
24 *
25 * @return Pi.
26 */
pi()27 inline double pi() { return boost::math::constants::pi<double>(); }
28
29 /**
30 * Smallest positive value.
31 */
32 constexpr double EPSILON = std::numeric_limits<double>::epsilon();
33
34 /**
35 * Positive infinity.
36 */
37 constexpr double INFTY = std::numeric_limits<double>::infinity();
38
39 /**
40 * Negative infinity.
41 */
42 constexpr double NEGATIVE_INFTY = -INFTY;
43
44 /**
45 * (Quiet) not-a-number value.
46 */
47 constexpr double NOT_A_NUMBER = std::numeric_limits<double>::quiet_NaN();
48
49 /**
50 * Twice the value of \f$ \pi \f$,
51 * \f$ 2\pi \f$.
52 */
53 const double TWO_PI = 2.0 * pi();
54
55 /**
56 * The natural logarithm of 0,
57 * \f$ \log 0 \f$.
58 */
59 const double LOG_ZERO = std::log(0.0);
60
61 /**
62 * The natural logarithm of machine precision \f$ \epsilon \f$,
63 * \f$ \log \epsilon \f$.
64 */
65 const double LOG_EPSILON = std::log(EPSILON);
66
67 /**
68 * The natural logarithm of \f$ \pi \f$,
69 * \f$ \log \pi \f$.
70 */
71 const double LOG_PI = std::log(pi());
72
73 /**
74 * The natural logarithm of 0.5,
75 * \f$ \log 0.5 \f$.
76 */
77 const double LOG_HALF = std::log(0.5);
78
79 /**
80 * The natural logarithm of 2,
81 * \f$ \log 2 \f$.
82 */
83 const double LOG_TWO = std::log(2.0);
84
85 /**
86 * The natural logarithm of 2 plus the natural logarithm of \f$ \pi \f$,
87 * \f$ \log(2\pi) \f$.
88 */
89 const double LOG_TWO_PI = LOG_TWO + LOG_PI;
90
91 /**
92 * The value of one quarter the natural logarithm of \f$ \pi \f$,
93 * \f$ \log(\pi) / 4 \f$.
94 */
95 const double LOG_PI_OVER_FOUR = 0.25 * LOG_PI;
96
97 /**
98 * The natural logarithm of the square root of \f$ \pi \f$,
99 * \f$ \log(sqrt{\pi}) \f$.
100 */
101 const double LOG_SQRT_PI = std::log(std::sqrt(pi()));
102
103 /**
104 * The natural logarithm of 10,
105 * \f$ \log 10 \f$.
106 */
107 const double LOG_TEN = std::log(10.0);
108
109 /**
110 * The value of the square root of 2,
111 * \f$ \sqrt{2} \f$.
112 */
113 const double SQRT_TWO = std::sqrt(2.0);
114
115 /**
116 * The value of the square root of \f$ \pi \f$,
117 * \f$ \sqrt{\pi} \f$.
118 */
119 const double SQRT_PI = std::sqrt(pi());
120
121 /**
122 * The value of the square root of \f$ 2\pi \f$,
123 * \f$ \sqrt{2\pi} \f$.
124 */
125 const double SQRT_TWO_PI = std::sqrt(TWO_PI);
126
127 /**
128 * The square root of 2 divided by the square root of \f$ \pi \f$,
129 * \f$ \sqrt{2} / \sqrt{\pi} \f$.
130 */
131 const double SQRT_TWO_OVER_SQRT_PI = SQRT_TWO / SQRT_PI;
132
133 /**
134 * The value of 1 over the square root of 2,
135 * \f$ 1 / \sqrt{2} \f$.
136 */
137 const double INV_SQRT_TWO = inv(SQRT_TWO);
138
139 /**
140 * The value of 1 over the square root of \f$ \pi \f$,
141 * \f$ 1 / \sqrt{\pi} \f$.
142 */
143 const double INV_SQRT_PI = inv(SQRT_PI);
144
145 /**
146 * The value of 1 over the square root of \f$ 2\pi \f$,
147 * \f$ 1 / \sqrt{2\pi} \f$.
148 */
149 const double INV_SQRT_TWO_PI = inv(SQRT_TWO_PI);
150
151 /**
152 * The value of 2 over the square root of \f$ \pi \f$,
153 * \f$ 2 / \sqrt{\pi} \f$.
154 */
155 const double TWO_OVER_SQRT_PI = 2.0 / SQRT_PI;
156
157 /**
158 * The value of half the natural logarithm 2,
159 * \f$ \log(2) / 2 \f$.
160 */
161 const double HALF_LOG_TWO = 0.5 * LOG_TWO;
162
163 /**
164 * The value of half the natural logarithm \f$ 2\pi \f$,
165 * \f$ \log(2\pi) / 2 \f$.
166 */
167 const double HALF_LOG_TWO_PI = 0.5 * LOG_TWO_PI;
168
169 /**
170 * The value of minus the natural logarithm of the square root of \f$ 2\pi \f$,
171 * \f$ -\log(\sqrt{2\pi}) \f$.
172 */
173 const double NEG_LOG_SQRT_TWO_PI = -std::log(SQRT_TWO_PI);
174
175 /**
176 * Largest rate parameter allowed in Poisson RNG
177 */
178 const double POISSON_MAX_RATE = std::pow(2.0, 30);
179
180 /**
181 * Return positive infinity.
182 *
183 * @return Positive infinity.
184 */
positive_infinity()185 static constexpr inline double positive_infinity() { return INFTY; }
186
187 /**
188 * Return negative infinity.
189 *
190 * @return Negative infinity.
191 */
negative_infinity()192 static constexpr inline double negative_infinity() { return NEGATIVE_INFTY; }
193
194 /**
195 * Return (quiet) not-a-number.
196 *
197 * @return Quiet not-a-number.
198 */
not_a_number()199 static constexpr inline double not_a_number() { return NOT_A_NUMBER; }
200
201 /**
202 * Returns the difference between 1.0 and the next value
203 * representable.
204 *
205 * @return Minimum positive number.
206 */
machine_precision()207 static constexpr inline double machine_precision() { return EPSILON; }
208
209 /**
210 * Returns the natural logarithm of ten.
211 *
212 * @return Natural logarithm of ten.
213 */
log10()214 inline double log10() { return LOG_TEN; }
215
216 /**
217 * Returns the square root of two.
218 *
219 * @return Square root of two.
220 */
sqrt2()221 inline double sqrt2() { return SQRT_TWO; }
222
223 } // namespace math
224 } // namespace stan
225
226 #endif
227