|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +// UNSUPPORTED: c++03, c++11, c++14 |
| 10 | + |
| 11 | +// <cmath> |
| 12 | + |
| 13 | +// double legendre(unsigned n, double x); |
| 14 | +// float legendre(unsigned n, float x); |
| 15 | +// long double legendre(unsigned n, long double x); |
| 16 | +// float legendref(unsigned n, float x); |
| 17 | +// long double legendrel(unsigned n, long double x); |
| 18 | +// template <class Integer> |
| 19 | +// double legendre(unsigned n, Integer x); |
| 20 | + |
| 21 | +#include <cassert> |
| 22 | +#include <cmath> |
| 23 | +#include <limits> |
| 24 | + |
| 25 | +template <class T> |
| 26 | +void testLegendreNaNPropagation() { |
| 27 | + const unsigned MaxN = 127; |
| 28 | + const T x = std::numeric_limits<T>::quiet_NaN(); |
| 29 | + for (unsigned n = 0; n <= MaxN; ++n) { |
| 30 | + assert(std::isnan(std::legendre(n, x))); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +template <class T> |
| 35 | +void testLegendreNotNaN(const T x) { |
| 36 | + assert(!std::isnan(x)); |
| 37 | + const unsigned MaxN = 127; |
| 38 | + for (unsigned n = 0; n <= MaxN; ++n) { |
| 39 | + assert(!std::isnan(std::legendre(n, x))); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +template <class T> |
| 44 | +void testLegendreThrows(const T x) { |
| 45 | +#ifndef _LIBCPP_NO_EXCEPTIONS |
| 46 | + const unsigned MaxN = 127; |
| 47 | + for (unsigned n = 0; n <= MaxN; ++n) { |
| 48 | + bool Throws = false; |
| 49 | + try { |
| 50 | + std::legendre(n, x); |
| 51 | + } catch (const std::domain_error&) { |
| 52 | + Throws = true; |
| 53 | + } |
| 54 | + assert(Throws); |
| 55 | + } |
| 56 | +#endif // _LIBCPP_NO_EXCEPTIONS |
| 57 | +} |
| 58 | + |
| 59 | +template <class T> |
| 60 | +void testLegendreAnalytic(const T x, const T AbsTolerance, const T RelTolerance) { |
| 61 | + assert(!std::isnan(x)); |
| 62 | + const auto compareFloatingPoint = [AbsTolerance, RelTolerance](const T Result, const T ExpectedResult) { |
| 63 | + if (std::isinf(ExpectedResult) && std::isinf(Result)) |
| 64 | + return true; |
| 65 | + |
| 66 | + if (std::isnan(ExpectedResult) || std::isnan(Result)) |
| 67 | + return false; |
| 68 | + |
| 69 | + const T Tolerance = AbsTolerance + std::abs(ExpectedResult) * RelTolerance; |
| 70 | + return std::abs(Result - ExpectedResult) < Tolerance; |
| 71 | + }; |
| 72 | + |
| 73 | + const auto l0 = [](T) { return T(1); }; |
| 74 | + const auto l1 = [](T y) { return y; }; |
| 75 | + const auto l2 = [](T y) { return (T(3) * y * y - T(1)) / T(2); }; |
| 76 | + const auto l3 = [](T y) { return (T(5) * y * y - T(3)) * y / T(2); }; |
| 77 | + const auto l4 = [](T y) { return (T(35) * y * y * y * y - T(30) * y * y + T(3)) / T(8); }; |
| 78 | + const auto l5 = [](T y) { return (T(63) * y * y * y * y - T(70) * y * y + T(15)) * y / T(8); }; |
| 79 | + const auto l6 = [](T y) { |
| 80 | + const T y2 = y * y; |
| 81 | + return (T(231) * y2 * y2 * y2 - T(315) * y2 * y2 + T(105) * y2 - T(5)) / T(16); |
| 82 | + }; |
| 83 | + |
| 84 | + assert(compareFloatingPoint(std::legendre(0, x), l0(x))); |
| 85 | + assert(compareFloatingPoint(std::legendre(1, x), l1(x))); |
| 86 | + assert(compareFloatingPoint(std::legendre(2, x), l2(x))); |
| 87 | + assert(compareFloatingPoint(std::legendre(3, x), l3(x))); |
| 88 | + assert(compareFloatingPoint(std::legendre(4, x), l4(x))); |
| 89 | + assert(compareFloatingPoint(std::legendre(5, x), l5(x))); |
| 90 | + assert(compareFloatingPoint(std::legendre(6, x), l6(x))); |
| 91 | +} |
| 92 | + |
| 93 | +template <class T> |
| 94 | +void testLegendre(const T AbsTolerance, const T RelTolerance) { |
| 95 | + testLegendreNaNPropagation<T>(); |
| 96 | + testLegendreThrows<T>(T(-5)); |
| 97 | + testLegendreThrows<T>(T(5)); |
| 98 | + |
| 99 | + const T Samples[] = {T(-1.0), T(-0.5), T(-0.1), T(0.0), T(0.1), T(0.5), T(1.0)}; |
| 100 | + |
| 101 | + for (T x : Samples) { |
| 102 | + testLegendreNotNaN(x); |
| 103 | + testLegendreAnalytic(x, AbsTolerance, RelTolerance); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +int main() { |
| 108 | + testLegendre<float>(1e-6f, 1e-6f); |
| 109 | + testLegendre<double>(1e-9, 1e-9); |
| 110 | + testLegendre<long double>(1e-12, 1e-12); |
| 111 | +} |
0 commit comments