0

I just found out that when entering a negative x and a decimal y, Math.Pow() returns the not-defined value as result, which is wrong I guess. Calculating this in other programs, even like the windows Calculator works with a correct result. Also this case is not mentioned in the documentation.

Target Framework is 4.

Can anyone explain this?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Ravior
  • 561
  • 2
  • 9
  • 30
  • 2
    It will be complex number: `(-2)^1.1 = -2.0386 - 0.6624i`. – Ulugbek Umirov May 09 '14 at 07:40
  • How is this not covered in the documentation? "x < 0 but not **NegativeInfinity**; y is not an integer, **NegativeInfinity**, or **PositiveInfinity**." - Return value is **NaN** – Damien_The_Unbeliever May 09 '14 at 07:42
  • 1
    What do you mean that Windows Calculator works? It says "Invalid Input" for me. – Lasse V. Karlsen May 09 '14 at 07:42
  • Remember that `X^1.1 == (X^(11/10)) == (X^(1/10))^11` so if X is negative you will be taking the 10th root of it, which yields a complex number as Ulugbek states. – Matthew Watson May 09 '14 at 07:48
  • 1
    Remember parenthesis in situations where infix operator `^` is used, like Windows Calculator. So `(-2)^1.1`, not just `- 2^1.1`. Raising a negative quantity to a non-integral power requires choosing a branch. There are different conventions. As @LasseV.Karlsen said, Windows Calculator won't do `(-2)^1.1`. It will do `(-2)^1.2`, however, and it calculates it as if `(-2)^1.2 = (-2)^(6/5) = ((-2)^6)^(1/5) = (+64)^(1/5)`. So it gets a real (not imaginary) result for that. Some other software chooses another branch and yields a complex number for `(-2)^1.2`. – Jeppe Stig Nielsen May 09 '14 at 08:08

2 Answers2

5

The result is going to be complex number, so you have to use Complex class from System.Numerics namespace.

Complex n = new Complex(-2, 0);
Complex result = Complex.Pow(n, 1.1);

In case if result is real number (integer power), then you can use Math.Pow.

As @JeppeStigNielsen mentioned, the conversion from int/double to Complex is implicit, so the code can be shortened to:

Complex result = Complex.Pow(-2, 1.1);
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
  • 1
    Upvoted for mentioning `Complex.Pow`. More simply, you can write `Complex.Pow(-2, 1.1)`, the conversion from real `double` (or actually real `int` in this case) to `Complex` is implicit. Note that complex exponentiation requires choosing a branch. For some inputs, like `Complex.Pow(-2, 1.2)`, the result differs from Windows Calculator, `(-2)^1.2`. – Jeppe Stig Nielsen May 09 '14 at 08:23
1

Also this case is not mentioned in the documentation

You sure? From it's documentation;

Parameters

x < 0 but not NegativeInfinity; y is not an integer, NegativeInfinity, or PositiveInfinity.

Return value

NaN

I'm not sure which OS you tried but it doesn't work in calc.exe (Win7 - 64bit) says Invalid Input.

As Ulugbek mentioned, taking 1.1 power of a negative value creates a complex number. Because (-2)1.1 = (-2)11/10 = (-2)1/1011 and getting 10 times rooth of -2 returns a complex number.

Since Math.Pow takes and returns double values, this doesn't fit with requirements. You can use Complex class from System.Numerics.

http://www.wolframalpha.com/input/?i=-2^1.1

enter image description here

Further reading: How is Math.Pow() implemented in .NET Framework?

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364