github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/common/overflow/README.md (about)

     1  [![Build Status](https://travis-ci.org/JohnCGriffin/overflow.png)](https://travis-ci.org/JohnCGriffin/overflow)
     2  # overflow
     3  Check for int/int8/int16/int64/int32 integer overflow in Golang arithmetic.
     4  ### Install
     5  ```
     6  go get github.com/johncgriffin/overflow
     7  ```
     8  Note that because Go has no template types, the majority of repetitive code is 
     9  generated by overflow_template.sh.  If you have to change an
    10  algorithm, change it there and regenerate the Go code via:
    11  ```
    12  go generate
    13  ```
    14  ### Synopsis
    15  
    16  ```
    17  package main
    18  
    19  import "fmt"
    20  import "math"
    21  import "github.com/JohnCGriffin/overflow"
    22  
    23  func main() {
    24  
    25  	addend := math.MaxInt64 - 5
    26  
    27  	for i := 0; i < 10; i++ {
    28  		sum, ok := overflow.Add(addend, i)
    29  		fmt.Printf("%v+%v -> (%v,%v)\n",
    30  			addend, i, sum, ok)
    31  	}
    32  
    33  }
    34  ```
    35  yields the output
    36  ```
    37  9223372036854775802+0 -> (9223372036854775802,true)
    38  9223372036854775802+1 -> (9223372036854775803,true)
    39  9223372036854775802+2 -> (9223372036854775804,true)
    40  9223372036854775802+3 -> (9223372036854775805,true)
    41  9223372036854775802+4 -> (9223372036854775806,true)
    42  9223372036854775802+5 -> (9223372036854775807,true)
    43  9223372036854775802+6 -> (0,false)
    44  9223372036854775802+7 -> (0,false)
    45  9223372036854775802+8 -> (0,false)
    46  9223372036854775802+9 -> (0,false)
    47  ```
    48  
    49  For int, int64, and int32 types, provide Add, Add32, Add64, Sub, Sub32, Sub64, etc.  
    50  Unsigned types not covered at the moment, but such additions are welcome.
    51  
    52  ### Stay calm and panic
    53  
    54  There's a good case to be made that a panic is an unidiomatic but proper response.  Iff you
    55  believe that there's no valid way to continue your program after math goes wayward, you can
    56  use the easier Addp, Mulp, Subp, and Divp versions which return the normal result or panic.
    57  
    58  
    59  
    60  
    61  
    62  
    63