Skip to content

Fix issue #939 (checkTruncation) #941

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

Merged
merged 1 commit into from
Feb 25, 2025
Merged
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
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
Loading