Skip to content

Integer roots #50

Description

@garnet420

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)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions