# Math cube root

**URL:** <https://www.ruby-forum.com/t/math-cube-root/171915>\
**Category:** Ruby\
**Created:** [July 11, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915 "2009-07-11T12:48:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 11, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/1 "2009-07-11T12:48:40Z")

</div>

Hi,

Is there a cube root (as [Cube root - Wikipedia](http://en.wikipedia.org/wiki/Cube_root))  
function in Ruby? Math module seems not offer it… except a  
Math.sqrt().

Thanks.

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 11, 2009, 1:07pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/2 "2009-07-11T13:07:05Z")

</div>

def cube\_root(x)  
Math.exp(Math.log(x.to\_f)/3.to\_f)  
end

Cheers,

Serabe

P.D. Proof:  
Cube root of x is equal to x^(1/3)

x^(1/3) = y  
ln(x^(1/3)) = ln y  
(1/3)ln x = ln y

Ergo

y = e^((ln x)/3)

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 11, 2009, 1:10pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/3 "2009-07-11T13:10:00Z")

</div>

So, so sorry.

Even easier.

def cube\_root(x)  
x\*\*(1/3.0)  
end

Cheers,

Serabe

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 11, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/4 "2009-07-11T18:45:22Z")

</div>

2009/7/11 Fabian S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> but nice math anyways 🙂

Four years studying maths at the university should be useful for  
something, should’nt it? 🙂

Cheers,

Serabe

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 11, 2009, 9:11pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/5 "2009-07-11T21:11:26Z")

</div>

one might argue that way, I guess _g_  
gives me the satisfaction that what I’m getting into my head  
right now for the next exam is not entirely useless _g_

2009/7/11 Sergio A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 11, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/6 "2009-07-11T18:17:53Z")

</div>

> So, so sorry.
> 
> Even easier.

but nice math anyways 🙂  
Greetz

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 11, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/7 "2009-07-11T22:59:31Z")

</div>

2009/7/11 Zangief I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Hi,
> 
> Is there a cube root (as [Cube root - Wikipedia](http://en.wikipedia.org/wiki/Cube_root))

class Numeric  
def root(arg)  
arg\*\*(1.0/self)  
end  
end

p 3.root 27  
p 2.root 9

## – Thomas P. removed\_email\_address@domain.invalid [http://thopre.googlepages.com/](http://thopre.googlepages.com/) [http://thopre.wordpress.com/](http://thopre.wordpress.com/) Jack Benny [http://www.brainyquote.com/quotes/authors/j/jack\_benny.html](http://www.brainyquote.com/quotes/authors/j/jack_benny.html)

“I don’t deserve this award, but I have arthritis and I don’t deserve  
that  
either.”

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 13, 2009, 2:17am UTC](https://www.ruby-forum.com/t/math-cube-root/171915/8 "2009-07-13T02:17:32Z")

</div>

Hi,

Am Samstag, 11. Jul 2009, 19:48:50 +0900 schrieb Zangief I.:

> Is there a cube root (as [Cube root - Wikipedia](http://en.wikipedia.org/wiki/Cube_root))  
> function in Ruby? Math module seems not offer it… except a  
> Math.sqrt().

I suppose you mean the method mentioned under “Cube root on standard  
calculator”. Here’s an implementation:

class Numeric  
def sqrt  
Math.sqrt self  
end  
def cbrt  
neg = self \< 0  
c = abs.sqrt.sqrt  
i = 2  
loop do  
w = c  
i.times { w = w.sqrt }  
i \*= 2  
w \*= c  
break if c == w  
c = w  
end  
neg ? -c : c  
end  
end

If you like it in C (only Ruby 1.8):

[http://bertram-scharpf.homelinux.com:8808/doc\_root/bs-ruby-2.3/rdoc/classes/Numeric.src/M000033.html](http://bertram-scharpf.homelinux.com:8808/doc_root/bs-ruby-2.3/rdoc/classes/Numeric.src/M000033.html)

Bertram

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 13, 2009, 8:05am UTC](https://www.ruby-forum.com/t/math-cube-root/171915/9 "2009-07-13T08:05:42Z")

</div>

On Jul 11, 3:58 pm, Thomas P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> arg**(1.0/self)
> 
> ```
> 
> “I don’t deserve this award, but I have arthritis and I don’t deserve that  
> either.”

Correction:  
class Numeric  
def root(arg); self\*\*(1.0/arg) end  
end

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [July 13, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/10 "2009-07-13T12:47:59Z")

</div>

Thanks you! Thomas P.'s method is perfect.  
There is no correction to do jzakiya.

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 11, 2011, 9:43pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/11 "2011-05-11T21:43:25Z")

</div>

How can you explain this:

```
$ irb
1.9.2p180 (main):001:0> 1000 ** (1.0/3)
9.999999999999998
1.9.2p180 (main):002:0> Math.sqrt(100)
10.0
```

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 12, 2011, 2:33pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/12 "2011-05-12T14:33:32Z")

</div>

On Wed, 11 May 2011 21:45:42 +0200, Sergey Avseyev  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How can you explain this:
> 
> ```
> $ irb
> 1.9.2p180 (main):001:0> 1000 ** (1.0/3)
> 9.999999999999998
> 1.9.2p180 (main):002:0> Math.sqrt(100)
> 10.0
> 
> ```

Floating-point numbers have a finite precision. As a result, the outcome  
of a numerical (computer) calculation usually differs from the outcome  
of  
the mathematical calculation. Assume you only can operate with integers  
and want to compute the square root of 133. You may then end up with  
either 11 (11² = 121) or 12 (12² = 144) while the actual result is  
approximately 11.5 (11.5² = 121 + 11 + 0.25 = 132.25; more precisely  
11.5325625947).

You may like to use formatted output of numbers that suppresses digits  
beyond the actual precision:

jupp@pen2:~ $ irb  
irb(main):001:0\> “%.15f” % 1000 \*\* (1.0/3)  
=\> “9.999999999999998”  
irb(main):002:0\> “%.14f” % 1000 \*\* (1.0/3)  
=\> “10.00000000000000”

The above example turns the numerical value into a string displaying a  
fractional part with 15 and 14 digits, respectively. Assuming that IEEE  
754 double precision floating point numbers (i.e. those used by Ruby)  
have  
a precision of a little less than 16 (decimal) digits it is safe to  
assume  
that the complexity of operation you perform results in a value that is  
precise to a little less than 15 digits - which means that you can  
assume  
14 digits to be correct. By chance it MAY be precise to more digits as  
it  
is the case for Math.sqrt(100) - but that is nothing you can rely on  
unless you learn some gory details of numerical mathematics.

HTH

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 12, 2011, 3:59pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/13 "2011-05-12T15:59:38Z")

</div>

On Thu, May 12, 2011 at 8:25 AM, Josef ‘Jupp’ Schugt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

instead of using floats you can try to use rational numbers in ruby:

ruby-1.9.2-p180 :001 \> require ‘mathn’  
=\> true  
ruby-1.9.2-p180 :002 \> 1000 \*\* (1.0/3)  
=\> 9.999999999999998  
ruby-1.9.2-p180 :003 \> 1000 \*\* (1/3)  
=\> 10  
ruby-1.9.2-p180 :004 \> 1.0/3  
=\> 0.3333333333333333  
ruby-1.9.2-p180 :005 \> 1/3  
=\> (1/3)

you need to require ‘mathn’ which will change integer division (and make  
all  
the numbers play nicely together) but other than that things will work  
better. the only down side to this whole thing is that rationals are  
slower  
than floats (probably significantly so) because it is a pair of  
integers.  
and some values when you look at them wont seem to make sense as a  
rational, like PI:

ruby-1.9.2-p180 :011 \> Math::PI  
=\> 3.141592653589793  
ruby-1.9.2-p180 :012 \> Math::PI.to\_r  
=\> (884279719003555/281474976710656)

you can always num.to\_f back but that also takes time. one last thing:  
if  
your inputs are floats i’m not sure there is a nice way to convert them  
to  
rationals:

ruby-1.9.2-p180 :016 \> (1.0/3).to\_r  
=\> (6004799503160661/18014398509481984)  
ruby-1.9.2-p180 :017 \> 1/3  
=\> (1/3)

so this entire e-mail may be moot 😛  
hex

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 12, 2011, 5:29pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/14 "2011-05-12T17:29:21Z")

</div>

On Thu, May 12, 2011 at 2:54 PM, serialhex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …  
> instead of using floats you can try to use rational numbers in ruby:  
> ruby-1.9.2-p180 :001 \> require ‘mathn’  
> which also changes this:  
> (-1) \*\* (1.0 / 3) #=\> NaN  
> to:  
> (-1) \*\* (1.0 / 3) #=\> (0.5+0.866025403784439i)  
> so we can also get complex roots if we want them!

> …  
> one last thing: if your inputs are floats i’m not sure there is a nice way  
> to convert them to rationals:  
> (1.0/3).to\_r #=\> (6004799503160661/18014398509481984)  
> I think that is a nice way! It’s telling us (I think) what is the  
> exact rational represented by the float. After all, a float is just a  
> special type of rational, or rather floats are a subset of the subset  
> of the rationals defined by them have powers of two as denominators in  
> their representation using the smallest positive denominator. (I hope  
> that’s correct.)

Whether it’s a _good_ idea to be able to convert floats to “ordinary”  
rationals is another question, and I’m sceptical about that: I think  
one should probably only want to do it if one fully understands  
floating point representation, and if one fully understands floating  
point representation, I suspect one wouldn’t want to do it!

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 12, 2011, 8:33pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/15 "2011-05-12T20:33:41Z")

</div>

On Thu, May 12, 2011 at 10:26 AM, Colin B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> 

Which we don’t here, given that the cube root of -1 isn’t imaginary 😕

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 11, 2011, 10:07pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/16 "2011-05-11T22:07:31Z")

</div>

On 5/11/2011 14:45, Sergey Avseyev wrote:

> How can you explain this:
> 
> ```
> $ irb
> 1.9.2p180 (main):001:0> 1000 ** (1.0/3)
> 9.999999999999998
> 1.9.2p180 (main):002:0> Math.sqrt(100)
> 10.0
> 
> ```

You’re using floating point arithmetic which is always inexact. The  
1.0/3 part cannot be represented with infinite precision, so it’s  
basically rounded at a certain point. The result is then used for the  
rest of the operation, which may compound the inaccuracy introduced by  
the initial rounding.

If you must use floating point operations, be prepared to accept results  
that are only _close_ to what you expect, where close is largely  
dependent on the operations being performed.

> **[Floating-point arithmetic](https://en.wikipedia.org/wiki/Floating_point)**
>
> In computing, floating-point arithmetic (FP) is arithmetic that represents a subset of real numbers using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base.
> Numbers of this form are called floating-point numbers.: 3 : 10 
> For example, 12.345 is a floating-point number in a base-ten representation with five digits of precision:
> However, 12.3456 is not a floating-point number with five base-ten digits of precision.
> In practice, most floatin...

-Jeremy

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 12, 2011, 8:53pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/17 "2011-05-12T20:53:29Z")

</div>

On Thu, May 12, 2011 at 2:30 PM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Which we don’t here, given that the cube root of -1 isn’t imaginary 😕

there are actually 3 cube roots of -1 (and 4 quad roots of -1, and 5  
penta  
roots of -1 and…)  
behold the code:

ruby-1.9.2-p180 :004 \> a = (-1)**(1/3)  
=\> (0.5000000000000001+0.8660254037844386i)  
ruby-1.9.2-p180 :005 \> a.conj  
=\> (0.5000000000000001-0.8660254037844386i)  
ruby-1.9.2-p180 :006 \> a.conj**3  
=\> (-1.0-3.885780586188048e-16i)  
ruby-1.9.2-p180 :007 \> a\*\*3  
=\> (-1.0+3.885780586188048e-16i)  
ruby-1.9.2-p180 :008 \> (-1)\*\*3  
=\> -1

(conjugation simply flips the sign of the imaginary number, which is  
helpful  
for a bunch of things) so while there may be 1 _real_ root for a given  
number, there are n roots for any number taken to the (1/n)th power.  
and  
while most people will want the (simple) (-1)\*\*(1/3) == (-1) answer,  
they  
are all right (minus the floating-point rounding errors… which is kind  
of  
odd cause i would think that i would be getting the rational  
representation  
of the complex numbers instead of floats… idfk… whatev!)  
hex

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 12, 2011, 8:57pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/18 "2011-05-12T20:57:11Z")

</div>

On May 12, 2011, at 2:30 PM, Josh C. wrote:

> > to:  
> > (-1) \*\* (1.0 / 3) #=\> (0.5+0.866025403784439i)  
> > so we can also get complex roots if we want them!
> 
> Which we don’t here, given that the cube root of -1 isn’t imaginary 😕

ONE of the cube roots of -1 isn’t imaginary, but the other two are.  
Since (1.0/3) isn’t exactly one-third, raising -1 to (1.0/3) isn’t  
exactly the same as taking the cube root either.

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 12, 2011, 10:31pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/19 "2011-05-12T22:31:48Z")

</div>

On May 12, 2:53pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > On Thu, May 12, 2011 at 10:26 AM, Colin B. \<removed\_email\_address@domain.invalid
> > 
> > > (-1) \*\* (1.0 / 3) #=\> (0.5+0.866025403784439i)  
> > > Rob B.  
> > > removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
> > > removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

See my Roots Module.

> <https://gist.github.com/jzakiya/422636>

It allows you to easily (and accurately) get all n values for a root  
n,  
for all number classes.

Instead of: 1000 **3** -1 of 1000\*\*(1.0/3) to get only default root  
Instead do: 1000.root(3) or 1000.roots(3), for all 3 cube roots

Jabari Zakiya

---

<div class="post-metadata">

**Author:** ![z4n9ief](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/7993a0/32.png) [@z4n9ief](https://www.ruby-forum.com/u/z4n9ief)\
**Post date:** [May 12, 2011, 10:36pm UTC](https://www.ruby-forum.com/t/math-cube-root/171915/20 "2011-05-12T22:36:44Z")

</div>

On Thu, May 12, 2011 at 4:25 PM, jzakiya [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> See my Roots Module.  
> [roots.rb · GitHub](https://gist.github.com/422636)

spiffy!!!  
hex

[Next page](https://www.ruby-forum.com/t/math-cube-root/171915.md?page=2)
