Is it possible to express the idea of a number bigger than any other number ($\infty$) in programming languanges?

91 Views Asked by At

I'm studying graphs and algorithms, most of the algorithms we're using (such as Prim's algorithm), have the need of a table with a symbol $\infty$. Which in some contexts mean that it is the biggest number of all - in my programming classes, whenever we needed such symbol, we've been taught to use an arbitrary large number, which wasn't general (given that we could always input values biggest than the assigned number) and the idea of a symbol that means biggest than all of them is a good thing. Is it possible to express this idea in programming languanges?

2

There are 2 best solutions below

0
On BEST ANSWER

Of course this is possible. Pick one:

  • Do not use any arbitrarily large number, but literally the largest number that the numerical type can express. In C#, for example, there is int.MaxValue and float.MaxValue.
  • IEEE floating point numbers already have special values for $\infty$ and $-\infty$.
  • Use a value that is invalid in the given context, such as $-1$ if all valid numbers are $>0$.
  • If your language supports it, use a nullable variable and encode your special value with null.
  • Create your own numerical type as a struct/class/record that contains two fields: the numerical value and a boolean flag for the special value.
0
On