We all know that 00 is indeterminate.
But, javascript says that:
Math.pow(0, 0) === 1 // true
The C language definition says (7.12.7.4/2):
A domain error may occur if x is zero and y is zero.
It also says (7.12.1/2):
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; if the integer expression math_errhandling & MATH_ERREXCEPT is nonzero, the ‘‘invalid’’ floating-point exception is raised.
By default, the value of math_errhandling
is MATH_ERRNO
, so check errno
for the value EDOM
.
According to Wikipedia:
In most settings not involving continuity in the exponent, interpreting 00 as 1 simplifies formulas and eliminates the need for special cases in theorems.
There are several possible ways to treat 0**0
with pros and cons to each (see Wikipedia for an extended discussion).
The IEEE 754-2008 floating point standard recommends three different functions:
pow
treats 0**0
as 1
. This is the oldest defined version. If the power is an exact integer the result is the same as for pown
, otherwise the result is as for powr
(except for some exceptional cases).pown
treats 0**0 as 1. The power must be an exact integer. The value is defined for negative bases; e.g., pown(−3,5)
is −243
.powr
treats 0**0 as NaN (Not-a-Number – undefined). The value is also NaN for cases like powr(−3,2)
where the base is less than zero. The value is defined by exp(power'×log(base)).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.
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:
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.
Update C++
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):
[...]A domain error may occur if x is zero and y is zero.[...]
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:
[...]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; [...]
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.
Update Javascript
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:
If y is +0, the result is 1, even if x is NaN.