Negative infinity

后端 未结 3 1960
情歌与酒
情歌与酒 2020-12-24 06:46

I\'m trying to figure out how to assign the value of negative infinity to a float or double variable. It seems that including the standard library limits, I can get the infi

相关标签:
3条回答
  • 2020-12-24 07:09

    If std::numeric_limits<double>::is_iec559 is true then it should be safe to use -

    double negative_infinity = - std::numeric_limits<double>::infinity();

    (IEC559 is the ISO equivalent of IEEE754)

    If it's false then there's a whole lot more work to do as I don't think the C++ standard gives you any help.

    0 讨论(0)
  • 2020-12-24 07:18

    At least if std::numeric_limits::is_iec559 (IEEE 754) is true (which guarantees, that std::numeric_limits::has_infinity is also true), you can express positive and negative infinity values the way you already stated.

    Short explanation of IEEE 754-1985 infinity values from Wikipedia:

    ......snip......

    The biased-exponent field is filled with all 1 bits to indicate either infinity or an invalid result of a computation.

    Positive and negative infinity

    Positive and negative infinity are represented thus:

     sign = 0 for positive infinity, 1 for negative infinity.
     biased exponent = all 1 bits.
     fraction = all 0 bits.
    

    ......snip......

    Assertions

    The following example will either work as expected, or cause a compile time error in case the target platform does not support IEEE 754 floats.

    #include <cstdlib>
    #include <cmath>
    #include <cassert>
    #include <limits>
    
    int main(void)
    {
        //Asserts floating point compatibility at compile time
        static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 required");
    
        //C99
        float negative_infinity1 = -INFINITY;
        float negative_infinity2 = -1 * INFINITY;
    
        float negative_infinity3 = -std::numeric_limits<float>::infinity();
        float negative_infinity4 = -1 * std::numeric_limits<float>::infinity();
    
        assert(std::isinf(negative_infinity1) && negative_infinity1 < std::numeric_limits<float>::lowest());
        assert(std::isinf(negative_infinity2) && negative_infinity2 < std::numeric_limits<float>::lowest());
        assert(std::isinf(negative_infinity3) && negative_infinity3 < std::numeric_limits<float>::lowest());
        assert(std::isinf(negative_infinity4) && negative_infinity4 < std::numeric_limits<float>::lowest());
    
        return EXIT_SUCCESS;
    }
    
    0 讨论(0)
  • 2020-12-24 07:29

    I don't know what compiler your using, but you can use -std::numeric_limits<double>::infinity() on gcc and MinGw see Infinity-and-NaN. Also I ran the following code on MSVC and it returned true:

    double infinity(std::numeric_limits<double>::infinity());
    double neg_infinity(-std::numeric_limits<double>::infinity());
    double lowest(std::numeric_limits<double>::lowest());
    
    bool lower_than_lowest(neg_infinity < lowest);
    std::cout << "lower_than_lowest: " << lower_than_lowest << std::endl;
    

    However, it maybe worthwhile considering using lowest in your application instead of negative infinity as it's likely to result in a more portable solution.

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