Understanding numbers in ruby ​​reasoning

from http://www.ruby-doc.org/core/classes/Rational.html

Rational(10) / 3 #=> (10/3) Rational(10) / 3.0 #=> 3.3333333333333335 Rational(-8) ** Rational(1, 3) #=> (1.0000000000000002+1.7320508075688772i) 

I understand the first two, but not the last. Note that Rational(8) ** Rational(1, 3) works just fine and there is no floating point context to stir up the waters. Can someone explain this to me, and how to get -2, how should I get?

edit : note that I do not mean how to get -2 only in this instance, but how to work with rational methods as a whole discovers that a complex numerical representation is necessary and context switches accordingly.

edit # 2 (thanks to pst and Mat): as per pst example:

 >> (Rational(-8) ** Rational(1,3)) ** Rational(3) => (-8.0+3.1086244689504383e-15i) 

This is a great example of why I still have to return a real answer (I would be much more forgiving if it were a Complex class that discards complex numbers, but this is a Rational class - I dare say it should behave rationally). The mat answer illustrates why one could use a general solution, such as a monkey patch for Rationals (or a complex class, etc.) or a wrapper class: since otherwise I cannot lazily encode my path using relative indicators of basic mathematical operations.

I think that I see the roots of the answer in Mata’s answer, but I don’t immediately understand how to convert it into a monkey patch or a wrapper class that will behave correctly in the general code.

+4
source share
3 answers

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.

+3
source

The easiest way is to write your own root function to find the real root that checks the sign if the root you want is odd and then restores it. Essentially:

 (-8) ** (1/3) = -(8 ** 1/3) 

I had a similar problem in another language (see here ), and one of the members there gave a pretty good explanation of why this is so.

+2
source

(1.0000000000000002+1.7320508075688772i) probably represents 1+\sqrt{3}i , and this (one of) the correct answer (s):

 (1+\sqrt{3}i)^3 = -8 

What is wrong with him?

Did you forget the fundamental theorem of algebra ? The root is not unique. But since the return value in this case must be a number, there must be some criteria (which can flow from the ruby ​​internal algorithm) that chose this particular one. If you want to choose a different root, then you need to explicitly part of it yourself. One way to do this is

 -Rational(8) ** Rational(1, 3) # => -2.0 

But note that this still gives you a float. This is because as soon as you introduce fractional power, it no longer closes within a rational number. If you still need rational answer 2 , you probably have to use software that can at least solve polynomial equations.

+2
source

All Articles