Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow creating BigNumber with {s, e, c}
  • Loading branch information
iamdoron committed Feb 12, 2019
1 parent bde4ac1 commit 5606fa9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
26 changes: 22 additions & 4 deletions bignumber.d.ts
Expand Up @@ -109,7 +109,7 @@ export namespace BigNumber {
* Calling `toString` with a base argument, e.g. `toString(10)`, will also always return normal
* notation.
*/
EXPONENTIAL_AT?: number|[number, number];
EXPONENTIAL_AT?: number | [number, number];

/**
* An integer, magnitude 1 to 1e+9, or an array, [-1e+9 to -1, 1 to 1e+9].
Expand Down Expand Up @@ -144,7 +144,7 @@ export namespace BigNumber {
* The largest possible magnitude of a finite BigNumber is 9.999...e+1000000000.
* The smallest possible magnitude of a non-zero BigNumber is 1e-1000000000.
*/
RANGE?: number|[number, number];
RANGE?: number | [number, number];

/**
* A boolean: `true` or `false`. Default value: `false`.
Expand Down Expand Up @@ -330,10 +330,28 @@ export namespace BigNumber {
suffix?: string;
}

export interface BigNumberData {
/**
* The coefficient of the value of this BigNumber, an array of base 1e14 integer numbers.
*/
readonly c: number[];

/**
* The exponent of the value of this BigNumber, an integer number, -1000000000 to 1000000000.
*/
readonly e: number;

/**
* The sign of the value of this BigNumber, -1 or 1.
*/
readonly s: number;

[key: string]: any;
}
export type Instance = BigNumber;
export type ModuloMode = 0 | 1 | 3 | 6 | 9;
export type RoundingMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
export type Value = string | number | BigNumber;
export type Value = string | number | BigNumber | BigNumberData;
}

export declare class BigNumber {
Expand Down Expand Up @@ -1057,7 +1075,7 @@ export declare class BigNumber {
* @param n A numeric value.
* @param [base] The base of n.
*/
multipliedBy(n: BigNumber.Value, base?: number) : BigNumber;
multipliedBy(n: BigNumber.Value, base?: number): BigNumber;

/**
* Returns a BigNumber whose value is the value of this BigNumber multiplied by `n`.
Expand Down
3 changes: 2 additions & 1 deletion bignumber.js
Expand Up @@ -195,7 +195,8 @@
if (b == null) {

// Duplicate.
if (n instanceof BigNumber) {
if (n instanceof BigNumber
|| (n != null && n.s != null && n.e != null && n.c != null)) {
x.s = n.s;
x.e = n.e;
x.c = (n = n.c) ? n.slice() : n;
Expand Down
6 changes: 5 additions & 1 deletion doc/API.html
Expand Up @@ -185,7 +185,7 @@ <h5 id="bignumber">
BigNumber<code class='inset'>BigNumber(n [, base]) <i>&rArr; BigNumber</i></code>
</h5>
<p>
<code>n</code>: <i>number|string|BigNumber</i><br />
<code>n</code>: <i>number|string|BigNumber|BigNumberData</i><br />
<code>base</code>: <i>number</i>: integer, <code>2</code> to <code>36</code> inclusive. (See
<a href='#alphabet'><code>ALPHABET</code></a> to extend this range).
</p>
Expand Down Expand Up @@ -268,6 +268,10 @@ <h5 id="bignumber">
new BigNumber(823456789123456.3)
// '[BigNumber Error] Not a base 2 number'
new BigNumber(9, 2)</pre>
<p>
As a way to interact with other modules that are using BigNumber you can use BigNumberData directly
</p>
<pre>new BigNumber({ s: 1, e: 2, c: [ 777, 12300000000000 ] }) // '777.123'</pre>



Expand Down
3 changes: 3 additions & 0 deletions test/methods/BigNumber.js
Expand Up @@ -199,6 +199,9 @@ Test('bigNumber', function () {
t('-102', new BigNumber('-0o146').toString());
t('0.5', new BigNumber('0o0.4').toString());

t('100002222.2222333322', new BigNumber({ s: 1, e: 8, c: [100002222, 22223333220000] }).toString())
t('7777777777.123123123', new BigNumber({ s: 1, e: 9, c: [7777777777, 12312312300000] }).toString())

// Base-conversion tests

//var alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_';
Expand Down

0 comments on commit 5606fa9

Please sign in to comment.