As google suggests -10=-1. And as I understand pow() function in javascript, python and C should return the same result. But it's not true. Why?
Python:
>>> pow(-1, 0)
1
As google suggests -10=-1. And as I understand pow() function in javascript, python and C should return the same result. But it's not true. Why?
Python:
>>> pow(-1, 0)
1
You forgot the parenthesis!
-1 ^ 0 = -(1 ^ 0) = -(1) = -1
because power operator has higher precedence.
But:
(-1)^0 = 1
Anything to the power of 0 will result to 1.
Remember BEDMASS. Your google example executes Brackets (1^0) which is 1, then you executed Multiplication, negating your expression in the brackets to -1.
(-10) is the same as saying (-1/-1) which is 1.
In division you substract the exponent of the denominator from the exponent of the numerator. For this rule to hold true all number elevated to the power of zero is 1. 51 / 51 = 50 = 1
1/1
? I think you wants to say (-1 ^ 0)
== ( -1 ^ (1 -1))
== -1 ^1 * -1 ^ -1
== -1/ -1
Then Nice answer!
Aug 19, 2013 at 18:10
Please, consider this patterns
...
x^3 = x*x*x
x^2 = x*x
x^1 = x
x^0 = ?????
x^-1 = ?????
...
...
x^3 = 1*x*x*x
x^2 = 1*x*x
x^1 = 1*x
x^0 = 1
x^-1 = 1/x
x^-2 = 1/x/x
x^-3 = 1/x/x/x
...
A simple traditional definition of pow
function means how many times we multiply a number by self.
But in more strict sense pow
function means how many times we multiply or divide the one by a number.
By this reason x^0 = 1
=> 0^0 = (-1)^0 = 1
too.
In a more general case the seed
may be any number.
static double Pow(double x, int pow, double seed = 1d)
{
var value = seed;
if (pow < 0)
for (var i = 0; i > pow; i--) value /= x;
else
for (var i = 0; i < pow; i++) value *= x;
return value;
}
-(1 ^ 0)
0 power of any_number = 1
Math.pow(-1, 0)
(JavaScript) andpow(-1.0, 0)
(C) also return 1.