Golang 获取有理数的分子

Golang 获取有理数的分子

在本文中,我们将讨论如何从有理数中获取分子。

语法

func NewRat(a, b int64) *Rat
func (x *Rat) Num() *Int

NewRat() 函数接受两个整数作为输入参数,并以a/b的形式返回一个有理数。这里a是有理数的分子,b是有理数的分母。

Num() 函数在Go语言中以整数格式返回有理数的分子作为结果。

方法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() 函数在屏幕上打印最终结果。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程