[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Nth Root in Ruby

Peter Laurens

10/22/2007 5:27:00 PM

Hi,

I have tried Googling, but am finding it difficult to track down an
answer to my question:

What's the easiest way to calculate the Nth root of a number in Ruby,
I've looked at the Math module but this doesn't seem to include this
functionality unless I'm missing something. Do I need to roll my own?

Thanks,

- N
--
Posted via http://www.ruby-....

3 Answers

Alex LeDonne

10/22/2007 5:33:00 PM

0

On 10/22/07, Peter Laurens <peterlaurenspublic@gmail.com> wrote:
> Hi,
>
> I have tried Googling, but am finding it difficult to track down an
> answer to my question:
>
> What's the easiest way to calculate the Nth root of a number in Ruby,
> I've looked at the Math module but this doesn't seem to include this
> functionality unless I'm missing something. Do I need to roll my own?
>
> Thanks,
>
> - N

Try raising to the power of the reciprocal:

irb(main):001:0> # calculate a third root
irb(main):002:0> 10.0 ** (1.0/3.0)
=> 2.15443469003188

-A

Peter Laurens

10/22/2007 5:35:00 PM

0

Alex LeDonne wrote:
> On 10/22/07, Peter Laurens <peterlaurenspublic@gmail.com> wrote:
>>
>> - N
>
> Try raising to the power of the reciprocal:
>
> irb(main):001:0> # calculate a third root
> irb(main):002:0> 10.0 ** (1.0/3.0)
> => 2.15443469003188
>
> -A

Of course - thanks.
--
Posted via http://www.ruby-....

Ron Fox

10/22/2007 5:35:00 PM

0

def rootn(x,n)
Math.exp(Math.log(x)/n)
end

Probably should check for X > 0 and do appropriate
special case code if not.

RF

Peter Laurens wrote:
> Hi,
>
> I have tried Googling, but am finding it difficult to track down an
> answer to my question:
>
> What's the easiest way to calculate the Nth root of a number in Ruby,
> I've looked at the Math module but this doesn't seem to include this
> functionality unless I'm missing something. Do I need to roll my own?
>
> Thanks,
>
> - N