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 ¶
- Variables
- func Add[N Number](a N, b N) N
- func CheckedAddReals[N Real](min N, max N, a N, b N) (N, bool)
- func CheckedAddRealsN[N Real](min N, max N, xs ...N) (N, bool)
- func CheckedMulReals[N Real](min N, max N, a N, b N) (N, bool)
- func CheckedSubReals[N Real](min N, max N, a N, b N) (N, bool)
- func Div[N Number](a N, b N) N
- func Max[N Number]() N
- func Min[N Number]() N
- func Mul[N Number](a N, b N) N
- func Smallest[N Real]() N
- func Sub[N Number](a N, b N) N
- type Complex
- type Number
- type Real
- type RealInfo
Constants ¶
This section is empty.
Variables ¶
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 CheckedAddReals ¶
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 ¶
CheckedAddRealsN is like CheckedAddReals, but returns the sum of any number of arguments, not just two.
func CheckedMulReals ¶ added in v2.0.9
CheckedMulReals returns (a * b, true) iff a times b would not overflow (see CheckedAddReals for notes).
func CheckedSubReals ¶
CheckedSubReals returns (a - b, true) iff a - b would not overflow (see CheckedAddReals for notes).
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
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 ¶
RealInfo stores filled-in information about a Real number type.
func (RealInfo[N]) CheckedAdd ¶
CheckedAdd returns (a + b, true) iff the sum would not overflow (including negative overflow if adding a negative number).
func (RealInfo[N]) CheckedAddN ¶
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
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 ¶
CheckedSub returns (a - b, true) iff a minus b would not overflow.