Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Math.Pow(-78.0921, -64.6294) return NaN?

Tags:

c#

I have two values: X = -78.0921 and Y = -64.6294. Now, when I want to compute Math.Pow(X, Y) it returns NaN. What should I do? How can I solve this problem?

How should I calculate this power? Is there any other function that can calculate this?...or maybe it is not defined mathematically ?

like image 801
Student Avatar asked Nov 28 '22 01:11

Student


1 Answers

You've tried to compute a number that is not real.

By not real I mean, if we tried every single number between the largest number and the smallest number you can think of, none of those numbers is the solution to -78.0921 to the power of -64.6294.

In fact, no real number is the solution to -1 to the power of 0.5, or the square root of -1, and in general for a^b if a is negative and b is non-integer, the result is not real.

The inability to express such a useful result in real numbers lead to the invention of complex numbers. We say sqrt(-1) = i, the imaginary unit, in the complex number system - all complex numbers have a real component and an imaginary component, expressed as a + b*i.

In general, no negative number to a fractional power produces a real result, as it will have some component of i in it - the closer to a .5 the power is, the more i, the closer to a .0, the more real, and the path follows a circle between real and imaginary, e.g.

-1^x = cos(pi*x)+i*sin(pi*x)

Read more about complex numbers: http://en.wikipedia.org/wiki/Complex_number


If you wish to work with complex numbers in C#, try http://msdn.microsoft.com/en-us/library/system.numerics.complex.aspx

However, unless complex numbers have some meaning in your problem domain (they are meaningful in many electrical engineering, physics and signal analysis problems, for example) it's possible that your data is wrong or your logic is wrong to be attempting to do such a thing in the first place.

like image 186
Patashu Avatar answered Dec 14 '22 22:12

Patashu