使用庫函式獲取浮點數餘數的Go語言程式
在本文中,我們將討論如何使用Go語言的庫函式獲取浮點數的餘數。
在程式設計中,浮點數是一個帶小數點的數字。例如:0.2、5.89、20.79等。
餘數:除法中的餘數定義為除法過程結束後剩下的結果。例如,如果4是被除數,2是除數,那麼用2除4後餘數為0。同樣,用3除4,餘數為1。
要獲取浮點值的餘數,我們可以使用`math.Mod()`庫函式。`mod()`函式的語法如下:
func mod(x, y float64) float64.
此函式接受兩個浮點型資料型別的引數。這裡用x和y表示,其中x是被除數,y是除數。此函式返回餘數,型別為浮點數。
下面編譯並執行使用庫函式獲取浮點數餘數的原始碼。
演算法
步驟1 - 匯入`fmt`和`math`包。
步驟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 predefined mathematical methods like mod(). // calling the main() function func main() { // initializing the variables to store the dividend, divisor and result respectively. var dividend, divisor, result float64 // assigning values of the dividend and the divisor dividend = 36.5 divisor = 3 // performing the division and storing the results result = math.Mod(dividend, divisor) // printing the results on the screen fmt.Println("The remainder of ", dividend, "/", divisor, "is: ") // limiting the result upto two decimal points fmt.Printf("%.2f", result) }
輸出
The remainder of 36.5 / 3 is: 0.50
程式碼描述
首先,我們分別匯入`fmt`包和`math`包,以便分別在螢幕上列印內容和使用數學方法。
然後我們呼叫`main()`函式。
之後,我們必須初始化64位浮點型變數,以賦值被除數、除數和儲存結果。
請注意,我們初始化了64位變數,因為`mod()`函式接受64位數字作為引數。
呼叫`math`包中定義的`Mod()`方法,並將被除數和除數的值作為引數傳遞給它。
將函式返回的值儲存在上面建立的名為`result`的單獨變數中。
使用`fmt.Println()`函式在螢幕上列印最終結果。
演算法
步驟1 - 匯入`fmt`和`math`包。
步驟2 - 初始化並定義`getRemainder()`函式。
步驟3 - 開始`main()`函式。
步驟4 - 初始化浮點型變數,並將被除數和除數的值賦給它們。
步驟5 - 呼叫`getRemainder()`函式。
步驟6 - 將其結果儲存在一個變數中。
步驟7 - 在螢幕上列印結果。
示例
使用這種方法獲取浮點數餘數的原始碼編譯和執行如下。
package main import ( "fmt" "math" ) // fmt package allows us to print anything on the screen. // math package allows us to use other pre-defined mathematical methods like mod(). // creating and defining the getRemainder() function. func getRemainder(dividend, divisor float64) float64 { // initializing a 64 bit value variable to store the result of division process var value float64 // performing the division and storing the results value = math.Mod(dividend, divisor) // returning the value of result return value } // calling the main() function func main() { // initializing the variables to store the dividend, divisor and result respectively. var dividend, divisor, result float64 // storing the values of dividend and divisor in the variables dividend = 20.5 divisor = 5 // storing the result result = getRemainder(dividend, divisor) // printing the results on the screen fmt.Println("The remainder of ", dividend, "/", divisor, "is: ") // limiting the result upto two decimal points fmt.Printf("%.2f", result) }
輸出
The remainder of 20.5 / 5 is: 0.50
程式碼描述
首先,我們必須分別匯入`fmt`包和`math`包,以便分別在螢幕上列印內容和使用數學方法。
然後我們建立並定義了`getRemainder()`函式,它將處理邏輯並在執行所有操作後返回最終值。
之後,我們必須初始化並賦值被除數和除數。
請注意,我們初始化了64位變數,因為`mod()`函式接受64位數字作為引數。
然後,我們必須呼叫`getRemainder()`函式,並將被除數和除數的值作為引數傳遞給它。
在`getRemainder()`函式中,我們使用`math.Mod()`方法來實現邏輯。
返回除法的結果值,在本例中為餘數。
將函式返回的值儲存在一個單獨的變數中。我們將其命名為`result`。
使用`fmt.Println()`函式在螢幕上列印結果。
結論
我們已經成功編譯並執行了Go語言程式,該程式使用庫函式以及示例來獲取浮點數的餘數。