package
4.2.0
Repository: https://github.com/fusionfoundation/efsn.git
Documentation: pkg.go.dev

# README

Build Status

overflow

Check for int/int8/int16/int64/int32 integer overflow in Golang arithmetic.

Install

go get github.com/johncgriffin/overflow

Note that because Go has no template types, the majority of repetitive code is generated by overflow_template.sh. If you have to change an algorithm, change it there and regenerate the Go code via:

go generate

Synopsis

package main

import "fmt"
import "math"
import "github.com/JohnCGriffin/overflow"

func main() {

	addend := math.MaxInt64 - 5

	for i := 0; i < 10; i++ {
		sum, ok := overflow.Add(addend, i)
		fmt.Printf("%v+%v -> (%v,%v)\n",
			addend, i, sum, ok)
	}

}

yields the output

9223372036854775802+0 -> (9223372036854775802,true)
9223372036854775802+1 -> (9223372036854775803,true)
9223372036854775802+2 -> (9223372036854775804,true)
9223372036854775802+3 -> (9223372036854775805,true)
9223372036854775802+4 -> (9223372036854775806,true)
9223372036854775802+5 -> (9223372036854775807,true)
9223372036854775802+6 -> (0,false)
9223372036854775802+7 -> (0,false)
9223372036854775802+8 -> (0,false)
9223372036854775802+9 -> (0,false)

For int, int64, and int32 types, provide Add, Add32, Add64, Sub, Sub32, Sub64, etc.
Unsigned types not covered at the moment, but such additions are welcome.

Stay calm and panic

There's a good case to be made that a panic is an unidiomatic but proper response. Iff you believe that there's no valid way to continue your program after math goes wayward, you can use the easier Addp, Mulp, Subp, and Divp versions which return the normal result or panic.

# Functions

Add sums two ints, returning the result and a boolean status.
Add16 performs + operation on two int16 operands returning a result and status.
Add16p is the unchecked panicing version of Add16.
Add32 performs + operation on two int32 operands returning a result and status.
Add32p is the unchecked panicing version of Add32.
Add64 performs + operation on two int64 operands returning a result and status.
Add64p is the unchecked panicing version of Add64.
Add8 performs + operation on two int8 operands returning a result and status.
Add8p is the unchecked panicing version of Add8.
Addp returns the sum of two ints, panicking on overflow.
Div returns the quotient of two ints and a boolean status.
Div16 performs / operation on two int16 operands returning a result and status.
Div16p is the unchecked panicing version of Div16.
Div32 performs / operation on two int32 operands returning a result and status.
Div32p is the unchecked panicing version of Div32.
Div64 performs / operation on two int64 operands returning a result and status.
Div64p is the unchecked panicing version of Div64.
Div8 performs / operation on two int8 operands returning a result and status.
Div8p is the unchecked panicing version of Div8.
Divp returns the quotient of two ints, panicking on overflow.
Mul returns the product of two ints and a boolean status.
Mul16 performs * operation on two int16 operands returning a result and status.
Mul16p is the unchecked panicing version of Mul16.
Mul32 performs * operation on two int32 operands returning a result and status.
Mul32p is the unchecked panicing version of Mul32.
Mul64 performs * operation on two int64 operands returning a result and status.
Mul64p is the unchecked panicing version of Mul64.
Mul8 performs * operation on two int8 operands returning a result and status.
Mul8p is the unchecked panicing version of Mul8.
Mulp returns the product of two ints, panicking on overflow.
Quotient returns the quotient, remainder and status of two ints.
Quotient16 performs + operation on two int16 operands returning a quotient, a remainder and status.
Quotient32 performs + operation on two int32 operands returning a quotient, a remainder and status.
Quotient64 performs + operation on two int64 operands returning a quotient, a remainder and status.
Quotient8 performs + operation on two int8 operands returning a quotient, a remainder and status.
Sub returns the difference of two ints and a boolean status.
Sub16 performs - operation on two int16 operands returning a result and status.
Sub16p is the unchecked panicing version of Sub16.
Sub32 performs - operation on two int32 operands returning a result and status.
Sub32p is the unchecked panicing version of Sub32.
Sub64 performs - operation on two int64 operands returning a result and status.
Sub64p is the unchecked panicing version of Sub64.
Sub8 performs - operation on two int8 operands returning a result and status.
Sub8p is the unchecked panicing version of Sub8.
Subp returns the difference of two ints, panicking on overflow.