Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what contexts do programming languages make real use of an Infinity value?

People also ask

What is infinity in programming?

As of 2020, there is no such way to represent infinity as an integer in any programming language so far. But in python, as it is a dynamic language, float values can be used to represent an infinite integer. One can use float('inf') as an integer to represent it as infinity.

How do you use infinity in Python?

In Python, there is no way or method to represent infinity as an integer. This matches the fundamental characteristic of many other popular programming languages. But due to python being dynamically typed language, you can use float(inf) as an integer to represent it as infinity.

How do you find the infinity value in Python?

The math. isinf() method checks whether a number is infinite or not. This method returns True if the specified number is a positive or negative infinity, otherwise it returns False.

What does INF mean in Python?

In Python, the float type (floating point numbers) includes inf , which represents infinity.


Dijkstra's Algorithm typically assigns infinity as the initial edge weights in a graph. This doesn't have to be "infinity", just some arbitrarily constant but in java I typically use Double.Infinity. I assume ruby could be used similarly.


Off the top of the head, it can be useful as an initial value when searching for a minimum value.

For example:

min = float('inf')

for x in somelist:
  if x<min: 
    min=x

Which I prefer to setting min initially to the first value of somelist

Of course, in Python, you should just use the min() built-in function in most cases.


There seems to be an implied "Why does this functionality even exist?" in your question. And the reason is that Ruby and Python are just giving access to the full range of values that one can specify in floating point form as specified by IEEE.

This page seems to describe it well: http://steve.hollasch.net/cgindex/coding/ieeefloat.html

As a result, you can also have NaN (Not-a-number) values and -0.0, while you may not immediately have real-world uses for those either.


In some physics calculations you can normalize irregularities (ie, infinite numbers) of the same order with each other, canceling them both and allowing a approximate result to come through.

When you deal with limits, calculations like (infinity / infinity) -> approaching a finite a number could be achieved. It's useful for the language to have the ability to overwrite the regular divide-by-zero error.