Skip to main content

FPSignedOperators

add

Adds two FPSigneds, reverting on overflow.

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

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]FPSignedthe sum of a and b.

sub

Subtracts two FPSigneds, reverting on overflow.

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

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]FPSignedthe difference of a and b.

mul

Multiplies two FPSigneds, reverting on overflow.

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

This will "floor" the product.

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]FPSignedthe product of a and b.

neg

Negate a FPSigned

function neg(FPSigned a) internal pure returns (FPSigned)

Parameters

NameTypeDescription
aFPSignedValue to negate

Return Values

NameTypeDescription
[0]FPSignedFPSigned a * -1

div

Divides one FPSigned by a FPSigned, reverting on overflow or division by 0. This will "floor" the quotient.

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

There are two caveats with this computation:

  1. Max value for a is ~10^41, otherwise an intermediate value overflows. 10^41 is stored internally as int256 10^59
  2. Results that can't be represented exactly are truncated, not rounded. E.g., 2 / 3 = 0.6 repeating, which would round to 0.666666666666666667, but this computation produces the result 0.666666666666666666.

Parameters

NameTypeDescription
aFPSigneda FPSigned numerator.
bFPSigneda FPSigned denominator.

Return Values

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

isEqual

Whether a is equal to b.

function isEqual(FPSigned a, FPSigned b) internal pure returns (bool)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]boolTrue if equal, or False.

isNotEqual

Whether a is equal to b.

function isNotEqual(FPSigned a, FPSigned b) internal pure returns (bool)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]boolTrue if equal, or False.

isGreaterThan

Whether a is greater than b.

function isGreaterThan(FPSigned a, FPSigned b) internal pure returns (bool)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]boolTrue if a > b, or False.

isGreaterThanOrEqual

Whether a is greater than or equal to b.

function isGreaterThanOrEqual(FPSigned a, FPSigned b) internal pure returns (bool)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]boolTrue if a >= b, or False.

isLessThan

Whether a is less than b.

function isLessThan(FPSigned a, FPSigned b) internal pure returns (bool)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]boolTrue if a < b, or False.

isLessThanOrEqual

Whether a is less than or equal to b.

function isLessThanOrEqual(FPSigned a, FPSigned b) internal pure returns (bool)

Parameters

NameTypeDescription
aFPSigneda FPSigned.
bFPSigneda FPSigned.

Return Values

NameTypeDescription
[0]boolTrue if a <= b, or False.

abs

Absolute value of a FPSigned

function abs(FPSigned value) internal pure returns (FPUnsigned)

Parameters

NameTypeDescription
valueFPSignedValue to get abs() of

Return Values

NameTypeDescription
[0]FPUnsignedFPUnsigned

trunc

Convert a FPUnsigned to uint, "truncating" any decimal portion.

function trunc(FPSigned value) internal pure returns (int256)

roundFloor

Round a FPSigned towards -∞, not towards 0 like normal rounding.

function roundFloor(FPSigned value) internal pure returns (int256)
Dev note

Useful to handle "dust" from trades. If the P/L is a trader gain/vault loss, then fractional gain of trade should be ignored. If the P/L is a trader loss/vault gain, then fractional loss should be rounded "up" to -1.

Parameters

NameTypeDescription
valueFPSignedSigned value to round + trunc

Return Values

NameTypeDescription
[0]int256int256 value with any decimal component rounded towards -∞