gpt4 book ai didi

c# - 我们什么时候需要使用 float.PositiveInfinity 和 float.NegativeInfinity?

转载 作者:太空狗 更新时间:2023-10-29 17:42:36 28 4
gpt4 key购买 nike

我们什么时候需要使用 Infinity 值,请添加一个真实世界的样本(如果可用)。

最佳答案

例如,负无穷大是空列表的自然最大值。有了这个,你有:max(l1 + l2) = max(max(l1), max(l2)),其中 l1l2 是任意列表,可能为空。

这一原则在现实世界中的应用:

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;
}

关于c# - 我们什么时候需要使用 float.PositiveInfinity 和 float.NegativeInfinity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4262033/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com