checked

package
v2.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package checked (operator/checked) implements operations on integers that are robust in the event of integer overflow.

Index

Examples

Constants

This section is empty.

Variables

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

func (l Limits[I]) Abs(i I) (I, bool)

Abs calls checked.Abs with min and max filled in with the associated Limits values.

func (Limits[I]) Add

func (l Limits[I]) Add(a I, b I) (I, bool)

Add calls checked.Add with min and max filled in with the associated Limits values.

func (Limits[I]) Inv

func (l Limits[I]) Inv(i I) (I, bool)

Inv calls checked.Inv with min and max filled in with the associated Limits values.

func (Limits[I]) Mul

func (l Limits[I]) Mul(a I, b I) (I, bool)

Mul calls checked.Mul with min and max filled in with the associated Limits values.

func (Limits[I]) Sub

func (l Limits[I]) Sub(a I, b I) (I, bool)

Sub calls checked.Sub with min and max filled in with the associated Limits values.

Jump to

Keyboard shortcuts

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