@bignum/core
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -35,2 +35,3 @@ type OverflowContext = { | ||
add(augend: Num | Inf): Num | Inf; | ||
subtract(subtrahend: Num): Num; | ||
subtract(subtrahend: Num | Inf): Num | Inf; | ||
@@ -43,6 +44,8 @@ multiply(multiplicand: Num): Num; | ||
modulo(divisor: Num | Inf): Num | Inf | null; | ||
pow(n: Num, options?: PowOptions): Num | null; | ||
pow(n: Num | Inf, options?: PowOptions): Num | Inf | null; | ||
scaleByPowerOfTen(n: Num | Inf): Num | Inf | null; | ||
sqrt(options?: SqrtOptions): Num; | ||
nthRoot(n: Num | Inf, options?: SqrtOptions): Num | Inf | null; | ||
nthRoot(n: Num, options?: NthRootOptions): Num; | ||
nthRoot(n: Num | Inf, options?: NthRootOptions): Num | Inf | null; | ||
trunc(): Num; | ||
@@ -49,0 +52,0 @@ round(): Num; |
@@ -54,2 +54,8 @@ // src/nth-root-utils.mts | ||
} | ||
function getDivForPowOptions(options = {}) { | ||
return { | ||
overflow: (ctx) => ctx.scale > 0n && ctx.precision > 20n, | ||
...options | ||
}; | ||
} | ||
var Num = class _Num { | ||
@@ -167,10 +173,18 @@ constructor(intValue, exponent) { | ||
} | ||
const bn = n.toBigInt(); | ||
if (bn >= 0n) | ||
return new _Num(this.i ** bn, this.e * bn); | ||
const divideOptions = { | ||
overflow: (ctx) => ctx.scale > 0n && ctx.precision > 20n, | ||
...options | ||
}; | ||
return ONE.divide(new _Num(this.i ** -bn, this.e * -bn), divideOptions); | ||
const bn = abs(n.#trunc()); | ||
let a = new _Num(this.i ** bn, this.e * bn); | ||
if (n.#scale()) { | ||
if (this.i < 0n) | ||
return null; | ||
const decimal = n.subtract(n.trunc()); | ||
const [nI, nD] = reduceFractions(abs(decimal.i), decimal.d); | ||
a = a.multiply( | ||
this.nthRoot(new _Num(nD, 0n), getDivForPowOptions(options)).pow( | ||
new _Num(nI, 0n) | ||
) | ||
); | ||
} | ||
if (n.i >= 0n) | ||
return a; | ||
return ONE.divide(a, getDivForPowOptions(options)); | ||
} | ||
@@ -180,4 +194,7 @@ scaleByPowerOfTen(n) { | ||
return n.signum() < 0 ? ZERO : !this.i ? null : this.i >= 0n ? INF : N_INF; | ||
const bn = n.toBigInt(); | ||
return new _Num(this.i, this.e + bn); | ||
if (!n.#scale()) { | ||
const bn = n.#trunc(); | ||
return new _Num(this.i, this.e + bn); | ||
} | ||
return this.multiply(new _Num(10n, 0n).pow(n)); | ||
} | ||
@@ -236,8 +253,8 @@ sqrt(options) { | ||
return this.pow(n); | ||
if (!this.i) | ||
if (!n.i) | ||
return this.pow(INF); | ||
if (n.i < 0n || this.i < 0n) | ||
if (this.i < 0n) | ||
throw new Error("Negative number"); | ||
const overflow = parseOFOption(options, this.#scale()); | ||
const iN = n.toBigInt(); | ||
const iN = n.abs().toBigInt(); | ||
const powOfTen = 10n ** iN; | ||
@@ -281,3 +298,6 @@ const decimalLength = this.#scale(); | ||
} | ||
return new _Num(digits, exponent); | ||
const a = new _Num(digits, exponent); | ||
if (n.i >= 0n) | ||
return a; | ||
return ONE.divide(a, getDivForPowOptions(options)); | ||
} | ||
@@ -394,2 +414,9 @@ trunc() { | ||
} | ||
function reduceFractions(n, d) { | ||
const g = gcd(n, d); | ||
return [n / g, d / g]; | ||
function gcd(a, b) { | ||
return b ? gcd(b, a % b) : a; | ||
} | ||
} | ||
@@ -396,0 +423,0 @@ // src/inf.mts |
{ | ||
"name": "@bignum/core", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Arbitrary-precision decimal arithmetic with BigInt.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -81,4 +81,2 @@ # @bignum/core | ||
If `n` is given a non-integer value, an error will be raised. | ||
An object can be given as an option. It's the same option for `divide()`. This is used in negative pows. | ||
@@ -107,4 +105,2 @@ | ||
If `n` is given a non-integer value, an error will be raised. | ||
### BigNum.prototype.sqrt([options]): BigNum | ||
@@ -122,3 +118,3 @@ | ||
If this BigNum or `n` are negative value, an error will be raised.\ | ||
If this BigNum is a negative value, an error will be raised.\ | ||
If `n` is given a non-integer value, an error will be raised. | ||
@@ -125,0 +121,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
52596
1459
166