| 1 |
// (C) Copyright Matt Borland 2021. |
| 2 |
// (C) Copyright John Maddock 2021. |
| 3 |
// Use, modification and distribution are subject to the |
| 4 |
// Boost Software License, Version 1.0. (See accompanying file |
| 5 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
#ifndef BOOST_MATH_CCMATH_SCALBN_HPP |
| 8 |
#define BOOST_MATH_CCMATH_SCALBN_HPP |
| 9 |
|
| 10 |
#include <boost/math/ccmath/detail/config.hpp> |
| 11 |
|
| 12 |
#ifdef BOOST_MATH_NO_CCMATH |
| 13 |
#error "The header <boost/math/scalbn.hpp> can only be used in C++17 and later." |
| 14 |
#endif |
| 15 |
|
| 16 |
#include <cfloat> |
| 17 |
#include <boost/math/ccmath/abs.hpp> |
| 18 |
#include <boost/math/ccmath/isinf.hpp> |
| 19 |
#include <boost/math/ccmath/isnan.hpp> |
| 20 |
|
| 21 |
namespace boost::math::ccmath { |
| 22 |
|
| 23 |
namespace detail { |
| 24 |
|
| 25 |
template <typename Real, typename Z> |
| 26 |
inline constexpr Real scalbn_impl(Real arg, Z exp) noexcept |
| 27 |
{ |
| 28 |
while(exp > 0) |
| 29 |
{ |
| 30 |
arg *= FLT_RADIX; |
| 31 |
--exp; |
| 32 |
} |
| 33 |
while(exp < 0) |
| 34 |
{ |
| 35 |
arg /= FLT_RADIX; |
| 36 |
++exp; |
| 37 |
} |
| 38 |
|
| 39 |
return arg; |
| 40 |
} |
| 41 |
|
| 42 |
} // Namespace detail |
| 43 |
|
| 44 |
template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true> |
| 45 |
inline constexpr Real scalbn(Real arg, int exp) noexcept |
| 46 |
{ |
| 47 |
if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg)) |
| 48 |
{ |
| 49 |
return boost::math::ccmath::abs(arg) == Real(0) ? arg : |
| 50 |
boost::math::ccmath::isinf(arg) ? arg : |
| 51 |
boost::math::ccmath::isnan(arg) ? arg : |
| 52 |
boost::math::ccmath::detail::scalbn_impl(arg, exp); |
| 53 |
} |
| 54 |
else |
| 55 |
{ |
| 56 |
using std::scalbn; |
| 57 |
return scalbn(arg, exp); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true> |
| 62 |
inline constexpr double scalbn(Z arg, int exp) noexcept |
| 63 |
{ |
| 64 |
return boost::math::ccmath::scalbn(static_cast<double>(arg), exp); |
| 65 |
} |
| 66 |
|
| 67 |
inline constexpr float scalbnf(float arg, int exp) noexcept |
| 68 |
{ |
| 69 |
return boost::math::ccmath::scalbn(arg, exp); |
| 70 |
} |
| 71 |
|
| 72 |
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
| 73 |
inline constexpr long double scalbnl(long double arg, int exp) noexcept |
| 74 |
{ |
| 75 |
return boost::math::ccmath::scalbn(arg, exp); |
| 76 |
} |
| 77 |
#endif |
| 78 |
|
| 79 |
} // Namespaces |
| 80 |
|
| 81 |
#endif // BOOST_MATH_CCMATH_SCALBN_HPP |