]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/hypot_test.cpp
7a8c5ddb6ceb71edc411c3bf7b74e14262f2e3d9
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)
6 #include <pch_light.hpp>
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/tools/floating_point_comparison.hpp>
12 #include <boost/math/special_functions/math_fwd.hpp>
16 #ifdef BOOST_NO_STDC_NAMESPACE
17 namespace std
{ using ::sqrt
; }
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.
28 // Tolerance is 2*epsilon, expressed here as a persentage:
30 static const float tolerance
= 200 * (std::numeric_limits
<float>::epsilon
)();
31 const float boundaries
[] = {
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,
44 void do_test_boundaries(float x
, float y
)
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
)));
51 static_cast<double>(x
),
52 static_cast<double>(y
)));
54 float found
= (boost::math::hypot
)(x
, y
);
55 BOOST_CHECK_CLOSE(expected
, found
, tolerance
);
58 void test_boundaries(float x
, float y
)
60 do_test_boundaries(x
, y
);
61 do_test_boundaries(-x
, y
);
62 do_test_boundaries(-x
, -y
);
63 do_test_boundaries(x
, -y
);
66 void test_boundaries(float x
)
68 for(unsigned i
= 0; i
< sizeof(boundaries
)/sizeof(float); ++i
)
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
]);
76 void test_boundaries()
78 for(unsigned i
= 0; i
< sizeof(boundaries
)/sizeof(float); ++i
)
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
]);
88 static const float zero
= 0;
89 for(unsigned i
= 0; i
< sizeof(boundaries
)/sizeof(float); ++i
)
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
)
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
]));
103 if((std::numeric_limits
<float>::has_infinity
) && (std::numeric_limits
<float>::has_quiet_NaN
))
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
)
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
);
125 BOOST_AUTO_TEST_CASE( test_main
)
127 BOOST_MATH_CONTROL_FP
;