Golang程序 来获取给定数字的大小
在这篇文章中,我们将讨论如何用Go语言获得一个数字的大小。
一个量的大小被定义为它的数值。一个数字的大小总是正的。在这篇文章中,我们将讨论不同的方法,通过这些方法我们可以获得任何数字的大小。
语法
func Abs(number float64) float64
Abs() 是数学库中定义的一个方法。这个方法接受一个64位的浮点数作为参数,并返回它的绝对值,不包括符号。
下面是上述问题的源代码的编译和执行。
例1
在go语言中获得任何整数的大小的最简单方法是使用库函数Abs()。这个函数返回一个整数的绝对值。
算法
- 第1步– 导入软件包fmt。
-
第2步– 启动主 函数()
-
第3步– 初始化一个数据类型为 int 的变量,并在其中存储数值。
-
第4步–调用数学包中定义的 abs() 函数并存储结果。
-
第5步–在屏幕上打印结果。
例子
// golang program to get the magnitude of a number
package main
import (
"fmt"
"math"
)
// fmt package allows us to print anything on the screen
// math package allows us to use mathematical methods in go language.
// calling the main() function
func main() {
// initializing a variable number and storing a value in it.
var number float64 = -3.8
// calling the absolute method defined in math package
result := math.Abs(number)
// printing the result on the screen.
fmt.Println("Magnitude of:",number,"is ", result)
}
输出
Magnitude of: -3.8 is 3.8
代码的描述
-
首先,我们导入允许我们打印任何东西的软件包 fmt 和使用 Abs() 方法的数学软件包。
-
然后我们调用 main() 函数。
-
现在我们需要得到一个我们想计算的数字的大小。
-
将这个数字传给数学包中定义的 Abs() 方法,并将结果存储在一个单独的变量中。
-
Abs() 是一个定义在数学包中的方法,它接收一个浮点数作为参数,并返回该数字的数值(不包括符号)。
-
使用fmt.Println()函数在屏幕上打印结果。
例2
现在还有一种方法可以让我们获得一个数字的大小。这个方法包括创建我们自己的逻辑来实现这个结果。
算法
- 第1步– 导入软件包 fmt
-
第2步– 启动主 函数()
-
第3步– 初始化一个变量并在其中存储数值。
-
第4步– 实现逻辑并存储结果。
-
第5步–在屏幕上打印结果。
例子
下面是其源代码的编译和执行。
package main
import (
"fmt"
"math"
)
// fmt package allows us to print anything on the screen.
// math package allows us to use mathematical methods in go language.
// creating and defining the magnitude function.
func magnitude (number float64) float64 {
// initializing a temp variable to store the value of square
var temp float64
// implementing the logic to get the magnitude.
// here we are using the logic from definition of magnitude of a
// number which says magnitude of a number is the root of its square.
// finding the square of the given number using pow() method defined
// in math library
// then storing the result of square in a temp variable
temp = math.Pow(number, 2)
// finding the root of the above obtained result and storing the
// final answer in result variable.
result := math.Pow(temp, 0.5)
// returning the final result
return result
}
// calling the main() function
func main() {
// initializing a variable number.
var number float64
// assigning value to the number
number = -8.9
// calling the magnitude() function and passing the number in it
result := magnitude(number)
// printing the result on the screen.
fmt.Println("Magnitude of:",number,"is ", result)
}
输出
Magnitude of: -3.8 is 3.8
代码的描述
- 首先,我们导入允许我们打印任何东西的包fmt和数学包,以使用数学包中定义的Pow()方法。
-
然后,我们创建并定义 magnitude() 函数,该函数将包含我们寻找数字大小的逻辑。
-
然后我们调用 main() 函数。
-
现在我们需要得到我们想计算的数字的大小。
-
通过将数字作为参数传入,调用magnitude函数。
-
为了找到幅度,我们使用幅度的标准数学定义,即一个数字的幅度是其平方的根。
-
在golang中,为了找到一个数字的平方,我们使用 math.Pow() 方法。这个方法需要两个参数,一个是我们需要提高其幂值的数字,另一个是我们要提高幂值的次数,例如:使用这个函数找到一个数字的平方,其代码将是math.Pow(number, 2)。
-
一旦计算出平方,我们需要将结果存储到一个变量中,在我们的例子中是temp,并使用类似的方法来寻找函数的根。在golang中使用 math.Pow() 方法来寻找一个数字的根,代码将是math.Pow(number, 0.5)。
-
一旦我们达到了这个量级,我们需要将这个值返回。
-
然后我们将最终的结果存储在结果变量中。
-
然后我们用 fmt.Println() 函数在屏幕上打印结果。
结论
我们已经成功地编译并执行了Go语言程序,该程序将使我们获得任何数字的大小,同时还有使用库和用户定义函数的例子。
极客教程