Giter Site home page Giter Site logo

performance of pow function about bignumber.js HOT 5 CLOSED

rbro avatar rbro commented on May 29, 2024
performance of pow function

from bignumber.js.

Comments (5)

MikeMcl avatar MikeMcl commented on May 29, 2024

Among other reasons, bcmath is a compiled C extension so it will of course be much faster.

In this library, the pow function can be slow as it always calculates its result (or, if the exponent is negative, its intermediate result) to full precision without rounding.

In your example, the function will first calculate 1.00110123456782000, the result of which is over 26000 digits long. Then, because the exponent is negative, the function will calculate and return the inverse, which will be rounded because it's a division operation.

So, although you will only see a result with as many decimal places as you have specified with config, behind the scenes the function will have perfomed many intermediate operations on numbers thousands of digits long - and that takes time.

It is a 'feature' of this library that all arithmetic operations are lossless, i.e. unrounded, except those involving division. This makes it hard to lose precision and makes the library simple to use, but as you have discovered it makes the pow function unviable for anything other than small integers.

What is the solution? The short answer is that the pow function needs to calculate its result to a limited number of significant digits. I did implement this in what was to be version 2 of this library, but I ended up making many other changes and eventually decided to release it separately as decimal.js. That is a more suitable library if you want to do power operations with larger exponents.

from bignumber.js.

rbro avatar rbro commented on May 29, 2024

Michael - thanks for your quick and detailed explanation. One question about decimal.js:

I see in decimal.js that the round function doesn't take any parameters, so it always rounds to a whole number. Is toDecimalPlaces in decimal.js the equivalent of round in bignumber.js? In other words, how can I do a calculation with a certain precision for the intermediate calculations, but then round the result to a lower precision?

For example, in bignumber.js, I can do:

var a = new BigNumber(1.125).plus(0.001).round(2);

and it returns 1.13.

Would the equivalent in decimal.js be:

var a = new Decimal(1.125).plus(0.001).toDecimalPlaces(2);

Thanks again for your help

from bignumber.js.

MikeMcl avatar MikeMcl commented on May 29, 2024

Yes, that is correct.

In decimal.js the round method just follows Math.round behaviour and rounds to an integer because of the ambiguity of whether the rounding is to decimal places or significant digits.

Your example would work fine unless you have previously set a Decimal.precision of less than 3, as in decimal.js all arithmetic operations are only performed to the number of significant digits specified by the current precision setting.

Please ask any further questions about decimal.js in its own issues section.

from bignumber.js.

MikeMcl avatar MikeMcl commented on May 29, 2024

TODO: Implement a global maximum precision setting in terms of significant digits for bignumber.js, in addition to the current division decimal places setting. This may also solve the issue of adding numbers with massively different exponents which is also currently so slow as to be unviable.

from bignumber.js.

MikeMcl avatar MikeMcl commented on May 29, 2024

Added a POW_PRECISION configuration option in v2.0.1, so the number of significant digits calculated by the power operation can be limited. This has greatly improved its performance.

from bignumber.js.

Related Issues (20)

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.