为什么Math.pow(0,0)=== 1? [英] Why is Math.pow(0, 0) === 1?

查看:401
本文介绍了为什么Math.pow(0,0)=== 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道0 0 是不确定的。

We all know that 00 is indeterminate.

但是 javascript 说:

Math.pow(0, 0) === 1 // true

C ++ 说同样的事情:

pow(0, 0) == 1 // true

为什么?

我知道:

>Math.pow(0.001, 0.001)
0.9931160484209338

但为什么 Math .pow(0,0)抛出没有错误?或者 NaN 可能比 1 更好。

But why does Math.pow(0, 0) throw no errors? Or maybe a NaN would be better than 1.

推荐答案

在C ++中 pow的结果(0,0 ) 结果基本上是实现定义的行为,因为在数学上我们有一个矛盾的情况,其中 N ^ 0 应始终 1 但是 0 ^ N 应始终为 0 N> ; 0 ,所以你不应该在数学上对这个结果有任何期望。这个 Wolfram Alpha 论坛帖子详细介绍了一些。

In C++ The result of pow(0, 0) the result is basically implementation defined behavior since mathematically we have a contradictory situation where N^0 should always be 1 but 0^N should always be 0 for N > 0, so you should have no expectations mathematically as to the result of this either. This Wolfram Alpha forum posts goes into a bit more details.

虽然 pow(0,0)结果 1 对许多人有用作为国际标准编程语言的基本原理-C-C 的申请表涵盖 IEC 60559浮点运算支持的部分:

Although having pow(0,0) result in 1 is useful for many applications as the Rationale for International Standard—Programming Languages—C states in the section covering IEC 60559 floating-point arithmetic support:


通常,C99避免使用NaN结果价值是有用的。 [...] pow(∞,0)和pow(0,0)的结果都是1,因为有些应用程序可以利用这个定义。例如,如果x(p)和y(p)是在p = a时变为零的任何解析函数,则当p接近时,等于exp(y * log(x))的pow(x,y)接近1 a。

Generally, C99 eschews a NaN result where a numerical value is useful. [...] The results of pow(∞,0) and pow(0,0) are both 1, because there are applications that can exploit this definition. For example, if x(p) and y(p) are any analytic functions that become zero at p = a, then pow(x,y), which equals exp(y*log(x)), approaches 1 as p approaches a.

更新C ++

As leemes正确地指出我最初链接到 pow 复杂版本的参考,而非复杂的版本声称它是域错误 草案C ++标准可以追溯到删除C标准以及 7.12.7.4 部分中的 C99 C11 pow函数 2 表示(强调我的):

As leemes correctly pointed out I originally linked to the reference for the complex version of pow while the non-complex version claims it is domain error the draft C++ standard falls back to the draft C standard and both C99 and C11 in section 7.12.7.4 The pow functions paragraph 2 says (emphasis mine):


[...]如果x为零且y为零,可能会出现域错误。[...]

哪个据我所知,这种行为是未指明的行为绕回一段 7.12.1 错误条件的处理说:

which as far as I can tell means this behavior is unspecified behavior Winding back a bit section 7.12.1 Treatment of error conditions says:


[...]域名错误如果输入参数在
以外的域之外,则会发生数学函数的定义。[...]在域错误中,函数返回实现定义的值;如果是整数表达式math_errhandling& MATH_ERRNO非零,整数表达式errno获取值EDOM; [...]

[...]a domain error occurs if an input argument is outside the domain over which the mathematical function is defined.[...] On a domain error, the function returns an implementation-defined value; if the integer expression math_errhandling & MATH_ERRNO is nonzero, the integer expression errno acquires the value EDOM; [...]

因此,如果出现域错误,那么这将是实施已定义的行为但在最新版本的 gcc clang 的值 errno 0 所以这些编译器不是域错误

So if there was a domain error then this would be implementation defined behavior but in both the latest versions of gcc and clang the value of errno is 0 so it is not a domain error for those compilers.

更新Javascript

对于 Javascript ECMAScript®语言规范 15.8 数学对象 15.8.2.13 pow(x,y)在其他条件中说:

For Javascript the ECMAScript® Language Specification in section 15.8 The Math Object under 15.8.2.13 pow (x, y) says amongst other conditions that:


如果y为+0,则结果为1,即使x为NaN。

If y is +0, the result is 1, even if x is NaN.

这篇关于为什么Math.pow(0,0)=== 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆