Abs()

StevenSwiniarski's avatar
Published Jun 9, 2023
Contribute to Docs

The Abs() function returns the absolute value of a given number.

Syntax

import "math"

result := math.Abs(number)

Where result is the absolute value of number.

  • The result of Abs(-Inf) is +Inf
  • The result of Abs(+Inf) is +Inf
  • The result of Abs(NaN) is NaN

Example

The following calculates the absolute value of -255 and prints out the result:

package main
import (
"fmt"
"math"
)
func main() {
number := math.Abs(-255)
fmt.Printf("%.1f\n", number)
}

The output will be:

255.0

Codebyte Example

The following example is runnable and shows the Abs() function handling an infinite value.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Go on Codecademy