]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright Paul Bristow 2007. |
2 | // Copyright John Maddock 2006. | |
3 | ||
4 | // Use, modification and distribution are subject to the | |
5 | // Boost Software License, Version 1.0. | |
6 | // (See accompanying file LICENSE_1_0.txt | |
7 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
8 | ||
9 | // test_uniform.cpp | |
10 | ||
11 | #include <pch.hpp> | |
12 | ||
13 | #ifdef _MSC_VER | |
14 | # pragma warning(disable: 4127) // conditional expression is constant. | |
15 | # pragma warning(disable: 4100) // unreferenced formal parameter. | |
16 | #endif | |
17 | ||
18 | #include <boost/math/concepts/real_concept.hpp> // for real_concept | |
19 | #define BOOST_TEST_MAIN | |
20 | #include <boost/test/unit_test.hpp> // Boost.Test | |
92f5a8d4 | 21 | #include <boost/test/tools/floating_point_comparison.hpp> |
7c673cae FG |
22 | |
23 | #include <boost/math/distributions/uniform.hpp> | |
24 | using boost::math::uniform_distribution; | |
25 | #include <boost/math/tools/test.hpp> | |
26 | #include "test_out_of_range.hpp" | |
27 | ||
28 | #include <iostream> | |
29 | #include <iomanip> | |
30 | using std::cout; | |
31 | using std::endl; | |
32 | using std::setprecision; | |
33 | #include <limits> | |
34 | using std::numeric_limits; | |
35 | ||
36 | template <class RealType> | |
37 | void check_uniform(RealType lower, RealType upper, RealType x, RealType p, RealType q, RealType tol) | |
38 | { | |
39 | BOOST_CHECK_CLOSE_FRACTION( | |
40 | ::boost::math::cdf( | |
41 | uniform_distribution<RealType>(lower, upper), // distribution. | |
42 | x), // random variable. | |
43 | p, // probability. | |
44 | tol); // tolerance. | |
45 | BOOST_CHECK_CLOSE_FRACTION( | |
46 | ::boost::math::cdf( | |
47 | complement( | |
48 | uniform_distribution<RealType>(lower, upper), // distribution. | |
49 | x)), // random variable. | |
50 | q, // probability complement. | |
51 | tol); // tolerance. | |
52 | BOOST_CHECK_CLOSE_FRACTION( | |
53 | ::boost::math::quantile( | |
54 | uniform_distribution<RealType>(lower, upper), // distribution. | |
55 | p), // probability. | |
56 | x, // random variable. | |
57 | tol); // tolerance. | |
58 | BOOST_CHECK_CLOSE_FRACTION( | |
59 | ::boost::math::quantile( | |
60 | complement( | |
61 | uniform_distribution<RealType>(lower, upper), // distribution. | |
62 | q)), // probability complement. | |
63 | x, // random variable. | |
64 | tol); // tolerance. | |
65 | } // void check_uniform | |
66 | ||
67 | template <class RealType> | |
68 | void test_spots(RealType) | |
69 | { | |
70 | // Basic sanity checks | |
71 | // | |
72 | // These test values were generated for the normal distribution | |
73 | // using the online calculator at | |
74 | // http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm | |
75 | // | |
76 | // Tolerance is just over 5 decimal digits expressed as a fraction: | |
77 | // that's the limit of the test data. | |
78 | RealType tolerance = 2e-5f; | |
79 | cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << "." << endl; | |
80 | ||
81 | using std::exp; | |
82 | ||
83 | // Tests for PDF | |
84 | // | |
85 | BOOST_CHECK_CLOSE_FRACTION( // x == upper | |
86 | pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0)), | |
87 | static_cast<RealType>(1), | |
88 | tolerance); | |
89 | BOOST_CHECK_CLOSE_FRACTION( // x == lower | |
90 | pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(1)), | |
91 | static_cast<RealType>(1), | |
92 | tolerance); | |
93 | BOOST_CHECK_CLOSE_FRACTION( // x > upper | |
94 | pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(-1)), | |
95 | static_cast<RealType>(0), | |
96 | tolerance); | |
97 | BOOST_CHECK_CLOSE_FRACTION( // x < lower | |
98 | pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(2)), | |
99 | static_cast<RealType>(0), | |
100 | tolerance); | |
101 | ||
102 | if(std::numeric_limits<RealType>::has_infinity) | |
103 | { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity() | |
104 | // Note that infinity is not implemented for real_concept, so these tests | |
105 | // are only done for types, like built-in float, double.. that have infinity. | |
106 | // Note that these assume that BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error. | |
107 | // #define BOOST_MATH_OVERFLOW_ERROR_POLICY == throw_on_error would give a throw here. | |
108 | // #define BOOST_MATH_DOMAIN_ERROR_POLICY == throw_on_error IS defined, so the throw path | |
109 | // of error handling is tested below with BOOST_MATH_CHECK_THROW tests. | |
110 | ||
111 | BOOST_MATH_CHECK_THROW( // x == infinity should NOT be OK. | |
112 | pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(std::numeric_limits<RealType>::infinity())), | |
113 | std::domain_error); | |
114 | ||
115 | BOOST_MATH_CHECK_THROW( // x == minus infinity should be OK too. | |
116 | pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::infinity())), | |
117 | std::domain_error); | |
118 | } | |
119 | if(std::numeric_limits<RealType>::has_quiet_NaN) | |
120 | { // BOOST_CHECK tests for NaN using std::numeric_limits<>::has_quiet_NaN() - should throw. | |
121 | BOOST_MATH_CHECK_THROW( | |
122 | pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN())), | |
123 | std::domain_error); | |
124 | BOOST_MATH_CHECK_THROW( | |
125 | pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::quiet_NaN())), | |
126 | std::domain_error); | |
127 | } // test for x = NaN using std::numeric_limits<>::quiet_NaN() | |
128 | ||
129 | // cdf | |
130 | BOOST_CHECK_EQUAL( // x < lower | |
131 | cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(-1)), | |
132 | static_cast<RealType>(0) ); | |
133 | BOOST_CHECK_CLOSE_FRACTION( | |
134 | cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0)), | |
135 | static_cast<RealType>(0), | |
136 | tolerance); | |
137 | BOOST_CHECK_CLOSE_FRACTION( | |
138 | cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.5)), | |
139 | static_cast<RealType>(0.5), | |
140 | tolerance); | |
141 | BOOST_CHECK_CLOSE_FRACTION( | |
142 | cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.1)), | |
143 | static_cast<RealType>(0.1), | |
144 | tolerance); | |
145 | BOOST_CHECK_CLOSE_FRACTION( | |
146 | cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.9)), | |
147 | static_cast<RealType>(0.9), | |
148 | tolerance); | |
149 | BOOST_CHECK_EQUAL( // x > upper | |
150 | cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(2)), | |
151 | static_cast<RealType>(1)); | |
152 | ||
153 | // cdf complement | |
154 | BOOST_CHECK_EQUAL( // x < lower | |
155 | cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0))), | |
156 | static_cast<RealType>(1)); | |
157 | BOOST_CHECK_EQUAL( // x == 0 | |
158 | cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0))), | |
159 | static_cast<RealType>(1)); | |
160 | BOOST_CHECK_CLOSE_FRACTION( // x = 0.1 | |
161 | cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.1))), | |
162 | static_cast<RealType>(0.9), | |
163 | tolerance); | |
164 | BOOST_CHECK_CLOSE_FRACTION( // x = 0.5 | |
165 | cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.5))), | |
166 | static_cast<RealType>(0.5), | |
167 | tolerance); | |
168 | BOOST_CHECK_EQUAL( // x == 1 | |
169 | cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(1))), | |
170 | static_cast<RealType>(0)); | |
171 | BOOST_CHECK_EQUAL( // x > upper | |
172 | cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(2))), | |
173 | static_cast<RealType>(0)); | |
174 | ||
175 | // quantile | |
176 | ||
177 | BOOST_CHECK_CLOSE_FRACTION( | |
178 | quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.9)), | |
179 | static_cast<RealType>(0.9), | |
180 | tolerance); | |
181 | BOOST_CHECK_CLOSE_FRACTION( | |
182 | quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.1)), | |
183 | static_cast<RealType>(0.1), | |
184 | tolerance); | |
185 | BOOST_CHECK_CLOSE_FRACTION( | |
186 | quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.5)), | |
187 | static_cast<RealType>(0.5), | |
188 | tolerance); | |
189 | BOOST_CHECK_CLOSE_FRACTION( | |
190 | quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0)), | |
191 | static_cast<RealType>(0), | |
192 | tolerance); | |
193 | BOOST_CHECK_CLOSE_FRACTION( | |
194 | quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(1)), | |
195 | static_cast<RealType>(1), | |
196 | tolerance); | |
197 | ||
198 | // quantile complement | |
199 | ||
200 | BOOST_CHECK_CLOSE_FRACTION( | |
201 | quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.1))), | |
202 | static_cast<RealType>(0.9), | |
203 | tolerance); | |
204 | BOOST_CHECK_CLOSE_FRACTION( | |
205 | quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.9))), | |
206 | static_cast<RealType>(0.1), | |
207 | tolerance); | |
208 | BOOST_CHECK_CLOSE_FRACTION( | |
209 | quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.5))), | |
210 | static_cast<RealType>(0.5), | |
211 | tolerance); | |
212 | BOOST_CHECK_CLOSE_FRACTION( | |
213 | quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0))), | |
214 | static_cast<RealType>(1), | |
215 | tolerance); | |
216 | BOOST_CHECK_CLOSE_FRACTION( | |
217 | quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(1))), | |
218 | static_cast<RealType>(0), | |
219 | tolerance); | |
220 | ||
221 | // Some tests using a different location & scale, neight zero or unity. | |
222 | BOOST_CHECK_CLOSE_FRACTION( // x == mid | |
223 | pdf(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(1)), | |
224 | static_cast<RealType>(0.3333333333333333333333333333333333333333333333333333), | |
225 | tolerance); | |
226 | ||
227 | BOOST_CHECK_CLOSE_FRACTION( // x == upper | |
228 | pdf(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(+2)), | |
229 | static_cast<RealType>(0.3333333333333333333333333333333333333333333333333333), // 1 / (2 - -1) = 1/3 | |
230 | tolerance); | |
231 | ||
232 | BOOST_CHECK_CLOSE_FRACTION( // x == lower | |
233 | cdf(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(-1)), | |
234 | static_cast<RealType>(0), | |
235 | tolerance); | |
236 | BOOST_CHECK_CLOSE_FRACTION( // x == upper | |
237 | cdf(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(0)), | |
238 | static_cast<RealType>(0.3333333333333333333333333333333333333333333333333333), | |
239 | tolerance); | |
240 | ||
241 | BOOST_CHECK_CLOSE_FRACTION( // x == upper | |
242 | cdf(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(1)), | |
243 | static_cast<RealType>(0.6666666666666666666666666666666666666666666666666667), | |
244 | tolerance); | |
245 | ||
246 | BOOST_CHECK_CLOSE_FRACTION( // x == lower | |
247 | cdf(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(2)), | |
248 | static_cast<RealType>(1), | |
249 | tolerance); | |
250 | ||
251 | BOOST_CHECK_CLOSE_FRACTION( // x == upper | |
252 | quantile(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(0.6666666666666666666666666666666666666666666666666667)), | |
253 | static_cast<RealType>(1), | |
254 | tolerance); | |
255 | ||
256 | check_uniform( | |
257 | static_cast<RealType>(0), // lower | |
258 | static_cast<RealType>(1), // upper | |
259 | static_cast<RealType>(0.5), // x | |
260 | static_cast<RealType>(0.5), // p | |
261 | static_cast<RealType>(1 - 0.5), // q | |
262 | tolerance); | |
263 | ||
264 | // Some Not-standard uniform tests. | |
265 | check_uniform( | |
266 | static_cast<RealType>(-1), // lower | |
267 | static_cast<RealType>(1), // upper | |
268 | static_cast<RealType>(0), // x | |
269 | static_cast<RealType>(0.5), // p | |
270 | static_cast<RealType>(1 - 0.5), // q = 1 - p | |
271 | tolerance); | |
272 | ||
273 | check_uniform( | |
274 | static_cast<RealType>(1), // lower | |
275 | static_cast<RealType>(3), // upper | |
276 | static_cast<RealType>(2), // x | |
277 | static_cast<RealType>(0.5), // p | |
278 | static_cast<RealType>(1 - 0.5), // q = 1 - p | |
279 | tolerance); | |
280 | ||
281 | check_uniform( | |
282 | static_cast<RealType>(-1), // lower | |
283 | static_cast<RealType>(2), // upper | |
284 | static_cast<RealType>(1), // x | |
285 | static_cast<RealType>(0.66666666666666666666666666666666666666666667), // p | |
286 | static_cast<RealType>(0.33333333333333333333333333333333333333333333), // q = 1 - p | |
287 | tolerance); | |
288 | tolerance = (std::max)( | |
289 | boost::math::tools::epsilon<RealType>(), | |
290 | static_cast<RealType>(boost::math::tools::epsilon<double>())) * 5; // 5 eps as a fraction. | |
291 | cout << "Tolerance (as fraction) for type " << typeid(RealType).name() << " is " << tolerance << "." << endl; | |
292 | uniform_distribution<RealType> distu01(0, 1); | |
293 | RealType x = static_cast<RealType>(0.5); | |
294 | using namespace std; // ADL of std names. | |
295 | // mean: | |
296 | BOOST_CHECK_CLOSE_FRACTION( | |
297 | mean(distu01), static_cast<RealType>(0.5), tolerance); | |
298 | // variance: | |
299 | BOOST_CHECK_CLOSE_FRACTION( | |
300 | variance(distu01), static_cast<RealType>(0.0833333333333333333333333333333333333333333), tolerance); | |
301 | // std deviation: | |
302 | BOOST_CHECK_CLOSE_FRACTION( | |
303 | standard_deviation(distu01), sqrt(variance(distu01)), tolerance); | |
304 | // hazard: | |
305 | BOOST_CHECK_CLOSE_FRACTION( | |
306 | hazard(distu01, x), pdf(distu01, x) / cdf(complement(distu01, x)), tolerance); | |
307 | // cumulative hazard: | |
308 | BOOST_CHECK_CLOSE_FRACTION( | |
309 | chf(distu01, x), -log(cdf(complement(distu01, x))), tolerance); | |
310 | // coefficient_of_variation: | |
311 | BOOST_CHECK_CLOSE_FRACTION( | |
312 | coefficient_of_variation(distu01), standard_deviation(distu01) / mean(distu01), tolerance); | |
313 | // mode: | |
314 | BOOST_CHECK_CLOSE_FRACTION( | |
315 | mode(distu01), static_cast<RealType>(0), tolerance); | |
316 | BOOST_CHECK_CLOSE_FRACTION( | |
317 | median(distu01), static_cast<RealType>(0.5), tolerance); | |
318 | // skewness: | |
319 | BOOST_CHECK_EQUAL( | |
320 | skewness(distu01), static_cast<RealType>(0)); | |
f67539c2 | 321 | // kurtosis: |
7c673cae FG |
322 | BOOST_CHECK_CLOSE_FRACTION( |
323 | kurtosis(distu01), kurtosis_excess(distu01) + static_cast<RealType>(3), tolerance); | |
f67539c2 | 324 | // kurtosis excess: |
7c673cae FG |
325 | BOOST_CHECK_CLOSE_FRACTION( |
326 | kurtosis_excess(distu01), static_cast<RealType>(-1.2), tolerance); | |
327 | ||
f67539c2 TL |
328 | BOOST_CHECK_SMALL( |
329 | entropy(distu01), tolerance); | |
330 | ||
7c673cae FG |
331 | if(std::numeric_limits<RealType>::has_infinity) |
332 | { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity() | |
333 | // Note that infinity is not implemented for real_concept, so these tests | |
334 | // are only done for types, like built-in float, double, long double, that have infinity. | |
335 | // Note that these assume that BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error. | |
336 | // #define BOOST_MATH_OVERFLOW_ERROR_POLICY == throw_on_error would give a throw here. | |
337 | // #define BOOST_MATH_DOMAIN_ERROR_POLICY == throw_on_error IS defined, so the throw path | |
338 | // of error handling is tested below with BOOST_MATH_CHECK_THROW tests. | |
339 | ||
340 | BOOST_MATH_CHECK_THROW(pdf(distu01, std::numeric_limits<RealType>::infinity()), std::domain_error); | |
341 | BOOST_MATH_CHECK_THROW(pdf(distu01, -std::numeric_limits<RealType>::infinity()), std::domain_error); | |
342 | } // test for infinity using std::numeric_limits<>::infinity() | |
343 | else | |
344 | { // real_concept case, does has_infinfity == false, so can't check it throws. | |
345 | // cout << std::numeric_limits<RealType>::infinity() << ' ' | |
346 | // << (boost::math::fpclassify)(std::numeric_limits<RealType>::infinity()) << endl; | |
347 | // value of std::numeric_limits<RealType>::infinity() is zero, so FPclassify is zero, | |
348 | // so (boost::math::isfinite)(std::numeric_limits<RealType>::infinity()) does not detect infinity. | |
349 | // so these tests would never throw. | |
350 | //BOOST_MATH_CHECK_THROW(pdf(distu01, std::numeric_limits<RealType>::infinity()), std::domain_error); | |
351 | //BOOST_MATH_CHECK_THROW(pdf(distu01, std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); | |
352 | // BOOST_MATH_CHECK_THROW(pdf(distu01, boost::math::tools::max_value<RealType>() * 2), std::domain_error); // Doesn't throw. | |
353 | BOOST_CHECK_EQUAL(pdf(distu01, boost::math::tools::max_value<RealType>()), 0); | |
354 | } | |
355 | // Special cases: | |
356 | BOOST_CHECK(pdf(distu01, 0) == 1); | |
357 | BOOST_CHECK(cdf(distu01, 0) == 0); | |
358 | BOOST_CHECK(pdf(distu01, 1) == 1); | |
359 | BOOST_CHECK(cdf(distu01, 1) == 1); | |
360 | BOOST_CHECK(cdf(complement(distu01, 0)) == 1); | |
361 | BOOST_CHECK(cdf(complement(distu01, 1)) == 0); | |
362 | BOOST_CHECK(quantile(distu01, 0) == 0); | |
363 | BOOST_CHECK(quantile(complement(distu01, 0)) == 1); | |
364 | BOOST_CHECK(quantile(distu01, 1) == 1); | |
365 | BOOST_CHECK(quantile(complement(distu01, 1)) == 0); | |
366 | ||
367 | // Error checks: | |
368 | if(std::numeric_limits<RealType>::has_quiet_NaN) | |
369 | { // BOOST_CHECK tests for constructing with quiet_NaN (not for real_concept, for example - see notes above). | |
370 | BOOST_MATH_CHECK_THROW(uniform_distribution<RealType>(0, std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); | |
371 | BOOST_MATH_CHECK_THROW(uniform_distribution<RealType>(0, -std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); | |
372 | } | |
373 | BOOST_MATH_CHECK_THROW(uniform_distribution<RealType>(1, 0), std::domain_error); // lower > upper! | |
374 | BOOST_MATH_CHECK_THROW(uniform_distribution<RealType>(1, 1), std::domain_error); // lower == upper! | |
375 | ||
376 | check_out_of_range<uniform_distribution<RealType> >(1, 5); | |
377 | } // template <class RealType>void test_spots(RealType) | |
378 | ||
379 | BOOST_AUTO_TEST_CASE( test_main ) | |
380 | { | |
381 | // Check that can construct uniform distribution using the two convenience methods: | |
382 | using namespace boost::math; | |
383 | uniform unistd; // Using typedef | |
384 | // == uniform_distribution<double> unistd; | |
385 | BOOST_CHECK_EQUAL(unistd.lower(), 0); // Check defaults. | |
386 | BOOST_CHECK_EQUAL(unistd.upper(), 1); | |
387 | uniform_distribution<> myu01(0, 1); // Using default RealType double. | |
388 | BOOST_CHECK_EQUAL(myu01.lower(), 0); // Check defaults again. | |
389 | BOOST_CHECK_EQUAL(myu01.upper(), 1); | |
390 | ||
391 | // Test on extreme values of random variate x, using just double because it has numeric_limit infinity etc.. | |
392 | // No longer allow x to be + or - infinity, then these tests should throw. | |
393 | BOOST_MATH_CHECK_THROW(pdf(unistd, +std::numeric_limits<double>::infinity()), std::domain_error); // x = + infinity | |
394 | BOOST_MATH_CHECK_THROW(pdf(unistd, -std::numeric_limits<double>::infinity()), std::domain_error); // x = - infinity | |
395 | BOOST_MATH_CHECK_THROW(cdf(unistd, +std::numeric_limits<double>::infinity()), std::domain_error); // x = + infinity | |
396 | BOOST_MATH_CHECK_THROW(cdf(unistd, -std::numeric_limits<double>::infinity()), std::domain_error); // x = - infinity | |
397 | ||
398 | BOOST_CHECK_EQUAL(pdf(unistd, +(std::numeric_limits<double>::max)()), 0); // x = + max | |
399 | BOOST_CHECK_EQUAL(pdf(unistd, -(std::numeric_limits<double>::min)()), 0); // x = - min | |
400 | BOOST_CHECK_EQUAL(cdf(unistd, +(std::numeric_limits<double>::max)()), 1); // x = + max | |
401 | BOOST_CHECK_EQUAL(cdf(unistd, -(std::numeric_limits<double>::min)()), 0); // x = - min | |
402 | #ifndef BOOST_NO_EXCEPTIONS | |
403 | BOOST_MATH_CHECK_THROW(uniform_distribution<> zinf(0, +std::numeric_limits<double>::infinity()), std::domain_error); // zero to infinity using default RealType double. | |
404 | #else | |
405 | BOOST_MATH_CHECK_THROW(uniform_distribution<>(0, +std::numeric_limits<double>::infinity()), std::domain_error); // zero to infinity using default RealType double. | |
406 | #endif | |
407 | uniform_distribution<> zmax(0, +(std::numeric_limits<double>::max)()); // zero to max using default RealType double. | |
408 | BOOST_CHECK_EQUAL(zmax.lower(), 0); // Check defaults again. | |
409 | BOOST_CHECK_EQUAL(zmax.upper(), +(std::numeric_limits<double>::max)()); | |
410 | ||
411 | BOOST_CHECK_EQUAL(pdf(zmax, -1), 0); // pdf is 1/(0 - max) = almost zero for all x | |
412 | BOOST_CHECK_EQUAL(pdf(zmax, 0), (std::numeric_limits<double>::min)()/4); // x = | |
413 | BOOST_CHECK_EQUAL(pdf(zmax, 1), (std::numeric_limits<double>::min)()/4); // x = | |
414 | BOOST_MATH_CHECK_THROW(pdf(zmax, +std::numeric_limits<double>::infinity()), std::domain_error); // pdf is 1/(0 - infinity) = zero for all x | |
415 | BOOST_MATH_CHECK_THROW(pdf(zmax, -std::numeric_limits<double>::infinity()), std::domain_error); | |
416 | BOOST_CHECK_EQUAL(pdf(zmax, +(std::numeric_limits<double>::max)()), (std::numeric_limits<double>::min)()/4); // x = | |
417 | BOOST_CHECK_EQUAL(pdf(zmax, -(std::numeric_limits<double>::max)()), 0); // x = | |
418 | #ifndef BOOST_NO_EXCEPTIONS | |
419 | // Ensure NaN throws an exception. | |
420 | BOOST_MATH_CHECK_THROW(uniform_distribution<> zNaN(0, std::numeric_limits<double>::quiet_NaN()), std::domain_error); | |
421 | BOOST_MATH_CHECK_THROW(pdf(unistd, std::numeric_limits<double>::quiet_NaN()), std::domain_error); | |
422 | #else | |
423 | BOOST_MATH_CHECK_THROW(uniform_distribution<>(0, std::numeric_limits<double>::quiet_NaN()), std::domain_error); | |
424 | BOOST_MATH_CHECK_THROW(pdf(unistd, std::numeric_limits<double>::quiet_NaN()), std::domain_error); | |
425 | #endif | |
426 | // Basic sanity-check spot values. | |
427 | // (Parameter value, arbitrarily zero, only communicates the floating point type). | |
428 | test_spots(0.0F); // Test float. OK at decdigits = 0 tolerance = 0.0001 % | |
429 | test_spots(0.0); // Test double. OK at decdigits 7, tolerance = 1e07 % | |
430 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
431 | test_spots(0.0L); // Test long double. | |
1e59de90 | 432 | #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x0582)) && !defined(BOOST_MATH_NO_REAL_CONCEPT_TESTS) |
7c673cae FG |
433 | test_spots(boost::math::concepts::real_concept(0.)); // Test real concept. |
434 | #endif | |
435 | #else | |
436 | std::cout << "<note>The long double tests have been disabled on this platform " | |
437 | "either because the long double overloads of the usual math functions are " | |
438 | "not available at all, or because they are too inaccurate for these tests " | |
439 | "to pass.</note>" << std::endl; | |
440 | #endif | |
441 | ||
442 | ||
443 | } // BOOST_AUTO_TEST_CASE( test_main ) | |
444 | ||
445 | /* | |
446 | ||
447 | Output: | |
448 | ||
449 | Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_uniform.exe" | |
450 | Running 1 test case... | |
451 | Tolerance for type float is 2e-005. | |
452 | Tolerance (as fraction) for type float is 5.96046e-007. | |
453 | Tolerance for type double is 2e-005. | |
454 | Tolerance (as fraction) for type double is 1.11022e-015. | |
455 | Tolerance for type long double is 2e-005. | |
456 | Tolerance (as fraction) for type long double is 1.11022e-015. | |
457 | Tolerance for type class boost::math::concepts::real_concept is 2e-005. | |
458 | Tolerance (as fraction) for type class boost::math::concepts::real_concept is 1.11022e-015. | |
459 | *** No errors detected | |
460 | ||
461 | */ | |
462 | ||
463 | ||
464 |