]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // (C) Copyright Matt Borland 2021. |
2 | // Use, modification and distribution are subject to the | |
3 | // Boost 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 | #ifndef BOOST_MATH_CCMATH_LOGB_HPP | |
7 | #define BOOST_MATH_CCMATH_LOGB_HPP | |
8 | ||
9 | #include <cmath> | |
10 | #include <limits> | |
11 | #include <type_traits> | |
12 | #include <boost/math/tools/is_constant_evaluated.hpp> | |
13 | #include <boost/math/ccmath/frexp.hpp> | |
14 | #include <boost/math/ccmath/isinf.hpp> | |
15 | #include <boost/math/ccmath/isnan.hpp> | |
16 | #include <boost/math/ccmath/abs.hpp> | |
17 | ||
18 | namespace boost::math::ccmath { | |
19 | ||
20 | namespace detail { | |
21 | ||
22 | // The value of the exponent returned by std::logb is always 1 less than the exponent returned by | |
23 | // std::frexp because of the different normalization requirements: for the exponent e returned by std::logb, | |
24 | // |arg*r^-e| is between 1 and r (typically between 1 and 2), but for the exponent e returned by std::frexp, | |
25 | // |arg*2^-e| is between 0.5 and 1. | |
26 | template <typename T> | |
27 | inline constexpr T logb_impl(T arg) noexcept | |
28 | { | |
29 | int exp = 0; | |
30 | boost::math::ccmath::frexp(arg, &exp); | |
31 | ||
32 | return exp - 1; | |
33 | } | |
34 | ||
35 | } // Namespace detail | |
36 | ||
37 | template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true> | |
38 | inline constexpr Real logb(Real arg) noexcept | |
39 | { | |
40 | if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg)) | |
41 | { | |
42 | return boost::math::ccmath::abs(arg) == Real(0) ? -std::numeric_limits<Real>::infinity() : | |
43 | boost::math::ccmath::isinf(arg) ? std::numeric_limits<Real>::infinity() : | |
44 | boost::math::ccmath::isnan(arg) ? std::numeric_limits<Real>::quiet_NaN() : | |
45 | boost::math::ccmath::detail::logb_impl(arg); | |
46 | } | |
47 | else | |
48 | { | |
49 | using std::logb; | |
50 | return logb(arg); | |
51 | } | |
52 | } | |
53 | ||
54 | template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true> | |
55 | inline constexpr double logb(Z arg) noexcept | |
56 | { | |
57 | return boost::math::ccmath::logb(static_cast<double>(arg)); | |
58 | } | |
59 | ||
60 | inline constexpr float logbf(float arg) noexcept | |
61 | { | |
62 | return boost::math::ccmath::logb(arg); | |
63 | } | |
64 | ||
65 | #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS | |
66 | inline constexpr long double logbl(long double arg) noexcept | |
67 | { | |
68 | return boost::math::ccmath::logb(arg); | |
69 | } | |
70 | #endif | |
71 | ||
72 | } // Namespaces | |
73 | ||
74 | #endif // BOOST_MATH_CCMATH_LOGB_HPP |