Golang程序 使用库函数获取商和余数
在这篇文章中,我们将讨论如何在GO语言中使用库函数获取商和余数。
语法
func Div(a, b, c uint) (q, r uint)
func Remainder(q, r float64) float64
div()函数接受三个参数为无符号整数,在计算除法过程后分别返回商和余数。
remainder()函数接受两个参数为64位浮点数,并在执行除法过程后以同样的格式返回余数。
下面是使用库函数获得商和除法余数的源代码的编译和执行。
用Div()查找两个数字的除法商数
算法
- 第1步– 导入软件包fmt和bits。
-
第2步 – 初始化并定义除法()函数。
-
第3步 – 启动主函数()。
-
第4步 – 初始化变量,并在其中存储红利和除数的值。
-
第5步 – 通过将股息和除数的值作为参数传递给函数,调用除法函数。
-
第6步 – 使用bits.Div()预定义库函数执行除法过程。
-
第7步 – 返回商和余数的值。
-
第8步 – 在屏幕上打印结果。
例子
Golang程序使用库函数获得两个数字的除法商 12.使用库函数获取商和余数的Golang程序
package main
import (
"fmt"
"math/bits"
)
// fmt package allows us to print anything on the screen.
// bits sub package defined in math package is used to perform bitwise operations on unsigned integer data types.
// initializing and defining the division() function
func division(dividend, divisor uint) (quotient, remainder uint) {
// Finding quotient and remainder Using Div() function
quotient, remainder = bits.Div(0, dividend, divisor)
// returning the results
return quotient, remainder
}
// calling the main() function
func main() {
// initializing the variables.
var dividend, divisor, quotient, remainder uint
// assigning the values of dividend.
dividend = 50
// assigning the values of divisor.
divisor = 7
// calling the division() function and storing the results in quotient and remainder variables
quotient, remainder = division(dividend, divisor)
// printing the Quotient on the screen
fmt.Println("The Quotient of ", dividend, "/", divisor, "is:")
fmt.Println(quotient)
// printing the Remainder on the screen
fmt.Println("The Remainder of ", dividend, "/", divisor, "is:")
fmt.Println(remainder)
}
输出
The Quotient of 50 / 7 is:
7
The Remainder of 50 / 7 is:
1
代码的描述
- 首先,我们导入软件包fmt和bits,它允许我们分别打印任何东西和进行位操作。
-
初始化并定义division()函数,它将包含执行除法过程的逻辑。
-
这个函数接受两个无符号int值的参数并返回结果。
-
我们在这里使用了无符号整数数据类型的变量,因为bit.Div()函数接受无符号整数值。
-
这个方法接受三个参数并返回两个值。为了两个数字的除法,我们在这里使用了其中一个值作为零。
-
这个函数返回的值是商和余数的值。
-
将该函数返回的值存储在不同的变量中,并返回它们。
-
启动main()函数。
-
初始化无符号int数据类型的变量,并在其中存储红利和除数的值。
-
调用除法()函数,并将股息和除数作为参数传给它。
-
将函数返回的结果存储在一个单独的变量中,即商和余数。
-
使用fmt.Println()函数在屏幕上打印结果。
用division()求两数除法的商
算法
-
第1步– 导入软件包fmt和比特。
-
第2步 – 初始化并定义division()函数。
-
第3步 – 启动main函数()。
-
第4步 – 初始化变量,并在其中存储分母和除数的值。
-
第5步 – 通过将股息和除数的值作为参数传递给函数,调用除法函数。
-
第6步 – 使用math.Remainder()预定义库函数执行除法过程。
-
第7步 – 返回商和余数的值。
-
第8步 – 在屏幕上打印结果。
例子
在go语言中还有一个库函数,可以用来计算两个整数除法的余数。这个例子将讨论这个方法。
package main
import (
"fmt"
"math"
)
// math package enables us to use other predefined mathematical functions.
// initializing and defining the division() function
func division(dividend, divisor float64) (remainder float64) {
// Finding the remainder Using math.Remainder() function
// storing the results in remainder variable
remainder = math.Remainder(dividend, divisor)
// returning the results
return remainder
}
// calling the main() function
func main() {
// initializing the variables.
var dividend, divisor, remainder float64
// storing the value of dividend
dividend = 100.0
// storing the value of divisor
divisor = 3.0
// calling the division() function
remainder = division(dividend, divisor)
// printing the Remainder on the screen
fmt.Println("The Remainder of ", dividend, "/", divisor, "is:")
fmt.Println(remainder)
}
输出
The Remainder of 100 / 3 is:
1
代码的描述
- 首先,我们导入软件包fmt和math,它们分别允许我们打印任何东西和使用预定义的数学运算。
-
初始化并定义除法()函数,它将包含执行除法过程的逻辑。
-
这个函数接受两个参数为64位浮点数并返回结果。
-
我们在这里使用了64位浮点数类型的变量,因为math.Remainder()函数接受64位浮点数类型的值。
-
math.Remainder()函数返回的值是余数,所以将其存储在一个单独的变量中。
-
返回这个值。
-
启动main()函数。
-
初始化各自的变量,并在其中存储红利和除数的值。
-
调用除法()函数,将红利和除数作为参数传给它。
-
将函数返回的结果存储在一个单独的变量中并打印其值。
总结
我们已经成功地编译并执行了Go语言程序,该程序将使用库函数和实例来获得除数和余数。
极客教程