npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

safe-qmath

v2.1.0

Published

Solidity library that provides basic mathematical operations on UQ128.64 fixed point numbers. UQ128.64 numbers are unsigned positive numbers with 128 bits of whole number precision and 64 bits of fractional precision.

Downloads

11

Readme

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 number

Documentation

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 uint256 whole number to its corresponding UQ128.64 number representation. Will revert if x is above the largest possible UQ128.64 whole number which is 2^128 - 1

  • function qToIntLossy(uint192 x) internal pure returns (uint256)

    converts a UQ128.64 number to it's corresponding whole number uint256 representation. This function truncates the decimal portion without reverting

  • function 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 number

  • function 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^-128

  • function 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 if y is zero

  • function qpow(uint192 base, uint256 exp) internal pure returns (uint192)

    calculates the base to the power of exp. The base is interpreted as a UQ128.64 number but the exponent exp as a simple whole number.

    Example:

    // 1.5^6
    uint192 res = (0x18000000000000000).qpow(6); // => 0xb6400000000000000
  • function 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 of uint192 is returned

  • function 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) -> BN

    converts 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 SafeQMath library

  • convertUQIntToFloat(x: BN) -> Number

    converts a UQ128.64 number to the corresponding number.

    utils.convertUQIntToFloat(new BN('18000000000000000', 16)) // => 1.5
  • round(x: Number, places: Number = 2) -> Number

    rounds 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.123

    By default the given number will be rounded to 2 decimal places.

  • constants: Object

    utils.constants is as the name suggests a bunch of constants relating to the SafeQMath library such as utils.constants.ONE, the number 1.0 as a UQ128.64 number stored as a Big Number. Other constants are also available on the utils.constants Object that are mainly used in the util functions internally.