|
| 1 | +/////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright 2026 Matt Borland. Distributed under the Boost |
| 3 | +// Software License, Version 1.0. (See accompanying file |
| 4 | +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | +// |
| 6 | +// See: https://github.com/boostorg/multiprecision/issues/759 |
| 7 | + |
| 8 | +#include <complex> |
| 9 | +#include <iostream> |
| 10 | +#include <limits> |
| 11 | +#include <boost/multiprecision/float128.hpp> |
| 12 | +#include "test.hpp" |
| 13 | + |
| 14 | +int main() |
| 15 | +{ |
| 16 | + using REAL = boost::multiprecision::float128; |
| 17 | + |
| 18 | + const REAL inf_v = std::numeric_limits<REAL>::infinity(); |
| 19 | + |
| 20 | + { |
| 21 | + using std::abs; |
| 22 | + const REAL r = abs(inf_v); |
| 23 | + BOOST_CHECK((boost::multiprecision::isinf)(r)); |
| 24 | + } |
| 25 | + |
| 26 | + { |
| 27 | + using std::abs; |
| 28 | + const std::complex<REAL> z{inf_v, REAL(0)}; |
| 29 | + const REAL r = abs(z); |
| 30 | + BOOST_CHECK((boost::multiprecision::isinf)(r)); |
| 31 | + BOOST_CHECK(!(boost::multiprecision::isnan)(r)); |
| 32 | + } |
| 33 | + |
| 34 | + { |
| 35 | + using std::abs; |
| 36 | + const std::complex<REAL> z{REAL(0), inf_v}; |
| 37 | + const REAL r = abs(z); |
| 38 | + BOOST_CHECK((boost::multiprecision::isinf)(r)); |
| 39 | + BOOST_CHECK(!(boost::multiprecision::isnan)(r)); |
| 40 | + } |
| 41 | + |
| 42 | + { |
| 43 | + using std::abs; |
| 44 | + const std::complex<REAL> z{-inf_v, REAL(3)}; |
| 45 | + const REAL r = abs(z); |
| 46 | + BOOST_CHECK((boost::multiprecision::isinf)(r)); |
| 47 | + BOOST_CHECK(!(boost::multiprecision::isnan)(r)); |
| 48 | + } |
| 49 | + |
| 50 | + { |
| 51 | + // Per IEEE 754: hypot(inf, NaN) = inf. |
| 52 | + using std::abs; |
| 53 | + const REAL nan_v = std::numeric_limits<REAL>::quiet_NaN(); |
| 54 | + const std::complex<REAL> z{inf_v, nan_v}; |
| 55 | + const REAL r = abs(z); |
| 56 | + BOOST_CHECK((boost::multiprecision::isinf)(r)); |
| 57 | + } |
| 58 | + |
| 59 | + { |
| 60 | + // Sanity check on a finite value: abs(3 + 4i) == 5. |
| 61 | + using std::abs; |
| 62 | + const std::complex<REAL> z{REAL(3), REAL(4)}; |
| 63 | + const REAL r = abs(z); |
| 64 | + BOOST_CHECK_EQUAL(r, REAL(5)); |
| 65 | + } |
| 66 | + |
| 67 | + return boost::report_errors(); |
| 68 | +} |
0 commit comments