Golang 获取有理数的分母
在本文中,我们将讨论如何从有理数中获取分母。
有理数 - 在数学中,有理数被定义为可以表示为a/b的形式,其中a和b是整数值。例如- 1/3,5/8等。
请注意,有理数的分母永远不能为零。
语法
func NewRat(a, b int64) *Rat
func (x *Rat) Denom() *Int
NewRat() 函数接受两个整数作为输入参数,并返回一个形如a/b的有理数。其中a是有理数的分子,b是有理数的分母。
Denom() 函数在Go语言中以整数格式返回有理数的分母作为结果。
方法1
Go语言程序用于从有理数中获取分母。
步骤
- 步骤1 – 导入fmt和big包。
-
步骤2 – 调用main()函数。
-
步骤3 – 初始化一个变量来存储有理数。使用big.NewRat()来创建有理数。
-
步骤4 – 使用denom()函数来获取有理数的分母。
-
步骤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 Denom()
// Calling main
func main() {
fmt.Println("Golang Program to get the denomenator of a rational number")
// NewRat creates a new Rational number with numerator a and denominator b
number := big.NewRat(6, 8)
// Printing the result on the screen
fmt.Println("The rational number is", number)
// getting the denomenator of the rational number
// storing the result in a result variable
result := number.Denom()
// printing the denomenator on the screen
fmt.Println("The denomenator of rational number", number, "is: ", result)
}
输出
Golang Program to get the denomenator of a rational number
The rational number is 3/4
The denomenator of rational number 3/4 is: 4
描述
- 首先,我们需要导入fmt包,该包允许我们打印任何东西,以及big包,以使用各种预定义方法。
-
调用main()函数。
-
使用big包中定义的NewRat()函数创建一个有理数,通过将两个输入作为函数的参数提供。这些输入以这样的方式给出,即第一个被视为有理数的分子,第二个被视为其分母。
-
然后将数字存储在名为number的变量中。
-
现在,我们需要获取该数字的分母并将其打印在屏幕上。
-
要获取分母,请使用big包中的Denom()函数。此函数以int格式返回分母作为结果。
-
将结果的值存储在一个变量中。
-
使用println()函数将结果打印在屏幕上。
方法2
Go语言程序使用两个函数从有理数中获取分母。
步骤
- 步骤1-导入fmt,big和rand包。
-
步骤2-创建一个getDenominator()函数。
-
步骤3-调用main()函数。
-
步骤4-调用getDenominator()函数。
-
步骤5-初始化一个变量,用于存储有理数。使用big.NewRat()创建有理数。
-
步骤6-使用denom()函数获取有理数的分母。
-
步骤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 rat
// rand package allows us to generate random numbers.
// Initializing the getDenominator() function.
func getDenominator() *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 denomenator of the rational number
// storing the result in a result variable
result := number.Denom()
// returning the result
return result
}
// Calling main
func main() {
fmt.Println("Golang Program to get the denomenator of a rational number")
// calling the getDenominator() function
result := getDenominator()
// printing the denomenator on the screen
fmt.Println("The denomenator of rational number is:", result)
}
输出
Golang Program to get the denomenator of a rational number
The rational number is 27/29
The denomenator of rational number is: 29
说明
-
导入 fmt、big 和 rand 包。
-
fmt 包允许我们在屏幕上打印任何内容。big 包允许我们使用各种预定义的数学方法,如 rat,而 rand 包允许我们生成随机数。
-
创建并定义 getDenominator() 函数。该函数将生成有理数并返回其分母。
-
在这里,我们使用 NewRat() 方法生成有理数。我们使用 RandInt() 函数生成最大为 200 的随机整数值,作为该函数的分子和分母的输入。
-
我们将以这种方式生成的 32 位整数转换为 64 位,因为 NewRat() 函数接受 64 位值作为输入。
-
使用 Denom() 函数获取分母的值,并将结果存储在一个变量中。
-
返回该值。
-
使用 fmt.Printf() 函数将最终结果打印在屏幕上。