For example
x ** 3 # => 125
Knowing that the result of applying **
with an argument 3
to x
is 125
, how can I get the value of x
?
Is there some kind of built-in method for this? I have been looking at the Math module but didn't find anything similar.
pow(n, 1.0 / 3) % ((int) Math. pow(n, 1.0 / 3)) . Use BigDecimal class, which is decimal representation of real numbers with arbitrary precision. Of course there's no method to calculate nth roots in the BigDecimal class.
Using **
with 1/3
:
125 ** (1.0/3)
# => 4.999999999999999
You could also try as below :
irb(main):005:0> 125**(3**-1)
=> 5
irb(main):006:0> 125**(3**-1.0)
=> 4.999999999999999
irb(main):007:0>
update
C:\Users >ruby -v
ruby 1.9.3p448 (2013-06-27) [i386-mingw32]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With