Setting an int to Infinity in C++

前端 未结 6 1164
终归单人心
终归单人心 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:47

    int is inherently finite; there's no value that satisfies your requirements.

    If you're willing to change the type of b, though, you can do this with operator overrides:

    class infinitytype {};
    
    template<typename T>
    bool operator>(const T &, const infinitytype &) {
      return false;
    }
    
    template<typename T>
    bool operator<(const T &, const infinitytype &) {
      return true;
    }
    
    bool operator<(const infinitytype &, const infinitytype &) {
      return false;
    }
    
    
    bool operator>(const infinitytype &, const infinitytype &) {
      return false;
    }
    
    // add operator==, operator!=, operator>=, operator<=...
    
    int main() {
      std::cout << ( INT_MAX < infinitytype() ); // true
    }
    
    0 讨论(0)
  • 2020-12-12 10:54

    int min and max values

    Int -2,147,483,648 / 2,147,483,647 Int 64 -9,223,372,036,854,775,808 / 9,223,372,036,854,775,807

    i guess you could set a to equal 9,223,372,036,854,775,807 but it would need to be an int64

    if you always want a to be grater that b why do you need to check it? just set it to be true always

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-12 10:57

    You can also use INT_MAX:

    http://www.cplusplus.com/reference/climits/

    it's equivalent to using numeric_limits.

    0 讨论(0)
  • 2020-12-12 11:08

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

    #include <limits>
    
    // ...
    
    int a = std::numeric_limits<int>::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<double>::infinity();
    
    0 讨论(0)
  • 2020-12-12 11:09

    Integers are finite, so sadly you can't have set it to a true infinity. However you can set it to the max value of an int, this would mean that it would be greater or equal to any other int, ie:

    a>=b
    

    is always true.

    You would do this by

    #include <limits>
    
    //your code here
    
    int a = std::numeric_limits<int>::max();
    
    //go off and lead a happy and productive life
    

    This will normally be equal to 2,147,483,647

    If you really need a true "infinite" value, you would have to use a double or a float. Then you can simply do this

    float a = std::numeric_limits<float>::infinity();
    

    Additional explanations of numeric limits can be found here

    Happy Coding!

    Note: As WTP mentioned, if it is absolutely necessary to have an int that is "infinite" you would have to write a wrapper class for an int and overload the comparison operators, though this is probably not necessary for most projects.

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