ウィキメディアプロジェクトへの貢献者 · ウィキメディア財団

このプログラムでは、BigInt から Number への変換、および Number から BigInt への変換を示しています。ただし、大きな値では精度が失われる可能性があることに注意してください。

if (typeof BigInt !== 'undefined') {
  Object.defineProperties(BigInt.prototype, {
    abs: {
      value: function () {
        return this < 0n ? -this : this;
      },
      writable: true,
      enumerable: false,
      configurable: true,
    },
    sign: {
      value: function () {
        return this === 0n ? 0n : this < 0n ? -1n : 1n;
      },
      writable: true,
      enumerable: false,
      configurable: true,
    },
    pow: {
      value: function (exponent) {
        if (exponent < 0n) {
          throw new RangeError('Exponent must be non-negative');
        }
        if (exponent === 0n) {
          return 1n;
        }
        if (exponent === 1n) {
          return this;
        }
        let result = 1n;
        let base = this;
        while (exponent > 0n) {
          if (exponent % 2n === 1n) {
            result *= base;
          }
          base *= base;
          exponent /= 2n;
        }
        return result;
      },
      writable: true,
      enumerable: false,
      configurable: true,
    },
    sqrt: {
      value: function () {
        if (this < 0n) {
          throw new RangeError('Cannot calculate square root of a negative BigInt');
        }
        if (this < 2n) {
          return this;
        }
        const newtonStep = (n, x) => (x + n / x) >> 1n;
        const initialGuess = (this >> (BigInt(this.toString(16).length) - 1n)) + 1n;
        let x0 = initialGuess;
        let x1 = newtonStep(this, x0);
        while (x1 < x0) {
          x0 = x1;
          x1 = newtonStep(this, x0);
        }
        return x0;
      },
      writable: true,
      enumerable: false,
      configurable: true,
    },
    gcd: {
      value: function (other) {
        if (typeof other !== 'bigint') {
          throw new TypeError('Argument must be a BigInt');
        }
        let a = this.abs();
        let b = other.abs();
        if (b === 0n) {
          return a;
        }
        let r;
        while ((r = a % b) !== 0n) {
          a = b;
          b = r;
        }
        return b;
      },
      writable: true,
      enumerable: false,
      configurable: true,
    },
    lcm: {
      value: function (other) {
        if (typeof other !== 'bigint') {
          throw new TypeError('Argument must be a BigInt');
        }
        if (this === 0n || other === 0n) {
          return 0n;
        }
        return (this / this.gcd(other)) * other;
      },
      writable: true,
      enumerable: false,
      configurable: true,
    },
  });
}
// 使用例
if (typeof BigInt !== 'undefined') {
  const bigIntValue = -123n;
  console.log(bigIntValue.abs()); // 123n
  console.log(bigIntValue.sign()); // -1n
  console.log(2n.pow(10n)); // 1024n
  console.log(25n.sqrt()); // 5n
  console.log(26n.sqrt()); // 5n
  console.log(12n.gcd(18n)); // 6n
  console.log(12n.lcm(18n)); // 36n
}

Read the original on ja.wikibooks.org ↗