File tree 1 file changed +6
-5
lines changed
include/behaviortree_cpp/utils
1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change @@ -93,15 +93,16 @@ inline void checkTruncation(const From& from)
93
93
if constexpr (std::is_integral_v<From> && std::is_floating_point_v<To>)
94
94
{
95
95
// 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)
99
98
{
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));
101
102
}
102
103
}
103
104
// 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>)
105
106
{
106
107
if (from > static_cast <From>(std::numeric_limits<To>::max ()) ||
107
108
from < static_cast <From>(std::numeric_limits<To>::lowest ()) ||
You can’t perform that action at this time.
0 commit comments