Skip to content

Commit

Permalink
preserve the changes fixing tm2/pkg/overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes committed Jan 14, 2025
1 parent de5a919 commit ee03341
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 183 deletions.
60 changes: 27 additions & 33 deletions tm2/pkg/overflow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,59 @@

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

Forked from https://github.com/JohnCGriffin/overflow
Originally forked from https://github.com/JohnCGriffin/overflow.

### 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:
```

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:

```sh
go generate
```

### Synopsis

```
```go
package main

import "fmt"
import "math"
import "github.com/JohnCGriffin/overflow"
import "github.com/gnolang/gno/tm2/pkg/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",
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)

```console
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.







There's a good case to be made that a panic is an unidiomatic but proper
response. If 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.
Loading

0 comments on commit ee03341

Please sign in to comment.