Go 語言程式從複數中獲取實部
在本文中,我們將討論如何在 Go 語言中從複數中獲取實部。
在數學中,複數是可以表示為 a + ib 形式的數,其中 i 稱為虛數單位。虛數單位 i 的值為 $\mathrm{\sqrt{-1}}$,a 和 b 是實數。
第一部分即‘a’稱為複數的實部,虛數單位 i 後面的部分即‘b’稱為複數的虛部。
語法
func real(number complex128) float64
real() 函式用於獲取任何複數的實部。此函式接受 64 位或 128 位複數作為引數。如果複數由兩個 32 位數字組成,則其大小為 64 位;如果由兩個 64 位數字組成,則大小為 128 位。此函式返回一個 64 位浮點數作為實部。
下面編譯並執行原始碼。
示例 1
演算法
步驟 1 − 匯入 fmt 包。
步驟 2 − 開始 main 函式()。
步驟 3 − 建立並初始化複數變數。
步驟 4 − 使用 real() 函式獲取所選複數的實部。
步驟 5 − 在螢幕上列印實部。
示例
package main import "fmt" // fmt package allows us to print anything on the screen. // calling the main() function func main() { // initializing the number variable to store the complex number. var number complex64 // initializing the real_part variable to store real part of complex number. var real_part float32 // assigning the complex number. number = 9.89 + 10i // getting the real part of the complex number. real_part = real(number) // printing the result. fmt.Println( "The real part of the complex number", number, "is",real_part) }
輸出
The real part of the complex number (9.89+10i) is 9.89
程式碼描述
首先,我們匯入 fmt 包,該包允許我們列印任何內容。
開始 main() 函式。
初始化一個複數變數,並在其中儲存一個複數。
使用 real() 函式獲取複數的實部。
Go 語言中的 real() 函式是一個預定義函式,它接受一個複數作為引數,並返回該複數的實部(作為浮點數)。
將 real() 函式返回的值儲存到一個單獨的變數中。在這個特定的示例中,我們使用 real_part 變數來儲存實部值。請注意,real_part 是一個 32 位浮點型變數,而不是複數型別變數。
在螢幕上列印結果。
為了列印結果,我們使用了 fmt.Println() 函式。
示例 2
演算法
步驟 1 − 匯入 fmt 包。
步驟 2 − 初始化並定義 complex_number() 函式,該函式將包含獲取複數實部的邏輯。
步驟 3 − 開始 main 函式()。
步驟 4 − 初始化併為 32 位浮點型變數賦值。
步驟 5 − 呼叫 complex_number() 函式。
步驟 6 − 使用 complex() 函式從上述浮點值生成複數。
步驟 7 − 使用 real() 函式獲取所選複數的實部。
步驟 8 − 在螢幕上列印結果。
示例
package main import "fmt" // fmt package allows us to print anything on the screen. // creating the complex function func complex_number (number1, number2 float32) { // combining the real and imaginary parts to create the complex number. // complex() is an inbuilt function in GO language that takes two numbers as inputs and combines //them to produces the complex number. var complex_num = complex(number1, number2) // printing the resultant on the screen fmt.Println("The resultant complex number is : ", complex_num) // getting the real part of the complex number. var real_part float32 real_part = real(complex_num) // printing the real part on the screen. fmt.Println( "The real part of the entered complex number is :" ,real_part) } // calling the main() function func main() { // defining the variables that will store the values entered by the user var number1 float32 var number2 float32 // assigning values to the above defined variables number1 = 53.987 number2 = -2.85 // calling the above made complex_number() function complex_number(number1, number2) }
輸出
The resultant complex number is : (53.987-2.85i) The real part of the entered complex number is : 53.987
程式碼描述
首先,我們匯入 fmt 包,該包允許我們列印任何內容。
然後,我們建立並定義 complex_number() 函式,該函式將獲取並列印複數的實部。
開始 main() 函式。
初始化併為 number1 和 number2 變數賦值。
呼叫 complex_number() 函式,並將上述兩個浮點型值作為引數傳遞給它。
使用 complex() 函式從上述兩個值建立一個複數。此函式接受兩個引數,並將第一個引數視為複數的實部,第二個引數視為其虛部。
將其返回值儲存到一個單獨的變數中。現在使用 real() 函式獲取上述生成的複數的實部,並將結果儲存在一個變數中。在這個特定的示例中,我們使用 real_part 變數來儲存實部值。請注意,real_part 是一個 32 位浮點型變數,而不是複數型別變數。
在螢幕上列印結果。
為了列印結果,我們使用了 fmt.Println() 函式。
結論
我們已經成功編譯並執行了 Go 語言程式,該程式將透過示例獲取複數的實部。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP