numbers

package
v2.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 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   = nInt
	Int8  = nInt8
	Int16 = nInt16
	Int32 = nInt32
	Int64 = nInt64

	Uint   = nUint
	Uint8  = nUint8
	Uint16 = nUint16
	Uint32 = nUint32
	Uint64 = nUint64

	Float32 = nFloat32
	Float64 = nFloat64
)

Filled-in RealInfo information about different Real 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 CheckedMulReals added in v2.0.9

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

CheckedMulReals returns (a * b, true) iff a times b would not overflow (see CheckedAddReals for notes).

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 Div added in v2.0.11

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

Div returns a / b

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 Mul added in v2.0.11

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

Mul returns a * b

func Smallest added in v2.0.11

func Smallest[N Real]() N

Smallest returns the smallest representable real number greater than zero. For all integer types, this is always 1. For float types, this is math.SmallestNonzeroFloat32 or math.SmallestNonzeroFloat64.

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
	Smallest 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]) CheckedMul added in v2.0.9

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

CheckedMul returns (a * b, true) iff a times b would not overflow (including negative overflow if multiplying by a negative number).

func (RealInfo[N]) CheckedSub

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

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

Jump to

Keyboard shortcuts

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