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是分母。
Go语言中的 Denom() 函数将有理数的分母作为结果以整数格式返回。
方法一
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的变量中。
-
现在我们需要得到这个数字的分母并将其打印在屏幕上。
-
为了得到分母,可以使用大软件包中的Denom()函数。这个函数以int格式返回分母的结果。
-
将结果的值存储在一个变量中。
-
使用println()函数在屏幕上打印结果。
方法二
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()函数在屏幕上打印最终结果。
极客教程