Math absolute function abs() in Golang
To find absolute value of the given number in Go language can be converted using abs() function. An abs() function provided in the math package. It takes number value as parameter and returns absolute value of given number.
Syntax
 1 func math.Abs(num float64) float64
num : A float number to find absolute value of given number
Return : It returns float value of given number by converting absolute
Converting absolute number
 1 package main
 2 
 3 import (
 4 	"fmt"
 5 	"math"
 6 )
 7 
 8 func main() {
 9 
 10 	a, b := -10.20, 39.32
 11 
 12 	fmt.Println("Absolute value of variable a : ", math.Abs(a))
 13 	fmt.Println("Absolute value of variable b : ", math.Abs(b))
 14 }
 15 
In the above example, we have define two float values which initial value. A first value is negative and second is positive. A abs() function converts the given number into absolute number and printed on console.
Output
 1 Absolute value of variable a :  10.2
 2 Absolute value of variable b :  39.32

Determine the difference between values

Determine the difference between values
 1 package main
 2 
 3 import (
 4 	"fmt"
 5 	"math"
 6 )
 7 
 8 func main() {
 9     
 10     var num1, num2 float64
 11     
 12     fmt.Printf("Enter first number : ")
 13     fmt.Scanf("%f", &num1)
 14     fmt.Printf("Enter second number : ")
 15     fmt.Scanf("%f", &num2)
 16     
 17     res := num1 - num2
 18     diff := math.Abs(res)
 19     fmt.Printf("\nThe difference between numbers are : %.2f", diff)
 20 }
In the above example, a variables declared of type float32 and accept inputs from user. A difference of value is computed using minus operator. A value can be either positive or negative, hence to compute difference in positive number, a math module Abs() function called that convert positive number. A difference between number is printed using printf() function.
Output
 1 Enter first number : 25.36
 2 Enter second number : 63.21
 3 The difference between numbers are : 37.85
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us