NaNs and Infinites can ruin such comparisons, as others have already mentioned.
However, there is another mistake: in C ++ you cannot rely on a compilation time expression of type float comparing it with the same expression that was evaluated at runtime.
The reason for this is that C ++ allows for extended fp precision in any way perforce. Example:
Results with one specific compiler:
C:\test> g++ --version | find "++" g++ (TDM-2 mingw32) 4.4.1 C:\test> g++ fp_comparision_problem.cpp & a "Wrong" comparision result. C:\test> g++ -O fp_comparision_problem.cpp & a OK. C:\test> _
Cheers and hth.,
- alf
Cheers and hth. - alf
source share