atomic-fns
Version:
Like Lodash, but for ESNext and with types. Stop shipping code built for browsers from 2015.
150 lines (149 loc) • 3.93 kB
TypeScript
/**
* `Decimal` provides support for correct rounded floating point arithmetic.
*
* @module decimal
*/
/**
* Casts the given value as a {@link Decimal}.
*/
export declare function decimal(x: any): Decimal;
/**
* `Decimal` provides support for correct rounded floating point arithmetic, unlike the standard
* `Number` type. It also supports user-defined precision (default is 20 decimal places), which
* can be as large as needed.
*/
export declare class Decimal {
static PRECISION: number;
private i;
private e;
/**
* Returns a new `Decimal` instance from the value or `0`.
* @param {*} value
* @returns {Decimal}
*/
constructor(value?: any);
/**
* Returns a new `Decimal` that has the opposite sign of this `Decimal`.
* @example
```js
let a = decimal('5')
a.negated() // -5
```
* @returns {Decimal}
*/
negated(): Decimal;
/**
* Returns a new `Decimal` that is the sum of this and `x`.
* @param x
* @example
```js
let a = decimal('50000000000')
let b = decimal('0.000000005')
a.add(b) // 50000000000.000000005
```
* @returns {Decimal}
*/
add(x: any): Decimal;
/**
* Returns a new `Decimal` that is the difference between this and `x`.
* @param x
* @example
```js
let a = decimal('1')
let b = decimal('0.0000000000000000001')
a.sub(b) // 0.9999999999999999999
```
* @returns {Decimal}
*/
sub(x: any): Decimal;
/**
* Returns a new `Decimal` that is the product of this and `x`.
* @param x
* @example
```js
let a = decimal('0.0000000000000000000025')
let b = decimal('400000000000000000000')
a.mul(b) // 1
```
* @returns {Decimal}
*/
mul(x: any): Decimal;
/**
* Returns a new `Decimal` that is the quotient of this and `x`
* @param x
* @example
```js
let a = decimal('1')
let b = decimal('3')
a.div(b) // 0.333333333333333333333333333333
```
* @returns {Decimal}
*/
div(x: any): Decimal;
/**
* Returns the square root of this `Decimal`.
* @returns {TDecimal}
*/
sqrt(): Decimal;
/** Returns a new copy of this Decimal value. */
clone(): Decimal;
/**
* Remove zeroes in the least-significant digit of this `Decimal`
*/
private _normalize;
/**
* Truncate this `Decimal` to the configured precision
*/
private _truncate;
/**
* Returns a string representation of this decimal.
* @returns {Decimal}
*/
toString(): string;
/**
* Converts this decimal to the `Number` value
* @returns {number}
*/
toNumber(): number;
static get precision(): number;
static set precision(value: number);
/**
* Returns `true` if this value is equal to the value of `x`, otherwise `false`.
* @param {*} x other
* @returns {boolean}
*/
eq(x: any): boolean;
/**
* Returns `true` if this value is less than the value of `x`, otherwise `false`.
* @param {*} x
* @returns {boolean}
*/
lt(x: any): boolean;
/**
* Returns a comparison value representing the ordering of this value in respect to `x`.
* * `-1` if this < x
* * `1` if this > x
* * `0` if this == x
* @param {*} x
* @returns {number}
*/
compare(x: any): 1 | -1 | 0;
/**
* Returns `true` if this value is greater than the value of `x`, otherwise `false`.
* @param {*} x
* @returns {boolean}
*/
gt(x: any): boolean;
/**
* Returns `true` if this value is less than or equal to the value of `x`, otherwise `false`.
* @param {*} x
* @returns {boolean}
*/
lte(x: any): boolean;
/**
* Returns `true` if this value is greater than or equal to the value of `x`, otherwise `false`.
* @param {*} x
* @returns {boolean}
*/
gte(x: any): boolean;
}