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

后端 未结 5 1800
滥情空心
滥情空心 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 16:58

    float('inf') can be used in the comparison, thus making the code simpler and clear. For instance, in merge sort, a float('inf') can be added to the end of subarrays as a sentinel value. Don't confuse with the usage of infinity in maths, after all, programming is not all about maths.

提交回复
热议问题