Topic: arbitrary math root (cube, 4th ...5th etc)

Is there anything built in (or otherwise I welcome clever programming solutions), to
find an arbitrary root ?

I see there is Math::sqrt and Math:log/log10 .. and I've done some googling and have found ways of "building" a 'root' function using them, but I haven't been able to get it to work.

Specficially I'm trying to calc the 12th root of x... the application being a little prog to calculate guitar fret positions (if you were building a guitar ..... I'm not, .. but I have a couple of guitars and am curious about checking their specs.

Mike

Last edited by mstram (2008-07-09 01:29:44)

Re: arbitrary math root (cube, 4th ...5th etc)

mstram wrote:

Is there anything built in (or otherwise I welcome clever programming solutions), to
find an arbitrary root ?

I see there is Math::sqrt and Math:log/log10 .. and I've done some googling and have found ways of "building" a 'root' function using them, but I haven't been able to get it to work.

Specficially I'm trying to calc the 12th root of x... the application being a little prog to calculate guitar fret positions (if you were building a guitar ..... I'm not, .. but I have a couple of guitars and am curious about checking their specs.

Mike

The ** (to the power of) operator will do this, just make sure that the right hand term is a float (if you do 1/12 you get integer division which gives you 0).

>> 100 ** (1.0/12)
=> 1.46779926762207

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: arbitrary math root (cube, 4th ...5th etc)

Max Williams wrote:

The ** (to the power of) operator will do this, just make sure that the right hand term is a float (if you do 1/12 you get integer division which gives you 0).

>> 100 ** (1.0/12)
=> 1.46779926762207

Cool !

YAMTIDK

Mike

Re: arbitrary math root (cube, 4th ...5th etc)

Max - thanks for the info ... first time caller and Ruby n00bie ...

puts 1000 ** (1/3.0)  | gets me 9.999999999999998
puts (1000 ** (1/3.0)).round | gets me 10

Depending on the application, I suppose, you might use round (eg, less precision required in say a Project Management app, than a mars lander) ...

That said, I've heard string rounding (in the past) was more processing efficient in Ruby than number rounding .. is this still the case in 1.9.x ...?

cheers,
pr