+ 2
Is there a bug in Ruby or what is it?
How to calculate the cubic root of a negative number and get the correct answer? A more general question concerns the all odd powers of negative numbers. https://code.sololearn.com/c4pGf2PQ73Wd/?ref=app
18 Answers
+ 10
Gordie My system crashed while gathering this so it's a little late, but I had traced what Ruby's producing:
First, -8.coerce(1.0/3) indicates the common class is Float.
Then, Float's power function sees a negative value + precision-losing power, so converts to complex [link 1]
1252: rb_float_pow()
1266: if (dx < 0 && dy != round(dy))
return num_funcall1(rb_complex_raw1(x), idPow, y);
(trace... rb_complex_raw1 (intern.h)
-> rb_complex_raw(x, INT2FIX(0)) # complex.c
-> nucomp_s_new_internal(rb_cComplex, x, y)
267: simply sets the real and imag components to x,y
...trace)
Then the exponent function in complex.c [link 2]
851: nucomp_expt(self, other)
866: if other is complex...
# calculate nr and ntheta
878: return complex.polar(Complex, nr, ntheta)
[or, edit]
914: if other is real...
923: return complex.polar(these defines come from [3])
I'm including this because you might want to see how the function calculates the final result (it's short).
[1] https://github.com/ruby/ruby/blob/trunk/numeric.c
[2] https://github.com/ruby/ruby/blob/trunk/complex.c
[3] https://github.com/ruby/date/blob/master/ext/date/date_strptime.c
+ 3
It was more for your benefit. It's easier for people to answer if it's a fresh question ☺ You can still link to the old question for reference.
+ 2
Gordie
Thank you very much!
+ 2
Gordie
Thank you very much!
+ 2
You need to create another question, rather than post a question in a question.
+ 2
Ok, no problems. I just thought that these topics are closely related.
+ 2
Thank you!
+ 1
Someone else had this problem too:
http://compsci.ca/v3/viewtopic.php?t=15472
+ 1
Thank you for information!
+ 1
Is it printing out imaginary numbers as a cube root? Where i^2 = -1
+ 1
Check excepted answer of cube root of -8
https://math.stackexchange.com/questions/25528/cubic-root-of-negative-numbers
Ruby is giving one of the imaginary roots, so not technically a bug, however most people probably want -2 in this situation.
+ 1
If only you'd been here earlier Gordie. Fantastic answer ☺
+ 1
I want to expand my previous question and ask about calculating any fractional powers of negative numbers? I mean only real roots. What rules or techniques are used to solve this problem?
Quite another question: is there a function in the Math library for calculating a fractional factorial?
Why do I continue to ask these questions because I have written the app (https://code.sololearn.com/c48HHyY3UZL2) for calculating large and nested arithmetic expressions and it has two problems described above.
0
Yes, imaginary numbers are printed, but they are incorrect. In my code this is shown.
0
Is it possible (in Ruby) to represent a decimal fraction in the form of a simple fraction? That is, for example, the number 0.2 to represent as 1/5? And if so, how to do it?