What is the point of float('inf') in Python?

后端 未结 5 1813
滥情空心
滥情空心 2020-12-02 16:26

Just wondering over here, what is the point of having a variable store an infinite value in a program? Is there any actual use and is there any case where it would be prefer

5条回答
  •  时光说笑
    2020-12-02 17:05

    float('inf')
    

    As stated in answer above, float('inf') is used for setting a variable with an infinitely large value. In simple words, it sets the value as +ve infinty.

    ALTERNATIVELY, we can make use of the following statement,

    import sys
    least_value = sys.maxsize
    

    The sys.maxsize is more commonly used, for setting large value initially. When our aim is to find the least value from given set of values.

    Also, in case if we want to find largest value from given set of values. We can use the following.

    import sys
    greatest_value = -sys.maxsize - 1
    
    # logic for comparing with rest of values
    

    The -sys.maxsize - 1 is used for setting initial value as -ve infinity.

提交回复
热议问题