math

package
v1.22.77 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package math provides safe arithmetic operations for the DEX VM.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrOverflow indicates an arithmetic overflow.
	ErrOverflow = errors.New("arithmetic overflow")

	// ErrDivisionByZero indicates division by zero.
	ErrDivisionByZero = errors.New("division by zero")
)

Functions

func AbsDiffU64

func AbsDiffU64(a, b uint64) uint64

AbsDiffU64 returns |a - b| without underflow.

func BigMulDiv

func BigMulDiv(a, b, c *big.Int) *big.Int

BigMulDiv computes (a * b) / c using big.Int arithmetic. All inputs and output are *big.Int. Returns nil if c is zero.

func BigMulDivRoundUp

func BigMulDivRoundUp(a, b, c *big.Int) *big.Int

BigMulDivRoundUp computes ceil((a * b) / c) using big.Int arithmetic.

func CheckAddOverflowU64

func CheckAddOverflowU64(a, b uint64) bool

CheckAddOverflowU64 returns true if a + b would overflow uint64.

func CheckMulOverflowU64

func CheckMulOverflowU64(a, b uint64) bool

CheckMulOverflowU64 returns true if a * b would overflow uint64.

func ClampU64

func ClampU64(value, min, max uint64) uint64

ClampU64 clamps a value to the range [min, max].

func MaxU64

func MaxU64(a, b uint64) uint64

MaxU64 returns the larger of two uint64 values.

func MinU64

func MinU64(a, b uint64) uint64

MinU64 returns the smaller of two uint64 values.

func MulDivRoundUpU64

func MulDivRoundUpU64(a, b, c uint64) (uint64, error)

MulDivRoundUpU64 computes ceil((a * b) / c) using big.Int. Returns an error if c is zero or if the result overflows uint64.

func MulDivU64

func MulDivU64(a, b, c uint64) (uint64, error)

MulDivU64 computes (a * b) / c using big.Int to avoid intermediate overflow. Returns an error if c is zero or if the result overflows uint64.

func MulU64Big

func MulU64Big(a, b uint64) *big.Int

MulU64Big multiplies two uint64 values using big.Int to avoid overflow. Returns the result as a big.Int.

func NotionalValueU64

func NotionalValueU64(price, quantity, precision uint64) (uint64, error)

NotionalValueU64 calculates price * quantity / precision safely. This is a common operation in orderbooks.

func SafeAddU64

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

SafeAddU64 adds two uint64 values and returns an error on overflow.

func SafeDivU64

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

SafeDivU64 divides two uint64 values and returns an error on division by zero.

func SafeMulU64

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

SafeMulU64 multiplies two uint64 values and returns an error on overflow.

func SafeSubU64

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

SafeSubU64 subtracts two uint64 values and returns an error on underflow.

Types

This section is empty.

Jump to

Keyboard shortcuts

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