Skip to content

Check for integer overflow in Go arithmetic and type conversion. 检查Go算术和类型转换中的整数溢出。

License

Notifications You must be signed in to change notification settings

rwxe/overflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

overflow

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

Install

go get github.com/JohnCGriffin/overflow 

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.

About

Check for integer overflow in Go arithmetic and type conversion. 检查Go算术和类型转换中的整数溢出。

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • Go 87.7%
  • Shell 12.3%