Python 中 x 的第 n 个根是否有简写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19255120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:15:55  来源:igfitidea点击:

Is there a short-hand for nth root of x in Python

pythonmathoperators

提问by whytheq

Simple syntax question.

简单的语法问题。

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 Pythonthis operation seems to be represented by the **syntax.

在数学中,如果我有两个数字 3 和 2,并且我希望计算 3 的 2 次方,那么不需要符号,但我将这两个写成小数。在Python这个操作中似乎是由**语法来表示的。

>>> 3**2
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:

如果我想反方向计算 9 的二阶根,那么在数学中我需要使用一个符号:

nth root of x

x 的第 n 个根

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 mathmodule ?

Python 中是否有简写符号,类似于**实现此目的的 ie 2<symbol>9。还是我需要使用math模块?

采纳答案by Hari Menon

nth root of xis 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:

xis 的第 n 个根,例如x^(1/n),您可以9**(1/2.0)找到 9 的第 2 个根。通常,您可以将 x 的第 n 个根计算为:

x**(1/float(n))

You can also do 1.0/ninstead of 1/float(n). It is required so that the result is a floatrather than an int.

你也可以1.0/n代替1/float(n). 它是必需的,以便结果是 afloat而不是int

回答by Ishaan

Basically sqrt(9) is equivalent to 9^.5

基本上 sqrt(9) 相当于 9^.5

>>>9**.5
3.0

回答by Mike 'Pomax' Kamermans

There is. It's just **=)

有。这只是**=)

Any nth root is an exponentiation by 1/n, so to get the square root of 9, you use 9**(1/2)(or 9**0.5) to get the cube root, you use 9 ** (1/3)(which we can't write with a simpler fraction), and to get the nth root, 9 ** (1/n).

任何第 n 个根都是由 求幂1/n,因此要获得 9 的平方根,您可以使用9**(1/2)(或9**0.5)来获得立方根,您使用9 ** (1/3)(我们不能用更简单的分数来写),并获得第 n 个根,9 ** (1/n).

Also note that as of Python 3, adding periods to integers to make them a float is no longer necessary. Saying 1/3works the way you would actually expect it to, giving 0.333...as result, rather than zero. For legacy versions of Python, you'll have to remember to use that period (but also critically wonder why you're using a legacy version of a programming language)

另请注意,从 Python 3 开始,不再需要为整数添加句点以使其成为浮点数。Saying1/3以您实际期望的方式工作,给出0.333...结果,而不是零。对于 Python 的遗留版本,您必须记住使用那个时期(但也很想知道为什么要使用编程语言的遗留版本)

回答by Nacib Neme

Also: x**(n**-1), which is the same but shorter than x**(1/float(n))

另外:x**(n**-1),它相同但比x**(1/float(n))

回答by PersianGulf

You should do

你应该做

16**(0.5) #If you print it, you get 4, So you can use this formula.

回答by ely

If you prefer to apply this operation functionally rather than with an infix operator (the **symbol), you can pass the base and exponent as arguments to the powfunction:

如果您更喜欢在功能上应用此操作而不是使用中缀运算符(**符号),您可以将基数和指数作为参数传递给pow函数:

In [23]: (9**(0.5)) == pow(9, 0.5)
Out[23]: True

I am also fond of finding new uses for this Infix hack in Pythonalthough it's more of a fun aside than a heavy-duty solution. But you could effectively make your own personal symbol for this by doing the following:

我也喜欢在 Python中找到这个 Infix hack 的新用途,尽管它比重型解决方案更有趣。但是您可以通过执行以下操作来有效地为此创建您自己的个人符号:

class Infix:
    def __init__(self, function):
        self.function = function
    def __ror__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __or__(self, other):
        return self.function(other)
    def __rlshift__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __rshift__(self, other):
        return self.function(other)
    def __call__(self, value1, value2):
        return self.function(value1, value2)


root_of = Infix(lambda x,y: y**(1.0/x))

print 2 |root_of| 9
3.0

回答by ravi tanwar

def nthrootofm(a,n):
    return pow(a,(1/n))
a=81
n=4
q=nthrootofm(a,n)
print(q)

pow() function takes two parameters .

pow() 函数有两个参数。

回答by Idvar

You may also use some logarithms:

您还可以使用一些对数:

Nth root of:

N次根:

 X = exp(log(n)/x)