Is there a bug in Ruby or what is it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

3rd Apr 2018, 10:33 PM
meloman K_Y
meloman K_Y - avatar
15 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
4th Apr 2018, 2:01 AM
Kirk Schafer
Kirk Schafer - avatar
+ 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.
7th Apr 2018, 1:24 AM
Emma
+ 2
Gordie Thank you very much!
3rd Apr 2018, 11:38 PM
meloman K_Y
meloman K_Y - avatar
+ 2
Gordie Thank you very much!
4th Apr 2018, 6:04 PM
meloman K_Y
meloman K_Y - avatar
+ 2
You need to create another question, rather than post a question in a question.
7th Apr 2018, 12:35 AM
Emma
+ 2
Ok, no problems. I just thought that these topics are closely related.
7th Apr 2018, 1:23 AM
meloman K_Y
meloman K_Y - avatar
+ 2
Thank you!
7th Apr 2018, 1:28 AM
meloman K_Y
meloman K_Y - avatar
+ 1
Someone else had this problem too: http://compsci.ca/v3/viewtopic.php?t=15472
3rd Apr 2018, 11:03 PM
Emma
+ 1
Thank you for information!
3rd Apr 2018, 11:14 PM
meloman K_Y
meloman K_Y - avatar
+ 1
Is it printing out imaginary numbers as a cube root? Where i^2 = -1
3rd Apr 2018, 11:14 PM
Emma
+ 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.
3rd Apr 2018, 11:27 PM
Emma
+ 1
If only you'd been here earlier Gordie. Fantastic answer ☺
3rd Apr 2018, 11:31 PM
Emma
+ 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.
7th Apr 2018, 12:18 AM
meloman K_Y
meloman K_Y - avatar
0
Yes, imaginary numbers are printed, but they are incorrect. In my code this is shown.
3rd Apr 2018, 11:25 PM
meloman K_Y
meloman K_Y - avatar
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?
4th Apr 2018, 5:51 PM
meloman K_Y
meloman K_Y - avatar