Git Product home page Git Product logo

ds-math's Introduction

DSMath

Safe Arithmetic

DS-Math provides arithmetic functions for the common numerical primitive types of Solidity. You can safely add, subtract, multiply, and divide uint numbers without fear of integer overflow. You can also find the minimum and maximum of two numbers.

Additionally, this package provides arithmetic functions for new two higher level numerical concepts called wad (18 decimals) and ray (27 decimals). These are used to represent fixed-point decimal numbers.

A wad is a decimal number with 18 digits of precision and a ray is a decimal number with 27 digits of precision. These functions are necessary to account for the difference between how integer arithmetic behaves normally, and how decimal arithmetic should actually work. A brief example using wmul, which returns the product of a wad and another number:

1.1 * 2.2 == 2.42

//Regular integer arithmetic adds orders of magnitude:

110 * 220 == 24200

// Wad arithmetic does not add orders of magnitude:

wmul(1.1 ether, 2.2 ether) == 2.42 ether

Naming Convention

The standard functions are the uint set, so their function names are not prefixed: add, sub, mul, min, and max. There is no div function, as divide-by-zero checking is built into the Solidity compiler.

The int functions have an i prefix: imin, and imax.

Wad functions have a w prefix: wmul, wdiv.

Ray functions have a r prefix: rmul, rdiv, rpow.

API Reference

add

Return x + y or an exception in case of uint overflow.

sub

Return x - y or an exception in case of uint overflow.

mul

Return x * y or an exception in case of uint overflow.

min

Return the smaller number of x and y.

max

Return the larger number of x and y.

imin

Return the smaller number of x and y.

imax

Return the larger number of x and y.

wmul

Multiply two Wads and return a new Wad with the correct level of precision. A Wad is a decimal number with 18 digits of precision that is being represented as an integer.

wdiv

Divide two Wads and return a new Wad with the correct level of precision. A Wad is a decimal number with 18 digits of precision that is being represented as an integer.

rmul

Multiply two Rays and return a new Ray with the correct level of precision. A Ray is a decimal number with 27 digits of precision that is being represented as an integer.

rdiv

Divide two Rays and return a new Ray with the correct level of precision. A Ray is a decimal number with 27 digits of precision that is being represented as an integer.

rpow

Raise a Ray to the n^th power and return a new Ray with the correct level of precision. A Ray is a decimal number with 27 digits of precision that is being represented as an integer.

ds-math's People

Contributors

apmilen avatar d-xo avatar dbrock avatar desaperados avatar gbalabasquer avatar mbrock avatar mrchico avatar nanexcool avatar nedlowe avatar nmushegian avatar rainbreak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ds-math's Issues

Allows dividing by 0?

Should dividing by 0 be allowed? IMO this is something that should be asserted against and if trying to divide by 0 cause a throw (i think it is unexpected that the divisor is 0).

Also you are not asserting that the result of the division is correct.

Typo in readme

This "110 * 220 == 22400" should be this "110 * 220 = 24200"..

Are these errors from `slither` valid problems?

I think it is a false positive so also opened an issue in slither crytic/slither#741, but thought to post it here in case you might give some more feedback on this issue and whether it is something to worry about

INFO:Detectors:
DSMath.rpow(uint256,uint256) (contracts/Main_merged.sol#2309-2319) uses a weak PRNG: "n % 2 != 0 (contracts/Main_merged.sol#2315)" 
DSMath.rpow(uint256,uint256) (contracts/Main_merged.sol#2309-2319) uses a weak PRNG: "n % 2 != 0 (contracts/Main_merged.sol#2310)" 
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#weak-PRNG
INFO:Detectors:
DSMath.mul(uint256,uint256) (contracts/Main_merged.sol#2233-2235) uses a dangerous strict equality:
        - require(bool,string)(y == 0 || (z = x * y) / y == x,ds-math-mul-overflow) (contracts/Main_merged.sol#2234)
Reference: https://github.com/crytic/slither/wiki/Detector-Documentation#dangerous-strict-equalities

`rpow(x, n)` broken in Remix

Hey,

just played around with ds-math, cool stuff! In Remix however, the rpow(x, n) function always returns 0 and seems broken. Any idea why?

Thanks!

Help with understanding rounding

Can you please help me understand the rounding behavior with this library. My calculation:

x = 500000000000000000000
y = 1000000001255014786207189902
rmul(x, y) = 500000000627507393104

For example, using BigNumber.js results in 500.000000627507393103 with rounding down. Assuming that this is expected behavior, but it's not clear to me why this calculation is rounding up.

I see some documentation here (ce67c0f) but I'm not getting it.

Multiplication of two positive numbers can result in zero

The wmul function does not behave as expected in certain cases. In particular, the multiplication of two positive numbers can result in zero. That is true for every a,b > 0 and b < 5*10^17 / a (as shown here).

In such cases, I was expecting the wmul function to revert the transaction instead of returning 0.
Were you aware of this behavior ? If you were, are you planning to modify the library ? I believe the way to go, for a far more reasonable behavior, would be to either revert the transaction if there's a rounding to 0, or properly document this unexpected behavior in the function's docs.

Note that a similar issue happens in rmul

Max and Min functions? Safer?

Thank you guys for the beautiful readme! It really helped me understand the purpose of the code SOOOOO much more.

I am curious about the Max and Min functions. Do they actually prevent inaccurate answers in the case of overflow?

Specifically:

function min(uint256 x, uint256 y) constant internal returns (uint256 z) {
return x <= y ? x : y;

If i have x== (2^256) +1 and y == 100 would it actually give me the y (the expected answer by humans) or would it give me x the overflow making x look like a small number to 'puters.

I am a novice when it comes to computer science... I might be getting overflow wrong and using the wrong numbers, but I think you get my point.

Thank you again for the beautiful library and the hard work you have put into it, I am excited about implementing this into our Giveth projects

Much love,
Griff

NPM package

With Truffle search node_modules path for imports, a package.json would be handy.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.