gpt4 book ai didi

Why is 0.0 multiplied by +/-Inf NaN?(为什么0.0乘以+/-inf NaN?)

转载 作者:bug小助手 更新时间:2023-10-25 10:05:41 25 4
gpt4 key购买 nike



Why is 0.0 multiplied by +/-Inf NaN but any other finite number multiplied by +/-Inf +/-Inf ?

Check this small program:

为什么0.0乘以+/-inf NaN,而任何其他有限数乘以+/-inf+/-inf?检查这个小程序:


#include <iostream>
#include <limits>

using namespace std;

int main()
{
constexpr double INF = numeric_limits<double>::infinity();
cout << 0.0 * INF << endl;
cout << -5.0 * INF << endl;
}

It prints:

它打印:


-nan(ind)
-inf

更多回答
优秀答案推荐

This is according to the rules in calculus. Any nonzero number (or infinity) times infinity is still infinity (with appropriate signs), and this makes computations coherent.

这是根据微积分中的规则。任何非零数(或无穷大)乘以无穷大仍然是无穷大(带有适当的符号),这使得计算连贯。


On another hand, the indeterminate forms such as 0*∞ or ∞-∞ have no well-defined value and so are considered NaN.

另一方面,像0*∞或∞-∞这样的不确定形式没有明确定义的值,因此被认为是NaN。


Note that assigning infinity to 0*∞ is neither logical nor possible, as you could not choose a sign, and assigning a finite value would be a terrible mistake.

请注意,将无穷大赋值给0*∞既不符合逻辑,也不可能,因为您不能选择符号,并且赋值有限值将是一个可怕的错误。



+/- Infinity * 0 is kind of a paradox.

+/-无穷*0是一种悖论。


It's like an unstoppable object vs an unmoveable wall.

这就像一个不可阻挡的物体对一堵不可移动的墙。


Infinity = a number greater than any assignable quantity.

无穷大=大于任何可分配数量的数字。


Nothing multiplied a number greater than any assignable quantity does not make too much sense. However +/- infinity multiplied by any finite number will just be a greater number beyond what is assignable.

没有什么比任何可分配的量更大的数字乘以了,这没有太大的意义。然而,+/-无穷大乘以任何有限数只会是一个超出可赋值范围的更大数字。


A simple way of thinking about it is:

思考这个问题的一个简单方法是:



  • 0 multiplied by anything is 0

  • Infinity multiplied by anything infinity

  • Therefore 0 * infinity is undefinable/NaN




the rule that everything multiplied by 0.0 should give zero (except NaN)



This alternative suggested by OP has some reasonableness, yet is not the mainstream consensus.

OP提出的这一替代方案有一定的合理性,但并不是主流共识。



Why is 0.0 multiplied by +/-Inf NaN?



More numerical programs found it more useful to return not-a-number than to return 0.0 as suggested by OP. Returning 0.0 would make for a more challenging and oft encountered code 0.0 * ∞ detection.

更多的数值程序发现,像OP建议的那样,返回非数字比返回0.0更有用。返回0.0会使代码0.0*∞检测更具挑战性,也更容易遇到。




The 0.0 * ∞ return value is not clearly 0.0, ∞, or any particular value. There are application specific cases where the return value of 0.0, ∞, NAN, 1.0, ... are the desired result. For OP's case. 0.0 is preferred.

0.0*∞返回值显然不是0.0、∞或任何特定值。在应用程序特定的情况下,返回值为0.0、∞、NaN、1.0、...都是想要的结果。关于特种部队的案子。最好是0.0。


Floating point arithmetic has, for decades, had the desire to handle extreme cases in a consistent and fast manner since the 1960s/1970s. To make fast, it needs a hardware implementation. To make a hardware implementation, a standard is very useful for consistency and garnering wide acceptance. Correcting hardware is very expensive.

自20世纪60/70年代以来,浮点算术几十年来一直渴望以一致和快速的方式处理极端情况。为了更快,它需要一个硬件实现。要实现硬件实现,标准对于一致性和获得广泛接受是非常有用的。改正硬件是非常昂贵的。


Rather than 0.0 * ∞ return 0.0, the standard choose not-a-number to reflect the ambiguity of the product. Too many applications would not want 0.0 * ∞ as 0.0 as for them as it is not a sensible answer, even if for OP it is OK. NAN is easy to detect. In typical applications, NANs are rare.

而不是0.0*∞返回0.0,标准选择NOT-A-NUMBER来反映产品的模糊性。太多的应用程序不希望将0.0*∞作为它们的0.0,因为这不是一个明智的答案,即使对于OP来说也是可以的。NaN很容易被发现。在典型的应用中,NAN很少见。




To achieve OP's goal, form a special multiplication routine.

为了实现OP的目标,形成一个特殊的乘法例程。


// Example
double EvM_mult(double a, double b) {
double product = a * b;
if (isnan(product)) {
if (!isnan(a) && !isnan(b)) {
return 0.0;
// or maybe to handle signed zero multiplication:
return copysign(0.0, a) * copysign(0.0, b);
}
}
return product;
}

更多回答

I would rather have a mathematical reasoning than a metaphor. For me anything multiplied by 0.0 should be 0.0.

我宁愿用数学推理,也不愿用比喻。对我来说,任何乘以0.0的值都应该是0.0。

Anything multiplied by infinity should be infinity - see the problem... Expanded my answer to illustrate this

任何乘以无穷大的东西都应该是无穷大--看看这个问题。扩展了我的答案以说明这一点

This is not an ultimate justification for me because the rule that everything multiplied by 0.0 should give zero (except NaN) and I think this rule should be in place above the previous rule. There must be deeper reasoning with that.

对于我来说,这不是一个最终的理由,因为所有乘以0.0的规则都应该是零(NaN除外),我认为这条规则应该放在前面的规则之上。这其中一定有更深层次的道理。

Why would that be the case? Infinity is not a number but a concept used to represent an ever increasing value. 0 * infinity is NaN by definition.

为什么会是这样呢?无限不是一个数字,而是一个用来代表不断增长的价值的概念。0*无穷大的定义是NaN。

If you're question is on the semantics of why is maths defined as it is better suited for the maths stack exchange: math.stackexchange.com

如果你的问题是关于为什么数学被定义为更适合数学堆栈交换的语义:math.stackexchange.com

For me there is no ambiguity. For me, infinity represents all results that exceed the range of finite numbers. And assuming you could represent them, their value times zero would be zero. To me this definition therefore seems arbitrary.

对我来说,这是没有歧义的。对我来说,无穷大代表所有超出有限数范围的结果。假设你可以代表它们,它们的值乘以零就是零。因此,在我看来,这个定义似乎是武断的。

@EdisonvonMyosotis With " infinity represents all results that exceed the range of finite numbers.", does finite numbers mean the numbers of encodable as a finite floating point value or all mathematically finite numbers?

@EdisonvonMyosotis,无穷大代表所有超出有限数范围的结果。有限数是指可编码为有限浮点值的数,还是所有数学上的有限数?

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