Golang Program to get the magnitude of the given number


In this article we will discuss about how to get the magnitude of a number in Go language.

Magnitude of a quantity is defined as its numeric value. Magnitude of a number is always positive. In this article we will discuss about different methods by which we can obtain the magnitude of any number.

Syntax

func Abs(number float64) float64

Abs() is a method defined in math library. This method accepts a 64 bit float number as an argument and return the absolute value of it excluding the sign.

The source code to the above stated problem is compiled and executed below.

Example 1

The simplest way to obtain the magnitude of any integer in go language is by using a library function Abs(). This function returns the absolute value of an integer.

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − start the main function().

  • Step 3 − Initialize a variable of data type int and store value in it.

  • Step 4 − call the abs() function defined in math package and store the result.

  • Step 5 − print the results on the screen.

Example

// 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) }

Output

Magnitude of: -3.8 is  3.8

Description of the Code

  • First, we import the package fmt that allows us to print anything and math package to use Abs() method.

  • Then we call the main() function.

  • Now we need to get the number whose magnitude we wish to calculate.

  • Pass this number in Abs() method defined in math package and store the result in a separate variable.

  • Abs() is a method defined in math package that takes a float number as an argument and returns the numeric value of the number (excluding the sign).

  • Print the results on the screen using fmt.Println() function.

Example 2

Now there is one more way by which we can obtain the magnitude of a number. This method includes creating our own logic to implement the result.

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − start the main function().

  • Step 3 − Initialize a variable and store values in it.

  • Step 4 − Implement the logic and store the result.

  • Step 5 − print the results on the screen.

Example

The source code for that is compiled and executed below.

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) }

Output

Magnitude of: -3.8 is  3.8

Description of the Code

  • First, we import the package fmt that allows us to print anything and math package to use Pow() method defined in the math package.

  • Then we create and define the magnitude() function which will contain our logic to find the magnitude of the number.

  • Then we call the main() function.

  • Now we need to get the number whose magnitude we wish to calculate.

  • Call the magnitude function by passing the number in it as an argument.

  • To find the magnitude we are using the standard mathematical definition of magnitude which says magnitude of a number is the root of its square.

  • To find the square of a number in golang we are using math.Pow() method. This method takes two arguments one is the number whose power we need to raise and second is the number of times we want to raise the power for example: to find the square of a number using this function the code will be math.Pow(number, 2).

  • Once the square is calculated we need to store the result to a variable which in our case is temp and use the similar approach to find the root of the function. To find the root of a number using math.Pow() method in golang the code will be math.Pow(number, 0.5).

  • Once we have achieved the magnitude we need to return this value back.

  • We have then stored the final result in the result variable.

  • Then we print the results on the screen using fmt.Println() function.

Conclusion

We have successfully compiled and executed the Go language program that will get us the magnitude of any number along with examples using both library and user defined functions.

Updated on: 14-Nov-2022

349 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements