Go語言程式獲取有理數的分子
在本文中,我們將討論如何從有理數中獲取分子。
語法
func NewRat(a, b int64) *Rat func (x *Rat) Num() *Int
NewRat() 函式以兩個整數作為輸入引數,並以 a/b 的形式返回一個有理數。其中 a 是分母,b 是有理數的分母。
Go 語言中的Num() 函式返回有理數的分子,結果為整數格式。
方法 1
Go 語言程式從有理數獲取分子。
演算法
步驟 1 - 匯入 fmt 和 big 包。
步驟 2 - 呼叫 main() 函式。
步驟 3 - 初始化一個變數來儲存有理數。使用 big.NewRat() 建立有理數。
步驟 4 - 使用 num() 函式獲取有理數的分子。
步驟 5 - 儲存結果並將其列印到螢幕上
示例
使用庫函式獲取有理數分子的原始碼如下所示:
// Including the main package
package main
// Importing fmt and math/big
import (
"fmt"
"math/big"
)
// fmt package allows us to print anything on the screen
// big defined in math package allows us to use various predefined methods like Num()
// Calling main
func main() {
fmt.Println("Golang Program to get the numerator of a rational number")
// NewRat creates a new Rational number with numerator a and denominator b
number := big.NewRat(10, 16)
// Printing the result on the screen
fmt.Println("The rational number is", number)
// getting the numerator of the rational number
// storing the result in a result variable
result := number.Num()
// printing the numerator on the screen
fmt.Println("The numerator of rational number", number, "is:", result)
}
輸出
Golang Program to get the numerator of a rational number The rational number is 5/8 The numerator of rational number 5/8 is: 5
描述
首先,我們需要匯入 fmt 包,它允許我們列印任何內容,以及 big 包,以使用各種預定義的方法。
呼叫 main() 函式。
使用 big 包中定義的 NewRat() 函式建立一個有理數,並向函式提供兩個輸入作為引數。這些輸入以這樣一種方式給出,即第一個被視為有理數的分子,第二個是被視為其分母。
然後將數字儲存在名為 number 的變數中。
現在我們需要獲取此數字的分子並將其列印到螢幕上。
要獲取分子,請使用 big 包中提供的 Num() 函式。此函式以 int 格式返回分子作為結果。
將結果值儲存在變數中。
使用 println() 函式將結果列印到螢幕上。
方法 2
Go 語言程式使用兩個函式從有理數獲取分子。
演算法
步驟 1 - 匯入 fmt、big 和 rand 包。
步驟 2 - 建立 getNumerator() 函式。
步驟 3 - 呼叫 main() 函式。
步驟 4 - 呼叫 getNumerator() 函式。
步驟 5 - 初始化一個變數以在其中儲存有理數。使用 big.NewRat() 建立有理數。
步驟 6 - 使用 Num() 函式獲取有理數的分子。
步驟 7 - 返回此值。
步驟 8 - 儲存結果並將其列印到螢幕上。
示例
使用兩個函式獲取有理數分子的原始碼如下所示:
// Including the main package
package main
// Importing fmt and math/big
import (
"fmt"
"math/big"
"math/rand"
)
// fmt package allows us to print anything on the screen.
// big package allows us to use various predefined mathematical methods like a rat
// rand package allows us to generate random numbers.
// Initializing the getNumerator() function.
func getNumerator() *big.Int {
// NewRat creates a new Rational number with numerator a and denominator b
number := big.NewRat(int64(rand.Intn(200)), int64(rand.Intn(200)))
// Printing the result
fmt.Println("The rational number is", number)
// getting the numerator of the rational number
// storing the result in a result variable
result := number.Num()
// returning the result
return result
}
// Calling main
func main() {
fmt.Println("Golang Program to get the numerator of a rational number")
// calling the getNumerator() function
result := getNumerator()
// printing the numerator on the screen
fmt.Println("The numerator of rational number is:", result)
}
輸出
Golang Program to get the numerator of a rational number The rational number is 27/29 The numerator of rational number is: 27
描述
匯入 fmt、big 和 rand 包。
fmt 包允許我們列印任何內容到螢幕上。big 包允許我們使用各種預定義的數學方法,例如 rat,而 rand 包允許我們生成隨機數。
建立並定義 getNumerator() 函式。此函式將生成有理數並返回其分子。
這裡,我們使用 NewRat() 方法生成有理數。我們使用 RandInt() 函式生成高達 200 的隨機整數值,這些值充當此函式的輸入,以分子和分母的形式。
我們將以這種方式生成的 32 位 int 數字轉換為 64 位,因為 NewRat() 接受 64 位值作為輸入。
使用 Num() 函式獲取分子的值並將結果儲存在變數中。
返回此值。
使用 fmt.Printf() 函式將最終結果列印到螢幕上。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP