Setting an int to Infinity in C++

前端 未结 6 1178
终归单人心
终归单人心 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 10:56

    This is a message for me in the future:

    Just use: (unsigned)!((int)0)

    It creates the largest possible number in any machine by assigning all bits to 1s (ones) and then casts it to unsigned

    Even better

    #define INF (unsigned)!((int)0)
    

    And then just use INF in your code

提交回复
热议问题