| 1 | // Copyright (C) 2022-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++23 } } |
| 19 | // { dg-require-cmath "" } |
| 20 | |
| 21 | #include <stdfloat> |
| 22 | #include <cmath> |
| 23 | #include <limits> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | template <typename T> |
| 27 | void |
| 28 | test () |
| 29 | { |
| 30 | using lim = std::numeric_limits<T>; |
| 31 | T t0 = std::nextafter(T(-0.0), T(2.0)); |
| 32 | VERIFY( t0 == lim::denorm_min() ); |
| 33 | T t1 = std::nextafter(T(), T(1.0)); |
| 34 | VERIFY( t1 == lim::denorm_min() ); |
| 35 | T t2 = std::nextafter(T(), T()); |
| 36 | VERIFY( t2 == T() && !std::signbit(t2) ); |
| 37 | T t3 = std::nextafter(lim::denorm_min(), T(-2.0)); |
| 38 | VERIFY( t3 == T() && !std::signbit(t3) ); |
| 39 | T t4 = std::nextafter(lim::min(), T(-0.0)); |
| 40 | VERIFY( std::fpclassify(t4) == FP_SUBNORMAL && t4 > T() ); |
| 41 | T t5 = std::nextafter(t4, T(1.0)); |
| 42 | VERIFY( t5 == lim::min() ); |
| 43 | T t6 = std::nextafter(lim::min(), lim::infinity()); |
| 44 | VERIFY( std::fpclassify(t6) == FP_NORMAL && t6 > lim::min() ); |
| 45 | T t7 = std::nextafter(t6, -lim::infinity()); |
| 46 | VERIFY( t7 == lim::min() ); |
| 47 | T t8 = std::nextafter(T(16.0), T(16.5)); |
| 48 | VERIFY( t8 > t7 ); |
| 49 | T t9 = std::nextafter(t8, T(15.5)); |
| 50 | VERIFY( t9 == T(16.0) ); |
| 51 | T t10 = std::nextafter(lim::max(), T(-0.5)); |
| 52 | VERIFY( std::fpclassify(t10) == FP_NORMAL && t10 < lim::max() ); |
| 53 | T t11 = std::nextafter(t10, lim::infinity()); |
| 54 | VERIFY( t11 == lim::max() ); |
| 55 | T t12 = std::nextafter(t11, lim::infinity()); |
| 56 | VERIFY( std::fpclassify(t12) == FP_INFINITE && !std::signbit(t12) ); |
| 57 | T t13 = std::nextafter(lim::infinity(), t12); |
| 58 | VERIFY( t13 == t12 ); |
| 59 | T t14 = std::nextafter(t13, T(1.0)); |
| 60 | VERIFY( t14 == lim::max() ); |
| 61 | T t15 = std::nextafter(lim::quiet_NaN(), T()); |
| 62 | VERIFY( std::fpclassify(t15) == FP_NAN ); |
| 63 | T t16 = std::nextafter(T(17.0), lim::quiet_NaN()); |
| 64 | VERIFY( std::fpclassify(t16) == FP_NAN ); |
| 65 | T t17 = std::nextafter(T(), T(-0.0)); |
| 66 | VERIFY( t17 == T() && std::signbit(t17) ); |
| 67 | T t20 = std::nextafter(T(-0.0), T(-2.0)); |
| 68 | VERIFY( t20 == -lim::denorm_min() ); |
| 69 | T t21 = std::nextafter(T(), T(-1.0)); |
| 70 | VERIFY( t21 == -lim::denorm_min() ); |
| 71 | T t22 = std::nextafter(T(-0.0), T(-0.0)); |
| 72 | VERIFY( t22 == T() && std::signbit(t22) ); |
| 73 | T t23 = std::nextafter(-lim::denorm_min(), T(2.0)); |
| 74 | VERIFY( t23 == T() && std::signbit(t23) ); |
| 75 | T t24 = std::nextafter(-lim::min(), T()); |
| 76 | VERIFY( std::fpclassify(t24) == FP_SUBNORMAL && t24 < T() ); |
| 77 | T t25 = std::nextafter(t24, T(-1.0)); |
| 78 | VERIFY( t25 == -lim::min() ); |
| 79 | T t26 = std::nextafter(-lim::min(), -lim::infinity()); |
| 80 | VERIFY( std::fpclassify(t26) == FP_NORMAL && t26 < -lim::min() ); |
| 81 | T t27 = std::nextafter(t26, lim::infinity()); |
| 82 | VERIFY( t27 == -lim::min() ); |
| 83 | T t28 = std::nextafter(T(-16.0), T(-16.5)); |
| 84 | VERIFY( t28 < t27 ); |
| 85 | T t29 = std::nextafter(t28, T(-15.5)); |
| 86 | VERIFY( t29 == T(-16.0) ); |
| 87 | T t30 = std::nextafter(-lim::max(), T(0.5)); |
| 88 | VERIFY( std::fpclassify(t30) == FP_NORMAL && t30 > -lim::max() ); |
| 89 | T t31 = std::nextafter(t30, -lim::infinity()); |
| 90 | VERIFY( t31 == -lim::max() ); |
| 91 | T t32 = std::nextafter(t31, -lim::infinity()); |
| 92 | VERIFY( std::fpclassify(t32) == FP_INFINITE && std::signbit(t32) ); |
| 93 | T t33 = std::nextafter(-lim::infinity(), t32); |
| 94 | VERIFY( t33 == t32 ); |
| 95 | T t34 = std::nextafter(t33, T(-1.0)); |
| 96 | VERIFY( t34 == -lim::max() ); |
| 97 | T t35 = std::nextafter(-lim::quiet_NaN(), T()); |
| 98 | VERIFY( std::fpclassify(t35) == FP_NAN ); |
| 99 | T t36 = std::nextafter(T(-17.0), lim::quiet_NaN()); |
| 100 | VERIFY( std::fpclassify(t36) == FP_NAN ); |
| 101 | T t37 = std::nextafter(T(-0.0), T()); |
| 102 | VERIFY( t37 == T() && !std::signbit(t37) ); |
| 103 | static_assert(std::nextafter(T(1.0), T(2.0)) > T(1.0)); |
| 104 | static_assert(std::nextafter(std::nextafter(T(1.0), T(5.0)), T(0.0)) == T(1.0)); |
| 105 | } |
| 106 | |
| 107 | int |
| 108 | main () |
| 109 | { |
| 110 | #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 111 | test <std::float16_t>(); |
| 112 | #endif |
| 113 | #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 114 | test <std::float32_t>(); |
| 115 | #endif |
| 116 | #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64) |
| 117 | test <std::float64_t>(); |
| 118 | #endif |
| 119 | #if defined(__STDCPP_FLOAT128_T__) \ |
| 120 | && (defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128) \ |
| 121 | || defined(_GLIBCXX_HAVE_FLOAT128_MATH)) |
| 122 | test <std::float128_t>(); |
| 123 | #endif |
| 124 | #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32) |
| 125 | test <std::bfloat16_t>(); |
| 126 | #endif |
| 127 | } |