| 1 |
// Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++17 } } |
| 19 |
|
| 20 |
#include <cmath> |
| 21 |
#include <type_traits> |
| 22 |
#if defined(__TEST_DEBUG) |
| 23 |
#include <iostream> |
| 24 |
#define VERIFY(A) \ |
| 25 |
if (!(A)) \ |
| 26 |
{ \ |
| 27 |
std::cout << "line " << __LINE__ \ |
| 28 |
<< " max_abs_frac = " << max_abs_frac \ |
| 29 |
<< " tolerance = " << toler \ |
| 30 |
<< std::endl; \ |
| 31 |
} |
| 32 |
#else |
| 33 |
#include <testsuite_hooks.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
using std::is_same_v; |
| 37 |
static_assert(is_same_v<double, decltype(std::hypot(0.0, 0.0, 0.0))>); |
| 38 |
static_assert(is_same_v<double, decltype(std::hypot(0.0f, 0.0, 0.0))>); |
| 39 |
static_assert(is_same_v<double, decltype(std::hypot(0.0, 0.0f, 0.0))>); |
| 40 |
static_assert(is_same_v<double, decltype(std::hypot(0.0, 0.0, 0.0f))>); |
| 41 |
static_assert(is_same_v<double, decltype(std::hypot(0.0f, 0.0f, 0.0))>); |
| 42 |
static_assert(is_same_v<double, decltype(std::hypot(0.0f, 0.0, 0))>); |
| 43 |
static_assert(is_same_v<long double, decltype(std::hypot(0.0f, 0.0, 0.0l))>); |
| 44 |
static_assert(is_same_v<long double, decltype(std::hypot(0, 0.0, 0.0l))>); |
| 45 |
|
| 46 |
template<typename T> struct testcase_hypot { T x, y, z, f0; }; |
| 47 |
|
| 48 |
template<typename Tp, unsigned int Num> |
| 49 |
void |
| 50 |
test(const testcase_hypot<Tp> (&data)[Num], Tp toler) |
| 51 |
{ |
| 52 |
const Tp eps = std::numeric_limits<Tp>::epsilon(); |
| 53 |
Tp max_abs_diff = -Tp(1); |
| 54 |
Tp max_abs_frac = -Tp(1); |
| 55 |
unsigned int num_datum = Num; |
| 56 |
for (unsigned int i = 0; i < num_datum; ++i) |
| 57 |
{ |
| 58 |
const Tp f = std::hypot(data[i].x, data[i].y, data[i].z); |
| 59 |
const Tp f0 = data[i].f0; |
| 60 |
const Tp diff = f - f0; |
| 61 |
if (std::abs(diff) > max_abs_diff) |
| 62 |
max_abs_diff = std::abs(diff); |
| 63 |
if (std::abs(f0) > Tp(10) * eps && std::abs(f) > Tp(10) * eps) |
| 64 |
{ |
| 65 |
const Tp frac = diff / f0; |
| 66 |
if (std::abs(frac) > max_abs_frac) |
| 67 |
max_abs_frac = std::abs(frac); |
| 68 |
} |
| 69 |
} |
| 70 |
VERIFY(max_abs_frac < toler); |
| 71 |
} |
| 72 |
|
| 73 |
const testcase_hypot<double> data1[] = { |
| 74 |
{ 0.0, 0.0, 0.0, 0.0 }, |
| 75 |
{ 0.0, 1.0, 1.0, std::sqrt(2.0) }, |
| 76 |
{ 1.0, 1.0, 1.0, std::sqrt(3.0) }, |
| 77 |
{ 1.0, 2.0, 2.0, 3.0 }, |
| 78 |
{ 2.0, 3.0, 6.0, 7.0 }, |
| 79 |
{ 1.0, 4.0, 8.0, 9.0 }, |
| 80 |
{ 4.0, 4.0, 7.0, 9.0 }, |
| 81 |
{ 12.0, 16.0, 21.0, 29.0 }, |
| 82 |
{ 1e8, 1., 1e-8, 1e8 }, |
| 83 |
{ 1., 1e8, 1e-8, 1e8 }, |
| 84 |
{ 1e-8, 1., 1e8, 1e8 }, |
| 85 |
{ 1e-2, 1e-4, 1e-4, 0.01000099995 }, |
| 86 |
{ 214748364., 214748364., 214748364., 371955077.2902952 } |
| 87 |
}; |
| 88 |
const double toler1 = 1e-12; |
| 89 |
|
| 90 |
const testcase_hypot<float> data2[] = { |
| 91 |
{ 0.0f, 0.0f, 0.0f, 0.0f }, |
| 92 |
{ 0.0f, 1.0f, 1.0f, std::sqrt(2.0f) }, |
| 93 |
{ 1.0f, 1.0f, 1.0f, std::sqrt(3.0f) }, |
| 94 |
{ 1.0f, 2.0f, 2.0f, 3.0f }, |
| 95 |
{ 2.0f, 3.0f, 6.0f, 7.0f }, |
| 96 |
{ 1.0f, 4.0f, 8.0f, 9.0f }, |
| 97 |
{ 4.0f, 4.0f, 7.0f, 9.0f }, |
| 98 |
{ 12.0f, 16.0f, 21.0f, 29.0f }, |
| 99 |
{ 1e8f, 1.f, 1e-8f, 1e8f }, |
| 100 |
{ 1.f, 1e8f, 1e-8f, 1e8f }, |
| 101 |
{ 1e-8f, 1.f, 1e8f, 1e8f }, |
| 102 |
{ 1e-2f, 1e-4f, 1e-4f, 0.010001f }, |
| 103 |
{ 214748364.f, 214748364.f, 214748364.f, 371955072.f } |
| 104 |
}; |
| 105 |
const float toler2 = 1e-7f; |
| 106 |
|
| 107 |
const testcase_hypot<long double> data3[] = { |
| 108 |
{ 0.0l, 0.0l, 0.0l, 0.0l }, |
| 109 |
{ 0.0l, 1.0l, 1.0l, std::sqrt(2.0l) }, |
| 110 |
{ 1.0l, 1.0l, 1.0l, std::sqrt(3.0l) }, |
| 111 |
{ 1.0l, 2.0l, 2.0l, 3.0l }, |
| 112 |
{ 2.0l, 3.0l, 6.0l, 7.0l }, |
| 113 |
{ 1.0l, 4.0l, 8.0l, 9.0l }, |
| 114 |
{ 4.0l, 4.0l, 7.0l, 9.0l }, |
| 115 |
{ 12.0l, 16.0l, 21.0l, 29.0l }, |
| 116 |
{ 1e8l, 1.l, 1e-8l, 1e8l }, |
| 117 |
{ 1.l, 1e8l, 1e-8l, 1e8l }, |
| 118 |
{ 1e-8l, 1.l, 1e8l, 1e8l }, |
| 119 |
{ 1e-2l, 1e-4l, 1e-4l, 0.010000999950004999375l }, |
| 120 |
{ 2147483647.l, 2147483647.l, 2147483647.l, 3719550785.027307813987l } |
| 121 |
}; |
| 122 |
const long double toler3 = 1e-16l; |
| 123 |
|
| 124 |
void |
| 125 |
test01() |
| 126 |
{ |
| 127 |
test(data1, toler1); |
| 128 |
test(data2, toler2); |
| 129 |
if (sizeof(long double) > sizeof(double)) |
| 130 |
test(data3, toler3); |
| 131 |
else |
| 132 |
test(data3, (long double)toler1); |
| 133 |
} |
| 134 |
|
| 135 |
int |
| 136 |
main() |
| 137 |
{ |
| 138 |
test01(); |
| 139 |
} |