How does language [X] handle indeterminate forms like 0^0?
https://softwareengineering.stackexchange.com/questions/9788
-
16-10-2019 - |
문제
As x goes to 0, the limits of x^0 and 0^x go to 1 and 0, respectively. Some say that the best value for 0^0 depends on context, others say that the value of 0^0 should be 1. I'm interested in knowing what your language implementers say. There doesn't seem to be a consensus. For example, in ActiveState Perl 5.12.0:
C:\>perl -e "print 0**0" 1
While in Mathematica 6.0:
In[1]:= 0^0 During evaluation of In[1]:= Power::indet: Indeterminate expression 0^0 encountered. >> Out[1]= Indeterminate
I'm also interested in knowing your opinion on the matter: What should 0^0 be? And also whether you have ever introduced a bug into a program you were writing (or had a hard time debugging a program) because of the way your language handles indeterminate forms?
해결책
According to this Wikipedia article,
"Most programming language with a power function are implemented using the IEEE pow function and therefore evaluate 0^0 as 1. The later C and C++ standards describe this as the normative behavior. The Java standard mandates this behavior. The .NET Framework method System.Math.Pow also treats 0^0 as 1."
다른 팁
Python:
>>> for t in [int, float, complex, fractions.Fraction, decimal.Decimal]:
... print(t, t(0)**0)
...
<type 'int'> 1
<type 'float'> 1.0
<type 'complex'> (1+0j)
<class 'fractions.Fraction'> 1
Traceback (most recent call last):
...
decimal.InvalidOperation: 0 ** 0
Google says:
0^0 = 1
Delphi's got 2 float values: NaN
and Infinity
. That get returned in case of weirdness.
But it doesn't have a built in exponentiation function.
^
is for pointers, as in "hey you up there, I'm pointing at you!"