📜  在 Golang 中找到复数的平方根

📅  最后修改于: 2021-10-25 02:19:08             🧑  作者: Mango

Go 语言在 cmplx 包的帮助下为复数的基本常量和数学函数提供了内置支持。您可以借助 math/cmplx 包提供的Sqrt()函数找到指定复数的平方根。在这个函数,q 被选择为使得 real(q) >= 0 并且 imag(q) 与 imag(y) 具有相同的符号。因此,要访问Sqrt()函数,您需要借助 import 关键字在程序中添加一个 math/cmplx 包。

句法:

func Sqrt(y complex128) complex128

让我们在给定示例的帮助下讨论这个概念:

示例 1:

// Golang program to illustrate how to find
// the square root of the given complex number
  
package main
  
import (
    "fmt"
    "math/cmplx"
)
  
// Main function
func main() {
  
    // Finding the square root of 
    // the specified complex number
    // Using Sqrt() function
    res_1 := cmplx.Sqrt(8 - 6i)
    res_2 := cmplx.Sqrt(-4 + 12i)
    res_3 := cmplx.Sqrt(-3 - 9i)
  
    // Displaying the result
    fmt.Printf("Result 1: %.2f", res_1)
    fmt.Printf("\nResult 2:  %.2f", res_2)
    fmt.Printf("\nResult 3:  %.2f", res_3)
}

输出:

Result 1: (3.00-1.00i)
Result 2:  (2.08+2.89i)
Result 3:  (1.80-2.50i)

示例 2:

// Golang program to illustrate how to find
// the square root of the given complex number
  
package main
  
import (
    "fmt"
    "math/cmplx"
)
  
// Main function
func main() {
  
    cnumber_1 := complex(0, 2)
    cnumber_2 := complex(4, 6)
  
    // Finding square root
    cvalue_1 := cmplx.Sqrt(cnumber_1)
    cvalue_2 := cmplx.Sqrt(cnumber_2)
  
    // Sum of the given square roots
    res := cvalue_1 + cvalue_2
  
    // Displaying results
    fmt.Println("Complex Number 1: ", cnumber_1)
    fmt.Printf("Square Root 1: %.1f", cvalue_1)
  
    fmt.Println("\nComplex Number 2: ", cnumber_2)
    fmt.Printf("Square Root: %.1f ", cvalue_2)
    fmt.Printf("\nSum : %.1f", res)
  
}

输出:

Complex Number 1:  (0+2i)
Square Root 1: (1.0+1.0i)
Complex Number 2:  (4+6i)
Square Root: (2.4+1.3i) 
Sum : (3.4+2.3i)