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.
Functions ¶
func Abs ¶
func Abs[N constraints.Integer](min N, max N, i N) (N, bool)
Abs returns (-i, true) for i < 0, or (i, true) for i >= 0 iff the result lies between min and max inclusive. Otherwise returns (0, false).
The input arguments must satisfy the inequality `min <= i <= max`.
func Add ¶
func Add[N constraints.Integer](min N, max N, a N, b N) (N, bool)
Add returns (a + b, true) iff the result lies between min and max inclusive, otherwise returns (0, false). This calculation is robust in the event of integer overflow.
The input arguments must satisfy the inequalities: `min <= a <= max` and `min <= b <= max`.
func Inv ¶
func Inv[N constraints.Integer](min N, max N, i N) (N, bool)
Inv returns (-i) iff the result lies between min and max inclusive. Otherwise returns (0, false).
The input arguments must satisfy the inequality `min <= i <= max`.
func Mul ¶
func Mul[N constraints.Integer](min N, max N, a N, b N) (N, bool)
Mul returns (a * b, true) iff the result lies between min and max inclusive, otherwise returns (0, false). This calculation is robust in the event of integer overflow.
The input arguments must satisfy the inequalities: `min <= a <= max` and `min <= b <= max`.
func Sub ¶
func Sub[N constraints.Integer](min N, max N, a N, b N) (N, bool)
Sub returns (a + b, true) iff the result lies between min and max inclusive, otherwise returns (0, false). This calculation is robust in the event of integer overflow.
The input arguments must satisfy the inequalities: `min <= a <= max` and `min <= b <= max`.
Types ¶
type Limits ¶
type Limits[I constraints.Integer] struct { Min I Max I }
Limits provides a convenient way to fill the min and max arguments to the checked operator functions. The inequality Min <= Max must be satisfied.
Example ¶
package main import ( "fmt" "github.com/tawesoft/golib/v2/operator/checked" ) func main() { { 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) } { 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] for the given integer type.
func (Limits[I]) Abs ¶
Abs calls checked.Abs with min and max filled in with the associated Limits values.
func (Limits[I]) Add ¶
Add calls checked.Add with min and max filled in with the associated Limits values.
func (Limits[I]) Inv ¶
Inv calls checked.Inv with min and max filled in with the associated Limits values.
func (Limits[I]) Mul ¶
Mul calls checked.Mul with min and max filled in with the associated Limits values.