Golang math.Abs - Syntax
mixed abs(mixed n);
The math.Abs() function in Golang is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
mixed abs(mixed n);
mixed n |
Specifies the real number (short int, integer, long, float or double) / infinity / NaN (*Required) |
---|---|
Return mixed |
Returns a number respective to an input argument. |
Convert the positive and negative integer to an absolute value.
package main
import (
"fmt"
"math"
)
func main() {
// Positive integer value
n := math.Abs(9)
fmt.Println("The absolute value of 9 is", n)
// Negative integer value
x := math.Abs(-6)
fmt.Println("The absolute value of -6 is", x)
}
Convert the positive and negative float to an absolute value.
package main
import (
"fmt"
"math"
)
func main() {
// Positive floating point value
n := math.Abs(9.3)
fmt.Println("The absolute value of 9.3 is", n)
// Negative floating point value
x := math.Abs(-6.3)
fmt.Println("The absolute value of -6.3 is", x)
}
Convert the positive and negative Infinity (i.e. math.Inf(±1)) to an absolute value.
package main
import (
"fmt"
"math"
)
func main() {
// Positive ∞ infinite value
n := math.Inf(+1)
n = math.Abs(n)
fmt.Println("The absolute value of +Inf is", n)
// Negative ∞ infinite value
x := math.Inf(-1)
x = math.Abs(x)
fmt.Println("The absolute value of -Inf is", x)
}
Convert the Nan (i.e. math.NaN()) to an absolute value.
package main
import (
"fmt"
"math"
)
func main() {
n := math.NaN()
n = math.Abs(n)
fmt.Println("The absolute value of NaN is", n)
}