Documentation
¶
Overview ¶
Package checked (operator/checked) implements operations on integers that are robust in the event of integer overflow.
Index ¶
- Variables
- func Abs[N constraints.Integer](min N, max N, i N) (N, bool)
- func Add[N constraints.Integer](min N, max N, a N, b N) (N, bool)
- func Inv[N constraints.Integer](min N, max N, i N) (N, bool)
- func Mul[N constraints.Integer](min N, max N, a N, b N) (N, bool)
- func Sub[N constraints.Integer](min N, max N, a N, b N) (N, bool)
- type Limits
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Int = Limits[int]{math.MinInt, math.MaxInt} Int8 = Limits[int8]{math.MinInt8, math.MaxInt8} Int16 = Limits[int16]{math.MinInt16, math.MaxInt16} Int32 = Limits[int32]{math.MinInt32, math.MaxInt32} Int64 = Limits[int64]{math.MinInt64, math.MaxInt64} Uint = Limits[uint]{0, math.MaxUint} Uint8 = Limits[uint8]{0, math.MaxUint8} Uint16 = Limits[uint16]{0, math.MaxUint16} Uint32 = Limits[uint32]{0, math.MaxUint32} Uint64 = Limits[uint64]{0, math.MaxUint64} )
Filled-in Limits about different integer types with minimum and maximum set to the largest range supported by the limit.
For signed integers, these are the appropriately sized math.MinInt and math.MaxInt constants. For unsigned integers, these are zero and the appropriately sized math.MaxUint constants.
Functions ¶
func Abs ¶
func Abs[N constraints.Integer](min N, max N, i N) (N, bool)
Abs returns (positive i, true) iff both i and the result lie between min and max inclusive. Otherwise, returns (0, false).
func Add ¶
func Add[N constraints.Integer](min N, max N, a N, b N) (N, bool)
Add returns (a + b, true) iff a, b, and the result all lie between min and max inclusive, otherwise returns (0, false). This calculation is robust in the event of integer overflow.
func Inv ¶
func Inv[N constraints.Integer](min N, max N, i N) (N, bool)
Inv returns (-i, true) iff both i and the result lie between min and max inclusive. Otherwise, returns (0, false).
func Mul ¶
func Mul[N constraints.Integer](min N, max N, a N, b N) (N, bool)
Mul returns (a * b, true) iff a, b, and the result all lie between min and max inclusive, otherwise returns (0, false). This calculation is robust in the event of integer overflow.
func Sub ¶
func Sub[N constraints.Integer](min N, max N, a N, b N) (N, bool)
Sub returns (a - b, true) iff a, b, and the result all lie between min and max inclusive, otherwise returns (0, false). This calculation is robust in the event of integer overflow.
Types ¶
type Limits ¶
type Limits[I constraints.Integer] struct { Min I Max I }
Limits are a pair of integer values defining a range between an (inclusive) minimum value and an (inclusive) maximum value.
Example ¶
ExampleLimits demonstrates checked subtraction against custom limits.
package main import ( "fmt" "github.com/tawesoft/golib/v2/operator/checked" ) func main() { // Call checked.Sub directly with min and max... { const min = 0 const max = 99 result, ok := checked.Sub(min, max, 10, 9) fmt.Printf("checked.Sub(min, max, 10, 9): %d, ok?=%t\n", result, ok) } // Or define a custom limit and call the Sub() method... { limit := checked.Limits[int]{Min: 0, Max: 99} result, ok := limit.Sub(10, 25) fmt.Printf("limit.Sub(10, 25): %d, ok?=%t\n", result, ok) } }
Output: checked.Sub(min, max, 10, 9): 1, ok?=true limit.Sub(10, 25): 0, ok?=false
func GetLimits ¶
func GetLimits[I constraints.Integer]() Limits[I]
GetLimits returns a filled-in [Limit] representing the widest possible minimum and maximum values for a generic integer type.
func (Limits[I]) Abs ¶
Abs returns (positive i, true) iff both i and the result lie between the Limit min and max inclusive. Otherwise, returns (0, false).
func (Limits[I]) Add ¶
Add returns (a + b, true) iff a, b, and the result all lie between the Limit min and max inclusive, otherwise returns (0, false). This calculation is robust in the event of integer overflow.
func (Limits[I]) Inv ¶
Inv returns (-i, true) iff both i and the result lie between the Limit min and max inclusive. Otherwise, returns (0, false).