Best way to check if double equals negative infinity in C++

前端 未结 2 621
孤城傲影
孤城傲影 2021-01-05 18:48

I found this: http://en.cppreference.com/w/cpp/numeric/math/isinf but it appears to check for either positive or negative infinity. I just want to check if a value is equal

2条回答
  •  既然无缘
    2021-01-05 19:25

    How about the obvious and explicit?

    To check that a double x is negative infinity, check

    x == -std::numeric_limits::infinity()
    

    If x is some other floating-point type, change double as appropriate.

    std::numeric_limits is defined in the standard header . Don't forget to add it to your #include list.

提交回复
热议问题