Ruby中Math Power(**)的反转是什么?

时间:2022-06-01 19:06:46

I was wondering how to get the inverse of power in Ruby?

我想知道如何在Ruby中获得相反的功效?

2 ** 4 # => 16

and then I would like to get the inverse of it, and I'm not sure which operator to use

然后我想得到它的反转,我不确定使用哪个运算符

16 ?? 2 # => 4

5 个解决方案

#1


61  

The inverse of exponentiation is the logarithm. If ab = c, then logac = b.

取幂的倒数是对数。如果ab = c,那么logac = b。

You can find logarithm functions in the Math module, specifically log() for base-e and log10() for base-10.

您可以在Math模块中找到对数函数,特别是base-e的log()和base-10的log10()。

To get a logarithm to a different base (say n), use the formula logNa = logxa/logxN, where x is a value such as e or 10.

要获得不同基数(比如n)的对数,请使用公式logNa = logxa / logxN,其中x是e或10等值。

For your specific case:

对于您的具体情况:

log216
= loge16/loge2
= Math.log(16) / Math.log(2)
= 4

log216 = loge16 / loge2 = Math.log(16)/ Math.log(2)= 4

Whether you consider the explanation good because it expands your knowledge, or bad because you hated high school math, is entirely up to you :-)

你是否认为这个解释是好的,因为它扩展了你的知识,或者因为你讨厌高中数学而不好,完全取决于你:-)

#2


5  

Math.log(16) / Math.log(2)

#3


4  

Or alternatively, do it this way:

或者,也可以这样做:

>> 16**(1.0/2)
=> 4.0

#4


0  

Actually:

其实:

  • Maro is factually correct but
  • Maro事实上是正确的但是
  • Maro comment does not support Jeremy Murray-Wakefield's answer, which does not apply to the OP's question.
  • Maro评论不支持Jeremy Murray-Wakefield的答案,这不适用于OP的问题。

Maro is therefore "right" on his own but not as a support for Jeremy Murray-Wakefields answer, which itself is not so much "wrong" as not the right answer to the OP's questions.

因此,Maro自己是“正确的”,但并不支持Jeremy Murray-Wakefields的答案,而这本身并不是“错误的”,而不是OP问题的正确答案。

Hope that's definitive!

希望这是肯定的!

#5


0  

A cleaner way to obtain the logarithm, starting with Ruby 1.9.1, would be to use Math.log2:

从Ruby 1.9.1开始,获取对数的更简洁方法是使用Math.log2:

[1] pry(main)> Math.log2(2**4)
=> 4.0

#1


61  

The inverse of exponentiation is the logarithm. If ab = c, then logac = b.

取幂的倒数是对数。如果ab = c,那么logac = b。

You can find logarithm functions in the Math module, specifically log() for base-e and log10() for base-10.

您可以在Math模块中找到对数函数,特别是base-e的log()和base-10的log10()。

To get a logarithm to a different base (say n), use the formula logNa = logxa/logxN, where x is a value such as e or 10.

要获得不同基数(比如n)的对数,请使用公式logNa = logxa / logxN,其中x是e或10等值。

For your specific case:

对于您的具体情况:

log216
= loge16/loge2
= Math.log(16) / Math.log(2)
= 4

log216 = loge16 / loge2 = Math.log(16)/ Math.log(2)= 4

Whether you consider the explanation good because it expands your knowledge, or bad because you hated high school math, is entirely up to you :-)

你是否认为这个解释是好的,因为它扩展了你的知识,或者因为你讨厌高中数学而不好,完全取决于你:-)

#2


5  

Math.log(16) / Math.log(2)

#3


4  

Or alternatively, do it this way:

或者,也可以这样做:

>> 16**(1.0/2)
=> 4.0

#4


0  

Actually:

其实:

  • Maro is factually correct but
  • Maro事实上是正确的但是
  • Maro comment does not support Jeremy Murray-Wakefield's answer, which does not apply to the OP's question.
  • Maro评论不支持Jeremy Murray-Wakefield的答案,这不适用于OP的问题。

Maro is therefore "right" on his own but not as a support for Jeremy Murray-Wakefields answer, which itself is not so much "wrong" as not the right answer to the OP's questions.

因此,Maro自己是“正确的”,但并不支持Jeremy Murray-Wakefields的答案,而这本身并不是“错误的”,而不是OP问题的正确答案。

Hope that's definitive!

希望这是肯定的!

#5


0  

A cleaner way to obtain the logarithm, starting with Ruby 1.9.1, would be to use Math.log2:

从Ruby 1.9.1开始,获取对数的更简洁方法是使用Math.log2:

[1] pry(main)> Math.log2(2**4)
=> 4.0