c# - Calculate power of a negative number -
i relatively new c# , since math.pow(x,y) returns nan negative number(read: not negative power), want know how calculate result more efficiently. doing write basic loop, want know if can implemented in shorter way (using linq maybe)?
for(int i=0;i<power;i++) { result = result * num; }
math.pow(x,y)
returnsnan
negative number.
that's because arbitrary exponentiation not defined negative base:
http://en.wikipedia.org/wiki/exponentiation
when base b positive real number, b n can defined real , complex exponents n via exponential function
now, exponentiation of negative numbers real result defined if power integer, in case, pow
not calculate you. should is:
- suppose want know
pow(-2, 5)
- give
pow
problempow(2, 5)
instead. - look @ exponent; if number you've got right answer. if odd number, make answer negative.
so in case, pow(2, 5)
returns 32. exponent 5 odd, make -32, , you're done.
Comments
Post a Comment