mirror of
https://github.com/boostorg/math.git
synced 2026-01-19 04:22:09 +00:00
constexpr frexp (#686)
This commit is contained in:
@@ -12,6 +12,7 @@ LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
`Constexpr` implementations of the functionality found in `<cmath>`.
|
||||
In a `constexpr` context the functions will use an implementation defined in boost.
|
||||
If the context is not `constexpr` the functionality will be directly from the STL implementation of `<cmath>` used by the compiler.
|
||||
All functions that take an `Integer` type and return a `double` simply cast the `Integer` argument to a `double`.
|
||||
All of the following functions require C++17 or greater.
|
||||
|
||||
[heading Synopsis]
|
||||
@@ -30,8 +31,8 @@ All of the following functions require C++17 or greater.
|
||||
template <typename Real>
|
||||
inline constexpr Real sqrt(Real x);
|
||||
|
||||
template <typename Z>
|
||||
inline constexpr double sqrt(Z x);
|
||||
template <typename Integer>
|
||||
inline constexpr double sqrt(Integer x);
|
||||
|
||||
template <typename T>
|
||||
inline constexpr T abs(T x);
|
||||
@@ -51,6 +52,12 @@ All of the following functions require C++17 or greater.
|
||||
template <typename T>
|
||||
inline constexpr int fpclassify(T x);
|
||||
|
||||
template <typename Real>
|
||||
inline constexpr Real frexp(Real arg, int* exp);
|
||||
|
||||
template <typename Integer>
|
||||
inline constexpr double frexp(Integer arg, int* exp);
|
||||
|
||||
} // Namespaces
|
||||
|
||||
[endsect] [/section:ccmath Constexpr CMath]
|
||||
|
||||
@@ -15,5 +15,6 @@
|
||||
#include <boost/math/ccmath/isfinite.hpp>
|
||||
#include <boost/math/ccmath/isnormal.hpp>
|
||||
#include <boost/math/ccmath/fpclassify.hpp>
|
||||
#include <boost/math/ccmath/frexp.hpp>
|
||||
|
||||
#endif // BOOST_MATH_CCMATH
|
||||
|
||||
98
include/boost/math/ccmath/frexp.hpp
Normal file
98
include/boost/math/ccmath/frexp.hpp
Normal file
@@ -0,0 +1,98 @@
|
||||
// (C) Copyright Christopher Kormanyos 1999 - 2021.
|
||||
// (C) Copyright Matt Borland 2021.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_MATH_CCMATH_FREXP_HPP
|
||||
#define BOOST_MATH_CCMATH_FREXP_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <boost/math/ccmath/isinf.hpp>
|
||||
#include <boost/math/ccmath/isnan.hpp>
|
||||
#include <boost/math/ccmath/isfinite.hpp>
|
||||
|
||||
namespace boost::math::ccmath {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
template <typename Real>
|
||||
inline constexpr Real frexp_zero_impl(Real arg, int* exp)
|
||||
{
|
||||
*exp = 0;
|
||||
return arg;
|
||||
}
|
||||
|
||||
template <typename Real>
|
||||
inline constexpr Real frexp_impl(Real arg, int* exp)
|
||||
{
|
||||
const bool negative_arg = (arg < Real(0));
|
||||
|
||||
Real f = negative_arg ? -arg : arg;
|
||||
int e2 = 0;
|
||||
constexpr Real two_pow_32 = Real(4294967296);
|
||||
|
||||
while (f >= two_pow_32)
|
||||
{
|
||||
f = f / two_pow_32;
|
||||
e2 += 32;
|
||||
}
|
||||
|
||||
while(f >= Real(1))
|
||||
{
|
||||
f = f / Real(2);
|
||||
++e2;
|
||||
}
|
||||
|
||||
if(exp != nullptr)
|
||||
{
|
||||
*exp = e2;
|
||||
}
|
||||
|
||||
return !negative_arg ? f : -f;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename Real, std::enable_if_t<!std::is_integral_v<Real>, bool> = true>
|
||||
inline constexpr Real frexp(Real arg, int* exp)
|
||||
{
|
||||
if(BOOST_MATH_IS_CONSTANT_EVALUATED(arg))
|
||||
{
|
||||
return arg == Real(0) ? detail::frexp_zero_impl(arg, exp) :
|
||||
arg == Real(-0) ? detail::frexp_zero_impl(arg, exp) :
|
||||
boost::math::ccmath::isinf(arg) ? detail::frexp_zero_impl(arg, exp) :
|
||||
boost::math::ccmath::isnan(arg) ? detail::frexp_zero_impl(arg, exp) :
|
||||
boost::math::ccmath::detail::frexp_impl(arg, exp);
|
||||
}
|
||||
else
|
||||
{
|
||||
using std::frexp;
|
||||
return frexp(arg, exp);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Z, std::enable_if_t<std::is_integral_v<Z>, bool> = true>
|
||||
inline constexpr double frexp(Z arg, int* exp)
|
||||
{
|
||||
return boost::math::ccmath::frexp(static_cast<double>(arg), exp);
|
||||
}
|
||||
|
||||
inline constexpr float frexpf(float arg, int* exp)
|
||||
{
|
||||
return boost::math::ccmath::frexp(arg, exp);
|
||||
}
|
||||
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
inline constexpr long double frexpl(long double arg, int* exp)
|
||||
{
|
||||
return boost::math::ccmath::frexp(arg, exp);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#endif // BOOST_MATH_CCMATH_FREXP_HPP
|
||||
@@ -128,6 +128,7 @@ test-suite special_fun :
|
||||
[ run ccmath_isfinite_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
|
||||
[ run ccmath_isnormal_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
|
||||
[ run ccmath_fpclassify_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
|
||||
[ run ccmath_frexp_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
|
||||
[ run log1p_expm1_test.cpp test_instances//test_instances pch_light ../../test/build//boost_unit_test_framework ]
|
||||
[ run powm1_sqrtp1m1_test.cpp test_instances//test_instances pch_light ../../test/build//boost_unit_test_framework ]
|
||||
[ run special_functions_test.cpp ../../test/build//boost_unit_test_framework ]
|
||||
|
||||
80
test/ccmath_frexp_test.cpp
Normal file
80
test/ccmath_frexp_test.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
// (C) Copyright Matt Borland 2021.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <cmath>
|
||||
#include <cfloat>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <boost/math/ccmath/frexp.hpp>
|
||||
#include <boost/math/ccmath/isnan.hpp>
|
||||
#include <boost/math/ccmath/isinf.hpp>
|
||||
|
||||
#ifdef BOOST_HAS_FLOAT128
|
||||
#include <boost/multiprecision/float128.hpp>
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
inline constexpr T base_helper(const T val)
|
||||
{
|
||||
int i = 0;
|
||||
const T ans = boost::math::ccmath::frexp(val, &i);
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline constexpr int exp_helper(const T val)
|
||||
{
|
||||
int i = 0;
|
||||
boost::math::ccmath::frexp(val, &i);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
constexpr void test()
|
||||
{
|
||||
if constexpr (std::numeric_limits<T>::has_quiet_NaN)
|
||||
{
|
||||
static_assert(boost::math::ccmath::isnan(base_helper(std::numeric_limits<T>::quiet_NaN())), "If the arg is NaN, NaN is returned");
|
||||
}
|
||||
|
||||
static_assert(!base_helper(T(0)), "If the arg is +- 0 the value is returned");
|
||||
static_assert(!base_helper(T(-0)), "If the arg is +- 0 the value is returned");
|
||||
static_assert(boost::math::ccmath::isinf(base_helper(std::numeric_limits<T>::infinity())), "If the arg is +- inf the value is returned");
|
||||
static_assert(boost::math::ccmath::isinf(base_helper(-std::numeric_limits<T>::infinity())), "If the arg is +- inf the value is returned");
|
||||
|
||||
// N[125/32, 30]
|
||||
// 3.90625000000000000000000000000
|
||||
// 0.976562500000000000000000000000 * 2^2
|
||||
constexpr T test_base = base_helper(T(125.0/32));
|
||||
static_assert(test_base == T(0.9765625));
|
||||
constexpr int test_exp = exp_helper(T(125.0/32));
|
||||
static_assert(test_exp == 2);
|
||||
}
|
||||
|
||||
#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P)
|
||||
int main()
|
||||
{
|
||||
test<float>();
|
||||
test<double>();
|
||||
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
test<long double>();
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_HAS_FLOAT128
|
||||
test<boost::multiprecision::float128>();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
17
test/compile_test/ccmath_frexp_incl_test.cpp
Normal file
17
test/compile_test/ccmath_frexp_incl_test.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
// (C) Copyright Matt Borland 2021.
|
||||
// Use, modification and distribution are subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/math/ccmath/frexp.hpp>
|
||||
#include "test_compile_result.hpp"
|
||||
|
||||
void compile_and_link_test()
|
||||
{
|
||||
int i;
|
||||
check_result<float>(boost::math::ccmath::frexp(1.0f, &i));
|
||||
check_result<double>(boost::math::ccmath::frexp(1.0, &i));
|
||||
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
|
||||
check_result<long double>(boost::math::ccmath::frexp(1.0l, &i));
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user