Setting an int to Infinity in C++

前端 未结 6 1189
终归单人心
终归单人心 2020-12-12 10:28

I have an int a that needs to be equal to \"infinity\". This means that if

int b = anyValue;

a>b is always tru

6条回答
  •  忘掉有多难
    2020-12-12 11:08

    Integers are inherently finite. The closest you can get is by setting a to int's maximum value:

    #include 
    
    // ...
    
    int a = std::numeric_limits::max();
    

    Which would be 2^31 - 1 (or 2 147 483 647) if int is 32 bits wide on your implementation.

    If you really need infinity, use a floating point number type, like float or double. You can then get infinity with:

    double a = std::numeric_limits::infinity();
    

提交回复
热议问题