numbers

package
v2.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package numbers implements assorted things that operate on numbers, such as generic access to limits, addition checked for integer overflow, and functions implementing builtin operators like addition so that they can be passed to higher order functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	Int = RealInfo[int]{
		Min:     Min[int](),
		Max:     Max[int](),
		Epsilon: Epsilon[int](),
		Signed:  true,
	}
	Int8 = RealInfo[int8]{
		Min:     Min[int8](),
		Max:     Max[int8](),
		Epsilon: Epsilon[int8](),
		Signed:  true,
	}
	Int16 = RealInfo[int16]{
		Min:     Min[int16](),
		Max:     Max[int16](),
		Epsilon: Epsilon[int16](),
		Signed:  true,
	}
	Int32 = RealInfo[int32]{
		Min:     Min[int32](),
		Max:     Max[int32](),
		Epsilon: Epsilon[int32](),
		Signed:  true,
	}
	Int64 = RealInfo[int64]{
		Min:     Min[int64](),
		Max:     Max[int64](),
		Epsilon: Epsilon[int64](),
		Signed:  true,
	}

	Uint = RealInfo[uint]{
		Min:     Min[uint](),
		Max:     Max[uint](),
		Epsilon: Epsilon[uint](),
	}
	Uint8 = RealInfo[uint8]{
		Min:     Min[uint8](),
		Max:     Max[uint8](),
		Epsilon: Epsilon[uint8](),
	}
	Uint16 = RealInfo[uint16]{
		Min:     Min[uint16](),
		Max:     Max[uint16](),
		Epsilon: Epsilon[uint16](),
	}
	Uint32 = RealInfo[uint32]{
		Min:     Min[uint32](),
		Max:     Max[uint32](),
		Epsilon: Epsilon[uint32](),
	}
	Uint64 = RealInfo[uint64]{
		Min:     Min[uint64](),
		Max:     Max[uint64](),
		Epsilon: Epsilon[uint64](),
	}

	Float32 = RealInfo[float32]{
		Min:     Min[float32](),
		Max:     Max[float32](),
		Epsilon: Epsilon[float32](),
		Signed:  true,
	}
	Float64 = RealInfo[float64]{
		Min:     Min[float64](),
		Max:     Max[float64](),
		Epsilon: Epsilon[float64](),
		Signed:  true,
	}
)

Filled-in information about different number types.

Functions

func Add

func Add[N Number](a N, b N) N

Add returns a + b

func CheckedAddReals

func CheckedAddReals[N Real](min N, max N, a N, b N) (N, bool)

CheckedAddReals returns (a + b, true) iff the sum would not overflow (including negative overflow if adding a negative number).

The arguments min and max should be the greatest magnitude negative (zero for unsigned) and the greatest magnitude maximum numbers representable by the type - see the generic functions Min and Max.

If you know the type of N in advance, you can use RealInfo.CheckedAdd, which populates min and max for you.

func CheckedAddRealsN

func CheckedAddRealsN[N Real](min N, max N, xs ...N) (N, bool)

CheckedAddRealsN is like CheckedAddReals, but returns the sum of any number of arguments, not just two.

func CheckedSubReals

func CheckedSubReals[N Real](min N, max N, a N, b N) (N, bool)

CheckedSubReals returns (a - b, true) iff a - b would not overflow (see CheckedAddReals for notes).

func Epsilon

func Epsilon[N Real]() N

Epsilon returns the smallest representable real number greater than zero. For all integer types, this is always 1.

func Max

func Max[N Number]() N

Max returns the maximum representable number of type N.

For integers, min <= 0 < max, and min != -max.

For floating point numbers, -inf < min < 0 < epsilon < max < inf, and min == -max.

e.g. Max[uint8]() // returns 255

func Min

func Min[N Number]() N

Min returns the minimum representable number of type N. By minimum, this means the negative number with the greatest magnitude.

For integers, min <= 0 < max, and min != -max.

For floating point numbers, -inf < min < 0 < epsilon < max < inf, and min == -max.

e.g. Max[uint8]() // returns 255

func Sub

func Sub[N Number](a N, b N) N

Sub returns a - b

Types

type Complex

type Complex interface {
	~complex64 | ~complex128
}

Complex represents any complex number

type Number

type Number interface {
	~int8 | ~int16 | ~int32 | ~int64 | ~int |
		~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint |
		~float32 | ~float64 |
		~complex64 | ~complex128
}

Number represents anything you can perform arithmetic on using standard Go operators (like a + b, or a ^ b).

See also Real, which doesn't include the complex numbers.

type Real

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

Real represents any real (i.e. not complex) number you can perform arithmetic on using standard Go operators (like a + b, or a ^ b).

type RealInfo

type RealInfo[N Real] struct {
	Min     N
	Max     N
	Epsilon N
	Signed  bool
}

RealInfo stores filled-in information about a Real number type.

func (RealInfo[N]) CheckedAdd

func (t RealInfo[N]) CheckedAdd(a N, b N) (N, bool)

CheckedAdd returns (a + b, true) iff the sum would not overflow (including negative overflow if adding a negative number).

func (RealInfo[N]) CheckedAddN

func (t RealInfo[N]) CheckedAddN(xs ...N) (N, bool)

CheckedAddN is like [RealType.CheckedAdd], but returns the sum of any number of arguments, not just two.

func (RealInfo[N]) CheckedSub

func (t RealInfo[N]) CheckedSub(a N, b N) (N, bool)

CheckedSub returns (a - b, true) iff a - b would not overflow.

Jump to

Keyboard shortcuts

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