overflow

package
v2.0.7+incompatible Latest Latest
Warning

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

Go to latest
Published: May 10, 2019 License: GPL-3.0, LGPL-3.0, LGPL-3.0-or-later Imports: 1 Imported by: 0

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.

Documentation

Overview

Package overflow offers overflow-checked integer arithmetic operations for int, int32, and int64. Each of the operations returns a result,bool combination. This was prompted by the need to know when to flow into higher precision types from the math.big library.

For instance, assuing a 64 bit machine:

10 + 20 -> 30 int(math.MaxInt64) + 1 -> -9223372036854775808

whereas

overflow.Add(10,20) -> (30, true) overflow.Add(math.MaxInt64,1) -> (0, false)

Add, Sub, Mul, Div are for int. Add64, Add32, etc. are specifically sized.

If anybody wishes an unsigned version, submit a pull request for code and new tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(a, b int) (int, bool)

Add sums two ints, returning the result and a boolean status.

func Add16

func Add16(a, b int16) (int16, bool)

Add16 performs + operation on two int16 operands returning a result and status

func Add16p

func Add16p(a, b int16) int16

Add16p is the unchecked panicing version of Add16

func Add32

func Add32(a, b int32) (int32, bool)

Add32 performs + operation on two int32 operands returning a result and status

func Add32p

func Add32p(a, b int32) int32

Add32p is the unchecked panicing version of Add32

func Add64

func Add64(a, b int64) (int64, bool)

Add64 performs + operation on two int64 operands returning a result and status

func Add64p

func Add64p(a, b int64) int64

Add64p is the unchecked panicing version of Add64

func Add8

func Add8(a, b int8) (int8, bool)

Add8 performs + operation on two int8 operands returning a result and status

func Add8p

func Add8p(a, b int8) int8

Add8p is the unchecked panicing version of Add8

func Addp

func Addp(a, b int) int

Addp returns the sum of two ints, panicking on overflow

func Div

func Div(a, b int) (int, bool)

Div returns the quotient of two ints and a boolean status

func Div16

func Div16(a, b int16) (int16, bool)

Div16 performs / operation on two int16 operands returning a result and status

func Div16p

func Div16p(a, b int16) int16

Div16p is the unchecked panicing version of Div16

func Div32

func Div32(a, b int32) (int32, bool)

Div32 performs / operation on two int32 operands returning a result and status

func Div32p

func Div32p(a, b int32) int32

Div32p is the unchecked panicing version of Div32

func Div64

func Div64(a, b int64) (int64, bool)

Div64 performs / operation on two int64 operands returning a result and status

func Div64p

func Div64p(a, b int64) int64

Div64p is the unchecked panicing version of Div64

func Div8

func Div8(a, b int8) (int8, bool)

Div8 performs / operation on two int8 operands returning a result and status

func Div8p

func Div8p(a, b int8) int8

Div8p is the unchecked panicing version of Div8

func Divp

func Divp(a, b int) int

Divp returns the quotient of two ints, panicking on overflow.

func Mul

func Mul(a, b int) (int, bool)

Mul returns the product of two ints and a boolean status.

func Mul16

func Mul16(a, b int16) (int16, bool)

Mul16 performs * operation on two int16 operands returning a result and status

func Mul16p

func Mul16p(a, b int16) int16

Mul16p is the unchecked panicing version of Mul16

func Mul32

func Mul32(a, b int32) (int32, bool)

Mul32 performs * operation on two int32 operands returning a result and status

func Mul32p

func Mul32p(a, b int32) int32

Mul32p is the unchecked panicing version of Mul32

func Mul64

func Mul64(a, b int64) (int64, bool)

Mul64 performs * operation on two int64 operands returning a result and status

func Mul64p

func Mul64p(a, b int64) int64

Mul64p is the unchecked panicing version of Mul64

func Mul8

func Mul8(a, b int8) (int8, bool)

Mul8 performs * operation on two int8 operands returning a result and status

func Mul8p

func Mul8p(a, b int8) int8

Mul8p is the unchecked panicing version of Mul8

func Mulp

func Mulp(a, b int) int

Mulp returns the product of two ints, panicking on overflow.

func Quotient

func Quotient(a, b int) (int, int, bool)

Quotient returns the quotient, remainder and status of two ints

func Quotient16

func Quotient16(a, b int16) (int16, int16, bool)

Quotient16 performs + operation on two int16 operands returning a quotient, a remainder and status

func Quotient32

func Quotient32(a, b int32) (int32, int32, bool)

Quotient32 performs + operation on two int32 operands returning a quotient, a remainder and status

func Quotient64

func Quotient64(a, b int64) (int64, int64, bool)

Quotient64 performs + operation on two int64 operands returning a quotient, a remainder and status

func Quotient8

func Quotient8(a, b int8) (int8, int8, bool)

Quotient8 performs + operation on two int8 operands returning a quotient, a remainder and status

func Sub

func Sub(a, b int) (int, bool)

Sub returns the difference of two ints and a boolean status.

func Sub16

func Sub16(a, b int16) (int16, bool)

Sub16 performs - operation on two int16 operands returning a result and status

func Sub16p

func Sub16p(a, b int16) int16

Sub16p is the unchecked panicing version of Sub16

func Sub32

func Sub32(a, b int32) (int32, bool)

Sub32 performs - operation on two int32 operands returning a result and status

func Sub32p

func Sub32p(a, b int32) int32

Sub32p is the unchecked panicing version of Sub32

func Sub64

func Sub64(a, b int64) (int64, bool)

Sub64 performs - operation on two int64 operands returning a result and status

func Sub64p

func Sub64p(a, b int64) int64

Sub64p is the unchecked panicing version of Sub64

func Sub8

func Sub8(a, b int8) (int8, bool)

Sub8 performs - operation on two int8 operands returning a result and status

func Sub8p

func Sub8p(a, b int8) int8

Sub8p is the unchecked panicing version of Sub8

func Subp

func Subp(a, b int) int

Subp returns the difference of two ints, panicking on overflow.

Types

This section is empty.

Jump to

Keyboard shortcuts

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