Math cube root

S

Sergio Arbeo

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

Sergio Arbeo

2009/7/11 Fabian Streitel said:
but nice math anyways :)

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

Cheers,

Serabe
 
F

Fabian Streitel

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

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*
 
T

Thomas Preymesser

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

2009/7/11 Zangief Ief said:

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

p 3.root 27
p 2.root 9

--
Thomas Preymesser
(e-mail address removed)
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."
 
B

Bertram Scharpf

Hi,

Am Samstag, 11. Jul 2009, 19:48:50 +0900 schrieb Zangief Ief:
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
 
J

jzakiya

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

2009/7/11 Zangief Ief said:
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
[email protected]://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."

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

Zangief Ief

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

Sergey Avseyev

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
 
J

Jeremy Bopp

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top