Comparing version 1.1.0 to 1.3.1
@@ -0,1 +1,18 @@ | ||
## 1.3.1 (July 6, 2024) | ||
* Use Node.js v20 | ||
## 1.3.0 (July 6, 2024) | ||
* Removed truncate parameter from toString() | ||
* Added toInterval() | ||
* Fixed bug with string formatting and removal of trailing zeros | ||
* Update GMP version to 6.3.0 | ||
* Update MPFR version to 4.2.1 | ||
* Update Emscripten | ||
## 1.2.0 (May 1, 2022) | ||
* Add truncate parameter to Float().toString() and mpfr_to_string() | ||
* Change default float precision to 53 (previously it was 52) to match double type | ||
* Performance improvements. Custom tuning for GMP | ||
* Remove some rarely used functions like mpfr_get_patches() to make the bundle smaller | ||
## 1.1.0 (Apr 22, 2022) | ||
@@ -2,0 +19,0 @@ * Rename mpfr_get_pretty_string() to mpfr_to_string() |
@@ -1,1 +0,1 @@ | ||
export declare const getBinding: (wasmPath: string) => Promise<any>; | ||
export declare const getBinding: (reset?: boolean) => Promise<any>; |
@@ -200,2 +200,4 @@ import { GMPFunctions } from './functions'; | ||
toInteger(): Integer; | ||
/** Get error intervals */ | ||
toInterval(): [Float, Float]; | ||
/** Converts the number to a rational number */ | ||
@@ -375,2 +377,4 @@ toRational(): Rational; | ||
toInteger(): Integer; | ||
/** Get error intervals */ | ||
toInterval(): [Float, Float]; | ||
/** Converts the number to a rational number */ | ||
@@ -549,2 +553,4 @@ toRational(): Rational; | ||
toInteger(): Integer; | ||
/** Get error intervals */ | ||
toInterval(): [Float, Float]; | ||
/** Converts the number to a rational number */ | ||
@@ -724,2 +730,4 @@ toRational(): Rational; | ||
toInteger(): Integer; | ||
/** Get error intervals */ | ||
toInterval(): [Float, Float]; | ||
/** Converts the number to a rational number */ | ||
@@ -898,2 +906,4 @@ toRational(): Rational; | ||
toInteger(): Integer; | ||
/** Get error intervals */ | ||
toInterval(): [Float, Float]; | ||
/** Converts the number to a rational number */ | ||
@@ -900,0 +910,0 @@ toRational(): Rational; |
@@ -23,3 +23,3 @@ import { Float, FloatFactory, FloatOptions, FloatRoundingMode } from './float'; | ||
binding: GMPFunctions; | ||
calculate: (fn: (gmp: CalculateType) => Integer | Rational | Float, options?: CalculateOptions) => void; | ||
calculate: (fn: (gmp: CalculateType) => Integer | Rational | Float | string, options?: CalculateOptions) => void; | ||
getContext: (options?: CalculateOptions) => CalculateTypeWithDestroy; | ||
@@ -26,0 +26,0 @@ reset: () => Promise<void>; |
@@ -9,5 +9,5 @@ export declare function isUint32(num: number): boolean; | ||
export declare const FLOAT_SPECIAL_VALUES: { | ||
readonly '@NaN@': "NaN"; | ||
readonly '@Inf@': "Infinity"; | ||
readonly '-@Inf@': "-Infinity"; | ||
readonly "@NaN@": "NaN"; | ||
readonly "@Inf@": "Infinity"; | ||
readonly "-@Inf@": "-Infinity"; | ||
}; | ||
@@ -14,0 +14,0 @@ export declare const FLOAT_SPECIAL_VALUE_KEYS: string[]; |
module.exports = { | ||
testEnvironment: 'node', | ||
roots: [ | ||
@@ -6,3 +7,3 @@ '<rootDir>/test', | ||
transform: { | ||
'^.+\\.ts$': 'ts-jest', | ||
'^.+\\.ts$': 'ts-jest/legacy', | ||
}, | ||
@@ -9,0 +10,0 @@ cacheDirectory: '<rootDir>/.jest-cache', |
{ | ||
"name": "gmp-wasm", | ||
"version": "1.1.0", | ||
"version": "1.3.1", | ||
"description": "Arbitrary-precision Integer, Rational and Float types based on the GMP and MPFR libraries", | ||
@@ -59,23 +59,23 @@ "main": "dist/index.umd.js", | ||
"@rollup/plugin-alias": "^3.1.9", | ||
"@rollup/plugin-commonjs": "^21.0.3", | ||
"@rollup/plugin-commonjs": "^22.0.0", | ||
"@rollup/plugin-json": "^4.1.0", | ||
"@rollup/plugin-node-resolve": "^13.1.3", | ||
"@rollup/plugin-typescript": "^8.3.1", | ||
"@rollup/plugin-node-resolve": "^13.2.1", | ||
"@rollup/plugin-typescript": "^8.3.2", | ||
"@types/estree": "^0.0.51", | ||
"@types/jest": "^27.4.1", | ||
"@types/node": "^16.11.26", | ||
"@types/node": "^17.0.29", | ||
"binaryen": "^106.0.0", | ||
"fflate": "^0.7.3", | ||
"jest": "^27.5.1", | ||
"rollup": "^2.70.1", | ||
"jest": "^28.0.2", | ||
"rollup": "^2.70.2", | ||
"rollup-plugin-copy": "^3.4.0", | ||
"rollup-plugin-license": "^2.6.1", | ||
"rollup-plugin-license": "^2.7.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"serve": "^13.0.2", | ||
"ts-jest": "^27.1.4", | ||
"ts-jest": "^28.0.0-next.1", | ||
"ts-node": "^10.7.0", | ||
"tslib": "^2.3.1", | ||
"typedoc": "^0.22.14", | ||
"tslib": "^2.4.0", | ||
"typedoc": "^0.22.15", | ||
"typescript": "^4.6.3" | ||
} | ||
} |
@@ -1,6 +0,6 @@ | ||
import GMPModule from '../binding/dist/gmp'; | ||
import GMPModule from '../binding/dist/gmpmini'; | ||
let instance: any = null; | ||
export const getBinding = async (wasmPath: string) => { | ||
export const getBinding = async (reset = false) => { | ||
if (instance !== null) { | ||
@@ -11,7 +11,7 @@ return instance; | ||
locateFile: function (path) { | ||
return wasmPath; | ||
return '/root/gmp-wasm/binding/dist/gmpmini.wasm'; | ||
}, | ||
}); | ||
instance = binding; | ||
instance = { ...binding.asm, heap: { HEAP8: binding.HEAP8 } }; | ||
return instance; | ||
}; |
@@ -49,3 +49,3 @@ import { mpfr_rnd_t } from './bindingTypes'; | ||
const globalRndMode = (ctxOptions.roundingMode ?? FloatRoundingMode.ROUND_NEAREST) as number as mpfr_rnd_t; | ||
const globalPrecisionBits = ctxOptions.precisionBits ?? 52; | ||
const globalPrecisionBits = ctxOptions.precisionBits ?? 53; // double precision by default | ||
const globalRadix = ctxOptions.radix ?? 10; | ||
@@ -799,2 +799,7 @@ | ||
/** Get error intervals */ | ||
toInterval(): [Float, Float] { | ||
return [this.nextBelow(), this.nextAbove()]; | ||
}, | ||
/** Converts the number to a rational number */ | ||
@@ -801,0 +806,0 @@ toRational(): Rational { |
@@ -38,3 +38,3 @@ import { Float, FloatFactory, FloatOptions, FloatRoundingMode, getFloatContext } from './float'; | ||
binding: GMPFunctions; | ||
calculate: (fn: (gmp: CalculateType) => Integer | Rational | Float, options?: CalculateOptions) => void; | ||
calculate: (fn: (gmp: CalculateType) => Integer | Rational | Float | string, options?: CalculateOptions) => void; | ||
getContext: (options?: CalculateOptions) => CalculateTypeWithDestroy; | ||
@@ -83,3 +83,3 @@ reset: () => Promise<void>; | ||
calculate: (fn: (gmp: CalculateType) => Integer | Rational | Float, options: CalculateOptions = {}): string => { | ||
calculate: (fn: (gmp: CalculateType) => Integer | Rational | Float | string, options: CalculateOptions = {}): string => { | ||
const context = createContext(options); | ||
@@ -86,0 +86,0 @@ if (typeof fn !== 'function') { |
@@ -7,3 +7,3 @@ export function isUint32(num: number) { | ||
if (!isUint32(num)) { | ||
throw new Error('Invalid number specified: uint32_t is required'); | ||
throw new Error("Invalid number specified: uint32_t is required"); | ||
} | ||
@@ -13,3 +13,7 @@ } | ||
export function isInt32(num: number) { | ||
return Number.isSafeInteger(num) && num >= -Math.pow(2, 31) && num < Math.pow(2, 31); | ||
return ( | ||
Number.isSafeInteger(num) && | ||
num >= -Math.pow(2, 31) && | ||
num < Math.pow(2, 31) | ||
); | ||
} | ||
@@ -19,3 +23,3 @@ | ||
if (!isInt32(num)) { | ||
throw new Error('Invalid number specified: int32_t is required'); | ||
throw new Error("Invalid number specified: int32_t is required"); | ||
} | ||
@@ -26,3 +30,3 @@ } | ||
if (!Array.isArray(arr)) { | ||
throw new Error('Invalid parameter specified. Array is required!'); | ||
throw new Error("Invalid parameter specified. Array is required!"); | ||
} | ||
@@ -37,3 +41,3 @@ } | ||
if (!isValidRadix(radix)) { | ||
throw new Error('radix must have a value between 2 and 36'); | ||
throw new Error("radix must have a value between 2 and 36"); | ||
} | ||
@@ -43,5 +47,5 @@ } | ||
export const FLOAT_SPECIAL_VALUES = { | ||
'@NaN@': 'NaN', | ||
'@Inf@': 'Infinity', | ||
'-@Inf@': '-Infinity', | ||
"@NaN@": "NaN", | ||
"@Inf@": "Infinity", | ||
"-@Inf@": "-Infinity", | ||
} as const; | ||
@@ -52,8 +56,10 @@ | ||
export const trimTrailingZeros = (num: string): string => { | ||
if (num.indexOf('.') === -1) return num; | ||
let pos = num.length - 1; | ||
while (pos >= 0) { | ||
if (num[pos] === '.') { | ||
if (num[pos] === ".") { | ||
pos--; | ||
break; | ||
} else if (num[pos] === '0') { | ||
} else if (num[pos] === "0") { | ||
pos--; | ||
@@ -65,29 +71,35 @@ } else { | ||
if (pos !== num.length - 1) { | ||
return num.slice(0, pos + 1); | ||
} | ||
const rem = num.slice(0, pos + 1); | ||
if (num.length === 0) { | ||
return '0'; | ||
if (rem === "-") { | ||
return "-0"; | ||
} else if (rem.length === 0) { | ||
return "0"; | ||
} | ||
return num; | ||
return rem; | ||
}; | ||
export const insertDecimalPoint = (mantissa: string, pointPos: number): string => { | ||
const isNegative = mantissa.startsWith('-'); | ||
export const insertDecimalPoint = ( | ||
mantissa: string, | ||
pointPos: number | ||
): string => { | ||
const isNegative = mantissa.startsWith("-"); | ||
const mantissaWithoutSign = isNegative ? mantissa.slice(1) : mantissa; | ||
const sign = isNegative ? '-' : ''; | ||
const sign = isNegative ? "-" : ""; | ||
let hasDecimalPoint = false; | ||
if (pointPos <= 0) { | ||
const zeros = '0'.repeat(-pointPos); | ||
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)}`; | ||
mantissa = `${sign}${mantissaWithoutSign.slice( | ||
0, | ||
pointPos | ||
)}.${mantissaWithoutSign.slice(pointPos)}`; | ||
hasDecimalPoint = true; | ||
} else { | ||
const zeros = '0'.repeat(pointPos - mantissaWithoutSign.length); | ||
const zeros = "0".repeat(pointPos - mantissaWithoutSign.length); | ||
mantissa = `${mantissa}${zeros}`; | ||
@@ -101,2 +113,2 @@ } | ||
return mantissa; | ||
} | ||
}; |
@@ -12,4 +12,4 @@ import { init as initGMP, precisionToBits } from '../src'; | ||
test('calculate()', () => { | ||
expect(gmp.calculate(g => g.Float('2').sqrt())).toBe('1.4142135623730949'); | ||
expect(gmp.calculate(g => g.Float('2').sqrt(), {})).toBe('1.4142135623730949'); | ||
expect(gmp.calculate(g => g.Float('2').sqrt())).toBe(Math.sqrt(2).toString()); | ||
expect(gmp.calculate(g => g.Float('2').sqrt(), {})).toBe(Math.sqrt(2).toString()); | ||
expect(gmp.calculate(g => g.Float('2').sqrt(), { precisionBits: 16 })).toBe('1.41422'); | ||
@@ -20,3 +20,3 @@ }); | ||
const calcDefault = gmp.getContext(); | ||
expect(calcDefault.Float('2').sqrt().toString()).toBe('1.4142135623730949'); | ||
expect(calcDefault.Float('2').sqrt().toString()).toBe(Math.sqrt(2).toString()); | ||
expect(calcDefault.destroy()).toBe(undefined); | ||
@@ -23,0 +23,0 @@ const calcPrecision = gmp.getContext({ precisionBits: 16 }); |
@@ -1,2 +0,3 @@ | ||
import { init as initGMP } from '../src'; | ||
import { trimTrailingZeros } from '../src/util'; | ||
import { FloatRoundingMode, init as initGMP, mpfr_rnd_t } from '../src'; | ||
/* global test, expect */ | ||
@@ -53,1 +54,31 @@ | ||
}); | ||
test('mpfr_to_string()', () => { | ||
const ctx = gmp.getContext(); | ||
const getPi = (prec, base = 10) => { | ||
const pi = ctx.Pi({ precisionBits: prec, roundingMode: FloatRoundingMode.ROUND_TO_ZERO }); | ||
return gmp.binding.mpfr_to_string(pi.mpfr_t, base, mpfr_rnd_t.MPFR_RNDZ); | ||
} | ||
const max = 10000; | ||
const ref = getPi(max, 2); | ||
for (let i = 10; i < max; i++) { | ||
expect(ref).toContain(getPi(i, 2)); | ||
} | ||
}); | ||
test('trimTrailingZeros()', () => { | ||
expect(trimTrailingZeros('0')).toBe('0'); | ||
expect(trimTrailingZeros('0.')).toBe('0'); | ||
expect(trimTrailingZeros('-0')).toBe('-0'); | ||
expect(trimTrailingZeros('-0.')).toBe('-0'); | ||
expect(trimTrailingZeros('0.0')).toBe('0'); | ||
expect(trimTrailingZeros('-0.00')).toBe('-0'); | ||
expect(trimTrailingZeros('-0.010')).toBe('-0.01'); | ||
expect(trimTrailingZeros('-123.000')).toBe('-123'); | ||
expect(trimTrailingZeros('-123.320000')).toBe('-123.32'); | ||
expect(trimTrailingZeros('-123.320001')).toBe('-123.320001'); | ||
expect(trimTrailingZeros('1000')).toBe('1000'); | ||
expect(trimTrailingZeros('-1000')).toBe('-1000'); | ||
}); |
@@ -10,3 +10,3 @@ import {CalculateTypeWithDestroy, FloatRoundingMode, FloatType, init as initGMP, IntegerType, RationalType} from '../src'; | ||
gmp = await initGMP(); | ||
ctx = gmp.getContext({ precisionBits: 16 }); | ||
ctx = gmp.getContext({ precisionBits: 24 }); | ||
}); | ||
@@ -18,3 +18,17 @@ | ||
const compare = (int: IntegerType | RationalType | FloatType, res: string, radix = 10) => { | ||
const printNum = (x: FloatType) => { | ||
return `${x.toString()} [${x.isZero() ? '' : x.toInterval()}]`; | ||
}; | ||
const compare = (x: IntegerType | RationalType | FloatType, res: string) => { | ||
const a = ctx.Float(x); | ||
const b = ctx.Float(res); | ||
const ok = (b.isZero() && x.abs().lessThan('0.00001')) || (b.nextBelow().lessOrEqual(a) && b.nextAbove().greaterOrEqual(a)); | ||
if (!ok) { | ||
throw new Error(`Compare failed: received ${printNum(a)}, expected: ${printNum(b)} provided as ${res} `); | ||
} | ||
expect(ok).toBeTruthy(); | ||
} | ||
const compareExact = (int: IntegerType | RationalType | FloatType, res: string, radix = 10) => { | ||
expect(int.toString(radix)).toBe(res); | ||
@@ -30,3 +44,3 @@ } | ||
compare(ctx.Float('-0.5'), '-0.5'); | ||
compare(ctx.Float('0.002'), '0.00199997'); | ||
compare(ctx.Float('0.002'), '0.002'); | ||
compare(ctx.Float('1000000'), '1000000'); | ||
@@ -40,2 +54,3 @@ compare(ctx.Float('10000.000'), '10000'); | ||
compare(ctx.Float('0.10000', { radix: 2 }), '0.5'); | ||
compare(ctx.Float('1.12345678901234567890'), '1.1234567890123456789012345'); | ||
}); | ||
@@ -70,7 +85,7 @@ | ||
test('Constants', () => { | ||
compare(ctx.Pi(), '3.1416'); | ||
compare(ctx.Log2(), '0.693146'); | ||
compare(ctx.Catalan(), '0.91597'); | ||
compare(ctx.EulerConstant(), '0.577209'); | ||
compare(ctx.EulerNumber(), '2.71826'); | ||
compare(ctx.Pi(), '3.14159265359'); | ||
compare(ctx.Log2(), '0.69314718056'); | ||
compare(ctx.Catalan(), '0.915965594177219'); | ||
compare(ctx.EulerConstant(), '0.5772156649015'); | ||
compare(ctx.EulerNumber(), '2.71828182845'); | ||
}); | ||
@@ -81,9 +96,9 @@ | ||
compare(ctx.Float(0.4).add(0.6), '1'); | ||
compare(ctx.Float(0.4).add('0.7'), '1.10001'); | ||
compare(ctx.Float(0.4).add('0.7'), '1.1'); | ||
compare(ctx.Float(0.5).add(ctx.Float(1)), '1.5'); | ||
compare(ctx.Float(0.4).add(ctx.Float(0.6)), '1'); | ||
compare(ctx.Float(0.4).add(ctx.Integer(2)), '2.40002'); | ||
compare(ctx.Float(0.4).add(ctx.Rational(1, 2)), '0.899994'); | ||
compare(ctx.Float(0.4).add(ctx.Integer(2)), '2.4'); | ||
compare(ctx.Float(0.4).add(ctx.Rational(1, 2)), '0.9'); | ||
compare(ctx.Float('1.01', { radix: 2 }).add('10.1'), '3.75'); | ||
compare(ctx.Float('1.01', { radix: 2 }).add(10.1), '11.3501'); | ||
compare(ctx.Float('1.01', { radix: 2 }).add(10.1), '11.35'); | ||
}); | ||
@@ -93,10 +108,10 @@ | ||
compare(ctx.Float(1).sub(0.5), '0.5'); | ||
compare(ctx.Float(0.6).sub(0.4), '0.200005'); | ||
compare(ctx.Float(0.6).sub('0.4'), '0.200005'); | ||
compare(ctx.Float(0.6).sub(0.4), '0.2'); | ||
compare(ctx.Float(0.6).sub('0.4'), '0.2'); | ||
compare(ctx.Float(1).sub(ctx.Float(0.5)), '0.5'); | ||
compare(ctx.Float(0.6).sub(ctx.Float(0.4)), '0.200005'); | ||
compare(ctx.Float(0.4).sub(ctx.Integer(2)), '-1.60001'); | ||
compare(ctx.Float(0.4).sub(ctx.Rational(1, 2)), '-0.0999985'); | ||
compare(ctx.Float(0.6).sub(ctx.Float(0.4)), '0.2'); | ||
compare(ctx.Float(0.4).sub(ctx.Integer(2)), '-1.6'); | ||
compare(ctx.Float(0.4).sub(ctx.Rational(1, 2)), '-0.1'); | ||
compare(ctx.Float('1.01', { radix: 2 }).sub('10.1'), '-1.25'); | ||
compare(ctx.Float('1.01', { radix: 2 }).sub(10.1), '-8.8501'); | ||
compare(ctx.Float('1.01', { radix: 2 }).sub(10.1), '-8.85'); | ||
}); | ||
@@ -110,4 +125,4 @@ | ||
compare(ctx.Float(6).mul(ctx.Float(2)), '12'); | ||
compare(ctx.Float(0.4).mul(ctx.Integer(2)), '0.800003'); | ||
compare(ctx.Float(0.4).mul(ctx.Rational(1, 2)), '0.200001'); | ||
compare(ctx.Float(0.4).mul(ctx.Integer(2)), '0.8'); | ||
compare(ctx.Float(0.4).mul(ctx.Rational(1, 2)), '0.2'); | ||
compare(ctx.Float('1.01', { radix: 2 }).mul('10.1'), '3.125'); | ||
@@ -126,3 +141,3 @@ compare(ctx.Float('1.01', { radix: 2 }).mul(10.1), '12.625'); | ||
compare(ctx.Float('1.01', { radix: 2 }).div('10.1'), '0.5'); | ||
compare(ctx.Float('1.01', { radix: 2 }).div(10.1), '0.123762'); | ||
compare(ctx.Float('1.01', { radix: 2 }).div(10.1), '0.12376237623762'); | ||
}); | ||
@@ -132,3 +147,3 @@ | ||
compare(ctx.Float(4).sqrt(), '2'); | ||
compare(ctx.Float(2).sqrt(), '1.41422'); | ||
compare(ctx.Float(2).sqrt(), '1.414213562373095048'); | ||
}); | ||
@@ -138,3 +153,3 @@ | ||
compare(ctx.Float(4).invSqrt(), '0.5'); | ||
compare(ctx.Float(2).invSqrt(), '0.707108'); | ||
compare(ctx.Float(2).invSqrt(), '0.7071067811865'); | ||
}); | ||
@@ -144,8 +159,8 @@ | ||
compare(ctx.Float(27).cbrt(), '3'); | ||
compare(ctx.Float(3).cbrt(), '1.44226'); | ||
compare(ctx.Float(3).cbrt(), '1.4422495703074083'); | ||
}); | ||
test('nthRoot()', () => { | ||
compare(ctx.Float(2).nthRoot(2), '1.41422'); | ||
compare(ctx.Float(3).nthRoot(3), '1.44226'); | ||
compare(ctx.Float(2).nthRoot(2), '1.414213562373095048'); | ||
compare(ctx.Float(3).nthRoot(3), '1.442249570307408382'); | ||
}); | ||
@@ -275,3 +290,3 @@ | ||
test('exponentials', () => { | ||
compare(ctx.Float(1).exp(), '2.71826'); | ||
compare(ctx.Float(1).exp(), '2.718281828459'); | ||
compare(ctx.Float(3).exp2(), '8'); | ||
@@ -292,6 +307,6 @@ compare(ctx.Float(3).exp10(), '1000'); | ||
compare(ctx.Pi().div(6).sin(), '0.5'); | ||
compare(ctx.Pi().div(4).sin(), ctx.Float(2).sqrt().div(2).toString()); | ||
compare(ctx.Pi().div(3).sin(), ctx.Float(3).sqrt().div(2).toString()); | ||
compare(ctx.Pi({precisionBits: 54}).div(4).sin(), '0.7071067811865475'); | ||
compare(ctx.Pi({precisionBits: 54}).div(3).sin(), '0.8660254037844386'); | ||
compare(ctx.Pi().div(2).sin(), '1'); | ||
compare(ctx.Pi().mul(4).sin(), '0.0000356352'); | ||
compare(ctx.Pi({precisionBits: 54}).mul(4).sin(), '0'); | ||
}); | ||
@@ -301,6 +316,6 @@ | ||
compare(ctx.Float(0).cos(), '1'); | ||
compare(ctx.Pi().div(6).cos(), ctx.Float(3).sqrt().div(2).toString()); | ||
compare(ctx.Pi().div(4).cos(), ctx.Float(2).sqrt().div(2).toString()); | ||
compare(ctx.Pi().div(3).cos(), '0.499992'); | ||
compare(ctx.Pi().div(2).cos(), '-0.0000044544'); | ||
compare(ctx.Pi({precisionBits: 54}).div(6).cos(), '0.8660254037844386'); | ||
compare(ctx.Pi({precisionBits: 54}).div(4).cos(), '0.7071067811865475'); | ||
compare(ctx.Pi({precisionBits: 54}).div(3).cos(), '0.5'); | ||
compare(ctx.Pi().div(2).cos(), '0'); | ||
compare(ctx.Pi().mul(4).cos(), '1'); | ||
@@ -311,5 +326,5 @@ }); | ||
compare(ctx.Float(0).tan(), '0'); | ||
compare(ctx.Pi().div(6).tan(), '0.577362'); | ||
compare(ctx.Pi().div(4).tan(), '1'); | ||
compare(ctx.Pi().div(3).tan(), '1.73212'); | ||
compare(ctx.Pi({precisionBits: 54}).div(6).tan(), '0.577350269189625764'); | ||
compare(ctx.Pi({precisionBits: 54}).div(4).tan(), '1'); | ||
compare(ctx.Pi({precisionBits: 54}).div(3).tan(), '1.7320508075688772935'); | ||
}); | ||
@@ -319,13 +334,13 @@ | ||
compare(ctx.Float(0).asin(), '0'); | ||
compare(ctx.Float(ctx.Float(1).div(2)).asin(), ctx.Pi().div(6).toString()); | ||
compare(ctx.Float(ctx.Float(2).sqrt().div(2)).asin(), ctx.Pi().div(4).toString()); | ||
compare(ctx.Float(ctx.Float(3).sqrt().div(2)).asin(), ctx.Pi().div(3).toString()); | ||
compare(ctx.Float(1).asin(), ctx.Pi().div(2).toString()); | ||
compare(ctx.Float(ctx.Float(1).div(2)).asin(), ctx.Pi().div(6).toString(10)); | ||
compare(ctx.Float(ctx.Float(2).sqrt().div(2)).asin(), ctx.Pi().div(4).toString(10)); | ||
compare(ctx.Float(ctx.Float(3).sqrt().div(2)).asin(), ctx.Pi().div(3).toString(10)); | ||
compare(ctx.Float(1).asin(), ctx.Pi().div(2).toString(10)); | ||
}); | ||
test('acos()', () => { | ||
compare(ctx.Float(0).acos(), ctx.Pi().div(2).toString()); | ||
compare(ctx.Float(ctx.Float(1).div(2)).acos(), ctx.Pi().div(3).toString()); | ||
compare(ctx.Float(ctx.Float(2).sqrt().div(2)).acos(), ctx.Pi().div(4).toString()); | ||
compare(ctx.Float(ctx.Float(3).sqrt().div(2)).acos(), '0.52359'); | ||
compare(ctx.Float(0).acos(), ctx.Pi().div(2).toString(10)); | ||
compare(ctx.Float(ctx.Float(1).div(2)).acos(), ctx.Pi().div(3).toString(10)); | ||
compare(ctx.Float(ctx.Float(2).sqrt().div(2)).acos(), ctx.Pi().div(4).toString(10)); | ||
compare(ctx.Float(ctx.Float(3).sqrt().div(2)).acos(), '0.523598775598298873'); | ||
compare(ctx.Float(1).acos(), '0'); | ||
@@ -336,41 +351,41 @@ }); | ||
compare(ctx.Float(0).atan(), '0'); | ||
compare(ctx.Float(ctx.Float(3).sqrt().div(3)).atan(), '0.52359'); | ||
compare(ctx.Float(ctx.Float(1)).atan(), ctx.Pi().div(4).toString()); | ||
compare(ctx.Float(ctx.Float(3).sqrt()).atan(), ctx.Pi().div(3).toString()); | ||
compare(ctx.Float(ctx.Float(3).sqrt().div(3)).atan(), '0.523598775598298873'); | ||
compare(ctx.Float(ctx.Float(1)).atan(), ctx.Pi().div(4).toString(10)); | ||
compare(ctx.Float(ctx.Float(3).sqrt()).atan(), ctx.Pi().div(3).toString(10)); | ||
}); | ||
test('csc()', () => { | ||
compare(ctx.Float(ctx.Pi().div(3)).csc(), ctx.Float(3).sqrt().mul(2).div(3).toString()); | ||
compare(ctx.Float(ctx.Pi().div(3)).csc(), ctx.Float(3).sqrt().mul(2).div(3).toString(10)); | ||
}); | ||
test('sec()', () => { | ||
compare(ctx.Float(ctx.Pi().div(3)).sec(), '2.00006'); | ||
compare(ctx.Float(ctx.Pi().div(3)).sec(), '2'); | ||
}); | ||
test('cot()', () => { | ||
compare(ctx.Float(ctx.Pi().div(3)).cot(), '0.577332'); | ||
compare(ctx.Float(ctx.Pi().div(3)).cot(), '0.5773502691896'); | ||
}); | ||
test('sinh()', () => { | ||
compare(ctx.Float('1.5').sinh(), '2.12927'); | ||
compare(ctx.Float('1.5').sinh(), '2.129279455094817496'); | ||
}); | ||
test('cosh()', () => { | ||
compare(ctx.Float('1.5').cosh(), '2.35242'); | ||
compare(ctx.Float('1.5').cosh(), '2.35240961524324732576'); | ||
}); | ||
test('tanh()', () => { | ||
compare(ctx.Float('1.5').tanh(), '0.905151'); | ||
compare(ctx.Float('1.5').tanh(), '0.9051482536448664382423'); | ||
}); | ||
test('asinh()', () => { | ||
compare(ctx.Float('2.12927').asinh(), '1.5'); | ||
compare(ctx.Float('2.129279455094817496').asinh(), '1.5'); | ||
}); | ||
test('acosh()', () => { | ||
compare(ctx.Float('2.35242').acosh(), '1.5'); | ||
compare(ctx.Float('2.35240961524324732576').acosh(), '1.5'); | ||
}); | ||
test('atanh()', () => { | ||
compare(ctx.Float('0.905151').atanh(), '1.50003'); | ||
compare(ctx.Float('0.9051482536448664382423').atanh(), '1.5'); | ||
}); | ||
@@ -419,12 +434,12 @@ | ||
test('frac()', () => { | ||
compare(ctx.Float('1.234').frac(), '0.234009'); | ||
compare(ctx.Float('-1.234').frac(), '-0.234009'); | ||
compare(ctx.Float('2.23').frac(), '0.23'); | ||
compare(ctx.Float('-2.23').frac(), '-0.23'); | ||
}); | ||
test('nextBelow()', () => { | ||
compare(ctx.Float('1').nextBelow(), '0.999985'); | ||
compareExact(ctx.Float('1').nextBelow(), '0.99999994'); | ||
}); | ||
test('nextAbove()', () => { | ||
compare(ctx.Float('1').nextAbove(), '1.00003'); | ||
compareExact(ctx.Float('1').nextAbove(), '1.00000012'); | ||
}); | ||
@@ -499,4 +514,4 @@ | ||
const options = { precisionBits: 10, roundingMode }; | ||
expect(gmp.calculate(g => g.Float(1).div(3), {})).toBe('0.33333333333333337'); | ||
expect(gmp.calculate(g => g.Float(1, {}).div(3))).toBe('0.33333333333333337'); | ||
expect(gmp.calculate(g => g.Float(1).div(3), {})).toBe('0.33333333333333331'); | ||
expect(gmp.calculate(g => g.Float(1, {}).div(3))).toBe('0.33333333333333331'); | ||
expect(gmp.calculate(g => g.Float(1).div(3), options)).toBe('0.333'); | ||
@@ -516,20 +531,28 @@ expect(gmp.calculate(g => g.Float(1, options).div(3))).toBe('0.333'); | ||
// merge radix | ||
expect(gmp.calculate(g => g.Float('3', { radix: 4 }).div('21'),{})).toBe('0.111111111111111111111111112'); | ||
expect(gmp.calculate(g => g.Float('3').div('21'),{ radix: 4 })).toBe('0.111111111111111111111111112'); | ||
expect(gmp.calculate(g => g.Float('3').div(g.Float('21', { radix: 4 })),{})).toBe('0.111111111111111111111111112'); | ||
expect(gmp.calculate(g => g.Float('3', { radix: 4 }).div('21'),{})).toBe('0.111111111111111111111111111'); | ||
expect(gmp.calculate(g => g.Float('3').div('21'),{ radix: 4 })).toBe('0.111111111111111111111111111'); | ||
expect(gmp.calculate(g => g.Float('3').div(g.Float('21', { radix: 4 })),{})).toBe('0.111111111111111111111111111'); | ||
}); | ||
test('toString()', () => { | ||
const options = { precisionBits: 16 }; | ||
const optionsZero = { precisionBits: 16, roundingMode: FloatRoundingMode.ROUND_TO_ZERO }; | ||
expect(ctx.Pi(optionsZero).toString()).toBe('3.14154'); | ||
expect(ctx.Pi(optionsZero).toString(10)).toBe('3.14154'); | ||
expect(ctx.Pi(optionsZero).toString(2)).toBe('11.00100100001111'); | ||
expect(ctx.Pi(options).toString(5)).toBe('3.0323223'); | ||
}); | ||
test('FloatOptions constants', () => { | ||
const roundingMode = FloatRoundingMode.ROUND_DOWN; | ||
const options = { precisionBits: 10, roundingMode }; | ||
expect(gmp.calculate(g => g.Pi(), options)).toBe('3.1406'); | ||
expect(gmp.calculate(g => g.Pi(options))).toBe('3.1406'); | ||
expect(gmp.calculate(g => g.Catalan(), options)).toBe('0.91503'); | ||
expect(gmp.calculate(g => g.Catalan(options))).toBe('0.91503'); | ||
expect(gmp.calculate(g => g.EulerConstant(), options)).toBe('0.57714'); | ||
expect(gmp.calculate(g => g.EulerConstant(options))).toBe('0.57714'); | ||
expect(gmp.calculate(g => g.EulerNumber(), options)).toBe('2.7148'); | ||
expect(gmp.calculate(g => g.EulerNumber(options))).toBe('2.7148'); | ||
expect(gmp.calculate(g => g.Log2(), options)).toBe('0.69238'); | ||
expect(gmp.calculate(g => g.Log2(options))).toBe('0.69238'); | ||
const options = { precisionBits: 15 }; | ||
expect(gmp.calculate(g => g.Pi(), options)).toBe('3.1416'); | ||
expect(gmp.calculate(g => g.Pi(options))).toBe('3.1416'); | ||
expect(gmp.calculate(g => g.Catalan(), options)).toBe('0.915955'); | ||
expect(gmp.calculate(g => g.Catalan(options))).toBe('0.915955'); | ||
expect(gmp.calculate(g => g.EulerConstant(), options)).toBe('0.577209'); | ||
expect(gmp.calculate(g => g.EulerConstant(options))).toBe('0.577209'); | ||
expect(gmp.calculate(g => g.EulerNumber(), options)).toBe('2.71826'); | ||
expect(gmp.calculate(g => g.EulerNumber(options))).toBe('2.71826'); | ||
expect(gmp.calculate(g => g.Log2(), options)).toBe('0.693146'); | ||
expect(gmp.calculate(g => g.Log2(options))).toBe('0.693146'); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
57
3644579
29609