Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

feat: Sync up with gno's math/overflow​ package #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# overflow
Check for int/int8/int16/int64/int32 integer overflow in Golang arithmetic.
### Install

```
go get github.com/johncgriffin/overflow
go get github.com/gnolang/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
Expand All @@ -13,7 +14,7 @@ go generate
```
### Synopsis

```
```go
package main

import "fmt"
Expand All @@ -22,18 +23,20 @@ import "github.com/JohnCGriffin/overflow"

func main() {

addend := math.MaxInt64 - 5
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)
}
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
```

```plain
9223372036854775802+0 -> (9223372036854775802,true)
9223372036854775802+1 -> (9223372036854775803,true)
9223372036854775802+2 -> (9223372036854775804,true)
Expand All @@ -54,10 +57,3 @@ Unsigned types not covered at the moment, but such additions are welcome.
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.







3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gnolang/overflow

go 1.23.1
33 changes: 20 additions & 13 deletions overflow.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*Package overflow offers overflow-checked integer arithmetic operations
/*
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.
Expand All @@ -16,17 +17,13 @@ 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. */
and new tests.
*/
package overflow

//go:generate ./overflow_template.sh

import "math"

func _is64Bit() bool {
maxU32 := uint(math.MaxUint32)
return ((maxU32 << 1) >> 1) == maxU32
}
const is64Bit = ^uint(0) == (1<<64)-1

/********** PARTIAL TEST COVERAGE FROM HERE DOWN *************

Expand All @@ -42,7 +39,7 @@ So, FEEL FREE to carefully review the code visually.

// Add sums two ints, returning the result and a boolean status.
func Add(a, b int) (int, bool) {
if _is64Bit() {
if is64Bit {
r64, ok := Add64(int64(a), int64(b))
return int(r64), ok
}
Expand All @@ -52,7 +49,7 @@ func Add(a, b int) (int, bool) {

// Sub returns the difference of two ints and a boolean status.
func Sub(a, b int) (int, bool) {
if _is64Bit() {
if is64Bit {
r64, ok := Sub64(int64(a), int64(b))
return int(r64), ok
}
Expand All @@ -62,7 +59,7 @@ func Sub(a, b int) (int, bool) {

// Mul returns the product of two ints and a boolean status.
func Mul(a, b int) (int, bool) {
if _is64Bit() {
if is64Bit {
r64, ok := Mul64(int64(a), int64(b))
return int(r64), ok
}
Expand All @@ -72,7 +69,7 @@ func Mul(a, b int) (int, bool) {

// Div returns the quotient of two ints and a boolean status
func Div(a, b int) (int, bool) {
if _is64Bit() {
if is64Bit {
r64, ok := Div64(int64(a), int64(b))
return int(r64), ok
}
Expand All @@ -82,14 +79,24 @@ func Div(a, b int) (int, bool) {

// Quotient returns the quotient, remainder and status of two ints
func Quotient(a, b int) (int, int, bool) {
if _is64Bit() {
if is64Bit {
q64, r64, ok := Quotient64(int64(a), int64(b))
return int(q64), int(r64), ok
}
q32, r32, ok := Quotient32(int32(a), int32(b))
return int(q32), int(r32), ok
}

// Quo returns the quotient, remainder and status of two ints
func Quo(a, b int) (int, int, bool) {
if is64Bit {
q64, r64, ok := Quo64(int64(a), int64(b))
return int(q64), int(r64), ok
}
q32, r32, ok := Quo32(int32(a), int32(b))
return int(q32), int(r32), ok
}

/************* Panic versions for int ****************/

// Addp returns the sum of two ints, panicking on overflow
Expand Down
Loading