在Go語言中查詢複數的自然對數


在數學和計算機程式設計領域,自然對數是一個重要的函式,用於許多計算。一個數的自然對數是以e(尤拉數)為底的對數,其中e約等於2.71828。在Go語言中,math/cmplx包提供了各種函式來執行復數運算,包括複數的自然對數。

在本文中,我們將討論如何藉助示例在Go語言中查詢複數的自然對數。

語法

在Go語言中查詢複數自然對數的語法為:

func Log(z complex128) complex128

引數

math/cmplx包的Log()函式接受一個引數,該引數是complex128型別的複數。

返回值

math/cmplx包的Log()函式接受一個引數,該引數是complex128型別的複數。

示例1:查詢複數的自然對數

讓我們取一個複數z = 3 + 4i並找到它的自然對數。

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   // Creating a complex number
   z := complex(3, 4)
   
   // Finding the natural logarithm of the complex number
   ln := cmplx.Log(z)
   
   // Displaying the result
   fmt.Println("Natural Logarithm of", z, "is", ln)
}

輸出

Natural Logarithm of (3+4i) is (1.6094379124341003+0.9272952180016122i)

示例2:查詢負複數的自然對數

讓我們取一個負複數z = -5 + 12i並找到它的自然對數。

package main

import (
   "fmt"
   "math/cmplx"
)
   
func main() {
   // Creating a complex number
   z := complex(-5, 12)
   
   // Finding the natural logarithm of the complex number
   ln := cmplx.Log(z)
   
   // Displaying the result
   fmt.Println("Natural Logarithm of", z, "is", ln)
}

輸出

Natural Logarithm of (-5+12i) is (2.5649493574615367+1.965587446494658i)

示例3:查詢零複數的自然對數

讓我們取一個零複數z = 0 + 0i並找到它的自然對數。

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   // Creating a complex number
   z := complex(0, 0)
   
   // Finding the natural logarithm of the complex number
   ln := cmplx.Log(z)
   
   // Displaying the result
   fmt.Println("Natural Logarithm of", z, "is", ln)
}

輸出

Natural Logarithm of (0+0i) is (-Inf+0i)

示例4:查詢實數的自然對數

讓我們取一個實數z = 5並找到它的自然對數。

package main

import (
   "fmt"
   "math"
)

func main() {
   // Taking natural logarithm of a real number
   x := 5.0
   result := math.Log(x)
   
   // Displaying the result
   fmt.Printf("Natural logarithm of %v is %v", x, result)
}

輸出

Natural logarithm of 5 is 1.6094379124341003

結論

在本文中,我們學習瞭如何使用cmplx.Log函式在Go語言中查詢複數的自然對數。我們還查看了一些演示此函式用法的示例。

cmplx.Log函式接受一個複數作為輸入並返回其自然對數。如果輸入為零,則函式返回-Inf。如果輸入為負數,則函式返回NaN。複數的自然對數定義為其模數的對數加上虛數單位乘以其輻角。

我們還查看了一些示例,這些示例展示瞭如何查詢具有不同值的複數的自然對數。這些示例包括查詢純虛數、純實數、負實數以及具有實部和虛部的複數的自然對數。

更新於:2023年4月17日

瀏覽量:155

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告