I want to know a right and maybe elegant way in C++ to detect whether a floating point number is ordinary. By "ordinary floating point numbers", I mean those of double/float/long double type except NAN or INF.
            Asked
            
        
        
            Active
            
        
            Viewed 119 times
        
    -1
            
            
        - 
                    1@Drew That makes sense. My apologies. – zell Jan 13 '15 at 03:41
- 
                    2In addition to the good answers, finite floating-point numbers are exactly those that make the condition `x - x == 0` true, for when you have good reasons to avoid including a header. Specialists will read it as an idiom but non-specialists will be annoyed, so use with care. – Pascal Cuoq Jan 13 '15 at 12:23
2 Answers
3
            
            
        You could check out Boost.Math. It defines all of these:
template <class T>
bool isfinite(T z); // Neither infinity nor NaN.
template <class T>
bool isinf(T t); // Infinity (+ or -).
template <class T>
bool isnan(T t); // NaN.
template <class T>
bool isnormal(T t); // isfinite and not denormalised.
Since C++11, these are in <cmath> too: std::isnan, std::isinf, std::isfinite, and std::isnormal. 
 
    
    
        Barry
        
- 286,269
- 29
- 621
- 977
3
            
            
        std::isfinite() will return true for values that are not INF or NaN.
(Edited to reflect a question edit)
 
    
    
        Drew Dormann
        
- 59,987
- 13
- 123
- 180
 
    