Search code examples
c++floating-pointstandardsinfinity

Guarantees about C++ infinity()


Since <cmath> originates from the C standard but <limits> is native C++, provided that numeric_limits<double>::has_infinity() is true (edit: and also that numeric_limits<double>::is_iec559() is true), does the C++ (98/11/14) standard guarantees anywhere that the followings are always true? The references on MSDN and cplusplus.com don't seem to have offered any useful explanation.

  • isinf(numeric_limits<double>::infinity())
  • x < numeric_limits<double>::infinity() given that isfinite(x) is true
  • !( numeric_limits<double>::infinity()<numeric_limits<double>::infinity() )

I ask this question because I want to write a function of the form f(double x, double upper_bound), where the function body will branch according to whether x breaches the upper bound or not.


Solution

  • There is little said on infinity() in the C++11 standard. Just that it's the representation of positive infinity.

    isinf() belongs in C++ to <cmath> which wraps the c math library, and C11 says: The isinf macro returns a nonzero value if and only if its argument has an infinite value. Hence, you can deduce from that definition that isinf(numeric_limits<double>::infinity()) should be granted.

    No formal guarantee is given in the standard itself, on the fact that every finite x should be smaler than infinity. However, if numeric_limits<double>::is_iec559 is true, then you can base your assumption on the iec 559 aka Ieee 754 standard, which ensures positive infinity is larger than everything except itself.