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.

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)

So, so sorry.

Even easier.

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

Cheers,

Serabe

2009/7/11 Fabian S. [email protected]:

but nice math anyways :slight_smile:

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

Cheers,

Serabe

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. [email protected]

So, so sorry.

Even easier.

but nice math anyways :slight_smile:
Greetz

2009/7/11 Zangief I. [email protected]

Hi,

Is there a cube root (as 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.
[email protected]
http://thopre.googlepages.com/
http://thopre.wordpress.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,

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

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):

http://bertram-scharpf.homelinux.com:8808/doc_root/bs-ruby-2.3/rdoc/classes/Numeric.src/M000033.html

Bertram

On Jul 11, 3:58 pm, Thomas P. [email protected] 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

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

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

On Wed, 11 May 2011 21:45:42 +0200, Sergey Avseyev
[email protected] 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:

[email protected]:~ $ 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 Thu, May 12, 2011 at 8:25 AM, Josef ‘Jupp’ Schugt [email protected]
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 :stuck_out_tongue:
hex

On Thu, May 12, 2011 at 2:54 PM, serialhex [email protected] 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!

On Thu, May 12, 2011 at 10:26 AM, Colin B.
[email protected]wrote:

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

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.

http://en.wikipedia.org/wiki/Floating_point

-Jeremy

On Thu, May 12, 2011 at 2:30 PM, Josh C. [email protected]
wrote:

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

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 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 :confused:

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.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On May 12, 2:53pm, Rob B. [email protected]
wrote:

On Thu, May 12, 2011 at 10:26 AM, Colin B. <[email protected]

(-1) ** (1.0 / 3) #=> (0.5+0.866025403784439i)
Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

See my Roots Module.
https://gist.github.com/422636

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

Instead of: 10003-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

On Thu, May 12, 2011 at 4:25 PM, jzakiya [email protected] wrote:

See my Roots Module.
https://gist.github.com/422636

spiffy!!!
hex