1

On my TI-84 Plus (Silver Edition), I can enter the following without error: (-1)^(1/3) = -1 I also know that entering some expressions like the following would yield a non-real -imaginary- number like: (-1)^.5

Now, my problem is with C#'s Math object. If I send any fractions like these: {1.667, 109.667, 0.667, 120.667} OR {4/3, 111/3, 2/3, 122/3}, I would get: {NaN, NaN, NaN, NaN}.

Do I have to write a new object MathHelper that checks the rational value and returns an answer according to a limited input switch? Or is there a feature to the Math object I am missing. I can do this on the calculator...

PS, I did not come across any similar questions online yet; so if this is a duplicate, please inform me ;)

[My new views]
Thank you all for your help! I had finished upgrading the "Microsoft.Solver.Foundation.dll" to the 4.0 targeted framework and it turned out that the 'Rational' object seemed to return only -1's and 'Indeterminate'. Then after entering (-1)^(1/2) [nonreal ans] on Google, it dawned on me that I was working with nth-roots!! So, it turned out that I had already managed imaginary numbers in the past in C#, hence having solved my problem:

Any even root 2n of a negative number -m will always equal an imaginary number i. (2n√-m)=i I can't believe I forgot this simple algebra property

TekuConcept
  • 1,264
  • 1
  • 11
  • 21
  • Are you using `double` or `Rational`? Have you tried [`Rational.Power`](http://msdn.microsoft.com/en-us/library/microsoft.solverfoundation.common.rational.power%28v=vs.93%29.aspx)? – nneonneo Sep 27 '12 at 00:20
  • No, that's interesting - never heard of it before in C#. Let me take a look... Um, is it a feature that only come with the paid version or do I need to find it online? – TekuConcept Sep 27 '12 at 00:29
  • 2
    There isn't a _free_ or _paid_ version of C# or the .NET Framework, there's only a free or paid version of Visual Studio. – Dave Zych Sep 27 '12 at 01:33
  • @nneonneo I found the SolverFoundation online - no wonder I haven't yet heard of it. Now, I need to make it mesh with my 4.0 assemblies. @ DaveZych Gotcha; Microsoft tends to make things a little confusing sometimes - I'll either misread or misinterpret their highlights sometimes... – TekuConcept Sep 27 '12 at 01:38
  • 1
    of course, 0.667 != 2/3, and neither value can be exactly represented by a double. – phoog Sep 27 '12 at 07:01
  • @phoog 0.667 is a formated value: `String.Format("{0:00.000}", 2.0/3.0)'` in other words, this is the number I see in the output and not the debugger. – TekuConcept Sep 27 '12 at 16:38
  • @ChristopherWalker yes, of course. But my point holds if you use the in-memory binary format of the number. You have more decimal places of precision (or, rather you have many binary places of precision), but you still cannot exactly represent 2/3 with a double. Rather, you're representing something like 187649984473770 / 281474976710655. This is much closer to 2/3 than 667/1000 is, but it's still not equal to it. – phoog Sep 28 '12 at 03:11
  • @phoog I see what your saying... – TekuConcept Sep 28 '12 at 05:41

2 Answers2

0

You will have to write your own Math helper to do functions like this (at least for Math.Pow). EDIT: Or you can use the Rational library like mentioned in the comments.

According to the documentation:

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

Result: NaN

Review the docs here: http://msdn.microsoft.com/en-us/library/system.math.pow.aspx

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
  • I see in the docs it says: `{x = -1; y = NegativeInfinity or PositiveInfinity.} result = NaN` but my 'y' is definitely _not_ infinity... – TekuConcept Sep 27 '12 at 05:50
  • @ChristoperWalker ... which means that this row in the table does not apply. The one that *does* apply is the one Dave Zych cites. – phoog Sep 27 '12 at 07:09
  • @ChristopherWalker - like phoog states, the one I referenced is the one that applies to you. Your x is less than 0, and your y is not an integer. – Dave Zych Sep 27 '12 at 14:11
  • Now, that I look over it again, I see what you mean. I guess I should continue upgrading SolverFoundation to Target Framework: 4.0... unless there is already one up to date? – TekuConcept Sep 27 '12 at 16:56
0

In C#, (-1)^R isn't defined for non-integer values of R. If you try to calculate (-1)^(1/3), C# will calculate 1/3 first, resulting in a non-integer floating point number for R.

Solution: Use the mathematical identity:

  a^(x/y) = a^x * a^(1/y)  x,y integers

For negative a, use:

 a^1/y = -(|a|^1/y)  // only works if y is odd

Or put together:

if (a < 0) {
  if (y % 2 = 1) {
    result = a^x * -1 * (-a)^(1.0/y);   // Replace ^ with the correct C# call.
  } else {
     // NaN
  }
} else {
  result = a^(x/y);
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • I tested this out and it only returned 1's for all powers. I then reviewed over the math and noticed that your ((-a)^(1.0/y)) where a = -1 would always yield a 1. I fixed this by eliminating both this and -1. – TekuConcept Sep 28 '12 at 14:43