1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
// (C) Copyright John Maddock 2005.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <pch_light.hpp>
#define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/math/special_functions/math_fwd.hpp>
#include <boost/math/special_functions/hypot.hpp>
#include <cmath>
#ifdef BOOST_NO_STDC_NAMESPACE
namespace std{ using ::sqrt; }
#endif
//
// test_boundaries:
// This is an accuracy test, sets the two arguments to hypot to just
// above or just below various boundary conditions, and checks the accuracy
// of the result. The values computed at double precision will use a
// different computation method to those computed at float precision:
// as long as these compute the same values then everything's OK.
//
// Tolerance is 2*epsilon, expressed here as a percentage:
//
static const float tolerance = 200 * (std::numeric_limits<float>::epsilon)();
const float boundaries[] = {
0,
1,
2,
(std::numeric_limits<float>::max)()/2,
(std::numeric_limits<float>::min)(),
std::numeric_limits<float>::epsilon(),
std::sqrt((std::numeric_limits<float>::max)()) / 2,
std::sqrt((std::numeric_limits<float>::min)()),
std::sqrt((std::numeric_limits<float>::max)()) / 4,
std::sqrt((std::numeric_limits<float>::min)()) * 2,
};
void do_test_boundaries(float x, float y, float z)
{
float expected = static_cast<float>((boost::math::hypot)(
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
static_cast<long double>(x),
static_cast<long double>(y),
static_cast<long double>(z)));
#else
static_cast<double>(x),
static_cast<double>(y),
static_cast<double>(z));
#endif
float found = (boost::math::hypot)(x, y, z);
BOOST_CHECK_CLOSE(expected, found, tolerance);
}
void do_test_boundaries(float x, float y)
{
float expected = static_cast<float>((boost::math::hypot)(
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
static_cast<long double>(x),
static_cast<long double>(y)));
#else
static_cast<double>(x),
static_cast<double>(y)));
#endif
float found = (boost::math::hypot)(x, y);
BOOST_CHECK_CLOSE(expected, found, tolerance);
}
void test_boundaries(float x, float y, float z)
{
do_test_boundaries(x, y, z);
do_test_boundaries(-x, y, z);
do_test_boundaries(x, -y, z);
do_test_boundaries(x, y, -z);
do_test_boundaries(-x, -y, z);
do_test_boundaries(-x, y, -z);
do_test_boundaries(x, -y, -z);
do_test_boundaries(-x, -y, -z);
}
void test_boundaries(float x, float y)
{
do_test_boundaries(x, y);
do_test_boundaries(-x, y);
do_test_boundaries(-x, -y);
do_test_boundaries(x, -y);
for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i)
{
test_boundaries(x, y, boundaries[i]);
test_boundaries(x, y, boundaries[i] + std::numeric_limits<float>::epsilon()*boundaries[i]);
test_boundaries(x, y, boundaries[i] - std::numeric_limits<float>::epsilon()*boundaries[i]);
}
}
void test_boundaries(float x)
{
for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i)
{
test_boundaries(x, boundaries[i]);
test_boundaries(x, boundaries[i] + std::numeric_limits<float>::epsilon()*boundaries[i]);
test_boundaries(x, boundaries[i] - std::numeric_limits<float>::epsilon()*boundaries[i]);
}
}
void test_boundaries()
{
for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i)
{
test_boundaries(boundaries[i]);
test_boundaries(boundaries[i] + std::numeric_limits<float>::epsilon()*boundaries[i]);
test_boundaries(boundaries[i] - std::numeric_limits<float>::epsilon()*boundaries[i]);
}
}
void test_spots()
{
static const float zero = 0;
for(unsigned i = 0; i < sizeof(boundaries)/sizeof(float); ++i)
{
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], zero), std::fabs(boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], zero), std::fabs(-boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], -zero), std::fabs(boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -zero), std::fabs(-boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], zero, zero), std::fabs(boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], zero, zero), std::fabs(-boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], -zero, zero), std::fabs(boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -zero, zero), std::fabs(-boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(zero, boundaries[i], zero), std::fabs(boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(zero, -boundaries[i], zero), std::fabs(-boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(zero, boundaries[i], -zero), std::fabs(boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(zero, -boundaries[i], -zero), std::fabs(-boundaries[i]));
for(unsigned j = 0; j < sizeof(boundaries)/sizeof(float); ++j)
{
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], boundaries[j]), boost::math::hypot(boundaries[j], boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], boundaries[j]), boost::math::hypot(boundaries[i], -boundaries[j]));
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -boundaries[j]), boost::math::hypot(-boundaries[j], -boundaries[i]));
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -boundaries[j]), boost::math::hypot(-boundaries[i], boundaries[j]));
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], boundaries[j], boundaries[j]), boost::math::hypot(boundaries[j], boundaries[i], boundaries[j]));
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[i], boundaries[j], boundaries[j]), boost::math::hypot(boundaries[i], boundaries[j], -boundaries[j]));
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -boundaries[j], boundaries[j]), boost::math::hypot(-boundaries[j], -boundaries[i], boundaries[j]));
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[i], -boundaries[j], boundaries[j]), boost::math::hypot(-boundaries[i], boundaries[j], boundaries[j]));
}
}
if((std::numeric_limits<float>::has_infinity) && (std::numeric_limits<float>::has_quiet_NaN))
{
static const float nan = std::numeric_limits<float>::quiet_NaN();
static const float inf = std::numeric_limits<float>::infinity();
BOOST_CHECK_EQUAL(boost::math::hypot(inf, nan), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-inf, nan), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(nan, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(nan, -inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(inf, nan, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-inf, nan, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(nan, inf, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(nan, -inf, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(nan, nan, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-inf, nan, nan), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(nan, inf, nan), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(nan, -inf, nan), inf);
for(unsigned j = 0; j < sizeof(boundaries)/sizeof(float); ++j)
{
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[j], inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[j], inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(inf, boundaries[j]), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(inf, -boundaries[j]), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[j], -inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[j], -inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-inf, boundaries[j]), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-inf, -boundaries[j]), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[j], inf, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[j], inf, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(inf, boundaries[j], inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(inf, -boundaries[j], inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(boundaries[j], -inf, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-boundaries[j], -inf, inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-inf, boundaries[j], inf), inf);
BOOST_CHECK_EQUAL(boost::math::hypot(-inf, -boundaries[j], inf), inf);
}
}
}
BOOST_AUTO_TEST_CASE( test_main )
{
BOOST_MATH_CONTROL_FP;
test_boundaries();
test_spots();
}
|