math

package
v4.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2024 License: GPL-3.0 Imports: 6 Imported by: 52

Documentation

Overview

Package math includes important helpers for Ethereum such as fast integer square roots.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOverflow     = errors.New("integer overflow")
	ErrDivByZero    = errors.New("integer divide by zero")
	ErrMulOverflow  = errors.New("multiplication overflows")
	ErrAddOverflow  = errors.New("addition overflows")
	ErrSubUnderflow = errors.New("subtraction underflow")
)

ErrOverflow occurs when an operation exceeds max or minimum values.

Functions

func Add64

func Add64(a, b uint64) (uint64, error)

Add64 adds 2 64-bit unsigned integers and checks if they lead to an overflow. If they do not, it returns the result without an error.

func AddInt

func AddInt(i ...int) (int, error)

AddInt adds two or more integers and checks for integer overflows.

func CachedSquareRoot added in v4.0.2

func CachedSquareRoot(balance uint64) uint64

CachedSquareRoot implements Newton's algorithm to compute the square root of the given uint64 starting from the last cached value

func CeilDiv8

func CeilDiv8(n int) int

CeilDiv8 divides the input number by 8 and takes the ceiling of that number.

func Div64

func Div64(a, b uint64) (uint64, error)

Div64 divides two 64-bit unsigned integers and checks for errors.

func Int

func Int(u uint64) (int, error)

Int returns the integer value of the uint64 argument. If there is an overflow, then an error is returned.

func IntegerSquareRoot

func IntegerSquareRoot(n uint64) uint64

IntegerSquareRoot defines a function that returns the largest possible integer root of a number using go's standard library.

func IsPowerOf2

func IsPowerOf2(n uint64) bool

IsPowerOf2 returns true if n is an exact power of two. False otherwise.

func IsValidUint256 added in v4.1.0

func IsValidUint256(bi *big.Int) bool

IsValidUint256 given a bigint checks if the value is a valid Uint256

func Max

func Max(a, b uint64) uint64

Max returns the larger integer of the two given ones.This is used over the Max function in the standard math library because that max function has to check for some special floating point cases making it slower by a magnitude of 10.

func Min

func Min(a, b uint64) uint64

Min returns the smaller integer of the two given ones. This is used over the Min function in the standard math library because that min function has to check for some special floating point cases making it slower by a magnitude of 10.

func Mod64

func Mod64(a, b uint64) (uint64, error)

Mod64 finds remainder of division of two 64-bit unsigned integers and checks for errors.

func Mul64

func Mul64(a, b uint64) (uint64, error)

Mul64 multiples 2 64-bit unsigned integers and checks if they lead to an overflow. If they do not, it returns the result without an error.

func PowerOf2

func PowerOf2(n uint64) uint64

PowerOf2 returns an integer that is the provided exponent of 2. Can only return powers of 2 till 63, after that it overflows

func Sub64

func Sub64(a, b uint64) (uint64, error)

Sub64 subtracts two 64-bit unsigned integers and checks for errors.

Types

type Gwei added in v4.1.0

type Gwei uint64

Gwei is a denomination of 1e9 Wei represented as an uint64.

func WeiToGwei added in v4.0.4

func WeiToGwei(v Wei) Gwei

WeiToGwei converts big int wei to uint64 gwei. The input `v` is copied before being modified.

type Wei added in v4.1.0

type Wei *big.Int

Wei is the smallest unit of Ether, represented as a pointer to a bigInt.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL