PERI Finance

Searchâ€¦

PERI Finance

Development Resources

Resources

SafeDecimalMath

Description

This is a library contract that provides the ability to manipulate fractional numbers, performing safe arithmetic with unsigned fixed-point decimals.

The decimals this library provides can operate at either of two different precision levels. Standard precision operations act on numbers with 18 decimal places. High precision numbers possess 27 decimal places, and have their own corresponding set of functions.

Several functions are included for converting between precision levels, and operations which round to the nearest increment to remove truncation bias. In SafeDecimalMath, a half-increment rounds up.

SafeDecimalMath uses OpenZeppelin's SafeMath library for most of its basic arithmetic operations in order to protect from arithmetic overflows and zero divisions.

In PeriFinance, the standard precision fixed point numbers are used to deal with most fractional quantities, such as token balances and prices. The high-precision numbers are mainly used for dealing with the debt ledger, which is constructed as an extended product of many fractional numbers very close to 11. As this is a financially-sensitive component of the system, representational precision matters in order to minimise errors resulting from rounding or truncation.

For a precision of dd deimal places, this fixed point library chooses a large integer Ë™u=10duË™=10d to represent the number 11 (e.g.

`UNIT`

= 10181018) and all operations at this precision level happen relative to Ë™uuË™. That is, the fixed point representation of a number qq is defined to be the integer Ë™qqË™:Ë™q := q Ë™u = qÃ—10dqË™ := q uË™ = qÃ—10dFor example, at 27 decimal places, Ë™2525Ë™ is equivalent to 25Ã—102725Ã—1027. We will use square brackets to capture the fixed point representation of composite expressions.

Note that this is only valid if Ë™qqË™ is an integer, so nothing is representable which has a positive value in the decimal places smaller than 1Ë™u1uË™ (i.e. the integer 1).

We define the fixed point operations Ë™++Ë™, Ë™âˆ’âˆ’Ë™, Ë™Ã—Ã—Ë™, Ë™//Ë™, corresponding to the ordinary arithmetic operations ++, âˆ’âˆ’, Ã—Ã—, //, where // corresponds to integer division. These are implemented by

`SafeMath`

and protect from overflow.We define our additive fixed point operators to be the same as the standard ones:

Definition: Fixed Point Addition and Subtractionx Ë™+ y := x+yx Ë™âˆ’ y := xâˆ’yx +Ë™ y := x+yx âˆ’Ë™ y := xâˆ’y

This is because:Ë™pÂ±Ë™q := pË™uÂ±qË™u = (pÂ±q)Ë™u =: Ë™[pÂ±q]pË™Â±qË™ := puË™Â±quË™ = (pÂ±q)uË™ =: [pÂ±q]Ë™

The multiplicative operations are defined as follows:

Definition: Fixed Point Multiplication and Divisionx Ë™Ã— y := (xÃ—y) / Ë™ux Ë™/ y := (xÃ—Ë™u) / yx Ã—Ë™ y := (xÃ—y) / uË™x /Ë™ y := (xÃ—uË™) / y

Some care has to be taken for multiplication and division. We desire, for example, Ë™p Ë™Ã— Ë™q=Ë™[pÃ—q]pË™ Ã—Ë™ qË™=[pÃ—q]Ë™. However, if the standard operations are performed naively, the following results are obtained:Ë™pÃ—Ë™q := pË™uÃ—qË™u = (pÃ—q)Ë™u2 =: Ë™[(pÂ±q)Ë™u]Ë™p / Ë™q := pË™u / qË™u = p / qpË™Ã—qË™ := puË™Ã—quË™ = (pÃ—q)uË™2 =: [(pÂ±q)uË™]Ë™pË™ / qË™ := puË™ / quË™ = p / q

So multiplication produces an extra unwanted unit factor, and division divides one out; the fixed point operations need to account for this. Note that to ensure minimum precision loss, Ë™uuË™ is divided out last in the case of multiplication and multiplied in first in the case of division.

Note that multiplication and division of fixed point numbers may involve some loss of precision in the lowest digit. Such inaccuracy can accumulate over many operations

PeriFinance provides versions of Ë™Ã—Ã—Ë™ and Ë™//Ë™ which perform the operation with one extra internal digit of precision, and then rounds up if the least significant digit is 5 or greater. Consequently, results exactly halfway between two increments are rounded up.

The representation of a number qq at two different fixed point precision levels Ë™q=qË™uqË™=quË™ and Â¨q=qÂ¨uqÂ¨=quÂ¨ is straightforward if Ë™uuË™ and Â¨uuÂ¨ divide evenly. If this is the case, and Â¨uuÂ¨ is the higher precision unit, then Â¨q/Ë™q=Â¨u/Ë™uqÂ¨/qË™=uÂ¨/uË™. So converting between the high and low precision only involves multiplying or dividing by a factor of Â¨u/Ë™uuÂ¨/uË™. Keep in mind that converting from a high precision to a low precision number involves some loss of information, and this operation is performed with rounding.

Variables

`PRECISE_UNIT`

Source

The high precision number (10271027) that represents 1.01.0.

`1e27`

`uint256`

`UNIT`

Source

The standard precision number (10181018) that represents 1.01.0.

`1e18`

`uint256`

`decimals`

Source

The number of decimals (1818) in the standard precision fixed point representation.

`18`

`uint8`

`highPrecisionDecimals`

Source

The number of decimals (2727) in the high precision fixed point representation.

`27`

`uint8`

Views

`preciseUnit`

Source

Pure alias to

`PRECISE_UNIT`

.Details`unit`

Source

Pure alias to

`UNIT`

.DetailsInternal Functions

`decimalToPreciseDecimal`

Source

Converts from standard precision to high precision numbers. This is just multiplication by 109109.Details

`divideDecimal`

Source

Returns the quotient of two standard precision fixed point numbers, handling precision loss by truncation.Details

`divideDecimalRound`

Source

Returns the quotient of two standard precision fixed point numbers, handling precision loss by rounding.

Equivalent to

`_divideDecimalRound(x, y, UNIT)`

.Details`divideDecimalRoundPrecise`

Source

Returns the quotient of two high precision fixed point numbers, handling precision loss by rounding.

Equivalent to

`_divideDecimalRound(x, y, PRECISE_UNIT)`

.Details`floorsub`

SourceDetails

`multiplyDecimal`

Source

Returns the product of two standard precision fixed point numbers, handling precision loss by truncation.Details

`multiplyDecimalRound`

Source

Returns the product of two standard precision fixed point numbers, handling precision loss by rounding.

Equivalent to

`_multiplyDecimalRound(x, y, UNIT)`

.Details`multiplyDecimalRoundPrecise`

Source

Returns the product of two high precision fixed point numbers, handling precision loss by rounding.

Equivalent to

`_multiplyDecimalRound(x, y, PRECISE_UNIT)`

.Details`preciseDecimalToDecimal`

Source

Converts from high precision to standard precision numbers. This is division by 109109, where precision loss is handled by rounding.Details

- 1.SafeDecimalMath provides two different precision levels because the Ethereum virtual machine encodes integers in 256 bits. Given a finite integer size, an increase in precision implies a decrease in the maximum representable number, since more bits are dedicated to representing the fractional part, and fewer to the integer part. â†©â€‹

Last modified 5mo ago

Export as PDF

Copy link