Skip to main content

FixedPoint

FixedPoint

Fixed-point decimals in Solidity.

Library for 18 decimal point numbers with common operators and math functions. 1 is represented by 1e18, 0.5 is represented by 5e17, etc.

Addresses

ChainAddress

Functions

fromUnscaledUint

Constructs an FPUnsigned from an unscaled uint, e.g., b=5 gets stored internally as 5*(10**18).

function fromUnscaledUint(uint256 a) internal pure returns (FPUnsigned)

Parameters

NameTypeDescription
auint256uint to convert into a FixedPoint.

Return Values

NameTypeDescription
[0]FPUnsignedthe converted FixedPoint.

fromScalar

Given a uint with a certain number of decimal places, normalize it to a FixedPoint

function fromScalar(uint256 value, uint8 decimals) internal pure returns (FPUnsigned)

Parameters

NameTypeDescription
valueuint256uint256, e.g. 10000000 wei USDC
decimalsuint8uint8 number of decimals to interpret value as, e.g. 6

Return Values

NameTypeDescription
[0]FPUnsignedoutput FPUnsigned, e.g. (10.000000)

fromUnscaledInt

Construct a FPSigned from an unscaled int, e.g., b=5 gets stored internally as 5*(10**18).

function fromUnscaledInt(int256 a) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aint256int to convert into a FPSigned.

Return Values

NameTypeDescription
[0]FPSignedFPSigned a represented as a FPSigned.

fromUnsigned

Convert a FPUnsigned into a FPSigned. Revert if too large.

function fromUnsigned(FPUnsigned a) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPUnsignedFPUnsigned to be converted

Return Values

NameTypeDescription
[0]FPSignedFPSigned a as a positive FPSigned

add

Adds an unscaled uint256 to an FPUnsigned, reverting on overflow.

function add(FPUnsigned a, uint256 b) internal pure returns (FPUnsigned)

Parameters

NameTypeDescription
aFPUnsigneda FPUnsigned.
buint256a uint256.

Return Values

NameTypeDescription
[0]FPUnsignedthe sum of a and b.

sub

Subtracts an unscaled uint256 from an FPUnsigned, reverting on overflow.

function sub(FPUnsigned a, uint256 b) internal pure returns (FPUnsigned)

Parameters

NameTypeDescription
aFPUnsigneda FPUnsigned.
buint256a uint256.

Return Values

NameTypeDescription
[0]FPUnsignedthe difference of a and b.

mul

Multiplies an FPUnsigned and an unscaled uint256, reverting on overflow.

function mul(FPUnsigned a, FPUnsigned b) internal pure returns (FPUnsigned)
Dev note

This will "floor" the product.

Parameters

NameTypeDescription
aFPUnsigneda FPUnsigned.
bFPUnsigneda FPUnsigned.

Return Values

NameTypeDescription
[0]FPUnsignedthe product of a and b.

mul

Multiplies an FPUnsigned and an unscaled uint256, reverting on overflow.

function mul(FPUnsigned a, uint256 b) internal pure returns (FPUnsigned)
Dev note

This will "floor" the product.

Parameters

NameTypeDescription
aFPUnsigneda FPUnsigned.
buint256a uint256.

Return Values

NameTypeDescription
[0]FPUnsignedthe product of a and b.

div

Divides one FPUnsigned by an unscaled uint256, reverting on overflow or division by 0.

function div(FPUnsigned a, uint256 b) internal pure returns (FPUnsigned)
Dev note

This will "floor" the quotient.

Parameters

NameTypeDescription
aFPUnsigneda FPUnsigned numerator.
buint256a uint256 denominator.

Return Values

NameTypeDescription
[0]FPUnsignedthe quotient of a divided by b.

min

The minimum of a and b.

function min(FPUnsigned a, FPUnsigned b) internal pure returns (FPUnsigned)

Parameters

NameTypeDescription
aFPUnsigneda FPUnsigned.
bFPUnsigneda FPUnsigned.

Return Values

NameTypeDescription
[0]FPUnsignedthe minimum of a and b.

max

The maximum of a and b.

function max(FPUnsigned a, FPUnsigned b) internal pure returns (FPUnsigned)

Parameters

NameTypeDescription
aFPUnsigneda FPUnsigned.
bFPUnsigneda FPUnsigned.

Return Values

NameTypeDescription
[0]FPUnsignedthe maximum of a and b.

fromSigned

function fromSigned(FPSigned a) internal pure returns (FPUnsigned)

add

Adds a FPSigned to an FPUnsigned, reverting on overflow.

function add(FPSigned a, FPUnsigned b) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPUnsignedan FPUnsigned.

Return Values

NameTypeDescription
[0]FPSignedthe sum of a and b.

sub

Subtracts an unscaled int256 from a FPSigned, reverting on overflow.

function sub(FPSigned a, int256 b) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bint256an int256.

Return Values

NameTypeDescription
[0]FPSignedthe difference of a and b.

sub

Subtracts an FPUnsigned from a FPSigned, reverting on overflow.

function sub(FPSigned a, FPUnsigned b) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPUnsigneda FPUnsigned.

Return Values

NameTypeDescription
[0]FPSignedthe difference of a and b.

sub

Subtracts an unscaled uint256 from a FPSigned, reverting on overflow.

function sub(FPSigned a, uint256 b) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
buint256a uint256.

Return Values

NameTypeDescription
[0]FPSignedthe difference of a and b.

mul

Multiplies a FPSigned and an unscaled uint256, reverting on overflow.

function mul(FPSigned a, uint256 b) internal pure returns (FPSigned)
Dev note

This will "floor" the product.

Parameters

NameTypeDescription
aFPSigneda FPSigned.
buint256a uint256.

Return Values

NameTypeDescription
[0]FPSignedthe product of a and b.

mul

Multiplies a FPSigned and FPUnsigned, reverting on overflow.

function mul(FPSigned a, FPUnsigned b) internal pure returns (FPSigned)
Dev note

This will "floor" the product.

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPUnsigneda FPUnsigned.

Return Values

NameTypeDescription
[0]FPSignedthe product of a and b.

div

Divides one FPSigned by an FPUnsigned, reverting on overflow or division by 0.

function div(FPSigned a, FPUnsigned b) internal pure returns (FPSigned)
Dev note

This will "floor" the quotient.

Parameters

NameTypeDescription
aFPSigneda FPSigned numerator.
bFPUnsigneda FPUnsigned denominator.

Return Values

NameTypeDescription
[0]FPSignedthe quotient of a divided by b.

div

Divides one FPSigned by an unscaled uint256, reverting on overflow or division by 0.

function div(FPSigned a, uint256 b) internal pure returns (FPSigned)
Dev note

This will "floor" the quotient.

Parameters

NameTypeDescription
aFPSigneda FPSigned numerator.
buint256a uint256 denominator.

Return Values

NameTypeDescription
[0]FPSignedthe quotient of a divided by b.

min

The minimum of a and b.

function min(FPSigned a, FPSigned b) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]FPSignedthe minimum of a and b.

max

The maximum of a and b.

function max(FPSigned a, FPSigned b) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]FPSignedthe maximum of a and b.

pow

The exponential of a^b.

function pow(FPSigned a, FPUnsigned b) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPSigneda FPSigned
bFPUnsigneda FPUnsigned

Return Values

NameTypeDescription
[0]FPSignedthe exponential of a^

Errors

Unrepresentable

Attempted to convert a scaled uint to a FixedPoint, but the number has more decimal places than FixedPoint and cannot be represented exactly.

error Unrepresentable()

SignedOverflow

Attempted to convert an Unsigned to Signed, but it was too large.

error SignedOverflow()

UnsignedUnderflow

Attempted to convert a Signed to Unsigned, but it was negative. Consider using abs.

error UnsignedUnderflow()

Variables

FP_DECIMALS

uint256 FP_DECIMALS = 18;

FP_SCALING_FACTOR

uint256 FP_SCALING_FACTOR = 10**18;

SFP_SCALING_FACTOR

int256 SFP_SCALING_FACTOR = 10**18;

ONE

FPUnsigned ONE = FPUnsigned.wrap(10**18);

ZERO

FPUnsigned ZERO = FPUnsigned.wrap(0);

MAX_UNSIGNED_FACTOR

Largest FPUnsigned which can be squared without reverting.

FPUnsigned MAX_UNSIGNED_FACTOR = FPUnsigned.wrap(340282366920938463463374607431768211455);

MIN_SIGNED_FACTOR

Most negative FPSigned which can be squared without reverting.

FPSigned MIN_SIGNED_FACTOR = FPSigned.wrap(-240615969168004511545033772477625056927);

MAX_SIGNED_FACTOR

Greatest FPSigned which can be squared without reverting.

FPSigned MAX_SIGNED_FACTOR = FPSigned.wrap(240615969168004511545033772477625056927);

MAX_UNSIGNED_CUBE_FACTOR

Largest FPUnsigned which can be cubed without reverting.

FPUnsigned MAX_UNSIGNED_CUBE_FACTOR = FPUnsigned.wrap(48740834812604276470692694885616);

MIN_SIGNED_CUBE_FACTOR

Most negative FPSigned which can be cubed without reverting.

FPSigned MIN_SIGNED_CUBE_FACTOR = FPSigned.wrap(-38685626227668133590597631999999);

MAX_SIGNED_CUBE_FACTOR

Greatest FPSigned which can be cubed without reverting.

FPSigned MAX_SIGNED_CUBE_FACTOR = FPSigned.wrap(38685626227668133590597631999999);

FPUnsigned

User-defined type: an unsigned fixed-point decimal, stored in a uint256. Represents a value up to (2^256 - 1)/10^18 = ~10^59.

FPSigned

User-defined type: a signed fixed-point decimal, stored in an int256. Represents a value up to ±(2^255 - 1)/10^18 = ~10^58.