File: xmath.go

package info (click to toggle)
golang-github-bradenaw-juniper 0.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 872 kB
  • sloc: sh: 27; makefile: 2
file content (14 lines) | stat: -rw-r--r-- 487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Package xmath contains extensions to the standard library package math.
package xmath

// Abs returns the absolute value of x. It panics if this value is not representable, for example
// because -math.MinInt32 requires more than 32 bits to represent and so does not fit in an int32.
func Abs[T ~int | ~int8 | ~int16 | ~int32 | ~int64](x T) T {
	if x < 0 {
		if -x == x {
			panic("can't xmath.Abs minimum value: positive equivalent not representable")
		}
		return -x
	}
	return x
}