Skip to content

Commit

Permalink
Add noexcept and fix is_representable for signed types
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland committed Mar 17, 2023
1 parent 5284fb7 commit 8b9727a
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions include/boost/math/special_functions/round.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace detail{

// https://stackoverflow.com/questions/8905246/how-to-check-if-float-can-be-exactly-represented-as-an-integer/17822304#17822304
template <typename T, typename ResultType>
inline ResultType float_to_int(T x)
inline ResultType float_to_int(T x) noexcept
{
BOOST_MATH_STD_USING

Expand All @@ -45,10 +45,13 @@ inline ResultType float_to_int(T x)
}

template <typename T, typename TargetType>
inline bool is_representable(T x)
inline bool is_representable(T x) noexcept
{
BOOST_MATH_STD_USING
return (floor(x) == x && x >= static_cast<T>(0.0L) && x < ldexp(static_cast<T>(1.0L), sizeof(TargetType) * CHAR_BIT));
constexpr int sign = std::is_signed<ResultType>::value ? -1 : 0;
return (floor(x) == x &&
x >= static_cast<T>(0.0L) &&
x < (ldexp(static_cast<T>(1.0L), (sizeof(TargetType) * CHAR_BIT) + sign) + sign));
}

template <class T, class Policy>
Expand Down

0 comments on commit 8b9727a

Please sign in to comment.