From 7704d94edd7052e7ad87eca2f84f8bc96c34c481 Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Tue, 25 Feb 2025 10:22:19 +0100 Subject: [PATCH] try fix (#941) --- include/behaviortree_cpp/utils/convert_impl.hpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/include/behaviortree_cpp/utils/convert_impl.hpp b/include/behaviortree_cpp/utils/convert_impl.hpp index dffe17c98..075001dc2 100644 --- a/include/behaviortree_cpp/utils/convert_impl.hpp +++ b/include/behaviortree_cpp/utils/convert_impl.hpp @@ -93,15 +93,16 @@ inline void checkTruncation(const From& from) if constexpr(std::is_integral_v && std::is_floating_point_v) { // Check if value can be represented exactly in the target type - To as_float = static_cast(from); - From back_conv = static_cast(as_float); - if(back_conv != from) + constexpr auto max_exact = (1LL << std::numeric_limits::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 && std::is_integral_v) + else if constexpr(std::is_floating_point_v && std::is_integral_v) { if(from > static_cast(std::numeric_limits::max()) || from < static_cast(std::numeric_limits::lowest()) ||