Documentation
¶
Overview ¶
Package intmath provides special-case integer arithmetic.
Index ¶
- Variables
- func BoundedAdd[T constraints.Unsigned](a, b, ceil T) T
- func BoundedMultiply[T constraints.Unsigned](a, b, ceil T) T
- func BoundedSubtract[T constraints.Unsigned](a, b, floor T) T
- func CeilDiv[T ~uint64](num, den T) T
- func MulDiv[T ~uint64](a, b, den T) (quo, rem T, err error)
- func MulDivCeil[T ~uint64](a, b, den T) (quo T, extra T, err error)
Constants ¶
This section is empty.
Variables ¶
var ErrOverflow = errors.New("overflow")
ErrOverflow is returned if a return value would have overflowed its type.
Functions ¶
func BoundedAdd ¶
func BoundedAdd[T constraints.Unsigned](a, b, ceil T) T
BoundedAdd returns `min(a+b,ceil)` without overflow.
func BoundedMultiply ¶
func BoundedMultiply[T constraints.Unsigned](a, b, ceil T) T
BoundedMultiply returns `min(a*b,ceil)` without overflow.
func BoundedSubtract ¶
func BoundedSubtract[T constraints.Unsigned](a, b, floor T) T
BoundedSubtract returns `max(a-b,floor)` without underflow.
func CeilDiv ¶
func CeilDiv[T ~uint64](num, den T) T
CeilDiv returns `ceil(num/den)`, i.e. the rounded-up quotient.
func MulDiv ¶
MulDiv returns the quotient and remainder of `(a*b)/den` without overflow in the event that `a*b>=2^64`. However, if the quotient were to overflow then ErrOverflow is returned.
func MulDivCeil ¶
MulDivCeil is equivalent to MulDiv except that it returns the rounded-up quotient and the complement of the remainder, i.e. the amount that would have had to be added to `a*b` to result in the same quotient exactly.
Types ¶
This section is empty.