The answer is not -2 : the answer is 2i . Oops, math fails. In any case, parts of the rest are still relevant in some way. See Matda Commentary and Savva's Answer.
This does not explain why the output is not just pretty 2i (or (0+2i) ), I suspect that this is due only to internal rounding errors (it does not try to replace matlab). See Savva's answer on how to “understand” the result of a returned complex number.
See Imaginary Numbers and Complex Numbers for the notation used as a result.
Consider:
>> (Rational(-8) ** Rational(1,3)) ** Rational(3) => (-8.0+3.1086244689504383e-15i)
What is not far off!
but
>> (Rational(8) ** Rational(1,3)) ** Rational(3) => 8.0
Is "perfect." Hope someone can handle it.
Happy coding.
Change, okay, this is what happens: The result of Rational ** Rational not Rational.
When the result is real Rational ** Rational -> Float , but when the result contains an imaginary component, then Rational ** Ration -> Complex .
In the “ideal” case, we simply remained within the accuracy of the Float (at least enough to get a “good result”). In the case, resulting in the Complex object, due to the fact that it is stored in the real + imaginary part, and then the math is performed on the data later, the relative accuracy of the Float from the combined components was not good enough.
user166390
source share