Python provides different functions and operators in order to calculate the square root of the given number. This number can be positive, negative, zero, real or complex number. There is also the Numpy library which providessqrt()
function in order to calculate square root.
Python提供了不同的函数和运算符,以便计算给定数字的平方根。 该数字可以是正数,负数,零,实数或复数。 还有一个Numpy库,它提供sqrt()
函数以计算平方根。
The most known and popular function is the sqrt()
function which is provided by the math
module or library. We can just provide the number we want to calculate square root for this function. In the following example, we will import the math
module and then use the sqrt() function.
最著名和最受欢迎的函数是math
模块或库提供的sqrt()
函数。 我们只需要提供要计算该函数平方根的数字即可。 在下面的示例中,我们将导入math
模块,然后使用sqrt()函数。
import math
math.sqrt(25)
//Result will be 5.0
math.sqrt(9)
//Result will be 3.0
math.sqrt(90)
//Result will be 9.486832980505138
math.sqrt(900)
//Result will be 30.0
math.sqrt(90000)
//Result will be 300.0
math.sqrt(49)
//Result will be 7.0
Math Module sqrt() Function 数学模块sqrt()函数
Zero is a special number where it can produce different results according to the different operations. When we take the square root of the zero we will get zero as a result.
零是一个特殊的数字,它可以根据不同的运算产生不同的结果。 当我们取零的平方根时,结果将为零。
import math
math.sqrt(0)
//The result is 0.0
math.sqrt(0.0)
//The result is 0.0
Square Root of Zero 零平方根
Floating-point numbers are different from decimal numbers. They provide some floating-point parts which can be confusing while calculating the square root. In this example, we will look at examples of calculating floating-point numbers.
浮点数与十进制数不同。 它们提供了一些浮点部分,这些部分在计算平方根时可能会造成混淆。 在此示例中,我们将查看计算浮点数的示例。
import math
math.sqrt(9.9)
// Result is 3.146426544510455
math.sqrt(9.0)
// Result is 3.0
math.sqrt(9.9225)
// Result is 3.15
math.sqrt(99.81)
// Result is 9.990495483208027
math.sqrt(111.408025)
// Result is 10.555
Square Root Of Floating-Point Numbers 浮点数的平方根
The square of any real number can not be negative. So if we try to get the square root of a negative number we will get an error that is related with the math domain as we can see below.
任何实数的平方不能为负。 因此,如果我们尝试获得负数的平方根,则会得到与数学域相关的错误,如下所示。
import math
math.sqrt(-25)
math.sqrt(-0)
Square Root of Negative Numbers 负数的平方根
Interestingly if we try to get zero as negative number square root we will get zero. Because zero cannot be positive or negative. For other negative numbers, we will get the ValueError
and math domain error
.
有趣的是,如果我们尝试获得零作为负数平方根,我们将获得零。 因为零不能为正或负。 对于其他负数,我们将获得ValueError
和math domain error
。
math
module also provides the pow()
function which is used to calculate the square of the given number. There is also **
operator which is the same as pow()
function. We will provide the number and 1/2
as the square root number.
math
模块还提供pow()
函数,该函数用于计算给定数字的平方。 还有**
运算符,它与pow()
函数相同。 我们将提供数字和1/2
作为平方根数字。
import math
math.pow(9,1/2)
//The result is 3.0
math.pow(9,0.5)
//The result is 3.0
math.pow(99.81,0.5)
//The result is 9.990495483208027
math.pow(111.408025,0.5)
//The result is 10.555
math.pow(111.408025,1/2)
//The result is 10.555
Math Module pow() Function 数学模块pow()函数
We can also calculate the square root of the real or complex numbers we can use the cmath
library which should be imported before usage. In these examples, we will provide the real numbers like 1+2j
, 5+10j
etc.
我们还可以计算实数或复数的平方根,我们可以使用应在使用前导入的cmath
库。 在这些示例中,我们将提供诸如1+2j
, 5+10j
等的实数。
import cmath
cmath.sqrt(1+2j)
//The result is (1.272019649514069+0.7861513777574233j)
cmath.sqrt(10+2j)
//The result is (3.177895453534113+0.3146736620576702j)
cmath.sqrt(10+20j)
//The result is (4.022479320953552+2.486028939392892j)
cmath.sqrt(9+9j)
//The result is (3.29605234040343+1.365269581686682j)
cmath.sqrt(64+25j)
//The result is (8.14584352738277+1.5345249338488343j)
Real or Complex Numbers Square Root 实数或复数平方根
Mathematics is magic where we can express different calculations in different ways. We can use square operator in order to calculate the square root. The square **
operator is used with the 1/2
number to calculate square root. We can also use 0.5
according to the 1/2
which is the same value with a different presentation.
数学是魔术,我们可以用不同的方式表达不同的计算结果。 我们可以使用平方算子来计算平方根。 平方**
运算符与1/2
数一起使用以计算平方根。 我们也可以根据1/2
使用0.5
,这是相同的值,但表示方式不同。
a = (9)**(1/2)
// The result is 3.0
a=(100)**(1/2)
// The result is 10.0
a=(64)**(1/2)
// The result is 8.0
a=(64)**(0.5)
// The result is 8.0
a=(99.81)**(0.5)
// The result is 9.990495483208027
a=(111.408025)**(0.5)
// The result is 10.555
Square Root Calculation with Square Operator 平方算子的平方根计算
numpy
is a third party library and module which provides calculations about matrix, series, big data, etc. numpy also provides the sqrt()
and pow()
functions and we can use these function to calculate square root.
numpy
是第三方库和模块,可提供有关矩阵,序列,大数据等的计算。numpy还提供sqrt()
和pow()
函数,我们可以使用这些函数来计算平方根。
import numy
numpy.sqrt(9)
//The result is 3
numpy.pow(9,1/2)
//The result is 3
翻译自: https://www.poftut.com/square-root-calculation-in-python/