Search code examples
c++doubleinfinity

Modeling infinity for the largest double value


The question is about modeling infinity in C++ for the double data type. I need it in a header file, so we cannot use functions like numeric_limits.

Is there a defined constant that represents the largest value?


Solution

  • floating point numbers(such as doubles) can actually hold positive and negative infinity. The constant INFINITY should be in your math.h header.

    Went standard diving and found the text:

    4 The macro INFINITY expands to a constant expression of type float representing positive or unsigned infinity, if available; else to a positive constant of type float that overflows at translation time.

    In Section 7.12 Mathematics <math.h>


    Then of course you have the helper function isinf to test for infinity(which is also in math.h).

    7.12.3.3 The isinf macro

    int isinf(real-floating x);

    Description: The isinf macro determines whether its argument value is an infinity (positive or negative). First, an argument represented in a format wider than its semantic type is converted to its semantic type. Then determination is based on the type of the argument.

    Returns: The isinf macro returns a nonzero value if and only if its argument has an infinite value.