Comparing version 1.0.2 to 1.1.0
@@ -0,1 +1,7 @@ | ||
## 1.1.0 (Apr 22, 2022) | ||
* Rename mpfr_get_pretty_string() to mpfr_to_string() | ||
* Add mpz_to_string() and mpq_to_string() helpers | ||
* Add mpz_set_string(), mpz_init_set_string(), mpq_set_string(), mpfr_set_string(), mpfr_init_set_string() helpers | ||
* Add radix parameter to Rational.toString() | ||
## 1.0.2 (Apr 18, 2022) | ||
@@ -2,0 +8,0 @@ * Add missing mpq_numref(), mpq_denref() functions |
@@ -49,3 +49,3 @@ import type { GMPFunctions } from './functions'; | ||
/** Converts the number to string */ | ||
toString(): string; | ||
toString(radix?: number): string; | ||
/** Converts the number to an integer */ | ||
@@ -52,0 +52,0 @@ toInteger(): Integer; |
{ | ||
"name": "gmp-wasm", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Arbitrary-precision Integer, Rational and Float types based on the GMP and MPFR libraries", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.umd.js", |
@@ -757,3 +757,3 @@ import { mpfr_rnd_t } from './bindingTypes'; | ||
const str = gmp.mpfr_get_pretty_string(this.mpfr_t, radix, this.rndMode); | ||
const str = gmp.mpfr_to_string(this.mpfr_t, radix, this.rndMode); | ||
return str; | ||
@@ -807,5 +807,3 @@ }, | ||
if (typeof val === 'string') { | ||
const strPtr = gmp.malloc_cstr(val); | ||
const res = gmp.mpfr_set_str(mpfr_t, strPtr, radix, rndMode); | ||
gmp.free(strPtr); | ||
const res = gmp.mpfr_set_string(mpfr_t, val, radix, rndMode); | ||
if (res !== 0) { | ||
@@ -812,0 +810,0 @@ throw new Error('Invalid number provided!'); |
@@ -6,4 +6,2 @@ import type { GMPFunctions } from './functions'; | ||
const decoder = new TextDecoder(); | ||
type IntegerFactoryReturn = ReturnType<typeof getIntegerContext>['Integer']; | ||
@@ -566,7 +564,3 @@ export interface IntegerFactory extends IntegerFactoryReturn {}; | ||
} | ||
const strptr = gmp.mpz_get_str(0, radix, this.mpz_t); | ||
const endptr = gmp.mem.indexOf(0, strptr); | ||
const str = decoder.decode(gmp.mem.subarray(strptr, endptr)); | ||
gmp.free(strptr); | ||
return str; | ||
return gmp.mpz_to_string(this.mpz_t, radix); | ||
}, | ||
@@ -593,5 +587,3 @@ | ||
assertValidRadix(radix); | ||
const strPtr = gmp.malloc_cstr(num); | ||
const res = gmp.mpz_init_set_str(instance.mpz_t, strPtr, radix); | ||
gmp.free(strPtr); | ||
const res = gmp.mpz_init_set_string(instance.mpz_t, num, radix); | ||
if (res !== 0) { | ||
@@ -598,0 +590,0 @@ throw new Error('Invalid number provided!'); |
@@ -6,4 +6,2 @@ import type { GMPFunctions } from './functions'; | ||
const decoder = new TextDecoder(); | ||
type RationalFactoryReturn = ReturnType<typeof getRationalContext>['Rational']; | ||
@@ -233,8 +231,7 @@ export interface RationalFactory extends RationalFactoryReturn {}; | ||
/** Converts the number to string */ | ||
toString(): string { | ||
const strptr = gmp.mpq_get_str(0, 10, this.mpq_t); | ||
const endptr = gmp.mem.indexOf(0, strptr); | ||
const str = decoder.decode(gmp.mem.subarray(strptr, endptr)); | ||
gmp.free(strptr); | ||
return str; | ||
toString(radix: number = 10): string { | ||
if (!Number.isSafeInteger(radix) || radix < 2 || radix > 62) { | ||
throw new Error('radix must have a value between 2 and 62'); | ||
} | ||
return gmp.mpq_to_string(this.mpq_t, radix); | ||
}, | ||
@@ -279,5 +276,6 @@ | ||
const finalString = p2 !== undefined ? `${p1.toString()}/${p2.toString()}` : p1.toString(); | ||
const strPtr = gmp.malloc_cstr(finalString); | ||
gmp.mpq_set_str(mpq_t, strPtr, 10); | ||
gmp.free(strPtr); | ||
const res = gmp.mpq_set_string(mpq_t, finalString, 10); | ||
if (res !== 0) { | ||
throw new Error('Invalid number provided!'); | ||
} | ||
} | ||
@@ -284,0 +282,0 @@ |
@@ -11,13 +11,2 @@ import { init as initGMP, precisionToBits } from '../src'; | ||
test('has 64 bit binding', () => { | ||
expect(gmp.binding.mp_bits_per_limb()).toBe(64); | ||
}); | ||
test('binding has macros', () => { | ||
const ctx = gmp.getContext(); | ||
const { mpq_t } = ctx.Rational(3, 4); | ||
expect(gmp.binding.mpz_get_si(gmp.binding.mpq_numref(mpq_t))).toBe(3); | ||
expect(gmp.binding.mpz_get_si(gmp.binding.mpq_denref(mpq_t))).toBe(4); | ||
}); | ||
test('calculate()', () => { | ||
@@ -24,0 +13,0 @@ expect(gmp.calculate(g => g.Float('2').sqrt())).toBe('1.4142135623730949'); |
@@ -42,1 +42,12 @@ import { init as initGMP } from '../src'; | ||
}); | ||
test('has 64 bit binding', () => { | ||
expect(gmp.binding.mp_bits_per_limb()).toBe(64); | ||
}); | ||
test('binding has macros', () => { | ||
const ctx = gmp.getContext(); | ||
const { mpq_t } = ctx.Rational(3, 4); | ||
expect(gmp.binding.mpz_get_si(gmp.binding.mpq_numref(mpq_t))).toBe(3); | ||
expect(gmp.binding.mpz_get_si(gmp.binding.mpq_denref(mpq_t))).toBe(4); | ||
}); |
@@ -464,2 +464,6 @@ import {CalculateTypeWithDestroy, FloatRoundingMode, FloatType, init as initGMP, IntegerType, RationalType} from '../src'; | ||
test('toString()', () => { | ||
expect(ctx.Float('1'.repeat(8000), { precisionBits: 32000 }).toString()).toBe('1'.repeat(8000)); | ||
}); | ||
test('special values to JS types', () => { | ||
@@ -512,8 +516,1 @@ expect(ctx.Float(0).toNumber()).toBe(0); | ||
}); | ||
test('mpfr_get_pretty_string()', () => { | ||
const roundingMode = FloatRoundingMode.ROUND_NEAREST; | ||
const options = { precisionBits: 16, roundingMode }; | ||
const val = ctx.Float('-123.456789000', options); | ||
expect(gmp.binding.mpfr_get_pretty_string(val.mpfr_t, 10, roundingMode as number)).toBe('-123.457'); | ||
}); |
@@ -456,2 +456,3 @@ import { DivMode } from '../src/integer'; | ||
expect(ctx.Integer('999 999 999 999').toString()).toBe('999999999999'); | ||
expect(ctx.Integer('1'.repeat(8000)).toString()).toBe('1'.repeat(8000)); | ||
expect(ctx.Integer(0b1101100).toString(2)).toBe('1101100'); | ||
@@ -458,0 +459,0 @@ expect(ctx.Integer(-0b1101100).toString(2)).toBe('-1101100'); |
@@ -183,1 +183,12 @@ import { CalculateTypeWithDestroy, FloatType, init as initGMP, IntegerType, RationalType } from '../src'; | ||
}); | ||
test('toString()', () => { | ||
expect(ctx.Rational('2/3').toString()).toBe('2/3'); | ||
expect(ctx.Rational('-2/3').toString()).toBe('-2/3'); | ||
expect(ctx.Rational('999 999 999 999/123').toString()).toBe('333333333333/41'); | ||
expect(ctx.Rational('1'.repeat(8000)).toString()).toBe('1'.repeat(8000)); | ||
expect(ctx.Rational(0b1101100, 5).toString(2)).toBe('1101100/101'); | ||
expect(ctx.Rational(-0b1101100, -7).toString(2)).toBe('1101100/111'); | ||
expect(ctx.Rational(0b1101101, 16).toString(16)).toBe('6d/10'); | ||
expect(ctx.Rational(-0b1101101, 15).toString(16)).toBe('-6d/f'); | ||
}); |
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
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
3704707
29795