Documentation
¶
Overview ¶
Package umath provides some useful unsigned math functions to prevent underflows. It also provides some type conversion functions to convert between different integer types to prevent underflows and overflows or overflows.
Index ¶
- Variables
- func Len[T any](slice []T) uint64
- func MustToUint32[N constraints.Integer](i N) uint32
- func Subtract(a, b uint64) (uint64, bool)
- func SubtractOrZero(a, b uint64) uint64
- func ToInt32[N constraints.Integer](i N) (int32, error)
- func ToInt64[N constraints.Integer](n N) (int64, error)
- func ToUint32[N constraints.Integer](i N) (uint32, error)
- func ToUint64[N constraints.Integer](i N) (uint64, error)
Constants ¶
This section is empty.
Variables ¶
var MaxUint128 = func() *big.Int { const twoPow128Sub1 = "340282366920938463463374607431768211455" maxUint128, ok := new(big.Int).SetString(twoPow128Sub1, 10) if !ok { panic("invalid max uint128") } return maxUint128 }()
var MaxUint256 = func() *big.Int { // Copied from uint256 package. const twoPow256Sub1 = "115792089237316195423570985008687907853269984665640564039457584007913129639935" maxUint256, ok := new(big.Int).SetString(twoPow256Sub1, 10) if !ok { panic("invalid max uint256") } return maxUint256 }()
MaxUint256 is the maximum value that can be represented by a uint256.
Functions ¶
func Len ¶
Len returns the length of the slice as a uint64. This convenience function to avoid gosec complaining about uint64(len(slice)).
func MustToUint32 ¶ added in v0.15.0
func MustToUint32[N constraints.Integer](i N) uint32
func SubtractOrZero ¶
SubtractOrZero returns a - b if a >= b, otherwise 0. This is a convenience function for inline usage.
func ToInt32 ¶
func ToInt32[N constraints.Integer](i N) (int32, error)
ToInt32 returns i as an int32 or an error if it cannot be represented as such.
func ToInt64 ¶
func ToInt64[N constraints.Integer](n N) (int64, error)
ToInt64 returns i as an int64 or an error if it cannot be represented as such.
Types ¶
This section is empty.