Comparing version 0.9.0 to 0.9.1
@@ -10,3 +10,3 @@ import type { GMPFunctions } from './functions'; | ||
} | ||
declare type AllTypes = Integer | Rational | Float | number; | ||
declare type AllTypes = Integer | Rational | Float | string | number; | ||
/** Represents the different rounding modes. */ | ||
@@ -37,6 +37,11 @@ export declare enum FloatRoundingMode { | ||
readonly setOptions: FloatOptions; | ||
/** Returns the sum of this number and the given one. */ | ||
add(val: AllTypes): Float; | ||
/** Returns the difference of this number and the given one. */ | ||
sub(val: AllTypes): Float; | ||
/** Returns the product of this number and the given one. */ | ||
mul(val: AllTypes): Float; | ||
/** Returns the result of the division of this number by the given one. */ | ||
div(val: AllTypes): Float; | ||
/** Returns the integer square root number of this number. */ | ||
sqrt(): Float; | ||
@@ -46,3 +51,5 @@ invSqrt(): Float; | ||
nthRoot(nth: number): Float; | ||
/** Returns the number with inverted sign. */ | ||
neg(): Float; | ||
/** Returns the absolute value of this number. */ | ||
abs(): Float; | ||
@@ -67,2 +74,3 @@ factorial(): any; | ||
exp10(): Float; | ||
/** Returns this number exponentiated to the given value. */ | ||
pow(val: Float | number): Float; | ||
@@ -130,3 +138,3 @@ sin(): Float; | ||
/** Rounds to the nearest representable integer, rounding halfway cases with the even-rounding rule */ | ||
roundeven(): Float; | ||
roundEven(): Float; | ||
/** Rounds to the next representable integer toward zero */ | ||
@@ -146,2 +154,5 @@ trunc(): Float; | ||
toString(): string; | ||
toFixed(digits?: number): any; | ||
toInteger(): Integer; | ||
toRational(): Rational; | ||
}; | ||
@@ -156,6 +167,11 @@ isFloat: (val: any) => boolean; | ||
readonly setOptions: FloatOptions; | ||
/** Returns the sum of this number and the given one. */ | ||
add(val: AllTypes): Float; | ||
/** Returns the difference of this number and the given one. */ | ||
sub(val: AllTypes): Float; | ||
/** Returns the product of this number and the given one. */ | ||
mul(val: AllTypes): Float; | ||
/** Returns the result of the division of this number by the given one. */ | ||
div(val: AllTypes): Float; | ||
/** Returns the integer square root number of this number. */ | ||
sqrt(): Float; | ||
@@ -165,3 +181,5 @@ invSqrt(): Float; | ||
nthRoot(nth: number): Float; | ||
/** Returns the number with inverted sign. */ | ||
neg(): Float; | ||
/** Returns the absolute value of this number. */ | ||
abs(): Float; | ||
@@ -186,2 +204,3 @@ factorial(): any; | ||
exp10(): Float; | ||
/** Returns this number exponentiated to the given value. */ | ||
pow(val: Float | number): Float; | ||
@@ -249,3 +268,3 @@ sin(): Float; | ||
/** Rounds to the nearest representable integer, rounding halfway cases with the even-rounding rule */ | ||
roundeven(): Float; | ||
roundEven(): Float; | ||
/** Rounds to the next representable integer toward zero */ | ||
@@ -265,2 +284,5 @@ trunc(): Float; | ||
toString(): string; | ||
toFixed(digits?: number): any; | ||
toInteger(): Integer; | ||
toRational(): Rational; | ||
}; | ||
@@ -274,6 +296,11 @@ EulerConstant: (options?: FloatOptions) => { | ||
readonly setOptions: FloatOptions; | ||
/** Returns the sum of this number and the given one. */ | ||
add(val: AllTypes): Float; | ||
/** Returns the difference of this number and the given one. */ | ||
sub(val: AllTypes): Float; | ||
/** Returns the product of this number and the given one. */ | ||
mul(val: AllTypes): Float; | ||
/** Returns the result of the division of this number by the given one. */ | ||
div(val: AllTypes): Float; | ||
/** Returns the integer square root number of this number. */ | ||
sqrt(): Float; | ||
@@ -283,3 +310,5 @@ invSqrt(): Float; | ||
nthRoot(nth: number): Float; | ||
/** Returns the number with inverted sign. */ | ||
neg(): Float; | ||
/** Returns the absolute value of this number. */ | ||
abs(): Float; | ||
@@ -304,2 +333,3 @@ factorial(): any; | ||
exp10(): Float; | ||
/** Returns this number exponentiated to the given value. */ | ||
pow(val: Float | number): Float; | ||
@@ -367,3 +397,3 @@ sin(): Float; | ||
/** Rounds to the nearest representable integer, rounding halfway cases with the even-rounding rule */ | ||
roundeven(): Float; | ||
roundEven(): Float; | ||
/** Rounds to the next representable integer toward zero */ | ||
@@ -383,2 +413,5 @@ trunc(): Float; | ||
toString(): string; | ||
toFixed(digits?: number): any; | ||
toInteger(): Integer; | ||
toRational(): Rational; | ||
}; | ||
@@ -393,6 +426,11 @@ EulerNumber: (options?: FloatOptions) => Float; | ||
readonly setOptions: FloatOptions; | ||
/** Returns the sum of this number and the given one. */ | ||
add(val: AllTypes): Float; | ||
/** Returns the difference of this number and the given one. */ | ||
sub(val: AllTypes): Float; | ||
/** Returns the product of this number and the given one. */ | ||
mul(val: AllTypes): Float; | ||
/** Returns the result of the division of this number by the given one. */ | ||
div(val: AllTypes): Float; | ||
/** Returns the integer square root number of this number. */ | ||
sqrt(): Float; | ||
@@ -402,3 +440,5 @@ invSqrt(): Float; | ||
nthRoot(nth: number): Float; | ||
/** Returns the number with inverted sign. */ | ||
neg(): Float; | ||
/** Returns the absolute value of this number. */ | ||
abs(): Float; | ||
@@ -423,2 +463,3 @@ factorial(): any; | ||
exp10(): Float; | ||
/** Returns this number exponentiated to the given value. */ | ||
pow(val: Float | number): Float; | ||
@@ -486,3 +527,3 @@ sin(): Float; | ||
/** Rounds to the nearest representable integer, rounding halfway cases with the even-rounding rule */ | ||
roundeven(): Float; | ||
roundEven(): Float; | ||
/** Rounds to the next representable integer toward zero */ | ||
@@ -502,2 +543,5 @@ trunc(): Float; | ||
toString(): string; | ||
toFixed(digits?: number): any; | ||
toInteger(): Integer; | ||
toRational(): Rational; | ||
}; | ||
@@ -511,6 +555,11 @@ Catalan: (options?: FloatOptions) => { | ||
readonly setOptions: FloatOptions; | ||
/** Returns the sum of this number and the given one. */ | ||
add(val: AllTypes): Float; | ||
/** Returns the difference of this number and the given one. */ | ||
sub(val: AllTypes): Float; | ||
/** Returns the product of this number and the given one. */ | ||
mul(val: AllTypes): Float; | ||
/** Returns the result of the division of this number by the given one. */ | ||
div(val: AllTypes): Float; | ||
/** Returns the integer square root number of this number. */ | ||
sqrt(): Float; | ||
@@ -520,3 +569,5 @@ invSqrt(): Float; | ||
nthRoot(nth: number): Float; | ||
/** Returns the number with inverted sign. */ | ||
neg(): Float; | ||
/** Returns the absolute value of this number. */ | ||
abs(): Float; | ||
@@ -541,2 +592,3 @@ factorial(): any; | ||
exp10(): Float; | ||
/** Returns this number exponentiated to the given value. */ | ||
pow(val: Float | number): Float; | ||
@@ -604,3 +656,3 @@ sin(): Float; | ||
/** Rounds to the nearest representable integer, rounding halfway cases with the even-rounding rule */ | ||
roundeven(): Float; | ||
roundEven(): Float; | ||
/** Rounds to the next representable integer toward zero */ | ||
@@ -620,2 +672,5 @@ trunc(): Float; | ||
toString(): string; | ||
toFixed(digits?: number): any; | ||
toInteger(): Integer; | ||
toRational(): Rational; | ||
}; | ||
@@ -622,0 +677,0 @@ destroy: () => void; |
@@ -10,3 +10,3 @@ import type { GMPFunctions } from './functions'; | ||
} | ||
declare type AllTypes = Integer | Rational | Float | number; | ||
declare type AllTypes = Integer | Rational | Float | string | number; | ||
declare type OutputType<T> = T extends number ? Integer : T extends Integer ? Integer : T extends Rational ? Rational : T extends Float ? Float : never; | ||
@@ -22,9 +22,17 @@ export declare enum DivMode { | ||
type: string; | ||
/** Returns the sum of this number and the given one. */ | ||
add<T extends AllTypes>(val: T): OutputType<T>; | ||
/** Returns the difference of this number and the given one. */ | ||
sub<T_1 extends AllTypes>(val: T_1): OutputType<T_1>; | ||
/** Returns the product of this number and the given one. */ | ||
mul<T_2 extends AllTypes>(val: T_2): OutputType<T_2>; | ||
/** Returns the number with inverted sign. */ | ||
neg(): Integer; | ||
/** Returns the absolute value of this number. */ | ||
abs(): Integer; | ||
/** Returns the result of the division of this number by the given one. */ | ||
div<T_3 extends AllTypes>(val: T_3, mode?: DivMode): OutputType<T_3>; | ||
/** Returns this number exponentiated to the given value. */ | ||
pow(exp: Rational | Integer | number, mod?: Integer | number): Integer; | ||
/** Returns the integer square root number of this number, rounded down. */ | ||
sqrt(): Integer; | ||
@@ -36,2 +44,3 @@ nthRoot(nth: number): Integer; | ||
nextPrime(): Integer; | ||
/** Returns the greatest common divisor of this number and the given one. */ | ||
gcd(val: Integer | number): Integer; | ||
@@ -41,6 +50,11 @@ lcm(val: Integer | number): Integer; | ||
complement2(): Integer; | ||
/** Returns the integer bitwise-and combined with another integer. */ | ||
and(val: Integer | number): Integer; | ||
/** Returns the integer bitwise-or combined with another integer. */ | ||
or(val: Integer | number): Integer; | ||
/** Returns the integer bitwise-xor combined with another integer. */ | ||
xor(val: Integer | number): Integer; | ||
/** Returns the integer left shifted by a given number of bits. */ | ||
shiftLeft(val: number): Integer; | ||
/** Returns the integer right shifted by a given number of bits. */ | ||
shiftRight(val: number): Integer; | ||
@@ -59,2 +73,3 @@ /** Sets the value of bit i to 1. The least significant bit is number 0 */ | ||
flipBits(indices: number[]): Integer; | ||
/** Returns 0 or 1 based on the value of a bit at the provided index. The least significant bit is number 0 */ | ||
getBit(index: number): number; | ||
@@ -76,2 +91,4 @@ msbPosition(): number; | ||
toString(radix?: number): string; | ||
toRational(): Rational; | ||
toFloat(): Float; | ||
}; | ||
@@ -78,0 +95,0 @@ isInteger: (val: any) => boolean; |
@@ -10,3 +10,3 @@ import type { GMPFunctions } from './functions'; | ||
} | ||
declare type AllTypes = Integer | Rational | Float | number; | ||
declare type AllTypes = Integer | Rational | Float | string | number; | ||
declare type OutputType<T> = T extends number ? Rational : T extends Integer ? Rational : T extends Rational ? Rational : T extends Float ? Float : never; | ||
@@ -17,8 +17,14 @@ export declare function getRationalContext(gmp: GMPFunctions, ctx: any): { | ||
type: string; | ||
/** Returns the sum of this number and the given one. */ | ||
add<T extends AllTypes>(val: T): OutputType<T>; | ||
/** Returns the difference of this number and the given one. */ | ||
sub<T_1 extends AllTypes>(val: T_1): OutputType<T_1>; | ||
/** Returns the product of this number and the given one. */ | ||
mul<T_2 extends AllTypes>(val: T_2): OutputType<T_2>; | ||
/** Returns the number with inverted sign. */ | ||
neg(): Rational; | ||
invert(): Rational; | ||
/** Returns the absolute value of this number. */ | ||
abs(): Rational; | ||
/** Returns the result of the division of this number by the given one. */ | ||
div<T_3 extends AllTypes>(val: T_3): OutputType<T_3>; | ||
@@ -35,2 +41,4 @@ isEqual(val: AllTypes): boolean; | ||
toString(): string; | ||
toInteger(): Integer; | ||
toFloat(): Float; | ||
}; | ||
@@ -37,0 +45,0 @@ isRational: (val: any) => boolean; |
{ | ||
"name": "gmp-wasm", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Arbitrary-precision Integer, Rational and Float types based on the GMP and MPFR libraries", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.umd.js", |
@@ -14,3 +14,3 @@ import type { GMPFunctions, mpfr_rnd_t } from './functions'; | ||
// these should not be exported | ||
type AllTypes = Integer | Rational | Float | number; | ||
type AllTypes = Integer | Rational | Float | string | number; | ||
@@ -36,2 +36,10 @@ // matches mpfr_rnd_t | ||
const SPECIAL_VALUES = { | ||
'@NaN@': 'NaN', | ||
'@Inf@': 'Infinity', | ||
'-@Inf@': '-Infinity', | ||
} as const; | ||
const SPECIAL_VALUE_KEYS = Object.keys(SPECIAL_VALUES); | ||
export interface FloatOptions { | ||
@@ -110,2 +118,6 @@ precisionBits?: number; | ||
} | ||
if (typeof val === 'string') { | ||
const f = FloatFn(val); | ||
return gmp.mpfr_cmp(mpfr_t, f.mpfr_t); | ||
} | ||
if (isInteger(val)) { | ||
@@ -153,2 +165,3 @@ return gmp.mpfr_cmp_z(mpfr_t, (val as Integer).mpz_t); | ||
/** Returns the sum of this number and the given one. */ | ||
add(val: AllTypes): Float { | ||
@@ -160,2 +173,7 @@ if (typeof val === 'number') { | ||
} | ||
if (typeof val === 'string') { | ||
const n = FloatFn(val, this.options); | ||
gmp.mpfr_add(n.mpfr_t, this.mpfr_t, n.mpfr_t, this.rndMode); | ||
return n; | ||
} | ||
if (isFloat(val)) { | ||
@@ -179,2 +197,3 @@ const n = FloatFn(null, mergeFloatOptions(this.setOptions, (val as Float).setOptions)); | ||
/** Returns the difference of this number and the given one. */ | ||
sub(val: AllTypes): Float { | ||
@@ -186,2 +205,7 @@ if (typeof val === 'number') { | ||
} | ||
if (typeof val === 'string') { | ||
const n = FloatFn(val, this.options); | ||
gmp.mpfr_sub(n.mpfr_t, this.mpfr_t, n.mpfr_t, this.rndMode); | ||
return n; | ||
} | ||
if (isFloat(val)) { | ||
@@ -205,2 +229,3 @@ const n = FloatFn(null, mergeFloatOptions(this.setOptions, (val as Float).setOptions)); | ||
/** Returns the product of this number and the given one. */ | ||
mul(val: AllTypes): Float { | ||
@@ -216,2 +241,7 @@ if (typeof val === 'number') { | ||
} | ||
if (typeof val === 'string') { | ||
const n = FloatFn(val, this.options); | ||
gmp.mpfr_mul(n.mpfr_t, this.mpfr_t, n.mpfr_t, this.rndMode); | ||
return n; | ||
} | ||
if (isFloat(val)) { | ||
@@ -235,2 +265,3 @@ const n = FloatFn(null, mergeFloatOptions(this.setOptions, (val as Float).setOptions)); | ||
/** Returns the result of the division of this number by the given one. */ | ||
div(val: AllTypes): Float { | ||
@@ -242,2 +273,7 @@ if (typeof val === 'number') { | ||
} | ||
if (typeof val === 'string') { | ||
const n = FloatFn(val, this.options); | ||
gmp.mpfr_div(n.mpfr_t, this.mpfr_t, n.mpfr_t, this.rndMode); | ||
return n; | ||
} | ||
if (isFloat(val)) { | ||
@@ -261,2 +297,3 @@ const n = FloatFn(null, mergeFloatOptions(this.setOptions, (val as Float).setOptions)); | ||
/** Returns the integer square root number of this number. */ | ||
sqrt(): Float { | ||
@@ -287,2 +324,3 @@ const n = FloatFn(null, this.options); | ||
/** Returns the number with inverted sign. */ | ||
neg(): Float { | ||
@@ -294,2 +332,3 @@ const n = FloatFn(null, this.options); | ||
/** Returns the absolute value of this number. */ | ||
abs(): Float { | ||
@@ -391,2 +430,3 @@ const n = FloatFn(null, this.options); | ||
/** Returns this number exponentiated to the given value. */ | ||
pow(val: Float | number): Float { | ||
@@ -671,3 +711,3 @@ const n = FloatFn(null, this.options); | ||
/** Rounds to the nearest representable integer, rounding halfway cases with the even-rounding rule */ | ||
roundeven(): Float { | ||
roundEven(): Float { | ||
const n = FloatFn(null, this.options); | ||
@@ -742,3 +782,5 @@ gmp.mpfr_roundeven(n.mpfr_t, this.mpfr_t); | ||
if (!['@NaN@', '@Inf@', '-@Inf@'].includes(ret)) { | ||
if (SPECIAL_VALUE_KEYS.includes(ret)) { | ||
ret = SPECIAL_VALUES[ret]; | ||
} else { | ||
// decimal point needs to be inserted | ||
@@ -752,3 +794,30 @@ const pointPos = gmp.memView.getInt32(mpfr_exp_t_ptr, true); | ||
return ret; | ||
} | ||
}, | ||
toFixed(digits = 0) { | ||
assertUint32(digits); | ||
const str = this.toString(); | ||
if (Object.values(SPECIAL_VALUES).includes(str)) { | ||
return str; | ||
} | ||
if (digits === 0) { | ||
return ctx.intContext.Integer(this).toString(); | ||
} | ||
const multiplied = this.mul(FloatFn(digits).exp10()); | ||
const int = ctx.intContext.Integer(multiplied); | ||
const isNegative = int.sign() === -1; | ||
let intStr = int.abs().toString(); | ||
if (intStr.length < digits + 1) { | ||
intStr = '0'.repeat(digits + 1 - intStr.length) + intStr; | ||
} | ||
return `${isNegative ? '-' : ''}${intStr.slice(0, -digits)}.${intStr.slice(-digits)}`; | ||
}, | ||
toInteger(): Integer { | ||
return ctx.intContext.Integer(this); | ||
}, | ||
toRational(): Rational { | ||
return ctx.rationalContext.Rational(this); | ||
}, | ||
}; | ||
@@ -755,0 +824,0 @@ |
@@ -14,3 +14,3 @@ import type { GMPFunctions } from './functions'; | ||
// these should not be exported | ||
type AllTypes = Integer | Rational | Float | number; | ||
type AllTypes = Integer | Rational | Float | string | number; | ||
type OutputType<T> = | ||
@@ -43,2 +43,6 @@ T extends number ? Integer : | ||
} | ||
if (typeof val === 'string') { | ||
const i = IntegerFn(val); | ||
return gmp.mpz_cmp(mpz_t, i.mpz_t); | ||
} | ||
if (isInteger(val)) { | ||
@@ -60,2 +64,3 @@ return gmp.mpz_cmp(mpz_t, (val as Integer).mpz_t); | ||
/** Returns the sum of this number and the given one. */ | ||
add<T extends AllTypes>(val: T): OutputType<T> { | ||
@@ -72,2 +77,7 @@ if (typeof val === 'number') { | ||
} | ||
if (typeof val === 'string') { | ||
const n = IntegerFn(val); | ||
gmp.mpz_add(n.mpz_t, this.mpz_t, n.mpz_t); | ||
return n as OutputType<T>; | ||
} | ||
if (isInteger(val)) { | ||
@@ -84,2 +94,3 @@ const n = IntegerFn(); | ||
/** Returns the difference of this number and the given one. */ | ||
sub<T extends AllTypes>(val: T): OutputType<T> { | ||
@@ -96,2 +107,7 @@ if (typeof val === 'number') { | ||
} | ||
if (typeof val === 'string') { | ||
const n = IntegerFn(val); | ||
gmp.mpz_sub(n.mpz_t, this.mpz_t, n.mpz_t); | ||
return n as OutputType<T>; | ||
} | ||
if (isInteger(val)) { | ||
@@ -108,2 +124,3 @@ const n = IntegerFn(); | ||
/** Returns the product of this number and the given one. */ | ||
mul<T extends AllTypes>(val: T): OutputType<T> { | ||
@@ -116,2 +133,7 @@ if (typeof val === 'number') { | ||
} | ||
if (typeof val === 'string') { | ||
const n = IntegerFn(val); | ||
gmp.mpz_mul(n.mpz_t, this.mpz_t, n.mpz_t); | ||
return n as OutputType<T>; | ||
} | ||
if (isInteger(val)) { | ||
@@ -128,2 +150,3 @@ const n = IntegerFn(); | ||
/** Returns the number with inverted sign. */ | ||
neg(): Integer { | ||
@@ -135,2 +158,3 @@ const n = IntegerFn(); | ||
/** Returns the absolute value of this number. */ | ||
abs(): Integer { | ||
@@ -142,2 +166,3 @@ const n = IntegerFn(); | ||
/** Returns the result of the division of this number by the given one. */ | ||
div<T extends AllTypes>(val: T, mode = DivMode.CEIL): OutputType<T> { | ||
@@ -160,10 +185,12 @@ if (typeof val === 'number') { | ||
} | ||
if (isInteger(val)) { | ||
if (typeof val === 'string' || isInteger(val)) { | ||
const n = IntegerFn(this); | ||
const intVal = typeof val === 'string' ? IntegerFn(val) : val as Integer; | ||
if (mode === DivMode.CEIL) { | ||
gmp.mpz_cdiv_q(n.mpz_t, this.mpz_t, (val as Integer).mpz_t); | ||
gmp.mpz_cdiv_q(n.mpz_t, this.mpz_t, intVal.mpz_t); | ||
} else if (mode === DivMode.FLOOR) { | ||
gmp.mpz_fdiv_q(n.mpz_t, this.mpz_t, (val as Integer).mpz_t); | ||
gmp.mpz_fdiv_q(n.mpz_t, this.mpz_t, intVal.mpz_t); | ||
} else if (mode === DivMode.TRUNCATE) { | ||
gmp.mpz_tdiv_q(n.mpz_t, this.mpz_t, (val as Integer).mpz_t); | ||
gmp.mpz_tdiv_q(n.mpz_t, this.mpz_t, intVal.mpz_t); | ||
} | ||
@@ -181,2 +208,3 @@ return n as OutputType<T>; | ||
/** Returns this number exponentiated to the given value. */ | ||
pow(exp: Rational | Integer | number, mod?: Integer | number): Integer { | ||
@@ -227,2 +255,3 @@ if (typeof exp === 'number') { | ||
/** Returns the integer square root number of this number, rounded down. */ | ||
sqrt(): Integer { | ||
@@ -275,2 +304,3 @@ const n = IntegerFn(); | ||
/** Returns the greatest common divisor of this number and the given one. */ | ||
gcd(val: Integer | number): Integer { | ||
@@ -317,2 +347,3 @@ const n = IntegerFn(); | ||
/** Returns the integer bitwise-and combined with another integer. */ | ||
and(val: Integer | number): Integer { | ||
@@ -332,2 +363,3 @@ const n = IntegerFn(); | ||
/** Returns the integer bitwise-or combined with another integer. */ | ||
or(val: Integer | number): Integer { | ||
@@ -347,2 +379,3 @@ const n = IntegerFn(); | ||
/** Returns the integer bitwise-xor combined with another integer. */ | ||
xor(val: Integer | number): Integer { | ||
@@ -359,2 +392,3 @@ const n = IntegerFn(); | ||
/** Returns the integer left shifted by a given number of bits. */ | ||
shiftLeft(val: number): Integer { | ||
@@ -367,2 +401,3 @@ assertUint32(val); | ||
/** Returns the integer right shifted by a given number of bits. */ | ||
shiftRight(val: number): Integer { | ||
@@ -432,2 +467,3 @@ assertUint32(val); | ||
/** Returns 0 or 1 based on the value of a bit at the provided index. The least significant bit is number 0 */ | ||
getBit(index: number): number { | ||
@@ -540,2 +576,10 @@ assertUint32(index); | ||
}, | ||
toRational(): Rational { | ||
return ctx.rationalContext.Rational(this); | ||
}, | ||
toFloat(): Float { | ||
return ctx.floatContext.Float(this); | ||
}, | ||
}; | ||
@@ -542,0 +586,0 @@ |
@@ -14,3 +14,3 @@ import type { GMPFunctions } from './functions'; | ||
// these should not be exported | ||
type AllTypes = Integer | Rational | Float | number; | ||
type AllTypes = Integer | Rational | Float | string | number; | ||
type OutputType<T> = | ||
@@ -37,2 +37,6 @@ T extends number ? Rational : | ||
} | ||
if (typeof val === 'string') { | ||
const r = RationalFn(val); | ||
return gmp.mpq_cmp(mpq_t, r.mpq_t); | ||
} | ||
if (isInteger(val)) { | ||
@@ -54,2 +58,3 @@ return gmp.mpq_cmp_z(mpq_t, (val as Integer).mpz_t); | ||
/** Returns the sum of this number and the given one. */ | ||
add<T extends AllTypes>(val: T): OutputType<T> { | ||
@@ -61,2 +66,7 @@ if (typeof val === 'number' || isInteger(val)) { | ||
} | ||
if (typeof val === 'string') { | ||
const n = RationalFn(val); | ||
gmp.mpq_add(n.mpq_t, this.mpq_t, n.mpq_t); | ||
return n as OutputType<T>; | ||
} | ||
if (isRational(val)) { | ||
@@ -68,3 +78,3 @@ const n = RationalFn(0, 1); | ||
if (isFloat(val)) { | ||
return val.add(this) as OutputType<T>; | ||
return (val as Float).add(this) as OutputType<T>; | ||
} | ||
@@ -74,2 +84,3 @@ throw new Error(INVALID_PARAMETER_ERROR); | ||
/** Returns the difference of this number and the given one. */ | ||
sub<T extends AllTypes>(val: T): OutputType<T> { | ||
@@ -81,2 +92,7 @@ if (typeof val === 'number' || isInteger(val)) { | ||
} | ||
if (typeof val === 'string') { | ||
const n = RationalFn(val); | ||
gmp.mpq_sub(n.mpq_t, this.mpq_t, n.mpq_t); | ||
return n as OutputType<T>; | ||
} | ||
if (isRational(val)) { | ||
@@ -88,3 +104,3 @@ const n = RationalFn(0, 1); | ||
if (isFloat(val)) { | ||
return val.neg().add(this) as OutputType<T>; | ||
return (val as Float).neg().add(this) as OutputType<T>; | ||
} | ||
@@ -94,2 +110,3 @@ throw new Error(INVALID_PARAMETER_ERROR); | ||
/** Returns the product of this number and the given one. */ | ||
mul<T extends AllTypes>(val: T): OutputType<T> { | ||
@@ -101,2 +118,7 @@ if (typeof val === 'number' || isInteger(val)) { | ||
} | ||
if (typeof val === 'string') { | ||
const n = RationalFn(val); | ||
gmp.mpq_mul(n.mpq_t, this.mpq_t, n.mpq_t); | ||
return n as OutputType<T>; | ||
} | ||
if (isRational(val)) { | ||
@@ -108,3 +130,3 @@ const n = RationalFn(0, 1); | ||
if (isFloat(val)) { | ||
return val.mul(this) as OutputType<T>; | ||
return (val as Float).mul(this) as OutputType<T>; | ||
} | ||
@@ -114,2 +136,3 @@ throw new Error(INVALID_PARAMETER_ERROR); | ||
/** Returns the number with inverted sign. */ | ||
neg(): Rational { | ||
@@ -127,2 +150,3 @@ const n = RationalFn(0, 1); | ||
/** Returns the absolute value of this number. */ | ||
abs(): Rational { | ||
@@ -134,2 +158,3 @@ const n = RationalFn(0, 1); | ||
/** Returns the result of the division of this number by the given one. */ | ||
div<T extends AllTypes>(val: T): OutputType<T> { | ||
@@ -141,2 +166,7 @@ if (typeof val === 'number' || isInteger(val)) { | ||
} | ||
if (typeof val === 'string') { | ||
const n = RationalFn(val); | ||
gmp.mpq_div(n.mpq_t, this.mpq_t, n.mpq_t); | ||
return n as OutputType<T>; | ||
} | ||
if (isRational(val)) { | ||
@@ -157,2 +187,6 @@ const n = RationalFn(0, 1); | ||
} | ||
if (typeof val === 'string') { | ||
const n = RationalFn(val); | ||
return gmp.mpq_equal(this.mpq_t, n.mpq_t) !== 0; | ||
} | ||
if (isRational(val)) { | ||
@@ -162,3 +196,3 @@ return gmp.mpq_equal(this.mpq_t, (val as Rational).mpq_t) !== 0; | ||
if (isFloat(val)) { | ||
return val.isEqual(this); | ||
return (val as Float).isEqual(this); | ||
} | ||
@@ -211,2 +245,10 @@ throw new Error(INVALID_PARAMETER_ERROR); | ||
}, | ||
toInteger(): Integer { | ||
return ctx.intContext.Integer(this); | ||
}, | ||
toFloat(): Float { | ||
return ctx.floatContext.Float(this); | ||
}, | ||
}; | ||
@@ -213,0 +255,0 @@ |
@@ -40,5 +40,5 @@ import { CalculateTypeWithDestroy, FloatRoundingMode, init as initGMP } from '../src'; | ||
compare(ctx.Float(-0.5), '-0.5'); | ||
compare(ctx.Float(Infinity), '@Inf@'); | ||
compare(ctx.Float(-Infinity), '-@Inf@'); | ||
compare(ctx.Float(NaN), '@NaN@'); | ||
compare(ctx.Float(Infinity), 'Infinity'); | ||
compare(ctx.Float(-Infinity), '-Infinity'); | ||
compare(ctx.Float(NaN), 'NaN'); | ||
}); | ||
@@ -71,2 +71,3 @@ | ||
compare(ctx.Float(0.4).add(0.6), '1'); | ||
compare(ctx.Float(0.4).add('0.7'), '1.10001'); | ||
compare(ctx.Float(0.5).add(ctx.Float(1)), '1.5'); | ||
@@ -81,2 +82,3 @@ compare(ctx.Float(0.4).add(ctx.Float(0.6)), '1'); | ||
compare(ctx.Float(0.6).sub(0.4), '0.200005'); | ||
compare(ctx.Float(0.6).sub('0.4'), '0.200005'); | ||
compare(ctx.Float(1).sub(ctx.Float(0.5)), '0.5'); | ||
@@ -91,2 +93,3 @@ compare(ctx.Float(0.6).sub(ctx.Float(0.4)), '0.200005'); | ||
compare(ctx.Float(6).mul(2), '12'); | ||
compare(ctx.Float(3).mul('0.5'), '1.5'); | ||
compare(ctx.Float(3).mul(ctx.Float(0.5)), '1.5'); | ||
@@ -101,2 +104,3 @@ compare(ctx.Float(6).mul(ctx.Float(2)), '12'); | ||
compare(ctx.Float(6).div(2), '3'); | ||
compare(ctx.Float(3).div('0.5'), '6'); | ||
compare(ctx.Float(3).div(ctx.Float(0.5)), '6'); | ||
@@ -183,2 +187,4 @@ compare(ctx.Float(7).div(ctx.Float(2)), '3.5'); | ||
expect(ctx.Float(1).isEqual(0)).toBe(false); | ||
expect(ctx.Float(0).isEqual('0')).toBe(true); | ||
expect(ctx.Float(1).isEqual('0')).toBe(false); | ||
expect(ctx.Float(0).isEqual(ctx.Float(0))).toBe(true); | ||
@@ -196,2 +202,4 @@ expect(ctx.Float(1).isEqual(ctx.Float(0))).toBe(false); | ||
expect(ctx.Float(1).lessThan(2)).toBe(true); | ||
expect(ctx.Float(1).lessThan('0')).toBe(false); | ||
expect(ctx.Float(1).lessThan('2')).toBe(true); | ||
expect(ctx.Float(0).lessThan(ctx.Float(0))).toBe(false); | ||
@@ -206,2 +214,4 @@ expect(ctx.Float(1).lessThan(ctx.Float(0))).toBe(false); | ||
expect(ctx.Float(1).lessOrEqual(2)).toBe(true); | ||
expect(ctx.Float(1).lessOrEqual('0')).toBe(false); | ||
expect(ctx.Float(1).lessOrEqual('2')).toBe(true); | ||
expect(ctx.Float(0).lessOrEqual(ctx.Float(0))).toBe(true); | ||
@@ -216,2 +226,4 @@ expect(ctx.Float(1).lessOrEqual(ctx.Float(0))).toBe(false); | ||
expect(ctx.Float(1).greaterThan(2)).toBe(false); | ||
expect(ctx.Float(1).greaterThan('0')).toBe(true); | ||
expect(ctx.Float(1).greaterThan('2')).toBe(false); | ||
expect(ctx.Float(0).greaterThan(ctx.Float(0))).toBe(false); | ||
@@ -226,2 +238,4 @@ expect(ctx.Float(1).greaterThan(ctx.Float(0))).toBe(true); | ||
expect(ctx.Float(1).greaterOrEqual(2)).toBe(false); | ||
expect(ctx.Float(1).greaterOrEqual('0')).toBe(true); | ||
expect(ctx.Float(1).greaterOrEqual('2')).toBe(false); | ||
expect(ctx.Float(0).greaterOrEqual(ctx.Float(0))).toBe(true); | ||
@@ -369,2 +383,22 @@ expect(ctx.Float(1).greaterOrEqual(ctx.Float(0))).toBe(true); | ||
test('roundEven()', () => { | ||
compare(ctx.Float('1.5').roundEven(), '2'); | ||
compare(ctx.Float('-1.5').roundEven(), '-2'); | ||
compare(ctx.Float('2.5').roundEven(), '2'); | ||
compare(ctx.Float('-2.5').roundEven(), '-2'); | ||
}); | ||
test('frac()', () => { | ||
compare(ctx.Float('1.234').frac(), '0.234009'); | ||
compare(ctx.Float('-1.234').frac(), '-0.234009'); | ||
}); | ||
test('nextBelow()', () => { | ||
compare(ctx.Float('1').nextBelow(), '0.999985'); | ||
}); | ||
test('nextAbove()', () => { | ||
compare(ctx.Float('1').nextAbove(), '1.00003'); | ||
}); | ||
test('exponent2()', () => { | ||
@@ -378,13 +412,39 @@ expect(ctx.Float(1).exponent2()).toBe(1); | ||
test('special values', () => { | ||
compare(ctx.Float(), '@NaN@'); | ||
compare(ctx.Float(null), '@NaN@'); | ||
compare(ctx.Float(undefined), '@NaN@'); | ||
compare(ctx.Float(), 'NaN'); | ||
compare(ctx.Float(null), 'NaN'); | ||
compare(ctx.Float(undefined), 'NaN'); | ||
compare(ctx.Float(0), '0'); | ||
compare(ctx.Float(-0), '-0'); | ||
compare(ctx.Float('-0'), '-0'); | ||
compare(ctx.Float(0).div(0), '@NaN@'); | ||
compare(ctx.Float(1).div(0), '@Inf@'); | ||
compare(ctx.Float(-1).div(0), '-@Inf@'); | ||
compare(ctx.Float(0).div(0), 'NaN'); | ||
compare(ctx.Float(1).div(0), 'Infinity'); | ||
compare(ctx.Float(-1).div(0), '-Infinity'); | ||
}); | ||
test('.toFixed()', () => { | ||
expect(ctx.Float().toFixed()).toBe('NaN'); | ||
expect(ctx.Float('0').toFixed()).toBe('0'); | ||
expect(ctx.Float('0').toFixed(5)).toBe('0.00000'); | ||
expect(ctx.Float('-1.234').toFixed()).toBe('-1'); | ||
expect(ctx.Float('-1.234').toFixed(2)).toBe('-1.23'); | ||
expect(ctx.Float('-1.678').toFixed()).toBe('-2'); | ||
expect(ctx.Float('-1.678').toFixed(2)).toBe('-1.68'); | ||
expect(ctx.Float('0.001').toFixed()).toBe('0'); | ||
expect(ctx.Float('0.001').toFixed(1)).toBe('0.0'); | ||
expect(ctx.Float('0.001').toFixed(3)).toBe('0.001'); | ||
expect(ctx.Float('0.001').toFixed(6)).toBe('0.001000'); | ||
expect(ctx.Float('-0.001').toFixed(6)).toBe('-0.001000'); | ||
expect(ctx.Float('123.5000').toFixed(0)).toBe('124'); | ||
expect(ctx.Float('123.56789').toFixed(2)).toBe('123.57'); | ||
expect(ctx.Float('0.1').mul(ctx.Float('0.2')).toFixed(2)).toBe('0.02'); | ||
}); | ||
test('toRational()', () => { | ||
expect(ctx.Float('2').toRational().toString()).toBe('2'); | ||
}); | ||
test('toInteger()', () => { | ||
expect(ctx.Float('2').toInteger().toString()).toBe('2'); | ||
}); | ||
test('special values to JS types', () => { | ||
@@ -391,0 +451,0 @@ expect(ctx.Float(0).toNumber()).toBe(0); |
@@ -54,2 +54,3 @@ import { DivMode } from '../src/integer'; | ||
compare(ctx.Integer(-2).add(1), '-1'); | ||
compare(ctx.Integer(-2).add('-1'), '-3'); | ||
compare(ctx.Integer(-2).add(ctx.Integer(-1)), '-3'); | ||
@@ -68,2 +69,3 @@ compare(ctx.Integer(-2).add(ctx.Integer(0)), '-2'); | ||
compare(ctx.Integer(-2).sub(1), '-3'); | ||
compare(ctx.Integer(-2).sub('1'), '-3'); | ||
compare(ctx.Integer(-2).sub(ctx.Integer(-1)), '-1'); | ||
@@ -81,2 +83,3 @@ compare(ctx.Integer(-2).sub(ctx.Integer(0)), '-2'); | ||
compare(ctx.Integer(3).mul(-4), '-12'); | ||
compare(ctx.Integer(3).mul('-4'), '-12'); | ||
compare(ctx.Integer(3).mul(ctx.Integer(4)), '12'); | ||
@@ -110,2 +113,3 @@ compare(ctx.Integer(3).mul(ctx.Integer(-4)), '-12'); | ||
compare(ctx.Integer(12).div('-5'), '-2'); | ||
compare(ctx.Integer(12).div(ctx.Integer(-5)), '-2'); | ||
@@ -320,2 +324,4 @@ compare(ctx.Integer(12).div(ctx.Integer(5)), '3'); | ||
expect(ctx.Integer('2').isEqual(-2)).toBe(false); | ||
expect(ctx.Integer('2').isEqual('2')).toBe(true); | ||
expect(ctx.Integer('2').isEqual('3')).toBe(false); | ||
expect(ctx.Integer('2').isEqual(ctx.Integer(2))).toBe(true); | ||
@@ -333,2 +339,4 @@ expect(ctx.Integer('2').isEqual(ctx.Integer(-2))).toBe(false); | ||
expect(ctx.Integer('2').lessThan(-2)).toBe(false); | ||
expect(ctx.Integer('2').lessThan('3')).toBe(true); | ||
expect(ctx.Integer('2').lessThan('-2')).toBe(false); | ||
expect(ctx.Integer('2').lessThan(ctx.Integer(3))).toBe(true); | ||
@@ -349,2 +357,4 @@ expect(ctx.Integer('2').lessThan(ctx.Integer(2))).toBe(false); | ||
expect(ctx.Integer('2').lessOrEqual(-2)).toBe(false); | ||
expect(ctx.Integer('2').lessOrEqual('2')).toBe(true); | ||
expect(ctx.Integer('2').lessOrEqual('-2')).toBe(false); | ||
expect(ctx.Integer('2').lessOrEqual(ctx.Integer(3))).toBe(true); | ||
@@ -365,2 +375,4 @@ expect(ctx.Integer('2').lessOrEqual(ctx.Integer(2))).toBe(true); | ||
expect(ctx.Integer('2').greaterThan(-2)).toBe(true); | ||
expect(ctx.Integer('2').greaterThan('2')).toBe(false); | ||
expect(ctx.Integer('2').greaterThan('-2')).toBe(true); | ||
expect(ctx.Integer('2').greaterThan(ctx.Integer(3))).toBe(false); | ||
@@ -381,2 +393,4 @@ expect(ctx.Integer('2').greaterThan(ctx.Integer(2))).toBe(false); | ||
expect(ctx.Integer('2').greaterOrEqual(-2)).toBe(true); | ||
expect(ctx.Integer('2').greaterOrEqual('2')).toBe(true); | ||
expect(ctx.Integer('2').greaterOrEqual('-2')).toBe(true); | ||
expect(ctx.Integer('2').greaterOrEqual(ctx.Integer(3))).toBe(false); | ||
@@ -405,2 +419,10 @@ expect(ctx.Integer('2').greaterOrEqual(ctx.Integer(2))).toBe(true); | ||
test('toFloat()', () => { | ||
expect(ctx.Integer('2').toFloat().toFixed(2)).toBe('2.00'); | ||
}); | ||
test('toRational()', () => { | ||
expect(ctx.Integer('2').toRational().toString()).toBe('2'); | ||
}); | ||
test('toBuffer()', () => { | ||
@@ -407,0 +429,0 @@ expect(ctx.Integer('0').toBuffer()).toStrictEqual(new Uint8Array([])); |
@@ -58,2 +58,3 @@ import { CalculateTypeWithDestroy, FloatType, init as initGMP, IntegerType, RationalType } from '../src'; | ||
compare(ctx.Rational(2, 3).add(1), '5/3'); | ||
compare(ctx.Rational(2, 3).add('2/3'), '4/3'); | ||
compare(ctx.Rational(2, 3).add(ctx.Rational(1, 3)), '1'); | ||
@@ -65,2 +66,3 @@ compare(ctx.Rational(2, 4).add(ctx.Float('1')), '1.5'); | ||
compare(ctx.Rational(2, 3).sub(1), '-1/3'); | ||
compare(ctx.Rational(2, 3).sub('1/3'), '1/3'); | ||
compare(ctx.Rational(2, 3).sub(ctx.Rational(1, 3)), '1/3'); | ||
@@ -72,2 +74,3 @@ compare(ctx.Rational(2, 4).sub(ctx.Float('1')), '-0.5'); | ||
compare(ctx.Rational(2, 3).mul(2), '4/3'); | ||
compare(ctx.Rational(2, 3).mul('2/3'), '4/9'); | ||
compare(ctx.Rational(2, 3).mul(ctx.Rational(3, 4)), '1/2'); | ||
@@ -79,2 +82,3 @@ compare(ctx.Rational(2, 4).mul(ctx.Float('2')), '1'); | ||
compare(ctx.Rational(2, 3).div(2), '1/3'); | ||
compare(ctx.Rational(2, 3).div('3/4'), '8/9'); | ||
compare(ctx.Rational(2, 3).div(ctx.Rational(3, 4)), '8/9'); | ||
@@ -114,2 +118,3 @@ compare(ctx.Rational(1, 2).div(ctx.Float('2')), '0.25'); | ||
expect(ctx.Rational('2/3').isEqual(2)).toBe(false); | ||
expect(ctx.Rational('4/6').isEqual('2/3')).toBe(true); | ||
expect(ctx.Rational('4/6').isEqual(ctx.Rational('2/3'))).toBe(true); | ||
@@ -121,5 +126,6 @@ expect(ctx.Rational('1/2').isEqual(ctx.Float('0.5'))).toBe(true); | ||
test('lessThan()', () => { | ||
expect(ctx.Rational('2/3').lessThan(ctx.Rational('2/3'))).toBe(false); | ||
expect(ctx.Rational('2').lessThan(3)).toBe(true); | ||
expect(ctx.Rational('2').lessThan(1)).toBe(false); | ||
expect(ctx.Rational('2/3').lessThan('2/3')).toBe(false); | ||
expect(ctx.Rational('2/3').lessThan(ctx.Rational('2/3'))).toBe(false); | ||
expect(ctx.Rational('2').lessThan(ctx.Integer(3))).toBe(true); | ||
@@ -132,5 +138,6 @@ expect(ctx.Rational('2').lessThan(ctx.Integer(1))).toBe(false); | ||
test('lessOrEqual()', () => { | ||
expect(ctx.Rational('2/3').lessOrEqual(ctx.Rational('2/3'))).toBe(true); | ||
expect(ctx.Rational('2').lessOrEqual(1)).toBe(false); | ||
expect(ctx.Rational('2').lessOrEqual(3)).toBe(true); | ||
expect(ctx.Rational('2/3').lessOrEqual(ctx.Rational('2/3'))).toBe(true); | ||
expect(ctx.Rational('2/3').lessOrEqual('2/3')).toBe(true); | ||
expect(ctx.Rational('2').lessOrEqual(ctx.Integer(1))).toBe(false); | ||
@@ -143,5 +150,6 @@ expect(ctx.Rational('2').lessOrEqual(ctx.Integer(3))).toBe(true); | ||
test('greaterThan()', () => { | ||
expect(ctx.Rational('2/3').greaterThan(ctx.Rational('2/3'))).toBe(false); | ||
expect(ctx.Rational('2').greaterThan(1)).toBe(true); | ||
expect(ctx.Rational('2').greaterThan(3)).toBe(false); | ||
expect(ctx.Rational('2/3').greaterThan(ctx.Rational('2/3'))).toBe(false); | ||
expect(ctx.Rational('2/3').greaterThan('2/3')).toBe(false); | ||
expect(ctx.Rational('2').greaterThan(ctx.Integer(1))).toBe(true); | ||
@@ -154,5 +162,6 @@ expect(ctx.Rational('2').greaterThan(ctx.Integer(3))).toBe(false); | ||
test('greaterOrEqual()', () => { | ||
expect(ctx.Rational('2/3').greaterOrEqual(ctx.Rational('2/3'))).toBe(true); | ||
expect(ctx.Rational('2').greaterOrEqual(1)).toBe(true); | ||
expect(ctx.Rational('2').greaterOrEqual(3)).toBe(false); | ||
expect(ctx.Rational('2/3').greaterOrEqual('2/3')).toBe(true); | ||
expect(ctx.Rational('2/3').greaterOrEqual(ctx.Rational('2/3'))).toBe(true); | ||
expect(ctx.Rational('2').greaterOrEqual(ctx.Integer(1))).toBe(true); | ||
@@ -175,1 +184,9 @@ expect(ctx.Rational('2').greaterOrEqual(ctx.Integer(3))).toBe(false); | ||
}); | ||
test('toFloat()', () => { | ||
expect(ctx.Rational('2').toFloat().toFixed(2)).toBe('2.00'); | ||
}); | ||
test('toInteger()', () => { | ||
expect(ctx.Rational('2').toInteger().toString()).toBe('2'); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2581678
44
19699