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
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
Name | Type | Description |
---|
a | uint256 | uint to convert into a FixedPoint. |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | the 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
Name | Type | Description |
---|
value | uint256 | uint256, e.g. 10000000 wei USDC |
decimals | uint8 | uint8 number of decimals to interpret value as, e.g. 6 |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | output 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
Name | Type | Description |
---|
a | int256 | int to convert into a FPSigned. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | FPSigned 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
Name | Type | Description |
---|
a | FPUnsigned | FPUnsigned to be converted |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | FPSigned 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
Name | Type | Description |
---|
a | FPUnsigned | a FPUnsigned. |
b | uint256 | a uint256. |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | the 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
Name | Type | Description |
---|
a | FPUnsigned | a FPUnsigned. |
b | uint256 | a uint256. |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | the 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)
This will "floor" the product.
Parameters
Name | Type | Description |
---|
a | FPUnsigned | a FPUnsigned. |
b | FPUnsigned | a FPUnsigned. |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | the 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)
This will "floor" the product.
Parameters
Name | Type | Description |
---|
a | FPUnsigned | a FPUnsigned. |
b | uint256 | a uint256. |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | the 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)
This will "floor" the quotient.
Parameters
Name | Type | Description |
---|
a | FPUnsigned | a FPUnsigned numerator. |
b | uint256 | a uint256 denominator. |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | the quotient of a divided by b . |
min
The minimum of a
and b
.
function min(FPUnsigned a, FPUnsigned b) internal pure returns (FPUnsigned)
Parameters
Name | Type | Description |
---|
a | FPUnsigned | a FPUnsigned. |
b | FPUnsigned | a FPUnsigned. |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | the minimum of a and b . |
max
The maximum of a
and b
.
function max(FPUnsigned a, FPUnsigned b) internal pure returns (FPUnsigned)
Parameters
Name | Type | Description |
---|
a | FPUnsigned | a FPUnsigned. |
b | FPUnsigned | a FPUnsigned. |
Return Values
Name | Type | Description |
---|
[0] | FPUnsigned | the 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
Name | Type | Description |
---|
a | FPSigned | a FPSigned. |
b | FPUnsigned | an FPUnsigned. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the 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
Name | Type | Description |
---|
a | FPSigned | a FPSigned. |
b | int256 | an int256. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the 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
Name | Type | Description |
---|
a | FPSigned | a FPSigned. |
b | FPUnsigned | a FPUnsigned. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the 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
Name | Type | Description |
---|
a | FPSigned | a FPSigned. |
b | uint256 | a uint256. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the 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)
This will "floor" the product.
Parameters
Name | Type | Description |
---|
a | FPSigned | a FPSigned. |
b | uint256 | a uint256. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the product of a and b . |
mul
Multiplies a FPSigned
and FPUnsigned
, reverting on overflow.
function mul(FPSigned a, FPUnsigned b) internal pure returns (FPSigned)
This will "floor" the product.
Parameters
Name | Type | Description |
---|
a | FPSigned | a FPSigned. |
b | FPUnsigned | a FPUnsigned. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the 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)
This will "floor" the quotient.
Parameters
Name | Type | Description |
---|
a | FPSigned | a FPSigned numerator. |
b | FPUnsigned | a FPUnsigned denominator. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the 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)
This will "floor" the quotient.
Parameters
Name | Type | Description |
---|
a | FPSigned | a FPSigned numerator. |
b | uint256 | a uint256 denominator. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the quotient of a divided by b . |
min
The minimum of a
and b
.
function min(FPSigned a, FPSigned b) internal pure returns (FPSigned)
Parameters
Name | Type | Description |
---|
a | FPSigned | a FPSigned. |
b | FPSigned | a FPSigned. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the minimum of a and b . |
max
The maximum of a
and b
.
function max(FPSigned a, FPSigned b) internal pure returns (FPSigned)
Parameters
Name | Type | Description |
---|
a | FPSigned | a FPSigned. |
b | FPSigned | a FPSigned. |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the maximum of a and b . |
pow
The exponential of a^b
.
function pow(FPSigned a, FPUnsigned b) internal pure returns (FPSigned)
Parameters
Name | Type | Description |
---|
a | FPSigned | a FPSigned |
b | FPUnsigned | a FPUnsigned |
Return Values
Name | Type | Description |
---|
[0] | FPSigned | the 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.
SignedOverflow
Attempted to convert an Unsigned to Signed, but it was too large.
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
FPUnsigned MAX_UNSIGNED = Maximum value of an unsigned FixedPoint number.;
MAX_SIGNED
FPSigned MAX_SIGNED = Maximum value of a signed FixedPoint number.;
MIN_SIGNED
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
.