safe-qmath v2.1.0
Library for UQInt128.64 math operations
This Library enables simple arithmetic operations for fractional numbers.
Fractional numbers are stored in a UQ128.64 format. This means that each
fractional number should be stored in a uint192. This is done on the one hand
to avoid mixing up fractional numbers with whole numbers and to make certain
calculations more efficient.
Installation
npm i safe-qmath
Usage
contract:
import 'safe-qmath/contracts/SafeQMath.sol';
import '@openzeppelin/contracts/math/SafeMath.sol';
contract FracMath {
using SafeQMath for uint192;
using SafeMath for uint256;
function calculateFee(uint256 principalAmount, uint192 fee)
public
pure
returns (uint256 remainder, uint256 takenFee)
{
takenFee = SafeQMath.intToQ(principalAmount).qmul(fee).qToIntLossy();
remainder = principalAmount.sub(takenFee);
}
}javascript:
const { convertFloatToUQInt } = require('safe-qmath/utils')
const uqintFee = convertFloatToUQInt(0.2) // returns BN storing the UQ128.64 numberDocumentation
1 Number format (UQ128.64)
When the SafeQMath library performs arithmetics on uin192 it expects UQ128.64
numbers. UQ128.64 numbers are unsigned fixed points numbers with a total length
of 192 bits with the last 64 bits representing the fractional part:
UQ128.64: <whole number: 128 bits> | <fraction: 64>
Mathematically to convert the raw uin192 to its "real" representation simply
divide it by 2^64. The reverse goes for converting a decimal number to a
UQ128.64 number, simply multiply your number by 2^64. UQ128.64 numbers have a
precision of up to 2^-64
2 Functions
2.1 Solidity
Import using: import 'safe-qmath/contracts/SafeQMath.sol';
function intToQ(uint256 x) internal pure returns (uint192)converts a
uint256whole number to its corresponding UQ128.64 number representation. Will revert ifxis above the largest possible UQ128.64 whole number which is2^128 - 1function qToIntLossy(uint192 x) internal pure returns (uint256)converts a UQ128.64 number to it's corresponding whole number
uint256representation. This function truncates the decimal portion without revertingfunction qToInt(uint192 x) internal pure returns (uint256)just like
qToIntLossy()except reverts if any decimal data is present. Only use if you're sure your calculations will result in a whole numberfunction qadd(uint192 x, uint192 y) internal pure returns (uint192)adds two UQ128.64 numbers reverting on overflow
function qsub(uint192 x, uint192 y) internal pure returns (uint192)subtracts two UQ128.64 numbers reverting on overflow
function qmul(uint192 x, uint192 y) internal pure returns (uint192)multiplies two UQ128.64 numbers reverting on overflow. Calculation can result in an error of up to
2^-64 - 2^-128function qdiv(uint192 x, uint192 y) internal pure returns (uint192)multiplies two UQ128.64 numbers reverting on overflow. Calculation can result in an error of up to
2^-64 - 2^-128. Will also revert ifyis zerofunction qpow(uint192 base, uint256 exp) internal pure returns (uint192)calculates the
baseto the power ofexp. Thebaseis interpreted as a UQ128.64 number but the exponentexpas a simple whole number.Example:
// 1.5^6 uint192 res = (0x18000000000000000).qpow(6); // => 0xb6400000000000000function qfloor(uint192 x) internal pure returns (uint192)applies the floor operation to the given UQ128.64 number. Flooring means rounding down to the closest whole number. Equivalent to
qToIntLossy()except a UQ128.64 in form ofuint192is returnedfunction qceil(uint192 x) internal pure returns (uint192)applies the ceil operation to the given UQ128.64 number. Applying the ceil operation means rounding up to the closest whole number. Will revert on overflow
2.2 Javascript
Import by using:
const utils = require('safe-qmath/utils') // or
const { convertFloatToUQInt } = require('safe-qmath/utils') // or
import utils from 'safe-qmath/utils' // or
import { convertFloatToUQInt } from 'safe-qmath/utils'convertFloatToUQInt(x: Number) -> BNconverts a float into its corresponding UQ128.64 representation stored in a js Big Number. Can directly be passed to web3 when calling a contract using the
SafeQMathlibraryconvertUQIntToFloat(x: BN) -> Numberconverts a UQ128.64 number to the corresponding number.
utils.convertUQIntToFloat(new BN('18000000000000000', 16)) // => 1.5round(x: Number, places: Number = 2) -> Numberrounds a javascript to Number up to a certain amount of places:
utils.round(1.12345) // => 1.12 utils.round(1.12345, 2) // => 1.12 utils.round(1.12345, 3) // => 1.123By default the given number will be rounded to
2decimal places.constants: Objectutils.constantsis as the name suggests a bunch of constants relating to theSafeQMathlibrary such asutils.constants.ONE, the number1.0as a UQ128.64 number stored as a Big Number. Other constants are also available on theutils.constantsObject that are mainly used in the util functions internally.