Python Infinity - Any caveats?

前端 未结 5 767
说谎
说谎 2020-12-04 07:35

So Python has positive and negative infinity:

float(\"inf\"), float(\"-inf\")

This just seems like the type of feature that has to have som

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 07:51

    I found a caveat that no one so far has mentioned. I don't know if it will come up often in practical situations, but here it is for the sake of completeness.

    Usually, calculating a number modulo infinity returns itself as a float, but a fraction modulo infinity returns nan (not a number). Here is an example:

    >>> from fractions import Fraction
    >>> from math import inf
    >>> 3 % inf
    3.0
    >>> 3.5 % inf
    3.5
    >>> Fraction('1/3') % inf
    nan
    

    I filed an issue on the Python bug tracker. It can be seen at https://bugs.python.org/issue32968.

    Update: this will be fixed in Python 3.8.

提交回复
热议问题