ABDK Libraries for Solidity

repository·master·Indexed 19 days ago

https://github.com/abdk-consulting/abdk-libraries-solidity

High-precision mathematical libraries for Solidity smart contracts. Includes Math 64.64 for signed 64.64-bit fixed-point arithmetic and Math Quad for IEEE 754 quadruple precision (128-bit) floating-point operations. Provides functions for basic arithmetic, power, roots, logarithms, and conversions between integers, fixed-point, and floating-point formats.

Tokens
1.5K
Snippets
0
Records
11
Agent score
64%

What's inside abdk-libraries-solidity

  1. What are signed 64.64 fixed point numbers?

    master
    A signed 64.64-bit fixed point number is a fraction where the numerator is a signed 128-bit integer and the denominator is $2^{64}$. In Solidity, these are represented using the int128 type, which holds only the numerator. This format provides a wide range and decent precision with high performance, making it suitable for real-number calculations in smart contracts.
  2. Understand IEEE 754 Quadruple Precision (128-bit) numbers

    master

    The ABDK Math Quad library operates on IEEE 754 quadruple precision (128-bit) floating point numbers, represented as bytes16 in Solidity.

    Technical Specifications:

    • Sign bit: 1 bit
    • Exponent: 15 bits
    • Significand precision: 113 bits (112 explicitly stored)
    • Decimal Precision: 33 to 36 significant digits
    • Minimum strictly positive (subnormal) value: 2^−16494 ≈ 10^−4965
    • Minimum positive normal value: 2^−16382 ≈ 3.3621 × 10^−4932
    • Maximum representable value: 2^16384 − 2^16271 ≈ 1.1897 × 10^4932

    Supported Special Values:

    • NaN (not a number)
    • +Infinity
    • -Infinity
    • 0 (positive zero)
    • -0 (negative zero)
  3. Calculate average, power, root, and logarithms

    master

    The library includes advanced mathematical functions for 64.64 fixed point numbers:

    Averages

    • avg(int128 x, int128 y) -> int128: Arithmetic average.
    • gavg(int128 x, int128 y) -> int128: Geometric average.

    Power and Root

    • pow(int128 x, uint256 y) -> int128: Raises x to a non-negative integer power y.
    • sqrt(int128 x) -> int128: Calculates the square root of x.

    Exponentiation and Logarithm

    • log_2(int128 x) -> int128: Binary logarithm ($\log_2(x)$).
    • ln(int128 x) -> int128: Natural logarithm ($\ln(x)$).
    • exp_2(int128 x) -> int128: Calculates $2^x$.
    • exp(int128 x) -> int128: Calculates $e^x$.
  4. Compare quadruple precision numbers

    master

    Use the following functions to test properties or compare two bytes16 quadruple precision numbers:

    • isNaN(bytes16 x): Returns true if x is NaN.
    • isInfinity(bytes16 x): Returns true if x is +Infinity or -Infinity.
    • sign(bytes16 x): Returns -1 if x is negative, 0 if x is zero (0 or -0), and 1 if x is positive.
    • cmp(bytes16 x, bytes16 y): Compares x and y. Returns -1 if x < y, 0 if x = y, and 1 if x > y. (Equivalent to sign(x - y)).
    • eq(bytes16 x, bytes16 y): Returns true if x and y are equal.
  5. Convert between integers and 64.64 fixed point numbers

    master

    Use these functions to move between standard Solidity integer types and the 64.64 fixed point format.

    Integer to 64.64

    • fromInt(int256 x) -> int128: Converts a signed 256-bit integer to 64.64.
    • fromUInt(uint256 x) -> int128: Converts an unsigned 256-bit integer to 64.64.

    64.64 to Integer

    • toInt(int128 x) -> int64: Converts 64.64 to a signed 64-bit integer.
    • toUInt(int128 x) -> uint64: Converts 64.64 to an unsigned 64-bit integer.

    128.128 to 64.64

    • from128x128(int256 x) -> int128: Converts a signed 128.128 fixed point number to 64.64.
    • to128x128(int128 x) -> int256: Converts a 64.64 fixed point number to a signed 128.128 fixed point number.
  6. Use simple arithmetic with 64.64 fixed point numbers

    master

    The library provides several internal pure functions for basic arithmetic. Note the different return types depending on whether you are multiplying by integers or dividing integers into fixed point numbers.

    Addition and Subtraction

    • add(int128 x, int128 y) -> int128: Adds two 64.64 numbers.
    • sub(int128 x, int128 y) -> int128: Subtracts y from x.
    • neg(int128 x) -> int128: Returns -x.
    • abs(int128 x) -> int128: Returns the absolute value.

    Multiplication

    • mul(int128 x, int128 y) -> int128: Multiplies two 64.64 numbers.
    • muli(int128 x, int256 y) -> int256: Multiplies a 64.64 number by a signed 256-bit integer. Returns int256.
    • mulu(int128 x, uint256 y) -> uint256: Multiplies a 64.64 number by an unsigned 256-bit integer. Returns uint256.
    • inv(int128 x) -> int128: Calculates the reciprocal 1/x.

    Division

    • div(int128 x, int128 y) -> int128: Divides two 64.64 numbers.
    • divi(int256 x, int256 y) -> int128: Divides two signed 256-bit integers. Returns int128.
    • divu(uint256 x, uint256 y) -> int128: Divides two unsigned 256-bit integers. Returns int128.
  7. Calculate roots, logarithms, and exponentiation

    master

    Advanced mathematical functions for bytes16 quadruple precision numbers:

    • sqrt(bytes16 x): Calculates the square root of x.
    • log_2(bytes16 x): Calculates the binary logarithm ($\log_2(x)$).
    • ln(bytes16 x): Calculates the natural logarithm ($\ln(x)$).
    • pow_2(bytes16 x): Calculates $2^x$.
    • exp(bytes16 x): Calculates $e^x$.
  8. Convert between different number formats and quadruple precision

    master

    Use these functions to convert bytes16 quadruple precision numbers to and from other types:

    Integers:

    • fromInt(int256 x) $\rightarrow$ bytes16
    • toInt(bytes16 x) $\rightarrow$ int256
    • fromUInt(uint256 x) $\rightarrow$ bytes16
    • toUInt(bytes16 x) $\rightarrow$ uint256

    Fixed Point:

    • from128x128(int256 x) $\rightarrow$ bytes16 (Signed 128.128 bit)
    • to128x128(bytes16 x) $\rightarrow$ int256 (Signed 128.128 bit)
    • from64x64(int128 x) $\rightarrow$ bytes16 (Signed 64.64 bit)
    • to64x64(bytes16 x) $\rightarrow$ int128 (Signed 64.64 bit)

    Floating Point (Other precisions):

    • fromOctuple(bytes32 x) $\rightarrow$ bytes16 (Octuple precision)
    • toOctuple(bytes16 x) $\rightarrow$ bytes32 (Octuple precision)
    • fromDouble(bytes8 x) $\rightarrow$ bytes16 (Double precision)
    • toDouble(bytes16 x) $\rightarrow$ bytes8 (Double precision)
  9. Perform simple arithmetic with quadruple precision numbers

    master

    The library provides standard arithmetic operations for bytes16 quadruple precision numbers:

    • add(bytes16 x, bytes16 y): Returns x + y.
    • sub(bytes16 x, bytes16 y): Returns x - y.
    • mul(bytes16 x, bytes16 y): Returns x * y.
    • div(bytes16 x, bytes16 y): Returns x / y.
    • neg(bytes16 x): Returns -x.
    • abs(bytes16 x): Returns the absolute value of x.