Skip to main content

StableMath

StableMath

scaleBy

function scaleBy(uint256 x, uint256 to, uint256 from) internal pure returns (uint256)

Adjust the scale of an integer

Parameters

NameTypeDescription
xuint256
touint256Decimals to scale to
fromuint256Decimals to scale from

mulTruncate

function mulTruncate(uint256 x, uint256 y) internal pure returns (uint256)

Multiplies two precise units, and then truncates by the full scale

Parameters

NameTypeDescription
xuint256Left hand input to multiplication
yuint256Right hand input to multiplication

Return Values

NameTypeDescription
[0]uint256Result after multiplying the two inputs and then dividing by the shared scale unit

mulTruncateScale

function mulTruncateScale(uint256 x, uint256 y, uint256 scale) internal pure returns (uint256)

Multiplies two precise units, and then truncates by the given scale. For example, when calculating 90% of 10e18, (10e18 * 9e17) / 1e18 = (9e36) / 1e18 = 9e18

Parameters

NameTypeDescription
xuint256Left hand input to multiplication
yuint256Right hand input to multiplication
scaleuint256Scale unit

Return Values

NameTypeDescription
[0]uint256Result after multiplying the two inputs and then dividing by the shared scale unit

mulTruncateCeil

function mulTruncateCeil(uint256 x, uint256 y) internal pure returns (uint256)

Multiplies two precise units, and then truncates by the full scale, rounding up the result

Parameters

NameTypeDescription
xuint256Left hand input to multiplication
yuint256Right hand input to multiplication

Return Values

NameTypeDescription
[0]uint256Result after multiplying the two inputs and then dividing by the shared scale unit, rounded up to the closest base unit.

divPrecisely

function divPrecisely(uint256 x, uint256 y) internal pure returns (uint256)

Precisely divides two units, by first scaling the left hand operand. Useful for finding percentage weightings, i.e. 8e18/10e18 = 80% (or 8e17)

Parameters

NameTypeDescription
xuint256Left hand input to division
yuint256Right hand input to division

Return Values

NameTypeDescription
[0]uint256Result after multiplying the left operand by the scale, and executing the division on the right hand input.