Skip to content

Latest commit

 

History

History
1500 lines (986 loc) · 33.1 KB

index.md

File metadata and controls

1500 lines (986 loc) · 33.1 KB

Solidity API

Num_Complex

Complex

struct Complex {
  int256 re;
  int256 im;
}

complex

function complex(int256 re, int256 im) public pure returns (struct Num_Complex.Complex)

Complex Type Wrapper

Parameters

Name Type Description
re int256 real part
im int256 imaginary part

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex type

add

function add(struct Num_Complex.Complex a, struct Num_Complex.Complex b) public pure returns (struct Num_Complex.Complex)

ADDITION

Parameters

Name Type Description
a struct Num_Complex.Complex Complex Number
b struct Num_Complex.Complex Complex Number

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex Number

sub

function sub(struct Num_Complex.Complex a, struct Num_Complex.Complex b) public pure returns (struct Num_Complex.Complex)

SUBTRACTION

Parameters

Name Type Description
a struct Num_Complex.Complex Complex number
b struct Num_Complex.Complex Complex number

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex Number

mul

function mul(struct Num_Complex.Complex a, struct Num_Complex.Complex b) public pure returns (struct Num_Complex.Complex)

MULTIPLICATION

Parameters

Name Type Description
a struct Num_Complex.Complex Complex number
b struct Num_Complex.Complex Complex number

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex Number

div

function div(struct Num_Complex.Complex a, struct Num_Complex.Complex b) public pure returns (struct Num_Complex.Complex)

DIVISION

Parameters

Name Type Description
a struct Num_Complex.Complex Complex number
b struct Num_Complex.Complex Complex number

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex Number

r2

function r2(int256 a, int256 b) public pure returns (int256)

CALCULATE HYPOTENUSE

r^2 = a^2 + b^2

Parameters

Name Type Description
a int256 a
b int256 b

Return Values

Name Type Description
[0] int256 r r

toPolar

function toPolar(struct Num_Complex.Complex a) public pure returns (int256, int256)

CONVERT COMPLEX NUMBER TO POLAR COORDINATES

WARNING R2 FUNCTION ALWAYS RETURNS POSITIVE VALUES => ELSE{code} IS UNREACHABLE // atan vs atan2

Return Values

Name Type Description
[0] int256 r r
[1] int256 T theta

fromPolar

function fromPolar(int256 r, int256 T) public pure returns (struct Num_Complex.Complex a)

CONVERT FROM POLAR TO COMPLEX

https://github.com/rust-num/num-complex/blob/3a89daa2c616154035dd27d706bf7938bcbf30a8/src/lib.rs#L182

Parameters

Name Type Description
r int256 r
T int256 theta

Return Values

Name Type Description
a struct Num_Complex.Complex Complex number

atan2

function atan2(int256 y, int256 x) public pure returns (int256 T)

ATAN2(Y,X) FUNCTION (LESS PRECISE LESS GAS)

Parameters

Name Type Description
y int256 y
x int256 x

Return Values

Name Type Description
T int256 T

p_atan2

function p_atan2(int256 y, int256 x) public pure returns (int256 T)

ATAN2(Y,X) FUNCTION (MORE PRECISE MORE GAS)

Parameters

Name Type Description
y int256 y
x int256 x

Return Values

Name Type Description
T int256 T

atan1to1

function atan1to1(int256 x) public pure returns (int256)

PRECISE ATAN2(Y,X) FROM range -1 to 1 (MORE PRECISE LESS GAS)

Parameters

Name Type Description
x int256 (y/x)

Return Values

Name Type Description
[0] int256 T T

ln

function ln(struct Num_Complex.Complex a) public pure returns (struct Num_Complex.Complex)

COMPLEX NATURAL LOGARITHM

Parameters

Name Type Description
a struct Num_Complex.Complex Complex number

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex Number

sqrt

function sqrt(struct Num_Complex.Complex a) public pure returns (struct Num_Complex.Complex)

COMPLEX SQUARE ROOT

only works if 0 < re & im

Parameters

Name Type Description
a struct Num_Complex.Complex Complex number

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex Number

exp

function exp(struct Num_Complex.Complex a) public pure returns (struct Num_Complex.Complex)

COMPLEX EXPONENTIAL

e^(a + bi) = e^a (cos(b) + i*sin(b))

Parameters

Name Type Description
a struct Num_Complex.Complex Complex number

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex Number

pow

function pow(struct Num_Complex.Complex a, int256 n) public pure returns (struct Num_Complex.Complex)

COMPLEX POWER

using Demoivre's formula overflow risk

Parameters

Name Type Description
a struct Num_Complex.Complex Complex number
n int256 base 1e18

Return Values

Name Type Description
[0] struct Num_Complex.Complex Complex number

Trigonometry

Solidity library offering basic trigonometry functions where inputs and outputs are integers. Inputs are specified in radians scaled by 1e18, and similarly outputs are scaled by 1e18.

This implementation is based off the Solidity trigonometry library written by Lefteris Karapetsas which can be found here: https://github.com/Sikorkaio/sikorka/blob/e75c91925c914beaedf4841c0336a806f2b5f66d/contracts/trigonometry.sol

Compared to Lefteris' implementation, this version makes the following changes:

  • Uses a 32 bits instead of 16 bits for improved accuracy
  • Updated for Solidity 0.8.x
  • Various gas optimizations
  • Change inputs/outputs to standard trig format (scaled by 1e18) instead of requiring the integer format used by the algorithm

Lefertis' implementation is based off Dave Dribin's trigint C library http://www.dribin.org/dave/trigint/

Which in turn is based from a now deleted article which can be found in the Wayback Machine: http://web.archive.org/web/20120301144605/http://www.dattalo.com/technical/software/pic/picsine.html

INDEX_WIDTH

uint256 INDEX_WIDTH

INTERP_WIDTH

uint256 INTERP_WIDTH

INDEX_OFFSET

uint256 INDEX_OFFSET

INTERP_OFFSET

uint256 INTERP_OFFSET

ANGLES_IN_CYCLE

uint32 ANGLES_IN_CYCLE

QUADRANT_HIGH_MASK

uint32 QUADRANT_HIGH_MASK

QUADRANT_LOW_MASK

uint32 QUADRANT_LOW_MASK

SINE_TABLE_SIZE

uint256 SINE_TABLE_SIZE

PI

uint256 PI

TWO_PI

uint256 TWO_PI

PI_OVER_TWO

uint256 PI_OVER_TWO

entry_bytes

uint8 entry_bytes

entry_mask

uint256 entry_mask

sin_table

bytes sin_table

sin

function sin(uint256 _angle) internal pure returns (int256)

Return the sine of a value, specified in radians scaled by 1e18

This algorithm for converting sine only uses integer values, and it works by dividing the circle into 30 bit angles, i.e. there are 1,073,741,824 (2^30) angle units, instead of the standard 360 degrees (2pi radians). From there, we get an output in range -2,147,483,647 to 2,147,483,647, (which is the max value of an int32) which is then converted back to the standard range of -1 to 1, again scaled by 1e18

Parameters

Name Type Description
_angle uint256 Angle to convert

Return Values

Name Type Description
[0] int256 Result scaled by 1e18

cos

function cos(uint256 _angle) internal pure returns (int256)

Return the cosine of a value, specified in radians scaled by 1e18

This is identical to the sin() method, and just computes the value by delegating to the sin() method using the identity cos(x) = sin(x + pi/2) Overflow when angle + PI_OVER_TWO > type(uint256).max is ok, results are still accurate

Parameters

Name Type Description
_angle uint256 Angle to convert

Return Values

Name Type Description
[0] int256 Result scaled by 1e18

PRBMath__MulDivFixedPointOverflow

error PRBMath__MulDivFixedPointOverflow(uint256 prod1)

Emitted when the result overflows uint256.

PRBMath__MulDivOverflow

error PRBMath__MulDivOverflow(uint256 prod1, uint256 denominator)

Emitted when the result overflows uint256.

PRBMath__MulDivSignedInputTooSmall

error PRBMath__MulDivSignedInputTooSmall()

Emitted when one of the inputs is type(int256).min.

PRBMath__MulDivSignedOverflow

error PRBMath__MulDivSignedOverflow(uint256 rAbs)

Emitted when the intermediary absolute result overflows int256.

PRBMathSD59x18__AbsInputTooSmall

error PRBMathSD59x18__AbsInputTooSmall()

Emitted when the input is MIN_SD59x18.

PRBMathSD59x18__CeilOverflow

error PRBMathSD59x18__CeilOverflow(int256 x)

Emitted when ceiling a number overflows SD59x18.

PRBMathSD59x18__DivInputTooSmall

error PRBMathSD59x18__DivInputTooSmall()

Emitted when one of the inputs is MIN_SD59x18.

PRBMathSD59x18__DivOverflow

error PRBMathSD59x18__DivOverflow(uint256 rAbs)

Emitted when one of the intermediary unsigned results overflows SD59x18.

PRBMathSD59x18__ExpInputTooBig

error PRBMathSD59x18__ExpInputTooBig(int256 x)

Emitted when the input is greater than 133.084258667509499441.

PRBMathSD59x18__Exp2InputTooBig

error PRBMathSD59x18__Exp2InputTooBig(int256 x)

Emitted when the input is greater than 192.

PRBMathSD59x18__FloorUnderflow

error PRBMathSD59x18__FloorUnderflow(int256 x)

Emitted when flooring a number underflows SD59x18.

PRBMathSD59x18__FromIntOverflow

error PRBMathSD59x18__FromIntOverflow(int256 x)

Emitted when converting a basic integer to the fixed-point format overflows SD59x18.

PRBMathSD59x18__FromIntUnderflow

error PRBMathSD59x18__FromIntUnderflow(int256 x)

Emitted when converting a basic integer to the fixed-point format underflows SD59x18.

PRBMathSD59x18__GmNegativeProduct

error PRBMathSD59x18__GmNegativeProduct(int256 x, int256 y)

Emitted when the product of the inputs is negative.

PRBMathSD59x18__GmOverflow

error PRBMathSD59x18__GmOverflow(int256 x, int256 y)

Emitted when multiplying the inputs overflows SD59x18.

PRBMathSD59x18__LogInputTooSmall

error PRBMathSD59x18__LogInputTooSmall(int256 x)

Emitted when the input is less than or equal to zero.

PRBMathSD59x18__MulInputTooSmall

error PRBMathSD59x18__MulInputTooSmall()

Emitted when one of the inputs is MIN_SD59x18.

PRBMathSD59x18__MulOverflow

error PRBMathSD59x18__MulOverflow(uint256 rAbs)

Emitted when the intermediary absolute result overflows SD59x18.

PRBMathSD59x18__PowuOverflow

error PRBMathSD59x18__PowuOverflow(uint256 rAbs)

Emitted when the intermediary absolute result overflows SD59x18.

PRBMathSD59x18__SqrtNegativeInput

error PRBMathSD59x18__SqrtNegativeInput(int256 x)

Emitted when the input is negative.

PRBMathSD59x18__SqrtOverflow

error PRBMathSD59x18__SqrtOverflow(int256 x)

Emitted when the calculating the square root overflows SD59x18.

PRBMathUD60x18__AddOverflow

error PRBMathUD60x18__AddOverflow(uint256 x, uint256 y)

Emitted when addition overflows UD60x18.

PRBMathUD60x18__CeilOverflow

error PRBMathUD60x18__CeilOverflow(uint256 x)

Emitted when ceiling a number overflows UD60x18.

PRBMathUD60x18__ExpInputTooBig

error PRBMathUD60x18__ExpInputTooBig(uint256 x)

Emitted when the input is greater than 133.084258667509499441.

PRBMathUD60x18__Exp2InputTooBig

error PRBMathUD60x18__Exp2InputTooBig(uint256 x)

Emitted when the input is greater than 192.

PRBMathUD60x18__FromUintOverflow

error PRBMathUD60x18__FromUintOverflow(uint256 x)

Emitted when converting a basic integer to the fixed-point format format overflows UD60x18.

PRBMathUD60x18__GmOverflow

error PRBMathUD60x18__GmOverflow(uint256 x, uint256 y)

Emitted when multiplying the inputs overflows UD60x18.

PRBMathUD60x18__LogInputTooSmall

error PRBMathUD60x18__LogInputTooSmall(uint256 x)

Emitted when the input is less than 1.

PRBMathUD60x18__SqrtOverflow

error PRBMathUD60x18__SqrtOverflow(uint256 x)

Emitted when the calculating the square root overflows UD60x18.

PRBMathUD60x18__SubUnderflow

error PRBMathUD60x18__SubUnderflow(uint256 x, uint256 y)

Emitted when subtraction underflows UD60x18.

PRBMath

Common mathematical functions used in both PRBMathSD59x18 and PRBMathUD60x18. Note that this shared library does not always assume the signed 59.18-decimal fixed-point or the unsigned 60.18-decimal fixed-point representation. When it does not, it is explicitly mentioned in the NatSpec documentation.

SD59x18

struct SD59x18 {
  int256 value;
}

UD60x18

struct UD60x18 {
  uint256 value;
}

SCALE

uint256 SCALE

How many trailing decimals can be represented.

SCALE_LPOTD

uint256 SCALE_LPOTD

Largest power of two divisor of SCALE.

SCALE_INVERSE

uint256 SCALE_INVERSE

SCALE inverted mod 2^256.

exp2

function exp2(uint256 x) internal pure returns (uint256 result)

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

Has to use 192.64-bit fixed-point numbers. See https://ethereum.stackexchange.com/a/96594/24693.

Parameters

Name Type Description
x uint256 The exponent as an unsigned 192.64-bit fixed-point number.

Return Values

Name Type Description
result uint256 The result as an unsigned 60.18-decimal fixed-point number.

mostSignificantBit

function mostSignificantBit(uint256 x) internal pure returns (uint256 msb)

Finds the zero-based index of the first one in the binary representation of x.

See the note on msb in the "Find First Set" Wikipedia article https://en.wikipedia.org/wiki/Find_first_set

Parameters

Name Type Description
x uint256 The uint256 number for which to find the index of the most significant bit.

Return Values

Name Type Description
msb uint256 The index of the most significant bit as an uint256.

mulDiv

function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result)

Calculates floor(x*y÷denominator) with full precision.

_Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv.

Requirements:

  • The denominator cannot be zero.
  • The result must fit within uint256.

Caveats:

  • This function does not work with fixed-point numbers._

Parameters

Name Type Description
x uint256 The multiplicand as an uint256.
y uint256 The multiplier as an uint256.
denominator uint256 The divisor as an uint256.

Return Values

Name Type Description
result uint256 The result as an uint256.

mulDivFixedPoint

function mulDivFixedPoint(uint256 x, uint256 y) internal pure returns (uint256 result)

Calculates floor(x*y÷1e18) with full precision.

_Variant of "mulDiv" with constant folding, i.e. in which the denominator is always 1e18. Before returning the final result, we add 1 if (x * y) % SCALE >= HALF_SCALE. Without this, 6.6e-19 would be truncated to 0 instead of being rounded to 1e-18. See "Listing 6" and text above it at https://accu.org/index.php/journals/1717.

Requirements:

  • The result must fit within uint256.

Caveats:

  • The body is purposely left uncommented; see the NatSpec comments in "PRBMath.mulDiv" to understand how this works.
  • It is assumed that the result can never be type(uint256).max when x and y solve the following two equations:
    1. x * y = type(uint256).max * SCALE
    2. (x * y) % SCALE >= SCALE / 2_

Parameters

Name Type Description
x uint256 The multiplicand as an unsigned 60.18-decimal fixed-point number.
y uint256 The multiplier as an unsigned 60.18-decimal fixed-point number.

Return Values

Name Type Description
result uint256 The result as an unsigned 60.18-decimal fixed-point number.

mulDivSigned

function mulDivSigned(int256 x, int256 y, int256 denominator) internal pure returns (int256 result)

Calculates floor(x*y÷denominator) with full precision.

_An extension of "mulDiv" for signed numbers. Works by computing the signs and the absolute values separately.

Requirements:

  • None of the inputs can be type(int256).min.
  • The result must fit within int256._

Parameters

Name Type Description
x int256 The multiplicand as an int256.
y int256 The multiplier as an int256.
denominator int256 The divisor as an int256.

Return Values

Name Type Description
result int256 The result as an int256.

sqrt

function sqrt(uint256 x) internal pure returns (uint256 result)

Calculates the square root of x, rounding down.

_Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.

Caveats:

  • This function does not work with fixed-point numbers._

Parameters

Name Type Description
x uint256 The uint256 number for which to calculate the square root.

Return Values

Name Type Description
result uint256 The result as an uint256.

PRBMathSD59x18

Smart contract library for advanced fixed-point math that works with int256 numbers considered to have 18 trailing decimals. We call this number representation signed 59.18-decimal fixed-point, since the numbers can have a sign and there can be up to 59 digits in the integer part and up to 18 decimals in the fractional part. The numbers are bound by the minimum and the maximum values permitted by the Solidity type int256.

LOG2_E

int256 LOG2_E

log2(e) as a signed 59.18-decimal fixed-point number.

HALF_SCALE

int256 HALF_SCALE

Half the SCALE number.

MAX_SD59x18

int256 MAX_SD59x18

The maximum value a signed 59.18-decimal fixed-point number can have.

MAX_WHOLE_SD59x18

int256 MAX_WHOLE_SD59x18

The maximum whole value a signed 59.18-decimal fixed-point number can have.

MIN_SD59x18

int256 MIN_SD59x18

The minimum value a signed 59.18-decimal fixed-point number can have.

MIN_WHOLE_SD59x18

int256 MIN_WHOLE_SD59x18

The minimum whole value a signed 59.18-decimal fixed-point number can have.

SCALE

int256 SCALE

How many trailing decimals can be represented.

abs

function abs(int256 x) internal pure returns (int256 result)

Calculate the absolute value of x.

_Requirements:

  • x must be greater than MIN_SD59x18._

Parameters

Name Type Description
x int256 The number to calculate the absolute value for.

avg

function avg(int256 x, int256 y) internal pure returns (int256 result)

Calculates the arithmetic average of x and y, rounding down.

Parameters

Name Type Description
x int256 The first operand as a signed 59.18-decimal fixed-point number.
y int256 The second operand as a signed 59.18-decimal fixed-point number.

Return Values

Name Type Description
result int256 The arithmetic average as a signed 59.18-decimal fixed-point number.

ceil

function ceil(int256 x) internal pure returns (int256 result)

Yields the least greatest signed 59.18 decimal fixed-point number greater than or equal to x.

_Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts. See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions.

Requirements:

  • x must be less than or equal to MAX_WHOLE_SD59x18._

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number to ceil.

div

function div(int256 x, int256 y) internal pure returns (int256 result)

Divides two signed 59.18-decimal fixed-point numbers, returning a new signed 59.18-decimal fixed-point number.

_Variant of "mulDiv" that works with signed numbers. Works by computing the signs and the absolute values separately.

Requirements:

  • All from "PRBMath.mulDiv".
  • None of the inputs can be MIN_SD59x18.
  • The denominator cannot be zero.
  • The result must fit within int256.

Caveats:

  • All from "PRBMath.mulDiv"._

Parameters

Name Type Description
x int256 The numerator as a signed 59.18-decimal fixed-point number.
y int256 The denominator as a signed 59.18-decimal fixed-point number.

e

function e() internal pure returns (int256 result)

Returns Euler's number as a signed 59.18-decimal fixed-point number.

See https://en.wikipedia.org/wiki/E_(mathematical_constant).

exp

function exp(int256 x) internal pure returns (int256 result)

Calculates the natural exponent of x.

_Based on the insight that e^x = 2^(x * log2(e)).

Requirements:

  • All from "log2".
  • x must be less than 133.084258667509499441.

Caveats:

  • All from "exp2".
  • For any x less than -41.446531673892822322, the result is zero._

Parameters

Name Type Description
x int256 The exponent as a signed 59.18-decimal fixed-point number.

Return Values

Name Type Description
result int256 The result as a signed 59.18-decimal fixed-point number.

exp2

function exp2(int256 x) internal pure returns (int256 result)

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

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

Requirements:

  • x must be 192 or less.
  • The result must fit within MAX_SD59x18.

Caveats:

  • For any x less than -59.794705707972522261, the result is zero._

Parameters

Name Type Description
x int256 The exponent as a signed 59.18-decimal fixed-point number.

Return Values

Name Type Description
result int256 The result as a signed 59.18-decimal fixed-point number.

floor

function floor(int256 x) internal pure returns (int256 result)

Yields the greatest signed 59.18 decimal fixed-point number less than or equal to x.

_Optimized for fractional value inputs, because for every whole value there are (1e18 - 1) fractional counterparts. See https://en.wikipedia.org/wiki/Floor_and_ceiling_functions.

Requirements:

  • x must be greater than or equal to MIN_WHOLE_SD59x18._

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number to floor.

frac

function frac(int256 x) internal pure returns (int256 result)

Yields the excess beyond the floor of x for positive numbers and the part of the number to the right of the radix point for negative numbers.

Based on the odd function definition. https://en.wikipedia.org/wiki/Fractional_part

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number to get the fractional part of.

fromInt

function fromInt(int256 x) internal pure returns (int256 result)

Converts a number from basic integer form to signed 59.18-decimal fixed-point representation.

_Requirements:

  • x must be greater than or equal to MIN_SD59x18 divided by SCALE.
  • x must be less than or equal to MAX_SD59x18 divided by SCALE._

Parameters

Name Type Description
x int256 The basic integer to convert.

gm

function gm(int256 x, int256 y) internal pure returns (int256 result)

Calculates geometric mean of x and y, i.e. sqrt(x * y), rounding down.

_Requirements:

  • x * y must fit within MAX_SD59x18, lest it overflows.
  • x * y cannot be negative._

Parameters

Name Type Description
x int256 The first operand as a signed 59.18-decimal fixed-point number.
y int256 The second operand as a signed 59.18-decimal fixed-point number.

Return Values

Name Type Description
result int256 The result as a signed 59.18-decimal fixed-point number.

inv

function inv(int256 x) internal pure returns (int256 result)

Calculates 1 / x, rounding toward zero.

_Requirements:

  • x cannot be zero._

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number for which to calculate the inverse.

Return Values

Name Type Description
result int256 The inverse as a signed 59.18-decimal fixed-point number.

ln

function ln(int256 x) internal pure returns (int256 result)

Calculates the natural logarithm of x.

_Based on the insight that ln(x) = log2(x) / log2(e).

Requirements:

  • All from "log2".

Caveats:

  • All from "log2".
  • This doesn't return exactly 1 for 2718281828459045235, for that we would need more fine-grained precision._

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number for which to calculate the natural logarithm.

Return Values

Name Type Description
result int256 The natural logarithm as a signed 59.18-decimal fixed-point number.

log10

function log10(int256 x) internal pure returns (int256 result)

Calculates the common logarithm of x.

_First checks if x is an exact power of ten and it stops if yes. If it's not, calculates the common logarithm based on the insight that log10(x) = log2(x) / log2(10).

Requirements:

  • All from "log2".

Caveats:

  • All from "log2"._

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number for which to calculate the common logarithm.

Return Values

Name Type Description
result int256 The common logarithm as a signed 59.18-decimal fixed-point number.

log2

function log2(int256 x) internal pure returns (int256 result)

Calculates the binary logarithm of x.

_Based on the iterative approximation algorithm. https://en.wikipedia.org/wiki/Binary_logarithm#Iterative_approximation

Requirements:

  • x must be greater than zero.

Caveats:

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

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number for which to calculate the binary logarithm.

Return Values

Name Type Description
result int256 The binary logarithm as a signed 59.18-decimal fixed-point number.

mul

function mul(int256 x, int256 y) internal pure returns (int256 result)

Multiplies two signed 59.18-decimal fixed-point numbers together, returning a new signed 59.18-decimal fixed-point number.

_Variant of "mulDiv" that works with signed numbers and employs constant folding, i.e. the denominator is always 1e18.

Requirements:

  • All from "PRBMath.mulDivFixedPoint".
  • None of the inputs can be MIN_SD59x18
  • The result must fit within MAX_SD59x18.

Caveats:

  • The body is purposely left uncommented; see the NatSpec comments in "PRBMath.mulDiv" to understand how this works._

Parameters

Name Type Description
x int256 The multiplicand as a signed 59.18-decimal fixed-point number.
y int256 The multiplier as a signed 59.18-decimal fixed-point number.

Return Values

Name Type Description
result int256 The product as a signed 59.18-decimal fixed-point number.

pi

function pi() internal pure returns (int256 result)

Returns PI as a signed 59.18-decimal fixed-point number.

pow

function pow(int256 x, int256 y) internal pure returns (int256 result)

Raises x to the power of y.

_Based on the insight that x^y = 2^(log2(x) * y).

Requirements:

  • All from "exp2", "log2" and "mul".
  • z cannot be zero.

Caveats:

  • All from "exp2", "log2" and "mul".
  • Assumes 0^0 is 1._

Parameters

Name Type Description
x int256 Number to raise to given power y, as a signed 59.18-decimal fixed-point number.
y int256 Exponent to raise x to, as a signed 59.18-decimal fixed-point number.

Return Values

Name Type Description
result int256 x raised to power y, as a signed 59.18-decimal fixed-point number.

powu

function powu(int256 x, uint256 y) internal pure returns (int256 result)

Raises x (signed 59.18-decimal fixed-point number) to the power of y (basic unsigned integer) using the famous algorithm "exponentiation by squaring".

_See https://en.wikipedia.org/wiki/Exponentiation_by_squaring

Requirements:

  • All from "abs" and "PRBMath.mulDivFixedPoint".
  • The result must fit within MAX_SD59x18.

Caveats:

  • All from "PRBMath.mulDivFixedPoint".
  • Assumes 0^0 is 1._

Parameters

Name Type Description
x int256 The base as a signed 59.18-decimal fixed-point number.
y uint256 The exponent as an uint256.

Return Values

Name Type Description
result int256 The result as a signed 59.18-decimal fixed-point number.

scale

function scale() internal pure returns (int256 result)

Returns 1 as a signed 59.18-decimal fixed-point number.

sqrt

function sqrt(int256 x) internal pure returns (int256 result)

Calculates the square root of x, rounding down.

_Uses the Babylonian method https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.

Requirements:

  • x cannot be negative.
  • x must be less than MAX_SD59x18 / SCALE._

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number for which to calculate the square root.

Return Values

Name Type Description
result int256 The result as a signed 59.18-decimal fixed-point .

toInt

function toInt(int256 x) internal pure returns (int256 result)

Converts a signed 59.18-decimal fixed-point number to basic integer form, rounding down in the process.

Parameters

Name Type Description
x int256 The signed 59.18-decimal fixed-point number to convert.

Return Values

Name Type Description
result int256 The same number in basic integer form.