Skip to content

Commit

Permalink
try fix (BehaviorTree#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide authored Feb 25, 2025
1 parent a7c9087 commit 7704d94
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions include/behaviortree_cpp/utils/convert_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ inline void checkTruncation(const From& from)
if constexpr(std::is_integral_v<From> && std::is_floating_point_v<To>)
{
// Check if value can be represented exactly in the target type
To as_float = static_cast<To>(from);
From back_conv = static_cast<From>(as_float);
if(back_conv != from)
constexpr auto max_exact = (1LL << std::numeric_limits<double>::digits) - 1;
if(from > max_exact || from < -max_exact)
{
throw std::runtime_error("Loss of precision in conversion to floating point");
throw std::runtime_error("Loss of precision when converting a large integer number "
"to floating point:" +
std::to_string(from));
}
}
// Handle floating point to integer
if constexpr(std::is_floating_point_v<From> && std::is_integral_v<To>)
else if constexpr(std::is_floating_point_v<From> && std::is_integral_v<To>)
{
if(from > static_cast<From>(std::numeric_limits<To>::max()) ||
from < static_cast<From>(std::numeric_limits<To>::lowest()) ||
Expand Down

0 comments on commit 7704d94

Please sign in to comment.