Go 語言程式獲取給定數字的模長
在本文中,我們將討論如何在 Go 語言中獲取數字的模長。
數量的模長定義為其數值。數字的模長始終為正。在本文中,我們將討論獲取任意數字模長的不同方法。
語法
func Abs(number float64) float64
Abs() 是在 math 庫中定義的方法。此方法接受一個 64 位浮點數作為引數,並返回其絕對值,不包括符號。
上面提到的問題的原始碼已編譯並在下面執行。
示例 1
在 Go 語言中獲取任何整數模長的最簡單方法是使用庫函式 Abs()。此函式返回整數的絕對值。
演算法
步驟 1 - 匯入 fmt 包。
步驟 2 - 啟動 main 函式()。
步驟 3 - 初始化一個數據型別為int 的變數,並在其中儲存值。
步驟 4 - 呼叫 math 包中定義的abs() 函式並存儲結果。
步驟 5 - 在螢幕上列印結果。
示例
// golang program to get the magnitude of a number package main import ( "fmt" "math" ) // fmt package allows us to print anything on the screen // math package allows us to use mathematical methods in go language. // calling the main() function func main() { // initializing a variable number and storing a value in it. var number float64 = -3.8 // calling the absolute method defined in math package result := math.Abs(number) // printing the result on the screen. fmt.Println("Magnitude of:",number,"is ", result) }
輸出
Magnitude of: -3.8 is 3.8
程式碼描述
首先,我們匯入允許我們列印任何內容的fmt 包和使用Abs() 方法的 math 包。
然後我們呼叫main() 函式。
現在我們需要獲取我們希望計算其模長的數字。
將此數字傳遞給 math 包中定義的Abs() 方法,並將結果儲存在單獨的變數中。
Abs() 是 math 包中定義的方法,它將浮點數作為引數,並返回數字的數值(不包括符號)。
使用 fmt.Println() 函式在螢幕上列印結果。
示例 2
現在還有另一種方法可以獲取數字的模長。此方法包括建立我們自己的邏輯來實現結果。
演算法
步驟 1 - 匯入fmt 包。
步驟 2 - 啟動 main 函式()。
步驟 3 - 初始化一個變數並在其中儲存值。
步驟 4 - 實現邏輯並存儲結果。
步驟 5 - 在螢幕上列印結果。
示例
該程式碼已編譯並在下面執行。
package main import ( "fmt" "math" ) // fmt package allows us to print anything on the screen. // math package allows us to use mathematical methods in go language. // creating and defining the magnitude function. func magnitude (number float64) float64 { // initializing a temp variable to store the value of square var temp float64 // implementing the logic to get the magnitude. // here we are using the logic from definition of magnitude of a // number which says magnitude of a number is the root of its square. // finding the square of the given number using pow() method defined // in math library // then storing the result of square in a temp variable temp = math.Pow(number, 2) // finding the root of the above obtained result and storing the // final answer in result variable. result := math.Pow(temp, 0.5) // returning the final result return result } // calling the main() function func main() { // initializing a variable number. var number float64 // assigning value to the number number = -8.9 // calling the magnitude() function and passing the number in it result := magnitude(number) // printing the result on the screen. fmt.Println("Magnitude of:",number,"is ", result) }
輸出
Magnitude of: -3.8 is 3.8
程式碼描述
首先,我們匯入允許我們列印任何內容的 fmt 包和使用 math 包中定義的 Pow() 方法的 math 包。
然後我們建立並定義magnitude() 函式,其中將包含我們查詢數字模長的邏輯。
然後我們呼叫main() 函式。
現在我們需要獲取我們希望計算其模長的數字。
透過將數字作為引數傳遞給它來呼叫 magnitude 函式。
為了找到模長,我們使用模長的標準數學定義,該定義指出數字的模長是其平方的根。
為了在 golang 中找到數字的平方,我們使用math.Pow() 方法。此方法接受兩個引數,一個是需要求冪的數字,第二個是需要求冪的次數,例如:要使用此函式查詢數字的平方,程式碼將為 math.Pow(number, 2)。
計算出平方後,我們需要將結果儲存到一個變數中,在本例中為 temp,並使用類似的方法來查詢函式的根。為了在 golang 中使用math.Pow() 方法查詢數字的根,程式碼將為 math.Pow(number, 0.5)。
獲得模長後,我們需要將此值返回。
然後我們將最終結果儲存在 result 變數中。
然後我們使用fmt.Println() 函式在螢幕上列印結果。
結論
我們已成功編譯並執行了 Go 語言程式,該程式將為我們提供任何數字的模長,以及使用庫函式和使用者定義函式的示例。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP