UNPKG

decimal128

Version:

Partial implementation of IEEE 754 Decimal128 decimal floating-point numbers

110 lines (109 loc) 3.32 kB
/** * decimal128.js -- Decimal128 implementation in JavaScript * * The purpose of this module is to provide a userland implementation of * IEEE 758 Decimal128, which are exact decimal floating point numbers fit into * 128 bits. This library provides basic arithmetic operations (addition, multiplication). * It's main purpose is to help gather data and experience about using Decimal128 * in JavaScript programs. Speed is not a concern; the main goal is to simply * make Decimal128 values available in some form in JavaScript. In the future, * JavaScript may get exact decimal numbers as a built-in data type, which will * surely be much faster than what this library can provide. * * @author Jesse Alama <jesse@igalia.com> */ import JSBI from "jsbi"; import { RoundingMode } from "./common.mjs"; import { Decimal } from "./Decimal.mjs"; export declare class Decimal128 { private readonly d; private readonly _isNaN; private readonly _isFinite; private readonly _isNegative; constructor(n: string | number | bigint | JSBI | Decimal); isNaN(): boolean; isFinite(): boolean; isNegative(): boolean; private cohort; private quantum; private isZero; exponent(): number; mantissa(): Decimal128; scale10(n: number): Decimal128; private coefficient; private emitExponential; private emitDecimal; /** * Returns a digit string representing this Decimal128. */ toString(): string; toFixed(opts?: { digits?: number; }): string; toPrecision(opts?: { digits?: number; }): string; toExponential(opts?: { digits?: number; }): string; private isInteger; toBigInt(): JSBI; toNumber(): number; /** * Compare two values. Return * * * NaN if either argument is a decimal NaN * + -1 if the mathematical value of this decimal is strictly less than that of the other, * + 0 if the mathematical values are equal, and * + 1 otherwise. * * @param x */ cmp(x: Decimal128): number; abs(): Decimal128; /** * Add this Decimal128 value to one or more Decimal128 values. * * @param x */ add(x: Decimal128): Decimal128; /** * Subtract another Decimal128 value from one or more Decimal128 values. * * @param x */ subtract(x: Decimal128): Decimal128; /** * Multiply this Decimal128 value by an array of other Decimal128 values. * * If no arguments are given, return this value. * * @param x */ multiply(x: Decimal128): Decimal128; private clone; /** * Divide this Decimal128 value by another Decimal128 value. * * @param x */ divide(x: Decimal128): Decimal128; /** * * @param numDecimalDigits * @param {RoundingMode} mode (default: ROUNDING_MODE_DEFAULT) */ round(numDecimalDigits?: number, mode?: RoundingMode): Decimal128; negate(): Decimal128; /** * Return the remainder of this Decimal128 value divided by another Decimal128 value. * * @param d * @throws RangeError If argument is zero */ remainder(d: Decimal128): Decimal128; isNormal(): boolean; isSubnormal(): boolean; truncatedExponent(): number; scaledSignificand(): JSBI; }