Logo

Programming-Idioms

  • Go
  • C++
  • Fortran
  • C#

Idiom #355 Absolute value

Assign to y the absolute value of the number n

y = abs(n)
from math import fabs
y = fabs(n)
import "math"
y := math.Abs(x)

x and y have type float64
import "math/big"
y := new(big.Float)
y.Abs(x)

x and y have type *big.Float
import "math/big"
y := new(big.Int)
y.Abs(x)

x and y have type *big.Int
y := x
if y < 0 {
	y = -x
}

x and y have type int

Go does not have an absolute value function for int in the standard library
y := abs(n);

New implementation...
< >
programming-idioms.org