# Math errors

**URL:** <https://rubytalk.org/t/math-errors/56690>\
**Category:** ruby-talk\
**Created:** [18 December 2009 06:45 UTC](https://rubytalk.org/t/math-errors/56690 "2009-12-18T06:45:21Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jzakiya1](https://rubytalk.org/u/jzakiya1)\
**Post date:** [18 December 2009 06:45 UTC](https://rubytalk.org/t/math-errors/56690/1 "2009-12-18T06:45:21Z")

</div>

(-3)\*\*3 =\> -27

(-27)\*\*(1/3.0) give NaN instead of -3

All odd roots (1/3,1/5,1/7 etc) of negative numbers should give  
negative root values, as above, but ruby (in irb) gives NaN (not a  
number), even when I require 'complex' .

Is this considered an error in Ruby?

---

<div class="post-metadata">

**Author:** ![Tanaka\_Akira](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@Tanaka\_Akira](https://rubytalk.org/u/Tanaka_Akira)\
**Post date:** [18 December 2009 07:10 UTC](https://rubytalk.org/t/math-errors/56690/2 "2009-12-18T07:10:47Z")

</div>

Ruby 1.9 has Math.cbrt.

% ruby -e 'p Math.cbrt(-27)'  
-3.0

> **···**
>
> 2009/12/18 jzakiya \<jzakiya@mail.com\>:
> 
> > (-3)\*\*3 =\> -27
> > 
> > (-27)\*\*(1/3.0) give NaN instead of -3
> 
> --  
> Tanaka Akira

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [18 December 2009 08:11 UTC](https://rubytalk.org/t/math-errors/56690/3 "2009-12-18T08:11:32Z")

</div>

jzakiya wrote:

> (-3)\*\*3 =\> -27
> 
> (-27)\*\*(1/3.0) give NaN instead of -3
> 
> All odd roots (1/3,1/5,1/7 etc) of negative numbers should give  
> negative root values, as above, but ruby (in irb) gives NaN (not a  
> number), even when I require 'complex' .
> 
> Is this considered an error in Ruby?

irb(main):001:0\> 1/3.0  
=\> 0.333333333333333

> **···**
>
> --

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [18 December 2009 08:30 UTC](https://rubytalk.org/t/math-errors/56690/4 "2009-12-18T08:30:15Z")

</div>

jzakiya wrote:

> (-3)\*\*3 =\> -27
> 
> (-27)\*\*(1/3.0) give NaN instead of -3
> 
> All odd roots (1/3,1/5,1/7 etc) of negative numbers should give  
> negative root values, as above, but ruby (in irb) gives NaN (not a  
> number), even when I require 'complex' .
> 
> Is this considered an error in Ruby?

irb(main):006:0\> (-27)\*\*(1/3.0)  
=\> NaN  
irb(main):007:0\> -27 \*\* (1/3.0)  
=\> -3.0

irb(main):012:0\> RUBY\_VERSION  
=\> "1.8.7"

> **···**
>
> --

---

<div class="post-metadata">

**Author:** ![Dasson\_Raghav](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dasson\_Raghav](https://rubytalk.org/u/Dasson_Raghav)\
**Post date:** [18 December 2009 09:36 UTC](https://rubytalk.org/t/math-errors/56690/5 "2009-12-18T09:36:28Z")

</div>

Removing the brackets across -27.0 does the trick, though would need to find the logic behind the same  
(-27)\*\*(1/3.0) gives NaN

-27\*\*(1/3.0) gives -3

Regards,  
Raghav

> **···**
>
> -----Original Message-----  
> From: jzakiya [[mailto:jzakiya@mail.com](mailto:jzakiya@mail.com)]  
> Sent: Friday, December 18, 2009 12:15 PM  
> To: ruby-talk ML  
> Subject: Math errors
> 
> (-3)\*\*3 =\> -27
> 
> (-27)\*\*(1/3.0) give NaN instead of -3
> 
> All odd roots (1/3,1/5,1/7 etc) of negative numbers should give  
> negative root values, as above, but ruby (in irb) gives NaN (not a  
> number), even when I require 'complex' .
> 
> Is this considered an error in Ruby?

---

<div class="post-metadata">

**Author:** ![Benoit\_Daloze](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/benoit_daloze/32/1967_2.png) [@Benoit\_Daloze](https://rubytalk.org/u/Benoit_Daloze)\
**Post date:** [18 December 2009 08:00 UTC](https://rubytalk.org/t/math-errors/56690/6 "2009-12-18T08:00:21Z")

</div>

IN 1.9.2 I got:

~ $\> ruby -ve 'p v=Math.cbrt(-27); p v\*\*3'  
ruby 1.9.2dev (2009-12-11 trunk 26067) [x86\_64-darwin10.2.0]  
-3.0  
-27.0

~ $\> ruby -ve 'p v=(-27)\*\*(1/3.0); p v\*\*3'  
(1.5000000000000004+2.598076211353316i)  
(-27.000000000000007+1.2434497875801753e-14i)

~ $\> ruby -ve 'p v=(-27)\*\*(Rational(1,3)); p v\*\*3'  
(1.5+2.5980762113533156i)  
(-26.99999999999999+3.552713678800501e-15i)

Got easy results, but with all different values ...  
So, the first is right, of course.  
The second is a good approximation, for floats ...  
And the last is almost good, while I would expect to be exact (I think it  
should use Math.cbrt in this case)

I would probably interested to use BigDecimal, but it's impossible I think  
to use it for Complex. (And actually it accept only Fixnum exponent)

Another error in the exception:  
TypeError: wrong argument type BigDecimal (expected Fixnum)

But it accept Float, really 😉

> **···**
>
> 2009/12/18 Tanaka Akira \<akr@fsij.org\>
> 
> > 2009/12/18 jzakiya \<jzakiya@mail.com\>:  
> > \> (-3)\*\*3 =\> -27  
> > \>  
> > \> (-27)\*\*(1/3.0) give NaN instead of -3
> > 
> > Ruby 1.9 has Math.cbrt.
> > 
> > % ruby -e 'p Math.cbrt(-27)'  
> > -3.0  
> > --  
> > Tanaka Akira

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [18 December 2009 08:35 UTC](https://rubytalk.org/t/math-errors/56690/7 "2009-12-18T08:35:20Z")

</div>

Hi,

> **···**
>
> In message "Re: Math errors" on Fri, 18 Dec 2009 17:30:15 +0900, "W. James" \<w\_a\_x\_man@yahoo.com\> writes:
> 
> > irb(main):007:0\> -27 \*\* (1/3.0)  
> > =\> -3.0
> 
> It's -(27 \*\* (1/3.0)).
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

---

<div class="post-metadata">

**Author:** ![Dasson\_Raghav](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dasson\_Raghav](https://rubytalk.org/u/Dasson_Raghav)\
**Post date:** [18 December 2009 09:52 UTC](https://rubytalk.org/t/math-errors/56690/8 "2009-12-18T09:52:37Z")

</div>

jzakiya wrote:

> (-3)\*\*3 =\> -27
> 
> (-27)\*\*(1/3.0) give NaN instead of -3
> 
> All odd roots (1/3,1/5,1/7 etc) of negative numbers should give  
> negative root values, as above, but ruby (in irb) gives NaN (not a  
> number), even when I require 'complex' .
> 
> Is this considered an error in Ruby?

I guess, you are right. THIS IS A 'MATH ERROR' in Ruby.

The \*\* method is not able to handle negative numbers raised to Floating points (that are not Integers)

---

<div class="post-metadata">

**Author:** ![jzakiya1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jzakiya1](https://rubytalk.org/u/jzakiya1)\
**Post date:** [18 December 2009 15:55 UTC](https://rubytalk.org/t/math-errors/56690/9 "2009-12-18T15:55:05Z")

</div>

This behavior was shown in ruby ree 1.8.6, 1.8.6, 1.8.7, 1.9.1

-27\*\*1/3.0 =\> -9.0  
-27 \*\* 1/3.0 =\> -9.0  
-27 \*\* (1/3.0) =\> -3.0  
-27\*\*(1/3.0) =\> -3.0  
(-27)\*\*(1/3.0) =\> NaN  
(-27) \*\* (1/3.0) =\> NaN

OK, the first two expression are evaluated as (-27\*\*1)/3 =\> -9  
The second two are correct (what I expected).  
But the last two, WHY??

> **···**
>
> On Dec 18, 4:52 am, "Dasson, Raghav" \<raghav.das...@sap.com\> wrote:
> 
> > jzakiya wrote:  
> > \> (-3)\*\*3 =\> -27
> > 
> > \> (-27)\*\*(1/3.0) give NaN instead of -3
> > 
> > \> All odd roots (1/3,1/5,1/7 etc) of negative numbers should give  
> > \> negative root values, as above, but ruby (in irb) gives NaN (not a  
> > \> number), even when I require 'complex' .
> > 
> > \> Is this considered an error in Ruby?
> > 
> > I guess, you are right. THIS IS A 'MATH ERROR' in Ruby.
> > 
> > The \*\* method is not able to handle negative numbers raised to Floating points (that are not Integers)

---

<div class="post-metadata">

**Author:** ![Unknown](https://avatars.discourse-cdn.com/v4/letter/u/4bbf92/32.png) [@Unknown](https://rubytalk.org/u/Unknown)\
**Post date:** [18 December 2009 18:45 UTC](https://rubytalk.org/t/math-errors/56690/10 "2009-12-18T18:45:04Z")

</div>

Yukihiro Matsumoto wrote:

> Hi,
> 
> \>irb(main):007:0\> -27 \*\* (1/3.0)  
> \>=\> -3.0
> 
> It's -(27 \*\* (1/3.0)).
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;matz.

Thanks for the explanation.

> **···**
>
> > In message "Re: Math errors" \> on Fri, 18 Dec 2009 17:30:15 +0900, "W. James" \> \<w\_a\_x\_man@yahoo.com\> writes:
> 
> --

---

<div class="post-metadata">

**Author:** ![jzakiya1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jzakiya1](https://rubytalk.org/u/jzakiya1)\
**Post date:** [18 December 2009 16:05 UTC](https://rubytalk.org/t/math-errors/56690/11 "2009-12-18T16:05:05Z")

</div>

And just to verify:

(27)\*\*(1/3.0) =\> 3.0

So Ruby is messing up parsing (-27)\*\*(1/3.0)

> **···**
>
> On Dec 18, 10:51 am, jzakiya \<jzak...@mail.com\> wrote:
> 
> > On Dec 18, 4:52 am, "Dasson, Raghav" \<raghav.das...@sap.com\> wrote:
> > 
> > \> jzakiya wrote:  
> > \> \> (-3)\*\*3 =\> -27
> > 
> > \> \> (-27)\*\*(1/3.0) give NaN instead of -3
> > 
> > \> \> All odd roots (1/3,1/5,1/7 etc) of negative numbers should give  
> > \> \> negative root values, as above, but ruby (in irb) gives NaN (not a  
> > \> \> number), even when I require 'complex' .
> > 
> > \> \> Is this considered an error in Ruby?
> > 
> > \> I guess, you are right. THIS IS A 'MATH ERROR' in Ruby.
> > 
> > \> The \*\* method is not able to handle negative numbers raised to Floating points (that are not Integers)
> > 
> > This behavior was shown in ruby ree 1.8.6, 1.8.6, 1.8.7, 1.9.1
> > 
> > -27\*\*1/3.0 =\> -9.0  
> > -27 \*\* 1/3.0 =\> -9.0  
> > -27 \*\* (1/3.0) =\> -3.0  
> > -27\*\*(1/3.0) =\> -3.0  
> > (-27)\*\*(1/3.0) =\> NaN  
> > (-27) \*\* (1/3.0) =\> NaN
> > 
> > OK, the first two expression are evaluated as (-27\*\*1)/3 =\> -9  
> > The second two are correct (what I expected).  
> > But the last two, WHY??

---

<div class="post-metadata">

**Author:** ![Fleck\_Jean-Julien](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Fleck\_Jean-Julien](https://rubytalk.org/u/Fleck_Jean-Julien)\
**Post date:** [18 December 2009 16:05 UTC](https://rubytalk.org/t/math-errors/56690/12 "2009-12-18T16:05:41Z")

</div>

Hello,

> This behavior was shown in ruby ree 1.8.6, 1.8.6, 1.8.7, 1.9.1
> 
> -27\*\*1/3.0 =\> -9.0  
> -27 \*\* 1/3.0 =\> -9.0  
> -27 \*\* (1/3.0) =\> -3.0  
> -27\*\*(1/3.0) =\> -3.0  
> (-27)\*\*(1/3.0) =\> NaN  
> (-27) \*\* (1/3.0) =\> NaN
> 
> OK, the first two expression are evaluated as (-27\*\*1)/3 =\> -9  
> The second two are correct (what I expected).  
> But the last two, WHY??

As Matz said, the second two are evaluated as - (27\*\*(1/3.0)), that is  
you take the cubic root of +27 which is 3 and then distribute the  
minus.  
That way, you take the cubic root of a positive number which does not  
raise any problem.

Cheers,

> **···**
>
> --  
> JJ Fleck  
> PCSI1 Lycée Kléber

---

<div class="post-metadata">

**Author:** ![jzakiya1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jzakiya1](https://rubytalk.org/u/jzakiya1)\
**Post date:** [18 December 2009 16:26 UTC](https://rubytalk.org/t/math-errors/56690/13 "2009-12-18T16:26:11Z")

</div>

That is WRONG, you cannot do that.  
That only works for odd roots of negative numbers.  
The even root of negative numbers are imaginary.

-27\*\*3\*\*-1 =\> -3 \*\*correct  
-27\*\*2\*\*-1 =\> -5.19615242270663 \*\*WRONG, its 5.196152i

> **···**
>
> On Dec 18, 11:05 am, Fleck Jean-Julien \<jeanjulien.fl...@gmail.com\> wrote:
> 
> > Hello,
> > 
> > \> This behavior was shown in ruby ree 1.8.6, 1.8.6, 1.8.7, 1.9.1
> > 
> > \> -27\*\*1/3.0 =\> -9.0  
> > \> -27 \*\* 1/3.0 =\> -9.0  
> > \> -27 \*\* (1/3.0) =\> -3.0  
> > \> -27\*\*(1/3.0) =\> -3.0  
> > \> (-27)\*\*(1/3.0) =\> NaN  
> > \> (-27) \*\* (1/3.0) =\> NaN
> > 
> > \> OK, the first two expression are evaluated as (-27\*\*1)/3 =\> -9  
> > \> The second two are correct (what I expected).  
> > \> But the last two, WHY??
> > 
> > As Matz said, the second two are evaluated as - (27\*\*(1/3.0)), that is  
> > you take the cubic root of +27 which is 3 and then distribute the  
> > minus.  
> > That way, you take the cubic root of a positive number which does not  
> > raise any problem.
> > 
> > Cheers,
> > 
> > --  
> > JJ Fleck  
> > PCSI1 Lycée Kléber

---

<div class="post-metadata">

**Author:** ![Fleck\_Jean-Julien](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Fleck\_Jean-Julien](https://rubytalk.org/u/Fleck_Jean-Julien)\
**Post date:** [18 December 2009 17:02 UTC](https://rubytalk.org/t/math-errors/56690/14 "2009-12-18T17:02:41Z")

</div>

> That is WRONG, you cannot do that.

Well, I never said that you should do that, I just explained how Ruby  
interpreted it...

> That only works for odd roots of negative numbers.  
> The even root of negative numbers are imaginary.
> 
> -27\*\*3\*\*-1 =\> -3 \*\*correct  
> -27\*\*2\*\*-1 =\> -5.19615242270663 \*\*WRONG, its 5.196152i

Sure. That's quite a hint why \*\* does not accept a negative number  
with a non integer exponent. To take into account all the special  
cases, you should first see if your exponent is a rational and in that  
case, see if the denominator is odd (after all due simplifications of  
course). In this case (and only this case), you could try to decipher  
a root for this negative number.

Cheers,

> **···**
>
> --  
> JJ Fleck  
> PCSI1 Lycée Kléber

---

<div class="post-metadata">

**Author:** ![Unknown](https://avatars.discourse-cdn.com/v4/letter/u/4bbf92/32.png) [@Unknown](https://rubytalk.org/u/Unknown)\
**Post date:** [18 December 2009 19:00 UTC](https://rubytalk.org/t/math-errors/56690/15 "2009-12-18T19:00:05Z")

</div>

Fleck Jean-Julien wrote:

> \> That is WRONG, you cannot do that.
> 
> Well, I never said that you should do that, I just explained how Ruby  
> interpreted it...
> 
> \> That only works for odd roots of negative numbers.  
> \> The even root of negative numbers are imaginary.  
> \>  
> \> -27\*\*3\*\*-1 =\> -3 \*\*correct  
> \> -27\*\*2\*\*-1 =\> -5.19615242270663 \*\*WRONG, its 5.196152i
> 
> Sure. That's quite a hint why \*\* does not accept a negative number  
> with a non integer exponent. To take into account all the special  
> cases, you should first see if your exponent is a rational and in that  
> case, see if the denominator is odd (after all due simplifications of  
> course). In this case (and only this case), you could try to decipher  
> a root for this negative number.
> 
> Cheers,

def root base, n  
&nbsp;&nbsp;exp = 1.0/n  
&nbsp;&nbsp;return base \*\* exp if base \>= 0 or n.even?  
&nbsp;&nbsp;-( base.abs \*\* exp )  
end

> **···**
>
> --

---

<div class="post-metadata">

**Author:** ![jzakiya1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jzakiya1](https://rubytalk.org/u/jzakiya1)\
**Post date:** [18 December 2009 22:25 UTC](https://rubytalk.org/t/math-errors/56690/16 "2009-12-18T22:25:09Z")

</div>

> Fleck Jean-Julien wrote:  
> \> \> That is WRONG, you cannot do that.
> 
> \> Well, I never said that you should do that, I just explained how Ruby  
> \> interpreted it...
> 
> \> \> That only works for odd roots of negative numbers.  
> \> \> The even root of negative numbers are imaginary.
> 
> \> \> -27\*\*3\*\*-1 =\> -3 \*\*correct  
> \> \> -27\*\*2\*\*-1 =\> -5.19615242270663 \*\*WRONG, its 5.196152i
> 
> \> Sure. That's quite a hint why \*\* does not accept a negative number  
> \> with a non integer exponent. To take into account all the special  
> \> cases, you should first see if your exponent is a rational and in that  
> \> case, see if the denominator is odd (after all due simplifications of  
> \> course). In this case (and only this case), you could try to decipher  
> \> a root for this negative number.
> 
> \> Cheers,
> 
> def root base, n  
> exp = 1.0/n  
> return base \*\* exp if base \>= 0 or n.even?  
> -( base.abs \*\* exp )  
> end
> 
> --

Remember

i = (-1)^(1/2)

i^1 = i  
i^2 = -1  
i^3 = -i  
i^4 = 1  
Then it repeats, for example: i^5 = i\*(i^4) = i

For negative real value roots:

x = (-a)^(1/n) where n is odd integer =\> x = -[a^(1/n)]

But for negative real value roots where n is even:

x = (-a)^(1/n) where n is even gives

x = |a^(1/n)|\*(-1)^(1/n)  
x = |a^(1/n)|\*(i^2)^(1/n)  
x = |a^(1/n)|\*(i)^(2/n)  
from e^(i\*x) = cos(x) + i\*sin(x) where x = PI/2  
x = |a^{1/n)|\*e^(PI\*i/2)^(2/n)  
x = |a^(1/n)|\*e^(PI\*i/n)  
x = |a^(1/n)|\*(cos(PI/n) + i\*sin(PI/n)) for n even

(-256)^(1/2) = |256^(1/2)|\*(cos(PI/2) + i\*sin(PI/2))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= (16)(0 + i) = 16i

(-256)^(/4) = |256^(1/4)|\*(cos(PI/4) + i\*sin(PI/4))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= (4)\*(0.707 + 0.707\*i)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 2.828 + i\*2.828  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 2.828\*(1+i)

Check in irb

> require 'complex'  
> include Math

x = Complex(-256,0)

x\*\*(1/2.0)  
=\> (9.79685083057902e-16+16.0i)

X\*\*(1/4.0)  
=\> (2.82842712474619+2.82842712474619i)

> **···**
>
> On Dec 18, 2:13 pm, "William James" \<\> wrote:

---

<div class="post-metadata">

**Author:** ![jzakiya1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jzakiya1](https://rubytalk.org/u/jzakiya1)\
**Post date:** [19 December 2009 01:05 UTC](https://rubytalk.org/t/math-errors/56690/17 "2009-12-19T01:05:07Z")

</div>

> \> Fleck Jean-Julien wrote:  
> \> \> \> That is WRONG, you cannot do that.
> 
> \> \> Well, I never said that you should do that, I just explained how Ruby  
> \> \> interpreted it...
> 
> \> \> \> That only works for odd roots of negative numbers.  
> \> \> \> The even root of negative numbers are imaginary.
> 
> \> \> \> -27\*\*3\*\*-1 =\> -3 \*\*correct  
> \> \> \> -27\*\*2\*\*-1 =\> -5.19615242270663 \*\*WRONG, its 5.196152i
> 
> \> \> Sure. That's quite a hint why \*\* does not accept a negative number  
> \> \> with a non integer exponent. To take into account all the special  
> \> \> cases, you should first see if your exponent is a rational and in that  
> \> \> case, see if the denominator is odd (after all due simplifications of  
> \> \> course). In this case (and only this case), you could try to decipher  
> \> \> a root for this negative number.
> 
> \> \> Cheers,
> 
> \> def root base, n  
> \> exp = 1.0/n  
> \> return base \*\* exp if base \>= 0 or n.even?  
> \> -( base.abs \*\* exp )  
> \> end
> 
> \> --
> 
> Remember
> 
> i = (-1)^(1/2)
> 
> i^1 = i  
> i^2 = -1  
> i^3 = -i  
> i^4 = 1  
> Then it repeats, for example: i^5 = i\*(i^4) = i
> 
> For negative real value roots:
> 
> x = (-a)^(1/n) where n is odd integer =\> x = -[a^(1/n)]
> 
> But for negative real value roots where n is even:
> 
> x = (-a)^(1/n) where n is even gives
> 
> x = |a^(1/n)|\*(-1)^(1/n)  
> x = |a^(1/n)|\*(i^2)^(1/n)  
> x = |a^(1/n)|\*(i)^(2/n)  
> from e^(i\*x) = cos(x) + i\*sin(x) where x = PI/2  
> x = |a^{1/n)|\*e^(PI\*i/2)^(2/n)  
> x = |a^(1/n)|\*e^(PI\*i/n)  
> x = |a^(1/n)|\*(cos(PI/n) + i\*sin(PI/n)) for n even
> 
> (-256)^(1/2) = |256^(1/2)|\*(cos(PI/2) + i\*sin(PI/2))  
> = (16)(0 + i) = 16i
> 
> (-256)^(/4) = |256^(1/4)|\*(cos(PI/4) + i\*sin(PI/4))  
> = (4)\*(0.707 + 0.707\*i)  
> = 2.828 + i\*2.828  
> = 2.828\*(1+i)
> 
> Check in irb
> 
> \> require 'complex'  
> \> include Math
> 
> x = Complex(-256,0)
> 
> x\*\*(1/2.0)  
> =\> (9.79685083057902e-16+16.0i)
> 
> X\*\*(1/4.0)  
> =\> (2.82842712474619+2.82842712474619i)

BTW there is an error (sort of) in 'complex' too

> require 'complex'  
> include Math

> x = Complex(-27,0)

=\> (-27+0i)

> y = x\*\*(1/3.0) # or x\*\*3\*\*-1

=\> (1.5+2.59807621135332i) # should be (-3+0i)

> y\*\*3

=\> (-27.0+1.24344978758018e-14i)

> Complex(-3,0)\*\*3

=\> -27

Whenever you take the root n of a number you actually  
get n values. If the value is positive you get n copies  
of the same positive real value.

When you take the root of a negative real value you  
get n roots too, for n even and odd.

For even odd, you get one real root and n/2 Complex Conjugate Pairs  
(CCP).

Thus, for n=3 for (-27)^(1/3) the real root is x1=-3  
and x2 is y above and x3 is the CCP of y.

For n=5, you get one real root and 2 pairs of CCPs, etc.

For n even, you get n/2 CCPs only.  
So, for n=2 there is one pair of CCP roots.  
For n=4 you get 2 different CCP roots, etc,  
Thus for n even there are no real roots.

So, I think it's more intuitive (for most people)  
to expect Complex(-27,0)\*\*(1/n-odd) to return the real  
root x1 only (i.e. (-3)\*(-3)\*(-3) = -27), so have it  
act as Complex(-27,0).real (for n odd) be the default.

I guess complex variables aren't called complex for nothing. 🙂

> **···**
>
> On Dec 18, 5:24 pm, jzakiya \<jzak...@mail.com\> wrote:
> 
> > On Dec 18, 2:13 pm, "William James" \<\> wrote:

---

<div class="post-metadata">

**Author:** ![jzakiya1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jzakiya1](https://rubytalk.org/u/jzakiya1)\
**Post date:** [19 December 2009 01:30 UTC](https://rubytalk.org/t/math-errors/56690/18 "2009-12-19T01:30:11Z")

</div>

Oooh, I just noticed:  
Complex(-27\*\*(1/3.0),0) =\> (-3.0+0i).

So that acts I expected/want.

So maybe it would be nice to be able to do:

Complex(-a\*\*(1/n),0).roots or  
Complex(a\*\*(1/n)).roots where a is already complex  
and have this return all the roots in an array so you  
can see/pick which one(s) you want.

> **···**
>
> On Dec 18, 8:02 pm, jzakiya \<jzak...@mail.com\> wrote:
> 
> > On Dec 18, 5:24 pm, jzakiya \<jzak...@mail.com\> wrote:
> > 
> > \> On Dec 18, 2:13 pm, "William James" \<\> wrote:
> > 
> > \> \> Fleck Jean-Julien wrote:  
> > \> \> \> \> That is WRONG, you cannot do that.
> > 
> > \> \> \> Well, I never said that you should do that, I just explained how Ruby  
> > \> \> \> interpreted it...
> > 
> > \> \> \> \> That only works for odd roots of negative numbers.  
> > \> \> \> \> The even root of negative numbers are imaginary.
> > 
> > \> \> \> \> -27\*\*3\*\*-1 =\> -3 \*\*correct  
> > \> \> \> \> -27\*\*2\*\*-1 =\> -5.19615242270663 \*\*WRONG, its 5.196152i
> > 
> > \> \> \> Sure. That's quite a hint why \*\* does not accept a negative number  
> > \> \> \> with a non integer exponent. To take into account all the special  
> > \> \> \> cases, you should first see if your exponent is a rational and in that  
> > \> \> \> case, see if the denominator is odd (after all due simplifications of  
> > \> \> \> course). In this case (and only this case), you could try to decipher  
> > \> \> \> a root for this negative number.
> > 
> > \> \> \> Cheers,
> > 
> > \> \> def root base, n  
> > \> \> exp = 1.0/n  
> > \> \> return base \*\* exp if base \>= 0 or n.even?  
> > \> \> -( base.abs \*\* exp )  
> > \> \> end
> > 
> > \> \> --
> > 
> > \> Remember
> > 
> > \> i = (-1)^(1/2)
> > 
> > \> i^1 = i  
> > \> i^2 = -1  
> > \> i^3 = -i  
> > \> i^4 = 1  
> > \> Then it repeats, for example: i^5 = i\*(i^4) = i
> > 
> > \> For negative real value roots:
> > 
> > \> x = (-a)^(1/n) where n is odd integer =\> x = -[a^(1/n)]
> > 
> > \> But for negative real value roots where n is even:
> > 
> > \> x = (-a)^(1/n) where n is even gives
> > 
> > \> x = |a^(1/n)|\*(-1)^(1/n)  
> > \> x = |a^(1/n)|\*(i^2)^(1/n)  
> > \> x = |a^(1/n)|\*(i)^(2/n)  
> > \> from e^(i\*x) = cos(x) + i\*sin(x) where x = PI/2  
> > \> x = |a^{1/n)|\*e^(PI\*i/2)^(2/n)  
> > \> x = |a^(1/n)|\*e^(PI\*i/n)  
> > \> x = |a^(1/n)|\*(cos(PI/n) + i\*sin(PI/n)) for n even
> > 
> > \> (-256)^(1/2) = |256^(1/2)|\*(cos(PI/2) + i\*sin(PI/2))  
> > \> = (16)(0 + i) = 16i
> > 
> > \> (-256)^(/4) = |256^(1/4)|\*(cos(PI/4) + i\*sin(PI/4))  
> > \> = (4)\*(0.707 + 0.707\*i)  
> > \> = 2.828 + i\*2.828  
> > \> = 2.828\*(1+i)
> > 
> > \> Check in irb
> > 
> > \> \> require 'complex'  
> > \> \> include Math
> > 
> > \> x = Complex(-256,0)
> > 
> > \> x\*\*(1/2.0)  
> > \> =\> (9.79685083057902e-16+16.0i)
> > 
> > \> X\*\*(1/4.0)  
> > \> =\> (2.82842712474619+2.82842712474619i)
> > 
> > BTW there is an error (sort of) in 'complex' too
> > 
> > \>require 'complex'  
> > \>include Math  
> > \> x = Complex(-27,0)
> > 
> > =\> (-27+0i)
> > 
> > \>y = x\*\*(1/3.0) # or x\*\*3\*\*-1
> > 
> > =\> (1.5+2.59807621135332i) # should be (-3+0i)
> > 
> > \>y\*\*3
> > 
> > =\> (-27.0+1.24344978758018e-14i)
> > 
> > \> Complex(-3,0)\*\*3
> > 
> > =\> -27
> > 
> > Whenever you take the root n of a number you actually  
> > get n values. If the value is positive you get n copies  
> > of the same positive real value.
> > 
> > When you take the root of a negative real value you  
> > get n roots too, for n even and odd.
> > 
> > For even odd, you get one real root and n/2 Complex Conjugate Pairs  
> > (CCP).
> > 
> > Thus, for n=3 for (-27)^(1/3) the real root is x1=-3  
> > and x2 is y above and x3 is the CCP of y.
> > 
> > For n=5, you get one real root and 2 pairs of CCPs, etc.
> > 
> > For n even, you get n/2 CCPs only.  
> > So, for n=2 there is one pair of CCP roots.  
> > For n=4 you get 2 different CCP roots, etc,  
> > Thus for n even there are no real roots.
> > 
> > So, I think it's more intuitive (for most people)  
> > to expect Complex(-27,0)\*\*(1/n-odd) to return the real  
> > root x1 only (i.e. (-3)\*(-3)\*(-3) = -27), so have it  
> > act as Complex(-27,0).real (for n odd) be the default.
> > 
> > I guess complex variables aren't called complex for nothing. 🙂

---

<div class="post-metadata">

**Author:** ![jzakiya1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jzakiya1](https://rubytalk.org/u/jzakiya1)\
**Post date:** [19 December 2009 01:40 UTC](https://rubytalk.org/t/math-errors/56690/19 "2009-12-19T01:40:21Z")

</div>

So if you have these methods:

Complex(a).root(n).roots # all the roots of a  
Complex(a).root(n).real # the real root(s) if exists

Then for any value a (real or complex) you will get  
what you want under the different conditions.

> **···**
>
> On Dec 18, 8:26 pm, jzakiya \<jzak...@mail.com\> wrote:
> 
> > On Dec 18, 8:02 pm, jzakiya \<jzak...@mail.com\> wrote:
> > 
> > \> On Dec 18, 5:24 pm, jzakiya \<jzak...@mail.com\> wrote:
> > 
> > \> \> On Dec 18, 2:13 pm, "William James" \<\> wrote:
> > 
> > \> \> \> Fleck Jean-Julien wrote:  
> > \> \> \> \> \> That is WRONG, you cannot do that.
> > 
> > \> \> \> \> Well, I never said that you should do that, I just explained how Ruby  
> > \> \> \> \> interpreted it...
> > 
> > \> \> \> \> \> That only works for odd roots of negative numbers.  
> > \> \> \> \> \> The even root of negative numbers are imaginary.
> > 
> > \> \> \> \> \> -27\*\*3\*\*-1 =\> -3 \*\*correct  
> > \> \> \> \> \> -27\*\*2\*\*-1 =\> -5.19615242270663 \*\*WRONG, its 5.196152i
> > 
> > \> \> \> \> Sure. That's quite a hint why \*\* does not accept a negative number  
> > \> \> \> \> with a non integer exponent. To take into account all the special  
> > \> \> \> \> cases, you should first see if your exponent is a rational and in that  
> > \> \> \> \> case, see if the denominator is odd (after all due simplifications of  
> > \> \> \> \> course). In this case (and only this case), you could try to decipher  
> > \> \> \> \> a root for this negative number.
> > 
> > \> \> \> \> Cheers,
> > 
> > \> \> \> def root base, n  
> > \> \> \> exp = 1.0/n  
> > \> \> \> return base \*\* exp if base \>= 0 or n.even?  
> > \> \> \> -( base.abs \*\* exp )  
> > \> \> \> end
> > 
> > \> \> \> --
> > 
> > \> \> Remember
> > 
> > \> \> i = (-1)^(1/2)
> > 
> > \> \> i^1 = i  
> > \> \> i^2 = -1  
> > \> \> i^3 = -i  
> > \> \> i^4 = 1  
> > \> \> Then it repeats, for example: i^5 = i\*(i^4) = i
> > 
> > \> \> For negative real value roots:
> > 
> > \> \> x = (-a)^(1/n) where n is odd integer =\> x = -[a^(1/n)]
> > 
> > \> \> But for negative real value roots where n is even:
> > 
> > \> \> x = (-a)^(1/n) where n is even gives
> > 
> > \> \> x = |a^(1/n)|\*(-1)^(1/n)  
> > \> \> x = |a^(1/n)|\*(i^2)^(1/n)  
> > \> \> x = |a^(1/n)|\*(i)^(2/n)  
> > \> \> from e^(i\*x) = cos(x) + i\*sin(x) where x = PI/2  
> > \> \> x = |a^{1/n)|\*e^(PI\*i/2)^(2/n)  
> > \> \> x = |a^(1/n)|\*e^(PI\*i/n)  
> > \> \> x = |a^(1/n)|\*(cos(PI/n) + i\*sin(PI/n)) for n even
> > 
> > \> \> (-256)^(1/2) = |256^(1/2)|\*(cos(PI/2) + i\*sin(PI/2))  
> > \> \> = (16)(0 + i) = 16i
> > 
> > \> \> (-256)^(/4) = |256^(1/4)|\*(cos(PI/4) + i\*sin(PI/4))  
> > \> \> = (4)\*(0.707 + 0.707\*i)  
> > \> \> = 2.828 + i\*2.828  
> > \> \> = 2.828\*(1+i)
> > 
> > \> \> Check in irb
> > 
> > \> \> \> require 'complex'  
> > \> \> \> include Math
> > 
> > \> \> x = Complex(-256,0)
> > 
> > \> \> x\*\*(1/2.0)  
> > \> \> =\> (9.79685083057902e-16+16.0i)
> > 
> > \> \> X\*\*(1/4.0)  
> > \> \> =\> (2.82842712474619+2.82842712474619i)
> > 
> > \> BTW there is an error (sort of) in 'complex' too
> > 
> > \> \>require 'complex'  
> > \> \>include Math  
> > \> \> x = Complex(-27,0)
> > 
> > \> =\> (-27+0i)
> > 
> > \> \>y = x\*\*(1/3.0) # or x\*\*3\*\*-1
> > 
> > \> =\> (1.5+2.59807621135332i) # should be (-3+0i)
> > 
> > \> \>y\*\*3
> > 
> > \> =\> (-27.0+1.24344978758018e-14i)
> > 
> > \> \> Complex(-3,0)\*\*3
> > 
> > \> =\> -27
> > 
> > \> Whenever you take the root n of a number you actually  
> > \> get n values. If the value is positive you get n copies  
> > \> of the same positive real value.
> > 
> > \> When you take the root of a negative real value you  
> > \> get n roots too, for n even and odd.
> > 
> > \> For even odd, you get one real root and n/2 Complex Conjugate Pairs  
> > \> (CCP).
> > 
> > \> Thus, for n=3 for (-27)^(1/3) the real root is x1=-3  
> > \> and x2 is y above and x3 is the CCP of y.
> > 
> > \> For n=5, you get one real root and 2 pairs of CCPs, etc.
> > 
> > \> For n even, you get n/2 CCPs only.  
> > \> So, for n=2 there is one pair of CCP roots.  
> > \> For n=4 you get 2 different CCP roots, etc,  
> > \> Thus for n even there are no real roots.
> > 
> > \> So, I think it's more intuitive (for most people)  
> > \> to expect Complex(-27,0)\*\*(1/n-odd) to return the real  
> > \> root x1 only (i.e. (-3)\*(-3)\*(-3) = -27), so have it  
> > \> act as Complex(-27,0).real (for n odd) be the default.
> > 
> > \> I guess complex variables aren't called complex for nothing. 🙂
> > 
> > Oooh, I just noticed:  
> > Complex(-27\*\*(1/3.0),0) =\> (-3.0+0i).
> > 
> > So that acts I expected/want.
> > 
> > So maybe it would be nice to be able to do:
> > 
> > Complex(-a\*\*(1/n),0).roots or  
> > Complex(a\*\*(1/n)).roots where a is already complex  
> > and have this return all the roots in an array so you  
> > can see/pick which one(s) you want.
