]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/logaddexp_test.cpp
d0793f27addc8c3a3be4a73bcdeb801bcd7af418
[ceph.git] / ceph / src / boost / libs / math / test / logaddexp_test.cpp
1 // (C) Copyright Matt Borland 2022.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5
6 #include "math_unit_test.hpp"
7 #include <limits>
8 #include <boost/math/special_functions/logaddexp.hpp>
9 #include <boost/math/constants/constants.hpp>
10
11 template <typename Real>
12 void test()
13 {
14 using boost::math::logaddexp;
15 using std::log;
16 using std::exp;
17
18 constexpr Real nan_val = std::numeric_limits<Real>::quiet_NaN();
19 constexpr Real inf_val = std::numeric_limits<Real>::infinity();
20
21 // NAN
22 CHECK_NAN(logaddexp(nan_val, Real(1)));
23 CHECK_NAN(logaddexp(Real(1), nan_val));
24 CHECK_NAN(logaddexp(nan_val, nan_val));
25
26 // INF
27 CHECK_EQUAL(logaddexp(inf_val, Real(1)), inf_val);
28 CHECK_EQUAL(logaddexp(Real(1), inf_val), inf_val);
29 CHECK_EQUAL(logaddexp(inf_val, inf_val), inf_val);
30
31 // Equal values
32 constexpr Real ln2 = boost::math::constants::ln_two<Real>();
33 CHECK_ULP_CLOSE(Real(2) + ln2, logaddexp(Real(2), Real(2)), 1);
34 CHECK_ULP_CLOSE(Real(1e-50) + ln2, logaddexp(Real(1e-50), Real(1e-50)), 1);
35
36 // Spot check
37 // https://numpy.org/doc/stable/reference/generated/numpy.logaddexp.html
38 // Calculated at higher precision using wolfram alpha
39 Real x1 = 1e-50l;
40 Real x2 = 2.5e-50l;
41 Real spot1 = static_cast<Real>(exp(x1));
42 Real spot2 = static_cast<Real>(exp(x2));
43 Real spot12 = logaddexp(x1, x2);
44
45 CHECK_ULP_CLOSE(log(spot1 + spot2), spot12, 1);
46 }
47
48 int main (void)
49 {
50 test<float>();
51 test<double>();
52 test<long double>();
53 return boost::math::test::report_errors();
54 }