Python中的平方根计算

时间:2024-11-20 07:44:26

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()函数以计算平方根。

数学模块sqrt()函数 (Math Module sqrt() Function)

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()函数。

  1. import math
  2. (25)
  3. //Result will be 5.0
  4. (9)
  5. //Result will be 3.0
  6. (90)
  7. //Result will be 9.486832980505138
  8. (900)
  9. //Result will be 30.0
  10. (90000)
  11. //Result will be 300.0
  12. (49)
  13. //Result will be 7.0
Math Module sqrt() Function
Math Module sqrt() Function
数学模块sqrt()函数

零平方根(Square Root of Zero)

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.

零是一个特殊的数字,它可以根据不同的运算产生不同的结果。 当我们取零的平方根时,结果将为零。

  1. import math
  2. (0)
  3. //The result is 0.0
  4. (0.0)
  5. //The result is 0.0
Square Root of Zero
Square Root of Zero
零平方根

浮点数的平方根(Square Root Of Floating-Point Numbers)

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.

浮点数与十进制数不同。 它们提供了一些浮点部分,这些部分在计算平方根时可能会造成混淆。 在此示例中,我们将查看计算浮点数的示例。

  1. import math
  2. (9.9)
  3. // Result is 3.146426544510455
  4. (9.0)
  5. // Result is 3.0
  6. (9.9225)
  7. // Result is 3.15
  8. (99.81)
  9. // Result is 9.990495483208027
  10. (111.408025)
  11. // Result is 10.555
Square Root Of Floating-Point Numbers
Square Root Of Floating-Point Numbers
浮点数的平方根

负数的平方根(Square Root of Negative 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.

任何实数的平方不能为负。 因此,如果我们尝试获得负数的平方根,则会得到与数学域相关的错误,如下所示。

  1. import math
  2. math.sqrt(-25)
  3. math.sqrt(-0)
Square Root of Negative Numbers
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.

有趣的是,如果我们尝试获得零作为负数平方根,我们将获得零。 因为零不能为正或负。 对于其他负数,我们将获得ValueErrormath domain error

Cmath模块sqrt()函数 (Cmath Module sqrt() Function)

数学模块pow()函数(Math Module pow() Function)

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作为平方根数字。

  1. import math
  2. (9,1/2)
  3. //The result is 3.0
  4. (9,0.5)
  5. //The result is 3.0
  6. (99.81,0.5)
  7. //The result is 9.990495483208027
  8. (111.408025,0.5)
  9. //The result is 10.555
  10. (111.408025,1/2)
  11. //The result is 10.555
Math Module pow() Function
Math Module pow() Function
数学模块pow()函数

实数或复数平方根(Real or Complex Numbers Square Root)

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+2j5+10j等的实数。

  1. import cmath
  2. (1+2j)
  3. //The result is (1.272019649514069+0.7861513777574233j)
  4. (10+2j)
  5. //The result is (3.177895453534113+0.3146736620576702j)
  6. (10+20j)
  7. //The result is (4.022479320953552+2.486028939392892j)
  8. (9+9j)
  9. //The result is (3.29605234040343+1.365269581686682j)
  10. (64+25j)
  11. //The result is (8.14584352738277+1.5345249338488343j)
Real or Complex Numbers Square Root
Real or Complex Numbers Square Root
实数或复数平方根

平方算子的平方根计算(Square Root Calculation with Square Operator)

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 ,这是相同的值,但表示方式不同。

  1. a = (9)**(1/2)
  2. // The result is 3.0
  3. a=(100)**(1/2)
  4. // The result is 10.0
  5. a=(64)**(1/2)
  6. // The result is 8.0
  7. a=(64)**(0.5)
  8. // The result is 8.0
  9. a=(99.81)**(0.5)
  10. // The result is 9.990495483208027
  11. a=(111.408025)**(0.5)
  12. // The result is 10.555
Square Root Calculation with Square Operator
Square Root Calculation with Square Operator
平方算子的平方根计算

Numpy sqrt()函数(Numpy sqrt() Function)

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()函数,我们可以使用这些函数来计算平方根。

  1. import numy
  2. (9)
  3. //The result is 3
  4. (9,1/2)
  5. //The result is 3

翻译自: /square-root-calculation-in-python/