github.com/0chain/gosdk@v1.17.11/core/common/math.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"math"
     6  )
     7  
     8  func TryAddInt(a, b int) (int, error) {
     9  	if a > 0 && b > 0 {
    10  		if math.MaxInt-a < b {
    11  			return 0, errors.New("math: integer overflow")
    12  		}
    13  
    14  	}
    15  
    16  	if a < 0 && b < 0 {
    17  		if math.MinInt-a > b {
    18  			return 0, errors.New("math: integer underflow")
    19  		}
    20  
    21  	}
    22  
    23  	return a + b, nil
    24  }
    25  
    26  func MustAddInt(a, b int) int {
    27  	i, err := TryAddInt(a, b)
    28  	if err != nil {
    29  		panic(err.Error())
    30  	}
    31  
    32  	return i
    33  }