Skip to content

Commit 7704d94

Browse files
try fix (#941)
1 parent a7c9087 commit 7704d94

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

include/behaviortree_cpp/utils/convert_impl.hpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,16 @@ inline void checkTruncation(const From& from)
9393
if constexpr(std::is_integral_v<From> && std::is_floating_point_v<To>)
9494
{
9595
// Check if value can be represented exactly in the target type
96-
To as_float = static_cast<To>(from);
97-
From back_conv = static_cast<From>(as_float);
98-
if(back_conv != from)
96+
constexpr auto max_exact = (1LL << std::numeric_limits<double>::digits) - 1;
97+
if(from > max_exact || from < -max_exact)
9998
{
100-
throw std::runtime_error("Loss of precision in conversion to floating point");
99+
throw std::runtime_error("Loss of precision when converting a large integer number "
100+
"to floating point:" +
101+
std::to_string(from));
101102
}
102103
}
103104
// Handle floating point to integer
104-
if constexpr(std::is_floating_point_v<From> && std::is_integral_v<To>)
105+
else if constexpr(std::is_floating_point_v<From> && std::is_integral_v<To>)
105106
{
106107
if(from > static_cast<From>(std::numeric_limits<To>::max()) ||
107108
from < static_cast<From>(std::numeric_limits<To>::lowest()) ||

0 commit comments

Comments
 (0)