1 | // (C) Copyright John Maddock 2005. |
---|
2 | // Use, modification and distribution are subject to the |
---|
3 | // Boost Software License, Version 1.0. (See accompanying file |
---|
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | #include <boost/test/included/test_exec_monitor.hpp> |
---|
7 | #include <boost/test/floating_point_comparison.hpp> |
---|
8 | #include <boost/math/special_functions/hypot.hpp> |
---|
9 | |
---|
10 | #include <cmath> |
---|
11 | |
---|
12 | #ifdef BOOST_NO_STDC_NAMESPACE |
---|
13 | namespace std{ using ::sqrt; } |
---|
14 | #endif |
---|
15 | |
---|
16 | // |
---|
17 | // test_boundaries: |
---|
18 | // This is an accuracy test, sets the two arguments to hypot to just |
---|
19 | // above or just below various boundary conditions, and checks the accuracy |
---|
20 | // of the result. The values computed at double precision will use a |
---|
21 | // different computation method to those computed at float precision: |
---|
22 | // as long as these compute the same values then everything's OK. |
---|
23 | // |
---|
24 | // Tolerance is 2*epsilon, expressed here as a persentage: |
---|
25 | // |
---|
26 | static const float tolerance = 200 * (std::numeric_limits<float>::epsilon)(); |
---|
27 | const float boundaries[] = { |
---|
28 | 0, |
---|
29 | 1, |
---|
30 | 2, |
---|
31 | (std::numeric_limits<float>::max)()/2, |
---|
32 | (std::numeric_limits<float>::min)(), |
---|
33 | std::numeric_limits<float>::epsilon(), |
---|
34 | std::sqrt((std::numeric_limits<float>::max)()) / 2, |
---|
35 | std::sqrt((std::numeric_limits<float>::min)()), |
---|
36 | std::sqrt((std::numeric_limits<float>::max)()) / 4, |
---|
37 | std::sqrt((std::numeric_limits<float>::min)()) * 2, |
---|
38 | }; |
---|
39 | |
---|
40 | void do_test_boundaries(float x, float y) |
---|
41 | { |
---|
42 | float expected = static_cast<float>((boost::math::hypot)( |
---|
43 | static_cast<long double>(x), |
---|
44 | static_cast<long double>(y))); |
---|
45 | float found = (boost::math::hypot)(x, y); |
---|
46 | BOOST_CHECK_CLOSE(expected, found, tolerance); |
---|
47 | } |
---|
48 | |
---|
49 | void test_boundaries(float x, float y) |
---|
50 | { |
---|
51 | do_test_boundaries(x, y); |
---|
52 | do_test_boundaries(-x, y); |
---|
53 | do_test_boundaries(-x, -y); |
---|
54 | do_test_boundaries(x, -y); |
---|
55 | } |
---|
56 | |
---|
57 | void test_boundaries(float x) |
---|
58 | { |
---|
59 | for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i) |
---|
60 | { |
---|
61 | test_boundaries(x, boundaries[i]); |
---|
62 | test_boundaries(x, boundaries[i] + std::numeric_limits<float>::epsilon()*boundaries[i]); |
---|
63 | test_boundaries(x, boundaries[i] - std::numeric_limits<float>::epsilon()*boundaries[i]); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | void test_boundaries() |
---|
68 | { |
---|
69 | for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i) |
---|
70 | { |
---|
71 | test_boundaries(boundaries[i]); |
---|
72 | test_boundaries(boundaries[i] + std::numeric_limits<float>::epsilon()*boundaries[i]); |
---|
73 | test_boundaries(boundaries[i] - std::numeric_limits<float>::epsilon()*boundaries[i]); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void test_spots() |
---|
78 | { |
---|
79 | static const float zero = 0; |
---|
80 | for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i) |
---|
81 | { |
---|
82 | BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], zero), std::fabs(boundaries[i])); |
---|
83 | BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], zero), std::fabs(-boundaries[i])); |
---|
84 | BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], -zero), std::fabs(boundaries[i])); |
---|
85 | BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -zero), std::fabs(-boundaries[i])); |
---|
86 | for(unsigned j = 0; j < sizeof(boundaries)/sizeof(float); ++j) |
---|
87 | { |
---|
88 | BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], boundaries[j]), boost::math::hypot(boundaries[j], boundaries[i])); |
---|
89 | BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], boundaries[j]), boost::math::hypot(boundaries[i], -boundaries[j])); |
---|
90 | BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -boundaries[j]), boost::math::hypot(-boundaries[j], -boundaries[i])); |
---|
91 | BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -boundaries[j]), boost::math::hypot(-boundaries[i], boundaries[j])); |
---|
92 | } |
---|
93 | } |
---|
94 | if((std::numeric_limits<float>::has_infinity) && (std::numeric_limits<float>::has_quiet_NaN)) |
---|
95 | { |
---|
96 | static const float nan = std::numeric_limits<float>::quiet_NaN(); |
---|
97 | static const float inf = std::numeric_limits<float>::infinity(); |
---|
98 | BOOST_CHECK_EQUAL(boost::math::hypot(inf, nan), inf); |
---|
99 | BOOST_CHECK_EQUAL(boost::math::hypot(-inf, nan), inf); |
---|
100 | BOOST_CHECK_EQUAL(boost::math::hypot(nan, inf), inf); |
---|
101 | BOOST_CHECK_EQUAL(boost::math::hypot(nan, -inf), inf); |
---|
102 | for(unsigned j = 0; j < sizeof(boundaries)/sizeof(float); ++j) |
---|
103 | { |
---|
104 | BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[j], inf), inf); |
---|
105 | BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[j], inf), inf); |
---|
106 | BOOST_CHECK_EQUAL(boost::math::hypot(inf, boundaries[j]), inf); |
---|
107 | BOOST_CHECK_EQUAL(boost::math::hypot(inf, -boundaries[j]), inf); |
---|
108 | BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[j], -inf), inf); |
---|
109 | BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[j], -inf), inf); |
---|
110 | BOOST_CHECK_EQUAL(boost::math::hypot(-inf, boundaries[j]), inf); |
---|
111 | BOOST_CHECK_EQUAL(boost::math::hypot(-inf, -boundaries[j]), inf); |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | int test_main(int, char* []) |
---|
117 | { |
---|
118 | test_boundaries(); |
---|
119 | test_spots(); |
---|
120 | return 0; |
---|
121 | } |
---|