minimum double value in C/C++

后端 未结 10 1394
天命终不由人
天命终不由人 2020-12-04 08:41

Is there a standard and/or portable way to represent the smallest negative value (e.g. to use negative infinity) in a C(++) program?

DBL_MIN in float.h is the smalle

相关标签:
10条回答
  • 2020-12-04 09:15

    The original question concerns infinity. So, why not use

    #define Infinity  ((double)(42 / 0.0))
    

    according to the IEEE definition? You can negate that of course.

    0 讨论(0)
  • 2020-12-04 09:16

    Is there a standard and/or portable way to represent the smallest negative value (e.g. to use negative infinity) in a C(++) program?

    C approach.

    Many implementations support +/- infinities, so the most negative double value is -INFINITY.

    #include <math.h>
    double most_negative = -INFINITY;
    

    Is there a standard and/or portable way ....?

    Now we need to also consider other cases:

    • No infinities

    Simply -DBL_MAX.

    • Only an unsigned infinity.

    I'd expect in this case, OP would prefer -DBL_MAX.

    • De-normal values greater in magnitude than DBL_MAX.

    This is an unusual case, likely outside OP's concern. When double is encoded as a pair of a floating points to achieve desired range/precession, (see double-double) there exist a maximum normal double and perhaps a greater de-normal one. I have seen debate if DBL_MAX should refer to the greatest normal, of the greatest of both.

    Fortunately this paired approach usually includes an -infinity, so the most negative value remains -INFINITY.


    For more portability, code can go down the route

    // HUGE_VAL is designed to be infinity or DBL_MAX (when infinites are not implemented)
    // .. yet is problematic with unsigned infinity.
    double most_negative1 = -HUGE_VAL;  
    
    // Fairly portable, unless system does not understand "INF"
    double most_negative2 = strtod("-INF", (char **) NULL);
    
    // Pragmatic
    double most_negative3 = strtod("-1.0e999999999", (char **) NULL);
    
    // Somewhat time-consuming
    double most_negative4 = pow(-DBL_MAX, 0xFFFF /* odd value */);
    
    // My suggestion
    double most_negative5 = (-DBL_MAX)*DBL_MAX;
    
    0 讨论(0)
  • 2020-12-04 09:23

    -DBL_MAX in ANSI C, which is defined in float.h.

    0 讨论(0)
  • 2020-12-04 09:23

    In C, use

    #include <float.h>
    
    const double lowest_double = -DBL_MAX;
    

    In C++pre-11, use

    #include <limits>
    
    const double lowest_double = -std::numeric_limits<double>::max();
    

    In C++11 and onwards, use

    #include <limits>
    
    constexpr double lowest_double = std::numeric_limits<double>::lowest();
    
    0 讨论(0)
  • 2020-12-04 09:27

    Try this:

    -1 * numeric_limits<double>::max()
    

    Reference: numeric_limits

    This class is specialized for each of the fundamental types, with its members returning or set to the different values that define the properties that type has in the specific platform in which it compiles.

    0 讨论(0)
  • 2020-12-04 09:28

    If you do not have float exceptions enabled (which you shouldn't imho), you can simply say:

    double neg_inf = -1/0.0;
    

    This yields negative infinity. If you need a float, you can either cast the result

    float neg_inf = (float)-1/0.0;
    

    or use single precision arithmetic

    float neg_inf = -1.0f/0.0f;
    

    The result is always the same, there is exactly one representation of negative infinity in both single and double precision, and they convert to each other as you would expect.

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