Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer roots #50

Open
garnet420 opened this issue Apr 8, 2019 · 0 comments
Open

Integer roots #50

garnet420 opened this issue Apr 8, 2019 · 0 comments

Comments

@garnet420
Copy link
Contributor

Consider adding support for integer roots, as it can be done somewhat faster than pow in general (and may be more accurate). This quick implementation is about 25% faster:

      Decimal.prototype.nthRoot = function (value) {
        if (this.mantissa === 0) return ME_NN(0, 0);
        if (value > 307) return this.pow(1 / value);
        if (value === 0) return ME_NN(1, 0);
        const newExp = Math.floor(this.exponent / value);
        const excessExp = this.exponent - value * newExp;
        return ME(Math.pow(powerOf10(excessExp) * this.mantissa, 1 / value), newExp);
      };

(It doesn't take the sign of the base into account, though)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants