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

Chris Lloyd picture Chris Lloyd · Dec 20, 2008 · Viewed 28.3k times · Source

So in Ruby there is a trick to specify infinity:

1.0/0
=> Infinity

I believe in Python you can do something like this

float('inf')

These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use this construct in the real world? Why would using it in a range be better than just using a boolean expression? For instance

(0..1.0/0).include?(number) == (number >= 0) # True for all values of number
=> true

To summarize, what I'm looking for is a real world reason to use Infinity.

EDIT: I'm looking for real world code. It's all well and good to say this is when you "could" use it, when have people actually used it.

Answer

mmcdole picture mmcdole · Dec 20, 2008

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.