Comparing version 1.0.1 to 1.0.2
@@ -0,1 +1,5 @@ | ||
## 1.0.2 (Apr 18, 2022) | ||
* Add missing mpq_numref(), mpq_denref() functions | ||
* Add mpfr_get_pretty_string() function | ||
## 1.0.1 (Apr 9, 2022) | ||
@@ -2,0 +6,0 @@ * Fix path of TS type declarations |
@@ -8,1 +8,9 @@ export declare function isUint32(num: number): boolean; | ||
export declare function assertValidRadix(radix: number): void; | ||
export declare const FLOAT_SPECIAL_VALUES: { | ||
readonly '@NaN@': "NaN"; | ||
readonly '@Inf@': "Infinity"; | ||
readonly '-@Inf@': "-Infinity"; | ||
}; | ||
export declare const FLOAT_SPECIAL_VALUE_KEYS: string[]; | ||
export declare const trimTrailingZeros: (num: string) => string; | ||
export declare const insertDecimalPoint: (mantissa: string, pointPos: number) => string; |
{ | ||
"name": "gmp-wasm", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Arbitrary-precision Integer, Rational and Float types based on the GMP and MPFR libraries", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.umd.js", |
@@ -5,6 +5,4 @@ import { mpfr_rnd_t } from './bindingTypes'; | ||
import { Rational } from './rational'; | ||
import {assertInt32, assertUint32, assertValidRadix, isInt32} from './util'; | ||
import { assertInt32, assertUint32, assertValidRadix, FLOAT_SPECIAL_VALUES, isInt32 } from './util'; | ||
const decoder = new TextDecoder(); | ||
type FloatFactoryReturn = ReturnType<typeof getFloatContext>['Float']; | ||
@@ -36,11 +34,2 @@ export interface FloatFactory extends FloatFactoryReturn {}; | ||
}; | ||
const SPECIAL_VALUES = { | ||
'@NaN@': 'NaN', | ||
'@Inf@': 'Infinity', | ||
'-@Inf@': '-Infinity', | ||
} as const; | ||
const SPECIAL_VALUE_KEYS = Object.keys(SPECIAL_VALUES); | ||
export interface FloatOptions { | ||
@@ -52,52 +41,2 @@ precisionBits?: number; | ||
const trimTrailingZeros = (num: string) => { | ||
let pos = num.length - 1; | ||
while (pos >= 0) { | ||
if (num[pos] === '.') { | ||
pos--; | ||
break; | ||
} else if (num[pos] === '0') { | ||
pos--; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (pos !== num.length - 1) { | ||
return num.slice(0, pos + 1); | ||
} | ||
if (num.length === 0) { | ||
return '0'; | ||
} | ||
return num; | ||
}; | ||
const insertDecimalPoint = (mantissa: string, pointPos: number) => { | ||
const isNegative = mantissa.startsWith('-'); | ||
const mantissaWithoutSign = isNegative ? mantissa.slice(1) : mantissa; | ||
const sign = isNegative ? '-' : ''; | ||
let hasDecimalPoint = false; | ||
if (pointPos <= 0) { | ||
const zeros = '0'.repeat(-pointPos); | ||
mantissa = `${sign}0.${zeros}${mantissaWithoutSign}`; | ||
hasDecimalPoint = true; | ||
} else if (pointPos < mantissaWithoutSign.length) { | ||
mantissa = `${sign}${mantissaWithoutSign.slice(0, pointPos)}.${mantissaWithoutSign.slice(pointPos)}`; | ||
hasDecimalPoint = true; | ||
} else { | ||
const zeros = '0'.repeat(pointPos - mantissaWithoutSign.length); | ||
mantissa = `${mantissa}${zeros}`; | ||
} | ||
// trim trailing zeros after decimal point | ||
if (hasDecimalPoint) { | ||
mantissa = trimTrailingZeros(mantissa); | ||
} | ||
return mantissa; | ||
} | ||
const INVALID_PARAMETER_ERROR = 'Invalid parameter!'; | ||
@@ -821,18 +760,4 @@ | ||
const mpfr_exp_t_ptr = gmp.malloc(4); | ||
const strptr = gmp.mpfr_get_str(0, mpfr_exp_t_ptr, radix, 0, this.mpfr_t, this.rndMode); | ||
const endptr = gmp.mem.indexOf(0, strptr); | ||
let ret = decoder.decode(gmp.mem.subarray(strptr, endptr)); | ||
if (SPECIAL_VALUE_KEYS.includes(ret)) { | ||
ret = SPECIAL_VALUES[ret]; | ||
} else { | ||
// decimal point needs to be inserted | ||
const pointPos = gmp.memView.getInt32(mpfr_exp_t_ptr, true); | ||
ret = insertDecimalPoint(ret, pointPos); | ||
} | ||
gmp.mpfr_free_str(strptr); | ||
gmp.free(mpfr_exp_t_ptr); | ||
return ret; | ||
const str = gmp.mpfr_get_pretty_string(this.mpfr_t, radix, this.rndMode); | ||
return str; | ||
}, | ||
@@ -847,3 +772,3 @@ | ||
const str = this.toString(radix); | ||
if (Object.values(SPECIAL_VALUES).includes(str)) { | ||
if (Object.values(FLOAT_SPECIAL_VALUES).includes(str)) { | ||
return str; | ||
@@ -850,0 +775,0 @@ } |
@@ -36,1 +36,59 @@ export function isUint32(num: number) { | ||
} | ||
export const FLOAT_SPECIAL_VALUES = { | ||
'@NaN@': 'NaN', | ||
'@Inf@': 'Infinity', | ||
'-@Inf@': '-Infinity', | ||
} as const; | ||
export const FLOAT_SPECIAL_VALUE_KEYS = Object.keys(FLOAT_SPECIAL_VALUES); | ||
export const trimTrailingZeros = (num: string): string => { | ||
let pos = num.length - 1; | ||
while (pos >= 0) { | ||
if (num[pos] === '.') { | ||
pos--; | ||
break; | ||
} else if (num[pos] === '0') { | ||
pos--; | ||
} else { | ||
break; | ||
} | ||
} | ||
if (pos !== num.length - 1) { | ||
return num.slice(0, pos + 1); | ||
} | ||
if (num.length === 0) { | ||
return '0'; | ||
} | ||
return num; | ||
}; | ||
export const insertDecimalPoint = (mantissa: string, pointPos: number): string => { | ||
const isNegative = mantissa.startsWith('-'); | ||
const mantissaWithoutSign = isNegative ? mantissa.slice(1) : mantissa; | ||
const sign = isNegative ? '-' : ''; | ||
let hasDecimalPoint = false; | ||
if (pointPos <= 0) { | ||
const zeros = '0'.repeat(-pointPos); | ||
mantissa = `${sign}0.${zeros}${mantissaWithoutSign}`; | ||
hasDecimalPoint = true; | ||
} else if (pointPos < mantissaWithoutSign.length) { | ||
mantissa = `${sign}${mantissaWithoutSign.slice(0, pointPos)}.${mantissaWithoutSign.slice(pointPos)}`; | ||
hasDecimalPoint = true; | ||
} else { | ||
const zeros = '0'.repeat(pointPos - mantissaWithoutSign.length); | ||
mantissa = `${mantissa}${zeros}`; | ||
} | ||
// trim trailing zeros after decimal point | ||
if (hasDecimalPoint) { | ||
mantissa = trimTrailingZeros(mantissa); | ||
} | ||
return mantissa; | ||
} |
@@ -15,2 +15,9 @@ import { init as initGMP, precisionToBits } from '../src'; | ||
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()', () => { | ||
@@ -17,0 +24,0 @@ expect(gmp.calculate(g => g.Float('2').sqrt())).toBe('1.4142135623730949'); |
@@ -511,1 +511,8 @@ import {CalculateTypeWithDestroy, FloatRoundingMode, FloatType, init as initGMP, IntegerType, RationalType} from '../src'; | ||
}); | ||
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'); | ||
}); |
Sorry, the diff of this file is not supported yet
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
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
3680801
29324