在 Go 語言中查詢複數的共軛
在數學中,複數是由實部和虛部組成的數。實部是普通數字,虛部是虛數單位的倍數,虛數單位用字母 i 表示。Go 語言內建支援複數,並允許使用者對其執行各種運算。在本文中,我們將討論如何在 Go 語言中查詢複數的共軛。
什麼是複數的共軛?
複數的共軛是透過改變其虛部的符號得到的。例如,如果我們有一個複數 a + bi,其共軛將是 a - bi。換句話說,複數 a + bi 的共軛是 a - bi。
在 Go 語言中查詢複數的共軛
在 Go 語言中,我們可以使用內建 cmplx 包提供的 cmplx.Conj() 函式來查詢複數的共軛。此函式以複數作為引數,並返回其共軛。
以下是一個演示如何在 Go 語言中查詢複數的共軛的示例程式:
示例
package main
import (
"fmt"
"math/cmplx"
)
func main() {
// Create a complex number
c := complex(3, 4)
// Find the conjugate
conj := cmplx.Conj(c)
// Print the result
fmt.Println("The conjugate of", c, "is", conj)
}
輸出
The conjugate of (3+4i) is (3-4i)
在上面的程式中,我們首先建立一個複數 c,其實部和虛部分別設定為 3 和 4。然後,我們使用 cmplx.Conj() 函式查詢其共軛,並將結果賦值給變數 conj。最後,我們使用 fmt.Println() 函式列印結果。
以下還有另一個示例:
示例
package main
import (
"fmt"
"math/cmplx"
)
func main() {
// Creating a complex number with imaginary part as 0
z1 := complex(5, 0)
// Taking the conjugate of the complex number
z2 := cmplx.Conj(z1)
// Printing the original and conjugate complex numbers
fmt.Printf("Original Complex Number: %v\n", z1)
fmt.Printf("Conjugate of Complex Number: %v\n", z2)
}
輸出
Original Complex Number: (5+0i) Conjugate of Complex Number: (5-0i)
在此示例中,我們建立了一個複數 z1,其實部為 5,虛部為 0。然後,我們使用 cmplx.Conj 函式查詢 z1 的共軛,並將其儲存在 z2 中。最後,我們列印原始複數和共軛複數。由於 z1 的虛部為 0,因此共軛的虛部也為 0,我們看到結果與原始數字相同。
結論
在本文中,我們討論瞭如何使用內建 cmplx 包提供的 cmplx.Conj() 函式在 Go 語言中查詢複數的共軛。我們希望本文能幫助您理解複數的概念以及如何在 Go 語言中使用它們。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP