Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

constexpr modf #696

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/sf/ccmath.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ All of the following functions require C++17 or greater.
template <typename Integer>
inline constexpr double trunc(Integer arg) noexcept

template <typename Real>
inline constexpr Real modf(Real x, Real* iptr) noexcept

} // Namespaces

[endsect] [/section:ccmath Constexpr CMath]
1 change: 1 addition & 0 deletions include/boost/math/ccmath/ccmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
#include <boost/math/ccmath/floor.hpp>
#include <boost/math/ccmath/ceil.hpp>
#include <boost/math/ccmath/trunc.hpp>
#include <boost/math/ccmath/modf.hpp>

#endif // BOOST_MATH_CCMATH_HPP
77 changes: 77 additions & 0 deletions include/boost/math/ccmath/modf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// (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_MODF_HPP
#define BOOST_MATH_CCMATH_MODF_HPP

#include <cmath>
#include <limits>
#include <type_traits>
#include <boost/math/tools/is_constant_evaluated.hpp>
#include <boost/math/ccmath/abs.hpp>
#include <boost/math/ccmath/isinf.hpp>
#include <boost/math/ccmath/isnan.hpp>
#include <boost/math/ccmath/trunc.hpp>

namespace boost::math::ccmath {

namespace detail {

template <typename Real>
inline constexpr Real modf_error_impl(Real x, Real* iptr)
{
*iptr = x;
return boost::math::ccmath::abs(x) == Real(0) ? x :
x > Real(0) ? Real(0) : -Real(0);
}

template <typename Real>
inline constexpr Real modf_nan_impl(Real x, Real* iptr)
{
*iptr = x;
return x;
}

template <typename Real>
inline constexpr Real modf_impl(Real x, Real* iptr)
{
*iptr = boost::math::ccmath::trunc(x);
return (x - *iptr);
}

} // Namespace detail

template <typename Real>
inline constexpr Real modf(Real x, Real* iptr)
{
if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
{
return boost::math::ccmath::abs(x) == Real(0) ? detail::modf_error_impl(x, iptr) :
boost::math::ccmath::isinf(x) ? detail::modf_error_impl(x, iptr) :
boost::math::ccmath::isnan(x) ? detail::modf_nan_impl(x, iptr) :
boost::math::ccmath::detail::modf_impl(x, iptr);
}
else
{
using std::modf;
return modf(x, iptr);
}
}

inline constexpr float modff(float x, float* iptr)
{
return boost::math::ccmath::modf(x, iptr);
}

#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
inline constexpr long double modfl(long double x, long double* iptr)
{
return boost::math::ccmath::modf(x, iptr);
}
#endif

} // Namespaces

#endif // BOOST_MATH_CCMATH_MODF_HPP
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ test-suite special_fun :
[ run ccmath_floor_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
[ run ccmath_ceil_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
[ run ccmath_trunc_test.cpp ../../test/build//boost_unit_test_framework : : : [ requires cxx17_if_constexpr ] ]
[ run ccmath_modf_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 ]
Expand Down
98 changes: 98 additions & 0 deletions test/ccmath_modf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// (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 <utility>
#include <type_traits>
#include <boost/math/ccmath/modf.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 floating_point_value(const T val)
{
T i = 0;
const T ans = boost::math::ccmath::modf(val, &i);

return ans;
}

template <typename T>
inline constexpr T integral_value(const T val)
{
T i = 0;
boost::math::ccmath::modf(val, &i);

return i;
}

template <typename T>
inline constexpr std::pair<T, T> pair_value(const T val)
{
T i = 0;
T ans = boost::math::ccmath::modf(val, &i);

return std::make_pair(i, ans);
}

template <typename T>
constexpr void test()
{
if constexpr (std::numeric_limits<T>::has_quiet_NaN)
{
constexpr std::pair<T, T> NaN_val = pair_value(std::numeric_limits<T>::quiet_NaN());
static_assert(boost::math::ccmath::isnan(NaN_val.first));
static_assert(boost::math::ccmath::isnan(NaN_val.second));
}

// if x is +-0, +-0 is returned and +-0 is stored in *iptr
static_assert(floating_point_value(T(0)) == 0);
static_assert(floating_point_value(-T(0)) == -0);
static_assert(integral_value(T(0)) == 0);
static_assert(integral_value(-T(0)) == -0);

// if x is +- inf, +-0 is returned and +-inf is stored in *iptr
static_assert(floating_point_value(std::numeric_limits<T>::infinity()) == 0);
static_assert(floating_point_value(-std::numeric_limits<T>::infinity()) == -0);
static_assert(integral_value(std::numeric_limits<T>::infinity()) == std::numeric_limits<T>::infinity());
static_assert(integral_value(-std::numeric_limits<T>::infinity()) == -std::numeric_limits<T>::infinity());

// The returned value is exact, the current rounding mode is ignored
// The return value and *iptr each have the same type and sign as x
static_assert(integral_value(T(123.45)) == 123);
static_assert(integral_value(T(-234.56)) == -234);
static_assert(floating_point_value(T(1.0/2)) == T(1.0/2));
static_assert(floating_point_value(T(-1.0/3)) == T(-1.0/3));
}

#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
21 changes: 21 additions & 0 deletions test/compile_test/ccmath_modf_incl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// (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/modf.hpp>
#include "test_compile_result.hpp"

void compile_and_link_test()
{
float i_f;
check_result<float>(boost::math::ccmath::modf(1.0f, &i_f));

double i_d;
check_result<double>(boost::math::ccmath::modf(1.0, &i_d));

#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
long double i_ld;
check_result<long double>(boost::math::ccmath::modf(1.0l, &i_ld));
#endif
}