Golang 如何找到二次方程的所有根

Golang 如何找到二次方程的所有根

在本教程中,我们将使用Golang编程语言找到二次方程的根。本教程包括两种不同的编程方法来找到二次方程的根。

解释

二次方程是指具有二次幂的方程。二次方程的标准方程为:

\mathrm{a:x:^{\wedge}:2:+:b:x:+:c:=:0}

在上述方程中,a、b和c是系数,其中a不能为零。为了确定二次方程的根的性质(即它们是实数还是虚数),我们首先需要使用以下公式计算方程的判别式:

\mathrm{D:=:b:^{*}:b:-:4:^{*}:a:^{*}:c}

如果D大于零,则根是不同且实数。

如果D等于零,则根相等且为实数。

如果D小于零,则根是虚数。

现在我们将使用以下公式找到方程的根:

\mathrm{X1:=:(-:b:+:underRoot:(b:^{*}:b:-:4:^{*}:a:^{*}:c):/:2:^{*}:a}

\mathrm{X1:=:(-:b:-:underRoot:(b:^{*}:b:-:4:^{*}:a:^{*}:c):/:2:^{*}:a}

步骤

步骤1 - 声明用于系数和判别式的变量。

步骤2 - 用相应的值初始化变量。

步骤3 - 使用相应的公式计算判别式。

步骤4 - 根据判别式的值找到根。

示例

在本示例中,我们将在函数内找到二次方程的根。

package main
import (

   // fmt package provides the function to print anything
   "fmt"

   // math package is to use all the math-related predefined functions
   "math"
)
func main() {

   // declaring the variables to store the value

   // of the coefficients of quadratic equation
   var a, b, c float64

   // declaring a variable of float type to store discriminant

   // of the quadratic equation
   var d float64

   // initializing the coefficients variable with respective value
   a = 1
   b = 1
   c = 1
   fmt.Println("Finding the roots of the equation with the below coefficients within the function:")
   fmt.Println("a =", a)
   fmt.Println("b =", b)
   fmt.Println("c =", c)

   // finding the discriminant using the respective formulae
   d = b*b - 4*a*c
   if d > 0 {
      fmt.Println("Roots are real and different.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      root2 := (-b - math.Sqrt(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", root1)
      fmt.Println("Second Root", root2)
   } else if d == 0 {
      fmt.Println("Roots are equal and same.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      fmt.Println("The root is", root1)
   } else {
      fmt.Println("Roots are complex.")
      realPart := -b / (2 * a)
      imaginaryPart := math.Sqrt(math.Abs(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", realPart, "+", "i", imaginaryPart)
      fmt.Println("Second Root", realPart, "-", "i", imaginaryPart)
   }
}

输出

Finding the roots of the equation with the below coefficients within the function:
a = 1
b = 1
c = 1
Roots are complex.
The roots are:
First Root -0.5 + i 0.8660254037844386
Second Root -0.5 - i 0.8660254037844386

步骤

步骤1 - 声明系数和判别式的变量。

步骤2 - 使用相应的值初始化变量。

步骤3 - 使用相应的公式计算判别式。

步骤4 - 根据判别式的值计算根。

示例

在本示例中,我们将在单独的函数中找到二次方程的根。

package main
import (

   // fmt package provides the function to print anything
   "fmt"

   // math package is to use all the math related predefined functions
   "math"
)
func rootsOfQuadraticEquation(a, b, c float64) {

   // declaring a variable of float type to store discriminant

   // of the quadratic equation
   var d float64

   // finding the discriminant using the respective formulae
   d = b*b - 4*a*c
   if d > 0 {
      fmt.Println("Roots are real and different.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      root2 := (-b - math.Sqrt(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", root1)
      fmt.Println("Second Root", root2)
   } else if d == 0 {
      fmt.Println("Roots are equal and same.")
      root1 := (-b + math.Sqrt(d)) / (2 * a)
      fmt.Println("The root is", root1)
   } else {
      fmt.Println("Roots are complex.")
      realPart := -b / (2 * a)
      imaginaryPart := math.Sqrt(math.Abs(d)) / (2 * a)
      fmt.Println("The roots are:")
      fmt.Println("First Root", realPart, "+", "i",   imaginaryPart)
      fmt.Println("Second Root", realPart, "-", "i", imaginaryPart)
   }
}
func main() {

   // declaring the variables to store the value

   // of the coefficients of quadratic equation
   var a, b, c float64

   // initializing the coefficients variable with respective value
   a = 4
   b = 12
   c = 9
   fmt.Println("Finding the roots of the equation with the below coefficients in the seperate function:")
   fmt.Println("a =", a)
   fmt.Println("b =", b)
   fmt.Println("c =", c)
   rootsOfQuadraticEquation(a, b, c)
}

输出

Finding the roots of the equation with the below coefficients in the seperate function:
a = 4
b = 12
c = 9Roots are equal and same.
The root is -1.5

结论

这是在Golang中找到二次方程根的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用该函数。要了解更多关于Golang的内容,您可以浏览这些教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程