Golang程序 使用库函数获取浮点数余数

Golang程序 使用库函数获取浮点数余数

在这篇文章中,我们将讨论如何使用Go语言中的库函数获得浮点数的余数。

在编程中,浮点数是一个带有小数点的数字。例如:0.2,5.89,20.79,等等。

余数。除法中的余数是指除法过程结束后剩下的结果。例如,如果4是红利,2是除数,那么4除以2后,余数为0。同样,4除以3后,余数为1。

为了得到浮动值的余数,我们可以使用math.Mod()库函数。mod()函数的语法定义如下

func mod(x, y float64) float64.

这个函数需要两个数据类型为float的参数。在这里,它们被描述为x和y,其中x是除数,y是被除数。该函数将余数作为一个浮点数返回。

下面是使用库函数获得浮点数余数的源代码的编译和执行。

算法

  • 第1步– 导入软件包fmt和math。

  • 第2步 – 启动main()函数。

  • 第3步 – 初始化数据类型为float的变量并给它们赋值。

  • 第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位数字作为参数。

  • 调用数学包中定义的Mod()方法,并将股息和除数的值作为参数传给它。

  • 将函数返回的值存储在上面创建的一个单独的变量中,作为结果。

  • 使用fmt.Println()函数在屏幕上打印最终结果。

运算法则

  • 第1步– 导入软件包fmt和数学。

  • 第2步– 初始化并定义getRemainder()函数。

  • 第3步- -启动main()函数。

  • 第4步– 初始化数据类型为float的变量,并在其中分配股息和除数的值。

  • 第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()方法来实现这一逻辑。

  • 返回除法结果的值,在本例中是余数。

  • 将函数返回的值存储在一个单独的变量中。我们将其命名为结果。

  • 使用fmt.Println()函数在屏幕上打印结果。

总结

我们已经成功地编译并执行了Go语言程序,使用库函数和例子来获得浮点数的余数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程