Skip to content

Commit

Permalink
Merge pull request #1051 from boostorg/1048
Browse files Browse the repository at this point in the history
Fix for issue 1048
  • Loading branch information
mborland authored Nov 17, 2023
2 parents 26d8675 + 6c5fd09 commit 0c5b957
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/boost/math/ccmath/floor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/math/ccmath/abs.hpp>
#include <boost/math/ccmath/isinf.hpp>
#include <boost/math/ccmath/isnan.hpp>
#include <limits>

namespace boost::math::ccmath {

Expand All @@ -23,6 +24,13 @@ namespace detail {
template <typename T>
inline constexpr T floor_pos_impl(T arg) noexcept
{
constexpr auto max_comp_val = T(1) / std::numeric_limits<T>::epsilon();

if (arg >= max_comp_val)
{
return arg;
}

T result = 1;

if(result < arg)
Expand Down
14 changes: 14 additions & 0 deletions test/ccmath_ceil_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ constexpr void test()
static_assert(boost::math::ccmath::ceil(T(2.9)) == T(3));
static_assert(boost::math::ccmath::ceil(T(-2.7)) == T(-2));
static_assert(boost::math::ccmath::ceil(T(-2)) == T(-2));

using std::ceil;

constexpr T half_max_ceil = boost::math::ccmath::ceil(T((std::numeric_limits<T>::max)() / 2));
static_assert(half_max_ceil == (std::numeric_limits<T>::max)() / 2);
BOOST_MATH_ASSERT(half_max_ceil == ceil((std::numeric_limits<T>::max)() / 2));

constexpr T third_max_ceil = boost::math::ccmath::ceil(T((std::numeric_limits<T>::max)() / 3));
static_assert(third_max_ceil == (std::numeric_limits<T>::max)() / 3);
BOOST_MATH_ASSERT(third_max_ceil == ceil((std::numeric_limits<T>::max)() / 3));

constexpr T one_over_eps = boost::math::ccmath::ceil(1 / T(std::numeric_limits<T>::epsilon()));
static_assert(one_over_eps == 1 / T(std::numeric_limits<T>::epsilon()));
BOOST_MATH_ASSERT(one_over_eps == ceil(1 / T(std::numeric_limits<T>::epsilon())));
}

#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P)
Expand Down
14 changes: 14 additions & 0 deletions test/ccmath_floor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ constexpr void test()
static_assert(boost::math::ccmath::floor(T(2.9)) == T(2));
static_assert(boost::math::ccmath::floor(T(-2.7)) == T(-3));
static_assert(boost::math::ccmath::floor(T(-2)) == T(-2));

using std::floor;

constexpr T half_max_floor = boost::math::ccmath::floor(T((std::numeric_limits<T>::max)() / 2));
static_assert(half_max_floor == (std::numeric_limits<T>::max)() / 2);
BOOST_MATH_ASSERT(half_max_floor == floor((std::numeric_limits<T>::max)() / 2));

constexpr T third_max_floor = boost::math::ccmath::floor(T((std::numeric_limits<T>::max)() / 3));
static_assert(third_max_floor == (std::numeric_limits<T>::max)() / 3);
BOOST_MATH_ASSERT(third_max_floor == floor((std::numeric_limits<T>::max)() / 3));

constexpr T one_over_eps = boost::math::ccmath::floor(1 / T(std::numeric_limits<T>::epsilon()));
static_assert(one_over_eps == 1 / T(std::numeric_limits<T>::epsilon()));
BOOST_MATH_ASSERT(one_over_eps == floor(1 / T(std::numeric_limits<T>::epsilon())));
}

#if !defined(BOOST_MATH_NO_CONSTEXPR_DETECTION) && !defined(BOOST_MATH_USING_BUILTIN_CONSTANT_P)
Expand Down

0 comments on commit 0c5b957

Please sign in to comment.