Python中x的第n个根是否有简写形式 [英] Is there a short-hand for nth root of x in Python

查看:101
本文介绍了Python中x的第n个根是否有简写形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的语法问题.

在数学中,如果我有两个数字3和2,并且希望将3计算为2的幂,则不需要符号,但我将两个小写.在Python中,此操作似乎由**语法表示.

>>> 3**2
9

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

Python中是否有一个速记符号,类似于**来实现此目的,即2<symbol>9.还是我需要使用math模块?

解决方案

x的第n个根是x^(1/n),因此您可以执行9**(1/2.0)查找9的第2个根.通常,您可以将x的第n个根计算为:

x**(1/float(n))

您也可以执行1.0/n而不是1/float(n).必须使结果为float而不是int.

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

>>> 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:

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 ?

解决方案

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))

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆