Modeling infinity for the largest double value

后端 未结 9 1382
予麋鹿
予麋鹿 2020-12-31 05:10

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

相关标签:
9条回答
  • 2020-12-31 05:58

    DBL_MAX can be used. This is found in float.h as follows

        #define DBL_MAX         1.7976931348623158e+308 /* max value */
    
    0 讨论(0)
  • 2020-12-31 06:00
    #include <cmath>
    ...
    double d = INFINITY;
    

    You can find INFINITY defined in <cmath> (math.h):

    A constant expression of type float representing positive or unsigned infinity, if available; else a positive constant of type float that overflows at translation time.

    0 讨论(0)
  • 2020-12-31 06:01

    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.

    0 讨论(0)
提交回复
热议问题