intmath

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package intmath provides special-case integer arithmetic.

Index

Constants

This section is empty.

Variables

View Source
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

func MulDiv[T ~uint64](a, b, den T) (quo, rem T, err error)

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

func MulDivCeil[T ~uint64](a, b, den T) (quo T, extra T, err error)

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.

Jump to

Keyboard shortcuts

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