Overview
math package of GO provides an Abs method that can be used to get the absolute value of a number.
Below is the signature of the function. It takes input a float and also returns a float.
func Abs(x float64) float64
Some special cases of Abs function are
- Abs(±0) = ±0
- Abs(±Inf) = ±Inf
- Abs(NaN) = NaN
Code:
package main
import (
"fmt"
"math"
)
func main() {
res := math.Abs(-2)
fmt.Println(res)
res = math.Abs(2)
fmt.Println(res)
res = math.Abs(3.5)
fmt.Println(res)
res = math.Abs(-3.5)
fmt.Println(res)
}
Output:
2
2
3.5
3.5