# Math cube root

Hi,

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

Thanks.

···

--
Posted via http://www.ruby-forum.com/.

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)

···

--

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

p 3.root 27
p 2.root 9

···

2009/7/11 Zangief Ief <z4n9ief@gmail.com>

Hi,

Is there a cube root (as http://en.wikipedia.org/wiki/Cube_root)

--
Thomas Preymesser
thopre@gmail.com

Jack Benny <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."

Hi,

Is there a cube root (as 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):

Bertram

···

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

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

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

···

--
Posted via http://www.ruby-forum.com/.

Thanks a lot for your explanations. They really help me a lot. You are
great

···

--
Posted via http://www.ruby-forum.com/.

So, so sorry.

Even easier.

def cube_root(x)
x**(1/3.0)
end

Cheers,

Serabe

···

--

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

···

On Jul 11, 3:58 pm, Thomas Preymesser <tho...@gmail.com> wrote:

[Note: parts of this message were removed to make it a legal post.]

2009/7/11 Zangief Ief <z4n9...@gmail.com>

> Hi,

> Is there a cube root (ashttp://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 Preymesser
Jack Benny <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."

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.

-Jeremy

···

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

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

···

On Wed, 11 May 2011 21:45:42 +0200, Sergey Avseyev <sergey.avseyev@gmail.com> 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

So, so sorry.

Even easier.

but nice math anyways
Greetz

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Thu, May 12, 2011 at 8:25 AM, Josef 'Jupp' Schugt <jupp@gmx.de> wrote:

On Wed, 11 May 2011 21:45:42 +0200, Sergey Avseyev < > sergey.avseyev@gmail.com> 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

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

Cheers,

Serabe

···

but nice math anyways

--

...
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!

···

On Thu, May 12, 2011 at 2:54 PM, serialhex <serialhex@gmail.com> wrote:

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 Arbeo <serabe@gmail.com>

> but nice math anyways

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

Cheers,

Serabe

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

···

On Thu, May 12, 2011 at 10:26 AM, Colin Bartlett <colinb2r@googlemail.com>wrote:

On Thu, May 12, 2011 at 2:54 PM, serialhex <serialhex@gmail.com> 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!

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

···

On Thu, May 12, 2011 at 2:30 PM, Josh Cheek <josh.cheek@gmail.com> wrote:

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

···

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

On Thu, May 12, 2011 at 10:26 AM, Colin Bartlett <colinb2r@googlemail.com > >wrote:

On Thu, May 12, 2011 at 2:54 PM, serialhex <serialhex@gmail.com> >> 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!

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

Think of -1 in polar form (r exp(i theta)) as 1.exp(i pi), then note
that the cube roots are (1 exp (i pi/3)), (1 exp (i 2pi/3)) and (1
exp(i pi)). The principal cube root is the one with the smallest value
of theta.

martin

···

On Fri, May 13, 2011 at 12:00 AM, Josh Cheek <josh.cheek@gmail.com> wrote:

On Thu, May 12, 2011 at 10:26 AM, Colin Bartlett <colinb2r@googlemail.com>wrote:

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!

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