It acts as an unbounded upper value for comparison. This is useful for finding lowest values for something. for example, calculating path route costs when traversing trees.
e.g. Finding the "cheapest" path in a list of options:
>>> lowest_path_cost = float('inf')
>>> # pretend that these were calculated using some worthwhile algorithm
>>> path_costs = [1, 100, 2000000000000, 50]
>>> for path in path_costs:
... if path < lowest_path_cost:
... lowest_path_cost = path
...
>>> lowest_path_cost
1
if you didn't have float('Inf') available to you, what value would you use for the initial lowest_path_cost? Would 9999999 be enough -- float('Inf') removes this guesswork.
It acts as an unbounded upper value for comparison. This is useful for finding lowest values for something. for example, calculating path route costs when traversing trees.
e.g. Finding the "cheapest" path in a list of options:
>>> lowest_path_cost = float('inf')
>>> # pretend that these were calculated using some worthwhile algorithm
>>> path_costs = [1, 100, 2000000000000, 50]
>>> for path in path_costs:
... if path < lowest_path_cost:
... lowest_path_cost = path
...
>>> lowest_path_cost
1
if you didn't have float('Inf') available to you, what value would you use for the initial lowest_path_cost? Would 9999999 be enough -- float('Inf') removes this guesswork.
From the Python 2.6 changelog:
Many floating-point features were added. The float() function will now turn the string nan into an IEEE 754 Not A Number value, and +inf and -inf into positive or negative infinity. This works on any platform with IEEE 754 semantics. (Contributed by Christian Heimes; issue 1635.)
Also refer this: Working with Infinity and NaNs
Number theory discussion regarding floats, ints and NaN/Inf
What does stating a variable as float('inf') do?
How do I type a floating point infinity literal in python - Stack Overflow
What's different between Decimal and Floating point numbers in Python?
I don't know why float('inf') was chosen as the way the language accesses infinity since this is using a magic string. Why couldn't it just be float.inf? That way magic string is avoided. Feels too basic of a best practice to be simply passed up. Anyone know the reason behind this?
I was doing the following leetcode problem: 121. Best Time to Buy and Sell Stock
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
My code was as follows:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profits = []
min_price = 0
maxprofit = 0
for p in prices:
min_price = min(p, min_price)
max_price = max(p, min_price)
profit = max_price - min_price
profits.append(profit)
maxprofit = max(profits)
return maxprofitHowever it was spitting out the wrong answer on the leetcode website.
So I was wondering why I was getting it wrong so I did a bit of searching and decided to change min_price = 0 to min_price = float('inf').
After that change, the result was accepted by LeetCode. Can someone please tell me why setting min_price to zero is incorrect and what exactly float('inf') does and means in this context?
In python 2.6 it is portable if the CPU supports it
The float() function will now turn the string nan into an IEEE 754 Not A Number value, and +inf and -inf into positive or negative infinity. This works on any platform with IEEE 754 semantics.
float('inf') is non portable as in not portable back to Python 2.5 when the string output varies between platforms. From 2.6 and onwards float('inf') is guaranteed to work on IEEE-754-compliance platforms (ref: http://www.python.org/dev/peps/pep-0754/).
(And the recommendation seems to be in the range 1e30000, not just 1e400.)