Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / math / test / hypot_test.cpp
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 <pch_light.hpp>
7
8 #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
9 #define BOOST_TEST_MAIN 
10 #include <boost/test/unit_test.hpp>
11 #include <boost/test/floating_point_comparison.hpp>
12 #include <boost/math/special_functions/math_fwd.hpp>
13
14 #include <cmath>
15
16 #ifdef BOOST_NO_STDC_NAMESPACE
17 namespace std{ using ::sqrt; }
18 #endif
19
20 //
21 // test_boundaries:
22 // This is an accuracy test, sets the two arguments to hypot to just
23 // above or just below various boundary conditions, and checks the accuracy
24 // of the result.  The values computed at double precision will use a 
25 // different computation method to those computed at float precision:
26 // as long as these compute the same values then everything's OK.
27 //
28 // Tolerance is 2*epsilon, expressed here as a persentage:
29 //
30 static const float tolerance = 200 * (std::numeric_limits<float>::epsilon)();
31 const float boundaries[] = {
32    0,
33    1,
34    2,
35    (std::numeric_limits<float>::max)()/2,
36    (std::numeric_limits<float>::min)(),
37    std::numeric_limits<float>::epsilon(),
38    std::sqrt((std::numeric_limits<float>::max)()) / 2,
39    std::sqrt((std::numeric_limits<float>::min)()),
40    std::sqrt((std::numeric_limits<float>::max)()) / 4,
41    std::sqrt((std::numeric_limits<float>::min)()) * 2,
42 };
43
44 void do_test_boundaries(float x, float y)
45 {
46    float expected = static_cast<float>((boost::math::hypot)(
47 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
48       static_cast<long double>(x), 
49       static_cast<long double>(y)));
50 #else
51       static_cast<double>(x), 
52       static_cast<double>(y)));
53 #endif
54    float found = (boost::math::hypot)(x, y);
55    BOOST_CHECK_CLOSE(expected, found, tolerance);
56 }
57
58 void test_boundaries(float x, float y)
59 {
60    do_test_boundaries(x, y);
61    do_test_boundaries(-x, y); 
62    do_test_boundaries(-x, -y);
63    do_test_boundaries(x, -y);
64 }
65
66 void test_boundaries(float x)
67 {
68    for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i)
69    {
70       test_boundaries(x, boundaries[i]);
71       test_boundaries(x, boundaries[i] + std::numeric_limits<float>::epsilon()*boundaries[i]);
72       test_boundaries(x, boundaries[i] - std::numeric_limits<float>::epsilon()*boundaries[i]);
73    }
74 }
75
76 void test_boundaries()
77 {
78    for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i)
79    {
80       test_boundaries(boundaries[i]);
81       test_boundaries(boundaries[i] + std::numeric_limits<float>::epsilon()*boundaries[i]);
82       test_boundaries(boundaries[i] - std::numeric_limits<float>::epsilon()*boundaries[i]);
83    }
84 }
85
86 void test_spots()
87 {
88    static const float zero = 0;
89    for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i)
90    {
91       BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], zero), std::fabs(boundaries[i]));
92       BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], zero), std::fabs(-boundaries[i]));
93       BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], -zero), std::fabs(boundaries[i]));
94       BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -zero), std::fabs(-boundaries[i]));
95       for(unsigned j = 0; j < sizeof(boundaries)/sizeof(float); ++j)
96       {
97          BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], boundaries[j]), boost::math::hypot(boundaries[j], boundaries[i]));
98          BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], boundaries[j]), boost::math::hypot(boundaries[i], -boundaries[j]));
99          BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -boundaries[j]), boost::math::hypot(-boundaries[j], -boundaries[i]));
100          BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -boundaries[j]), boost::math::hypot(-boundaries[i], boundaries[j]));
101       }
102    }
103    if((std::numeric_limits<float>::has_infinity) && (std::numeric_limits<float>::has_quiet_NaN))
104    {
105       static const float nan = std::numeric_limits<float>::quiet_NaN();
106       static const float inf = std::numeric_limits<float>::infinity();
107       BOOST_CHECK_EQUAL(boost::math::hypot(inf, nan), inf);
108       BOOST_CHECK_EQUAL(boost::math::hypot(-inf, nan), inf);
109       BOOST_CHECK_EQUAL(boost::math::hypot(nan, inf), inf);
110       BOOST_CHECK_EQUAL(boost::math::hypot(nan, -inf), inf);
111       for(unsigned j = 0; j < sizeof(boundaries)/sizeof(float); ++j)
112       {
113          BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[j], inf), inf);
114          BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[j], inf), inf);
115          BOOST_CHECK_EQUAL(boost::math::hypot(inf, boundaries[j]), inf);
116          BOOST_CHECK_EQUAL(boost::math::hypot(inf, -boundaries[j]), inf);
117          BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[j], -inf), inf);
118          BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[j], -inf), inf);
119          BOOST_CHECK_EQUAL(boost::math::hypot(-inf, boundaries[j]), inf);
120          BOOST_CHECK_EQUAL(boost::math::hypot(-inf, -boundaries[j]), inf);
121       }
122    }
123 }
124
125 BOOST_AUTO_TEST_CASE( test_main )
126 {
127    BOOST_MATH_CONTROL_FP;
128    test_boundaries();
129    test_spots();
130 }