I saw float('inf') when I was doing my homework, so I recorded this usage as
usage
float("INF") represents positive infinity;
float("-INF") represents negative infinity;
Specific usage:
- Using INF to perform arithmetic operations such as addition and multiplication will still result in inf:
In [1]: 1 + float('INF')
Out[1]: inf
In [2]: 88 * float('INF')
Out[2]: inf
Note: Using INF * 0 here will result in not-a-number(nan)
- Positive infinity * 0 gets nan
In [3]: 0 * float('INF')
Out[3]: nan
除了 INF 外的其他数除以 INF ,会得到0:
In [4]: float('INF') / float('INF')
Out[4]: nan
In [5]: 2020 / float('INF')
Out[5]: 0.0
In [6]: 12138 / float('INF')
Out[6]: 0.0
- Any other number divided by INF will result in INF, because INF represents positive infinity
In [7]: float('INF') / 1203434
Out[7]: inf
- If INF involves < and > inequalities:
just remember that all numbers are greater than -inf and all numbers are less than +inf.
In [8]: 12138 < float('INF')
Out[8]: True
In [9]: 12138 < float('-INF')
Out[9]: False
Similar usage in other languages:
- In C and C++, you can use the INFINITY macro to represent positive infinity. For example, double x = INFINITY; sets variable x to positive infinity.
- In Java, you can use Double.POSITIVE_INFINITY to represent positive infinity. For example, double x = Double.POSITIVE_INFINITY; sets the variable x to positive infinity.
- In JavaScript, you can use Infinity to represent positive infinity. For example, var x = Infinity; sets variable x to positive infinity.
In Ruby, you can use Float::INFINITY to represent positive infinity. For example, x = Float::INFINITY sets the variable x to positive infinity.