11

When do we need to use the Infinity values, kindly add a real-world sample if available.

Homam
  • 23,263
  • 32
  • 111
  • 187
  • Is your question when do you need to use them or when do we need to use them? Only you can answer the former so I can only presume you mean the latter! – David Heffernan Nov 23 '10 at 23:25
  • I often use infinity values when I want to know a non-null variable is set eg when I have an `int` param I will default it to negative infinity, this way if the value is other than negativeInfinity then I know it was purposefully set. – Jacksonkr Mar 01 '22 at 01:36

2 Answers2

11

For example, negative infinity is a natural maximum value of an empty list. With this, you have: max(l1 + l2) = max(max(l1), max(l2)), where l1 and l2 are arbitrary lists, possibly empty.

A real-world application of this principle:

float Max(IEnumerable<float> list)
{
    // invariant: max contains maximum over the part of the list
    // considered so far
    float max = float.NegativeInfinity;
    foreach (float v in list)
        if (v > max)
            max = v;
    return max;
}
Vlad
  • 35,022
  • 6
  • 77
  • 199
  • I think returning a `float?` that's `null` in the case of empty lists is a much more natural answer than returning `-infinity`. What if my list is a list of distances? Negative distance wouldn't make sense. – Alexander Apr 04 '17 at 20:49
  • 2
    Considering that empty lists might be a very specific extreme case for most application, I'd prefer the above solution instead of always having to take care of `null` values. – miho May 26 '17 at 10:29
  • @Alexander using nulls is no more natural and I would argue c#'s design choices around null comparisons are unnatural. The above code shows that using +/-inf is a sensible initial value for doing max/min searches. Corresponding code with NaNs or nulls would be a lot more unwieldy. In either case validating the result with a .IsFinite vs .HasValue makes little difference. – Paul Childs Oct 12 '22 at 02:32
  • "Corresponding code with NaNs or nulls would be a lot more unwieldy." That's only a consequence of the particular syntax chosen by the language. C# has pretty nice tools for handling `null` (e.g. `??`, `?.`, flow sensitive typing). "In either case validating the result with a .IsFinite vs .HasValue makes little difference." There's a huge difference. Infinity and NaN are among the possible values in the `input` list. That makes it impossible to distinguish "-inf because I called `Max([-inf])` vs -inf because I called Max([])`" from the result value alone. – Alexander Oct 12 '22 at 02:41
  • The value of `null` is that it provides a value outside the codomain of a function that usually returns `float`. I.e. if your call to `Max` returns `null`, you can be certain it's because you gave it an empty list. Of course, `null` itself isn't good enough, because if this function took an `IEnumerable` as an input, `null` no longer works as a good "no result" value (because you can't distinguish `Max([null])` from `Max([])`). This is where the composable nature of `Maybe`/`Optional` becomes so great. You're _always_ guaranteed to have a value available outside the codomain. – Alexander Oct 12 '22 at 02:49
  • @Alexander: `null` cannot be a "natural" choice because, well, it's not a `float`. NaN is not a good choice because it doesn't compare less-or-equal to other floats, and the code assuming NaN is Min([]) would need to special-case NaN value (which is a strong sing of unnnaturalness). Minimum over `IEnumerable` doesn't make much sense because objects are not comparable. In general, min/max make good sense only for fully order sets. – Vlad Oct 12 '22 at 11:53
  • By the way, analysis considers min([]) = +inf [too](https://math.stackexchange.com/q/432295/2977). – Vlad Oct 12 '22 at 12:02
  • 1. "null cannot be a "natural" choice because, well, it's not a float" That's the whole point. It's a unique value that unambiguously signals an empty input over an input containing literally `inf`. 2. "`NaN` is not a good choice" Agree, but for a different reason. `NaN` isn't a good choice because, again, it's ambiguous if `NaN` on the output means "`NaN` because the input was empty, or `NaN` because your input literally only contained a `NaN`". 3. "Minimum over IEnumerable" true, I meant `IEnumerable`, but the same point stands (about the uncomposable nature of `null`) – Alexander Oct 12 '22 at 13:31
  • @Alexander: 1. But my argument and examples show that nevertheless infinities **are** the natural values for the extrema, because there code _doesn't need any special cases_ when using infinities. I don't see any value in having a separate value for signifying extremum of empty sequence. 3. `IComparable`'s are out of luck because there is no clearly accessible "total maximum" = "infinity" (it may even not exist for e. g. strings). Floats are luckier. – Vlad Oct 13 '22 at 12:12
  • @Vlad "Don't see any value in having a separate value for signifying extremum of empty sequence" Counterpoint examples: "Current high score: +inf", "The shortest route to your destination is -inf km", "Your next calendar event is in +inf hours" – Alexander Oct 14 '22 at 13:07
  • @Alexander: perhaps "Current high score: -inf"? But no, this is all about presentation, which is expected to need strange special-casing: UI needs special-casing in many perfectly uniform cases like "1 kilometer remaining, 2 kilometers remaining", etc. I'm speaking about model representation, where I still don't see a value in special casing. – Vlad Oct 14 '22 at 13:16
  • @Vlad The issue is that seeing `float` gives no indication of needing special treatment, unlike `Maybe` or `float?`, which makes it clear (and more importantly, statically-enforceable) that special handling is needed. How often have you seen `nan` and `inf` leak into the end user interfaces of web apps? It's pretty common, because it's really easy to miss. It's a silly mistake, but it can be trivially defined out of existence by applying nullable types or the maybe monad to make it impossible to present edge case values without unwrapping them and handling the non-happy path. – Alexander Oct 14 '22 at 13:22
  • @Alexander: I've seen numerous times "1 file(s) removed" and even worse cases in other languages, but UI problems should never govern the model design. I assume you aren't going to propose having `1` a separate from `int` compiler-enforced type, are you? – Vlad Oct 14 '22 at 14:25
9

PostiveInfinity

This constant is returned when the result of an operation is greater than MaxValue.

NegativeInfinity

This constant is returned when the result of an operation is less than MinValue.

So you would use these constants to verify that your values are out of range for their type.

ChrisF
  • 134,786
  • 31
  • 255
  • 325