Golang 获取给定数字的大小

Golang 获取给定数字的大小

本文将讨论如何在Go语言中获取数字的大小。

数量的大小被定义为它的数值。数字的大小始终为正数。本文将讨论各种方法,通过这些方法可以获取任何数字的大小。

语法

func Abs(number float64) float64

Abs() 是在数学库中定义的一个方法。该方法接受一个64位浮点数作为参数,返回该数的绝对值,不包括符号。

上述问题的源代码在下面被编译并执行。

示例1

用Go语言获取任何整数的大小最简单的方法是使用一个名为Abs()的库函数。该函数返回一个整数的绝对值。

步骤

  • 步骤1 - 导入fmt包。

  • 步骤2 - 开始主函数 ()

  • 步骤3 - 初始化一个变量,数据类型为 int ,并将值存储在其中。

  • 步骤4 - 调用在math包中定义的 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 ,它允许我们打印任何内容,并且导入math包以使用 Abs() 方法。

  • 然后我们调用 main() 函数。

  • 现在我们需要获取希望计算幅值的数字。

  • 将这个数字传入math包中定义的 Abs() 方法,并将结果存储在一个单独的变量中。

  • Abs() 是在math包中定义的一个方法,它以一个浮点数作为参数,并返回该数字的数值(不包括符号)。

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

示例2

现在有一种更多的方法可以获取一个数字的幅值。这种方法包括创建我们自己的逻辑来实现结果。

步骤

  • 步骤1 - 导入 fmt 包。

  • 步骤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 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 package,它允许我们打印任何东西,以及math包来使用math包中定义的Pow()方法。

  • 然后我们创建和定义magnitude()函数,该函数将包含我们找到数字大小的逻辑。

  • 接下来我们调用main()函数。

  • 现在我们需要获取我们想要计算大小的数字。

  • 通过将数字作为参数传递给magnitude()函数来调用该函数。

  • 为了找到大小,我们使用了标准的数学定义,即数字的大小是其平方根。

  • 为了在 Go 语言中找到一个数的平方,我们使用了math.Pow()方法。此方法接受两个参数,第一个是我们需要求幂的数字,第二个是我们希望求幂的次数,例如:使用此函数求一个数的平方的代码将是math.Pow(number, 2)

  • 计算了平方后,我们需要将结果存储到一个变量中,我们的情况下是temp,并使用类似的方法找到函数的平方根。要在 Go 语言中找到一个数的平方根,我们使用math.Pow()方法的代码将是math.Pow(number, 0.5)

  • 一旦我们得到了大小,我们需要返回这个值。

  • 然后,我们将最终结果存储在result变量中。

  • 然后,我们使用fmt.Println()函数在屏幕上打印结果。

结论

我们已经成功编译并执行了 Go 语言程序,该程序通过使用库和用户定义的函数实例来获取任何数字的大小。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程