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) returns nan 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 problem pow(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

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -