使用库函数获取浮点数余数的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语言程序,该程序使用库函数以及示例来获取浮点数的余数。

更新于:2022年10月25日

432 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.