在Python中,是否存在x的第n个根的简写形式? [英] Is there a short-hand for nth root of x, in Python?
问题描述
简单的语法问题.
在数学中,如果我有两个数字3和2,并且希望将3乘以2的幂,则不需要符号,但我将两个小写.在 Python
中,此操作似乎由 **
语法表示.
In maths if I have two number 3 and 2 and I wish to calculate 3 to the power of 2 then no symbol is required but I write the two small. In Python
this operation seems to be represented by the **
syntax.
>>> 3**2
9
如果我想反方向计算9的第二根,那么在数学中我需要使用符号:
If I want to go the other direction and calculate the 2nd root of 9 then in maths I need to use a symbol:
Python中是否有一个速记符号,类似于实现此目标的 **
,即 2< symbol> 9
.还是我需要使用 math
模块?
Is there a short-hand symbol in Python, similar to **
that achieves this i.e.2<symbol>9
. Or do I need to use the math
module ?
推荐答案
x
的第n个根是 x ^(1/n)
,因此您可以执行 9 **(1/2.0)
可以找到9的第二根.通常,您可以将x的第n个根计算为:
nth root of x
is x^(1/n)
, so you can do 9**(1/2.0)
to find the 2nd root of 9, for example. In general, you can compute the nth root of x as:
x**(1/float(n))
您也可以执行 1.0/n
而不是 1/float(n)
.要求使结果为 float
而不是 int
.
You can also do 1.0/n
instead of 1/float(n)
. It is required so that the result is a float
rather than an int
.
这篇关于在Python中,是否存在x的第n个根的简写形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!