添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams 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, 2010 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 1, 2022 at 1:36

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;
                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 4, 2017 at 20:49
                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, 2017 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, 2022 at 2: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, 2022 at 2: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<object> 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<T>/Optional<T> becomes so great. You're always guaranteed to have a value available outside the codomain.
– Alexander
                Oct 12, 2022 at 2:49
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.