g++ warning: comparing floating point with == or != is unsafe

In: /usr/include/boost/test/floating_point_comparison.hpp inside: template<typename FPT> inline FPT safe_fpt_division( FPT f1, FPT f2 ){ the warning is cause by the code: // Avoid underflow. if( f1 == static_cast<FPT>(0) || f2 > static_cast<FPT>(1) && f1 < f2 * (std::numeric_limits<FPT>::min)() ) return static_cast<FPT>(0); Could someone modify the code to allow it to compile without warnings on g++? Latest CVS version still has this problem. I'm too dumb to think of a reasonable patch to submit - sorry. ---------------------------------------------------------------------- PS. Fajny portal... >>> http://link.interia.pl/f196a

cybernet@interia.pl writes:
In: /usr/include/boost/test/floating_point_comparison.hpp
inside:
template<typename FPT> inline FPT safe_fpt_division( FPT f1, FPT f2 ){
the warning is cause by the code:
// Avoid underflow. if( f1 == static_cast<FPT>(0) || f2 > static_cast<FPT>(1) && f1 < f2 * (std::numeric_limits<FPT>::min)() ) return static_cast<FPT>(0);
Could someone modify the code to allow it to compile without warnings on g++?
Try replacing f1 == static_cast<FPT>(0) with f1 HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com

"David Abrahams"
cybernet@interia.pl writes:
In: /usr/include/boost/test/floating_point_comparison.hpp
inside:
template<typename FPT> inline FPT safe_fpt_division( FPT f1, FPT f2 ){
the warning is cause by the code:
// Avoid underflow. if( f1 == static_cast<FPT>(0) || f2 > static_cast<FPT>(1) && f1 < f2 * (std::numeric_limits<FPT>::min)() ) return static_cast<FPT>(0);
Could someone modify the code to allow it to compile without warnings on g++?
Try replacing
f1 == static_cast<FPT>(0)
with
f1
I am not sure that it will be 100% equivalent for all FP types including user defined. Gennadiy

David Abrahams wrote:
cybernet@interia.pl writes:
In: /usr/include/boost/test/floating_point_comparison.hpp
inside:
template<typename FPT> inline FPT safe_fpt_division( FPT f1, FPT f2 ){
the warning is cause by the code:
// Avoid underflow. if( f1 == static_cast<FPT>(0) || f2 > static_cast<FPT>(1) && f1 < f2 * (std::numeric_limits<FPT>::min)() ) return static_cast<FPT>(0);
Could someone modify the code to allow it to compile without warnings on g++?
Try replacing
f1 == static_cast<FPT>(0)
with
f1
Unfortunately that would make Boost.Test unusable with real-number types that are classes not usable in boolian contexts (for example NTL::RR). I was all set to suggest that the leading test for zero appears to be superfluous in any case, but actually in this context it's not: we really do want 0/0 to be 0 here since we're calculating relative error. So I'm left with thinking that gcc is being unreasonable frankly and the code should stay as it is? John.
participants (4)
-
cybernetļ¼ interia.pl
-
David Abrahams
-
Gennadiy Rozental
-
John Maddock