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.
知道使用参数3到x的**的结果是125,我怎样才能得到x的值?这有什么内置方法吗?我一直在看数学模块,但没有找到类似的东西。
3 个解决方案
#1
18
Using **
with 1/3
:
使用**和1/3:
125 ** (1.0/3)
# => 4.999999999999999
#2
1
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]
#3
0
You can do an Nth root by raising to a fractional power. For example, the 4th root of 625 is 5.
你可以通过提高分数幂来做第N个根。例如,625的第4个根是5。
(BigDecimal.new(625)**(1.0/4.0)).to_f
# => 5.0
Note, the .to_f
is added for readability in this answer only. Don't cast it to a Float in your code unless you need to. IMHO, BigDecimals are "better" than Floats in Ruby - Floats lose precision too easily and you won't get accurate results. Case in point the accepted awnser above. The cube root of 125 is not 4.99999(repeat). It is 5.0 exactly.
请注意,仅在此答案中添加.to_f以提高可读性。除非您需要,否则不要将其转换为代码中的Float。恕我直言,BigDecimals比Ruby中的Floats“更好” - 浮点数太容易失去精度,你将无法获得准确的结果。例如上面接受的awnser。 125的立方根不是4.99999(重复)。它完全是5.0。
Edit: the Ruby Class Rational seems to handle nth roots a little better.
编辑:Ruby类Rational似乎更好地处理第n个根。
2.3.3 :007 > 625.to_r ** 0.25
=> 5.0
But it still isn't as precise with a number that produces an irrational root.
但是,产生无理根的数字仍然不够准确。
2.3.3 :024 > (999.to_r ** 0.25) ** 4
=> 998.9999999999999
Close...but you should be able to get back to 999.0 exactly. My Mac's calculator and excel can do it. Main point - just be careful. If precision is important, Ruby may not handle this exactly the way one would expect.
关闭......但你应该能够准确地回到999.0。我的Mac的计算器和excel可以做到。要点 - 小心点。如果精度很重要,Ruby可能无法完全按照预期的方式处理。
#1
18
Using **
with 1/3
:
使用**和1/3:
125 ** (1.0/3)
# => 4.999999999999999
#2
1
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]
#3
0
You can do an Nth root by raising to a fractional power. For example, the 4th root of 625 is 5.
你可以通过提高分数幂来做第N个根。例如,625的第4个根是5。
(BigDecimal.new(625)**(1.0/4.0)).to_f
# => 5.0
Note, the .to_f
is added for readability in this answer only. Don't cast it to a Float in your code unless you need to. IMHO, BigDecimals are "better" than Floats in Ruby - Floats lose precision too easily and you won't get accurate results. Case in point the accepted awnser above. The cube root of 125 is not 4.99999(repeat). It is 5.0 exactly.
请注意,仅在此答案中添加.to_f以提高可读性。除非您需要,否则不要将其转换为代码中的Float。恕我直言,BigDecimals比Ruby中的Floats“更好” - 浮点数太容易失去精度,你将无法获得准确的结果。例如上面接受的awnser。 125的立方根不是4.99999(重复)。它完全是5.0。
Edit: the Ruby Class Rational seems to handle nth roots a little better.
编辑:Ruby类Rational似乎更好地处理第n个根。
2.3.3 :007 > 625.to_r ** 0.25
=> 5.0
But it still isn't as precise with a number that produces an irrational root.
但是,产生无理根的数字仍然不够准确。
2.3.3 :024 > (999.to_r ** 0.25) ** 4
=> 998.9999999999999
Close...but you should be able to get back to 999.0 exactly. My Mac's calculator and excel can do it. Main point - just be careful. If precision is important, Ruby may not handle this exactly the way one would expect.
关闭......但你应该能够准确地回到999.0。我的Mac的计算器和excel可以做到。要点 - 小心点。如果精度很重要,Ruby可能无法完全按照预期的方式处理。