Technical skill practice
Python Infinity: Use a Sentinel and Distinguish NaN
In Python, float("inf") produces positive floating-point infinity, and float("-inf") produces negative infinity. They can act as comparison sentinels, but they are not ordinary very large integers. NaN is different again: use math.isnan to identify it rather than assuming all nonfinite values behave alike.
Start with a small, explicit task
Find a minimum among three finite synthetic route costs using infinity as the initial value. Check the result with and without any candidates. Then inspect infinity minus itself to expose an invalid numeric result that cannot be treated as a usable bound.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_infinity.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
4
empty remains infinite: True
inf - inf is NaN: True
import math
def lowest(costs):
best = float("inf")
for cost in costs:
if cost < best:
best = cost
return best
infinity = float("inf")
assert infinity > 10**100
assert float("-inf") < -10**100
assert lowest([9, 4, 12]) == 4
assert math.isinf(lowest([]))
invalid = infinity - infinity
assert math.isnan(invalid)
assert invalid != invalid
print(lowest([9, 4, 12]))
print("empty remains infinite:", math.isinf(lowest([])))
print("inf - inf is NaN:", math.isnan(invalid))
Explain the result
Each finite candidate is compared with the current best, so 4 becomes the minimum. With no candidates, the sentinel remains unchanged. A caller should interpret that explicitly as no finite result under this function’s contract rather than printing infinity as if it were a measured route cost.
Infinity compares above the large finite integer used in the assertion. Subtracting infinity from itself instead yields NaN, and the self-inequality check demonstrates a distinctive NaN property. The fixture restricts candidates to finite values; accepting NaNs as input would require an additional policy because ordinary comparisons would no longer provide a reliable minimum rule.
Avoid these mistakes
Do not use equality with NaN as its detection method. Check isfinite, isinf or isnan according to the state you need to distinguish. If a business domain permits only finite costs, reject nonfinite inputs before an optimization or report silently carries them forward.
Questions, answered
Is infinity just a very large int?
No. This example creates a special floating-point value and checks it with math.isinf.
What happens with no candidate costs?
The initial infinity remains. The caller must handle that explicit no-result state.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.