math

package
v1.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrOverflow = errors.New("integer overflow")

Functions

func Max added in v1.5.0

func Max[T Integer]() uint64

Max returns the maximum value for a type T.

The function panics if the type is not supported.

func MaxInt

func MaxInt(a, b int) int

func MaxInt64

func MaxInt64(a, b int64) int64

func Min added in v1.5.0

func Min[T Integer]() int64

Min returns the minimum value for a type T.

func MinInt

func MinInt(a, b int) int

func MinInt64

func MinInt64(a, b int64) int64

func MustConvert added in v1.5.0

func MustConvert[FROM Integer, TO Integer](a FROM) TO

MustConvert converts a value of type T to a value of type U. It panics if the conversion would cause an overflow.

See SafeConvert for non-panicking version.

func MustConvertInt added in v1.5.0

func MustConvertInt[T Integer](a T) int

func MustConvertInt16 added in v1.5.0

func MustConvertInt16[T Integer](a T) int16

func MustConvertInt32 added in v1.3.0

func MustConvertInt32[T Integer](a T) int32

MustConvertInt32 takes an Integer and converts it to int32. Panics if the conversion overflows.

func MustConvertInt64 added in v1.5.0

func MustConvertInt64[T Integer](a T) int64

func MustConvertUint added in v1.5.0

func MustConvertUint[T Integer](a T) uint

func MustConvertUint8 added in v1.5.0

func MustConvertUint8[T Integer](a T) uint8

func MustConvertUint16 added in v1.5.0

func MustConvertUint16[T Integer](a T) uint16

func MustConvertUint32 added in v1.3.0

func MustConvertUint32[T Integer](a T) uint32

MustConvertInt32 takes an Integer and converts it to int32. Panics if the conversion overflows.

func MustConvertUint64 added in v1.5.0

func MustConvertUint64[T Integer](a T) uint64

func SafeAddClipInt64 added in v1.3.0

func SafeAddClipInt64(a, b int64) int64

SafeAddClipInt64 adds two int64 integers and clips the result to the int64 range.

func SafeAddInt32

func SafeAddInt32(a, b int32) (int32, error)

SafeAddInt32 adds two int32 integers.

func SafeAddInt64 added in v1.3.0

func SafeAddInt64(a, b int64) (int64, error)

SafeAddInt64 adds two int64 integers.

func SafeConvert added in v1.5.0

func SafeConvert[F Integer, T Integer](from F) (T, error)

SafeConvert converts a value of type T to a value of type U. It returns an error if the conversion would cause an overflow.

func SafeConvertInt8

func SafeConvertInt8(a int64) (int8, error)

SafeConvertInt8 takes an int64 and checks if it overflows.

func SafeConvertInt16 added in v1.5.0

func SafeConvertInt16[T Integer](a T) (int16, error)

SafeConvertInt16 takes a int and checks if it overflows.

func SafeConvertInt32

func SafeConvertInt32[T Integer](a T) (int32, error)

SafeConvertInt32 takes a int and checks if it overflows.

func SafeConvertInt64 added in v1.5.0

func SafeConvertInt64[T Integer](a T) (int64, error)

SafeConvertInt64 takes a int and checks if it overflows.

func SafeConvertUint8

func SafeConvertUint8(a int64) (uint8, error)

SafeConvertUint8 takes an int64 and checks if it overflows.

func SafeConvertUint16 added in v1.5.0

func SafeConvertUint16[T Integer](a T) (uint16, error)

SafeConvertUint16 takes a int and checks if it overflows.

func SafeConvertUint32 added in v1.3.0

func SafeConvertUint32[T Integer](a T) (uint32, error)

SafeConvertInt32 takes a int and checks if it overflows.

func SafeConvertUint64 added in v1.5.0

func SafeConvertUint64[T Integer](a T) (uint64, error)

SafeConvertUint64 takes a int and checks if it overflows.

func SafeMulInt64 added in v1.3.0

func SafeMulInt64(a, b int64) (int64, bool)

func SafeSubClipInt64 added in v1.3.0

func SafeSubClipInt64(a, b int64) int64

func SafeSubInt32

func SafeSubInt32(a, b int32) (int32, error)

SafeSubInt32 subtracts two int32 integers.

func SafeSubInt64 added in v1.3.0

func SafeSubInt64(a, b int64) (int64, bool)

Types

type Fraction

type Fraction struct {
	// The portion of the denominator in the faction, e.g. 2 in 2/3.
	Numerator uint64 `json:"numerator"`
	// The value by which the numerator is divided, e.g. 3 in 2/3.
	Denominator uint64 `json:"denominator"`
}

Fraction defined in terms of a numerator divided by a denominator in uint64 format. Fraction must be positive.

func ParseFraction

func ParseFraction(f string) (Fraction, error)

ParseFractions takes the string of a fraction as input i.e "2/3" and converts this to the equivalent fraction else returns an error. The format of the string must be one number followed by a slash (/) and then the other number.

func (Fraction) String

func (fr Fraction) String() string

type Integer added in v1.3.0

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL