Skip to main content

UD60x18

UD60x18

Alias for uint256. See source code for documentation.

uUNIT

UNIT

uHALF_UNIT

uUNIT_SQUARED

ZERO

uEXP2_MAX_INPUT

EXP2_MAX_INPUT

PRBMath_UD60x18_Log_InputTooSmall

Thrown when taking the logarithm of a number less than 1.

error PRBMath_UD60x18_Log_InputTooSmall(UD60x18 x)

PRBMath_UD60x18_Exp2_InputTooBig

Thrown when taking the binary exponent of a base greater than 192e18.

error PRBMath_UD60x18_Exp2_InputTooBig(UD60x18 x)

wrap

Wraps a uint256 number into the UD60x18 value type.

function wrap(uint256 x) internal pure returns (UD60x18 result)

unwrap

Unwraps a UD60x18 number into uint256.

function unwrap(UD60x18 x) internal pure returns (uint256 result)

pow

Raises x to the power of y.

For 1x1 \leq x \leq \infty, the following standard formula is used:

xy=2{log2{x}y}x^y = 2^\{log_2\{x\} * y\}

For 0x<10 \leq x \lt 1, since the unsigned {log2} is undefined, an equivalent formula is used:

i={1}{x}w=2{log2{i}y}xy={1}{w}i = \frac\{1\}\{x\} w = 2^\{log_2\{i\} * y\} x^y = \frac\{1\}\{w\}
function pow(UD60x18 x, UD60x18 y) internal pure returns (UD60x18 result)
Dev note

Notes:

  • Refer to the notes in {log2} and {mul}.
  • Returns UNIT for 0^0.
  • It may not perform well with very small values of x. Consider using SD59x18 as an alternative.

Requirements:

  • Refer to the requirements in {exp2}, {log2}, and {mul}.

Parameters

NameTypeDescription
xUD60x18The base as a UD60x18 number.
yUD60x18The exponent as a UD60x18 number.

Fields

NameTypeDescription

Return Values

NameTypeDescription
resultUD60x18The result as a UD60x18 number.

log2

Calculates the binary logarithm of x using the iterative approximation algorithm:

log2{x}=n+log2{y},{where}y=x2{n}, y[1,2)log_2\{x\} = n + log_2\{y\}, \text\{ where \} y = x*2^\{-n\}, \ y \in [1, 2)

For 0x<10 \leq x \lt 1, the input is inverted:

log2{x}=log2{{1}{x}}log_2\{x\} = -log_2\{\frac\{1\}\{x\}\}
function log2(UD60x18 x) internal pure returns (UD60x18 result)
Dev note

See https://en.wikipedia.org/wiki/Binary_logarithm#Iterative_approximation

Notes:

  • Due to the lossy precision of the iterative approximation, the results are not perfectly accurate to the last decimal.

Requirements:

  • x must be greater than zero.

Parameters

NameTypeDescription
xUD60x18The UD60x18 number for which to calculate the binary logarithm.

Fields

NameTypeDescription

Return Values

NameTypeDescription
resultUD60x18The binary logarithm as a UD60x18 number.

mul

Multiplies two UD60x18 numbers together, returning a new UD60x18 number.

function mul(UD60x18 x, UD60x18 y) internal pure returns (UD60x18 result)
Dev note

Uses {Common.mulDiv} to enable overflow-safe multiplication and division.

Notes:

  • Refer to the notes in {Common.mulDiv}.

Requirements:

  • Refer to the requirements in {Common.mulDiv}.

See the documentation in {Common.mulDiv18}.

Parameters

NameTypeDescription
xUD60x18The multiplicand as a UD60x18 number.
yUD60x18The multiplier as a UD60x18 number.

Fields

NameTypeDescription

Return Values

NameTypeDescription
resultUD60x18The product as a UD60x18 number.

div

Divides two UD60x18 numbers, returning a new UD60x18 number.

function div(UD60x18 x, UD60x18 y) internal pure returns (UD60x18 result)
Dev note

Uses {Common.mulDiv} to enable overflow-safe multiplication and division.

Notes:

  • Refer to the notes in {Common.mulDiv}.

Requirements:

  • Refer to the requirements in {Common.mulDiv}.

Parameters

NameTypeDescription
xUD60x18The numerator as a UD60x18 number.
yUD60x18The denominator as a UD60x18 number.

Fields

NameTypeDescription

exp2

Calculates the binary exponent of x using the binary fraction method.

function exp2(UD60x18 x) internal pure returns (UD60x18 result)
Dev note

See https://ethereum.stackexchange.com/q/79903/24693

Requirements:

  • x must be less than 192e18.
  • The result must fit in UD60x18.

Parameters

NameTypeDescription
xUD60x18The exponent as a UD60x18 number.

Fields

NameTypeDescription

Return Values

NameTypeDescription
resultUD60x18The result as a UD60x18 number.