📜  在 Golang 中求复数的自然对数

📅  最后修改于: 2021-10-24 13:36:26             🧑  作者: Mango

Go 语言在 cmplx 包的帮助下为复数的基本常量和数学函数提供了内置支持。您可以借助 math/cmplx 包提供的Log()函数找到指定复数的自然对数。因此,您需要借助 import 关键字在程序中添加一个 math/cmplx 包来访问 Log()函数。

句法:

func Log(y complex128) complex128

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

示例 1:

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

输出:

Result 1: (0.0+1.6i)
Result 2:  (2.5+1.9i)
Result 3:  (2.2-1.9i)

示例 2:

// Golang program to illustrate how to find the
// natural logarithm 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 natural logarithm
    cvalue_1 := cmplx.Log(cnumber_1)
    cvalue_2 := cmplx.Log(cnumber_2)
  
    // Sum of the given natural logarithm
    res := cvalue_1 + cvalue_2
  
    // Displaying results
    fmt.Println("Complex Number 1: ", cnumber_1)
    fmt.Printf("Natural logarithm 1: %.1f", cvalue_1)
  
    fmt.Println("\nComplex Number 2: ", cnumber_2)
    fmt.Printf("Natural logarithm 2: %.1f ", cvalue_2)
    fmt.Printf("\nSum : %.1f", res)
  
}

输出:

Complex Number 1:  (0+2i)
Natural logarithm 1: (0.7+1.6i)
Complex Number 2:  (4+6i)
Natural logarithm 2: (2.0+1.0i) 
Sum : (2.7+2.6i)