Added typing for big.js library

This commit is contained in:
Steve Ognibene 2014-07-20 09:10:21 -04:00
parent 5326ecda45
commit 4227be18c1
2 changed files with 433 additions and 0 deletions

233
big.js/big.js-tests.ts Normal file
View File

@ -0,0 +1,233 @@
// Type definitions for big.js
// Project: https://github.com/MikeMcl/big.js/
// Definitions by: Steve Ognibene <https://github.com/nycdotnet/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
///<reference path="big.js.d.ts" />
/*
Tests include code from http://mikemcl.github.io/big.js/
Minor changes have been made such as adding variable definitions where required.
*/
function constructorTests() {
var x = new Big(9) // '9'
var y = new Big(x) // '9'
var d = Big(435.345) // 'new' is optional
var e = Big('435.345') // 'new' is optional
var a = new Big('5032485723458348569331745.33434346346912144534543')
var b = new Big('4.321e+4') // '43210'
var c = new Big('-735.0918e-430') // '-7.350918e-428'
}
function staticPropertiesTests() {
Big.DP = 40;
Big.RM = 3;
Big.RM = BigJsLibrary.RoundingMode.RoundAwayFromZero;
}
function absTests() {
var x = new Big(-0.8);
x.abs(); // '0.8'
}
function cmpTests() {
var x = new Big(6);
var y = new Big(5);
x.cmp(y); // 1
y.cmp(x.minus(1)); // 0
}
function divTests() {
var x = new Big(355);
var y = new Big(113);
x.div(y); // '3.14159292035398230088'
Big.DP = 2;
x.div(y); // '3.14'
x.div(5); // '71'
}
function eqTests() {
0 === 1e-324; // true
var x = new Big(0);
x.eq('1e-324'); // false
Big(-0).eq(x); // true ( -0 === 0 )
}
function gtTests() {
0.1 > 0.3 - 0.2; // true
var x = new Big(0.1);
x.gt(Big(0.3).minus(0.2)); // false
Big(0).gt(x); // false
}
function gteTests() {
0.3 - 0.2 >= 0.1; // false
var x = new Big(0.3).minus(0.2);
x.gte(0.1); // true
Big(1).gte(x); // true
}
function ltTests() {
0.3 - 0.2 < 0.1; // true
var x = new Big(0.3).minus(0.2);
x.lt(0.1); // false
Big(0).lt(x); // true
}
function lteTests() {
0.1 <= 0.3 - 0.2; // false
var x = new Big(0.1);
x.lte(Big(0.3).minus(0.2)); // true
Big(-1).lte(x); // true
}
function minusTests() {
0.3 - 0.1; // 0.19999999999999998
var x = new Big(0.3);
x.minus(0.1); // '0.2'
}
function modTests() {
1 % 0.9 // 0.09999999999999998
var x = Big(1);
x.mod(0.9) // '0.1'
}
function plusTests() {
0.1 + 0.2 // 0.30000000000000004
var x = new Big(0.1)
var y = x.plus(0.2) // '0.3'
Big(0.7).plus(x).plus(y) // '1'
}
function powTests() {
Math.pow(0.7, 2) // 0.48999999999999994
var x = new Big(0.7)
x.pow(2) // '0.49'
Big.DP = 20
Big(3).pow(-2) // '0.11111111111111111111'
new Big(123.456).pow(1000).toString().length // 5099
new Big(2).pow(1e+6) // Time taken (Node.js): 9 minutes 34 secs.
}
function roundTests() {
var x = 123.45
Math.round(x) // 123
var y = new Big(x)
y.round() // '123'
y.round(2) // '123.45'
y.round(10) // '123.45'
y.round(1, 0) // '123.4'
y.round(1, 1) // '123.5'
y.round(1, 2) // '123.4'
y.round(1, 3) // '123.5'
y // '123.45'
}
function sqrtTests() {
var x = new Big(16)
x.sqrt() // '4'
var y = new Big(3)
y.sqrt() // '1.73205080756887729353'
}
function timesTests() {
0.6 * 3 // 1.7999999999999998
var x = new Big(0.6)
var y = x.times(3) // '1.8'
Big('7e+500').times(y) // '1.26e+501'
}
function toExponentialTests() {
var x = 45.6
var y = new Big(x)
x.toExponential() // '4.56e+1'
y.toExponential() // '4.56e+1'
x.toExponential(0) // '5e+1'
y.toExponential(0) // '5e+1'
x.toExponential(1) // '4.6e+1'
y.toExponential(1) // '4.6e+1'
x.toExponential(3) // '4.560e+1'
y.toExponential(3) // '4.560e+1'
}
function toFixedTests() {
var x = 45.6
var y = new Big(x)
x.toFixed() // '46'
y.toFixed() // '45.6'
y.toFixed(0) // '46'
x.toFixed(3) // '45.600'
y.toFixed(3) // '45.600'
}
function toPrecisionTests() {
var x = 45.6
var y = new Big(x)
x.toPrecision() // '45.6'
y.toPrecision() // '45.6'
x.toPrecision(1) // '5e+1'
y.toPrecision(1) // '5e+1'
x.toPrecision(5) // '45.600'
y.toPrecision(5) // '45.600'
}
function toStringTests() {
var x = new Big('9.99e+20')
x.toString() // '999000000000000000000'
var y = new Big('1E21')
x.toString() // '1e+21'
}
function valueOfTests() {
var x = new Big('177.7e+457')
x.valueOf() // '1.777e+459'
}
function toJSONTests() {
var x = new Big('177.7e+457')
var y = new Big(235.4325)
var z = new Big('0.0098074')
var str = JSON.stringify([x, y, z])
var a = new Big('123').toJSON();
JSON.parse(str, function (k, v) { return k === '' ? v : new Big(v) }) // Returns an array of three Big numbers.
}
function propertiesTest1() {
var x = new Big(0.123) // '0.123'
x.toExponential() // '1.23e-1'
x.c // '1,2,3'
x.e // -1
x.s // 1
var y = new Number(-123.4567000e+2) // '-12345.67'
y.toExponential() // '-1.234567e+4'
var z = new Big('-123.4567000e+2') // '-12345.67'
z.toExponential() // '-1.234567e+4'
z.c // '1,2,3,4,5,6,7'
z.e // 4
z.s // -1
}
function propertiesTest2() {
var x = new Big('1234.000') // '1234'
x.toExponential() // '1.234e+3'
x.c // '1,2,3,4'
x.e // 3
x.e = -5
x // '0.00001234'
}
function propertiesTest3() {
var y = new Big(-0) // '0'
y.c // '0' [0].toString()
y.e // 0
y.s // -1
}

200
big.js/big.js.d.ts vendored Normal file
View File

@ -0,0 +1,200 @@
// Type definitions for big.js
// Project: https://github.com/MikeMcl/big.js/
// Definitions by: Steve Ognibene <https://github.com/nycdotnet/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module BigJsLibrary {
export enum RoundingMode {
RoundTowardsZero = 0,
RoundTowardsNearestAwayFromZero = 1,
RoundTowardsNearestTowardsEven = 2,
RoundAwayFromZero = 3
}
interface BigJS extends BigJS_Constructors {
/** The maximum number of decimal places of the results of operations involving division.
It is relevant only to the div and sqrt methods, and the pow method when the exponent is negative.
@default 40 */
DP: number;
/** The rounding mode used in the above operations and by round, toExponential, toFixed and toPrecision.
Default is RoundTowardsNearestAwayFromZero
@default 1 */
RM: RoundingMode;
}
interface BigJS_Constructors {
/** A decimal value. */
new (value: number): BigJS;
/** A decimal value.
String values may be in exponential, as well as normal (non-exponential) notation. There is no limit to the number of digits of a string value (other than that of Javascript's maximum array size), but the largest recommended exponent magnitude is 1e+6. Infinity, NaN and hexadecimal literal strings, e.g. '0xff', are not valid.
String values in octal literal form will be interpreted as decimals, e.g. '011' is 11, not 9. */
new (value: string): BigJS;
/** A decimal value. */
new (value: BigJS): BigJS;
/** A decimal value. */
(value: number): BigJS;
/** A decimal value.
String values may be in exponential, as well as normal (non-exponential) notation. There is no limit to the number of digits of a string value (other than that of Javascript's maximum array size), but the largest recommended exponent magnitude is 1e+6. Infinity, NaN and hexadecimal literal strings, e.g. '0xff', are not valid.
String values in octal literal form will be interpreted as decimals, e.g. '011' is 11, not 9. */
(value: string): BigJS;
/** A decimal value. */
(value: BigJS): BigJS;
}
/** BigJS instance methods */
interface BigJS extends BigJS_Constructors {
/** Returns a Big number whose value is the absolute value, i.e. the magnitude, of this Big number. */
abs(): BigJS;
/** Compare
@returns {Number}
1 = If the value of this Big number is greater than the value of n
-1 = If the value of this Big number is less than the value of n
0 = If this Big number and n have the same value */
cmp(n: number): number;
/** Compare
@returns {Number}
1 = If the value of this Big number is greater than the value of n
-1 = If the value of this Big number is less than the value of n
0 = If this Big number and n have the same value */
cmp(n: string): number;
/** Compare
@returns {Number}
1 = If the value of this Big number is greater than the value of n
-1 = If the value of this Big number is less than the value of n
0 = If this Big number and n have the same value */
cmp(n: BigJS): number;
/** Returns a Big number whose value is the value of this Big number divided by n. */
div(n: number): BigJS;
/** Returns a Big number whose value is the value of this Big number divided by n. */
div(n: string): BigJS;
/** Returns a Big number whose value is the value of this Big number divided by n. */
div(n: BigJS): BigJS;
/** Returns true if the value of this Big equals the value of n, otherwise returns false. */
eq(n: number): boolean;
/** Returns true if the value of this Big equals the value of n, otherwise returns false. */
eq(n: string): boolean;
/** Returns true if the value of this Big equals the value of n, otherwise returns false. */
eq(n: BigJS): boolean;
/** Returns true if the value of this Big is greater than the value of n, otherwise returns false. */
gt(n: number): boolean;
/** Returns true if the value of this Big is greater than the value of n, otherwise returns false. */
gt(n: string): boolean;
/** Returns true if the value of this Big is greater than the value of n, otherwise returns false. */
gt(n: BigJS): boolean;
/** Returns true if the value of this Big is greater than or equal to the value of n, otherwise returns false. */
gte(n: number): boolean;
/** Returns true if the value of this Big is greater than or equal to the value of n, otherwise returns false. */
gte(n: string): boolean;
/** Returns true if the value of this Big is greater than or equal to the value of n, otherwise returns false. */
gte(n: BigJS): boolean;
/** Returns true if the value of this Big is less than the value of n, otherwise returns false. */
lt(n: number): boolean;
/** Returns true if the value of this Big is less than the value of n, otherwise returns false. */
lt(n: string): boolean;
/** Returns true if the value of this Big is less than the value of n, otherwise returns false. */
lt(n: BigJS): boolean;
/** Returns true if the value of this Big is less than or equal to the value of n, otherwise returns false. */
lte(n: number): boolean;
/** Returns true if the value of this Big is less than or equal to the value of n, otherwise returns false. */
lte(n: string): boolean;
/** Returns true if the value of this Big is less than or equal to the value of n, otherwise returns false. */
lte(n: BigJS): boolean;
/** Returns a Big number whose value is the value of this Big number minus n. */
minus(n: number): BigJS;
/** Returns a Big number whose value is the value of this Big number minus n. */
minus(n: string): BigJS;
/** Returns a Big number whose value is the value of this Big number minus n. */
minus(n: BigJS): BigJS;
/** Returns a Big number whose value is the value of this Big number modulo n, i.e. the integer remainder of dividing this Big number by n.
The result will have the same sign as this Big number, and it will match that of Javascript's % operator (within the limits of its precision) and BigDecimal's remainder method. */
mod(n: number): BigJS;
/** Returns a Big number whose value is the value of this Big number modulo n, i.e. the integer remainder of dividing this Big number by n.
The result will have the same sign as this Big number, and it will match that of Javascript's % operator (within the limits of its precision) and BigDecimal's remainder method. */
mod(n: string): BigJS;
/** Returns a Big number whose value is the value of this Big number modulo n, i.e. the integer remainder of dividing this Big number by n.
The result will have the same sign as this Big number, and it will match that of Javascript's % operator (within the limits of its precision) and BigDecimal's remainder method. */
mod(n: BigJS): BigJS;
/** Returns a Big number whose value is the value of this Big number plus n. */
plus(n: number): BigJS;
/** Returns a Big number whose value is the value of this Big number plus n. */
plus(n: string): BigJS;
/** Returns a Big number whose value is the value of this Big number plus n. */
plus(n: BigJS): BigJS;
/** Returns a Big number whose value is the value of this Big number raised to the power exp.
If exp is negative and the result has more fraction digits than is specified by Big.DP, it will be rounded to Big.DP decimal places using rounding mode Big.RM.
@param exp integer, -1e+6 to 1e+6 inclusive */
pow(exp: number): BigJS;
/** Returns a Big number whose value is the value of this Big number rounded to a whole number. */
round(): BigJS;
/** Returns a Big number whose value is the value of this Big number rounded using rounding mode rm to a maximum of dp decimal places.
@param dp Number of decimal places (0 to 1e+6 inclusive). If dp is omitted or is null or undefined, the return value is n rounded to a whole number. */
round(dp: number): BigJS;
/** Returns a Big number whose value is the value of this Big number rounded using rounding mode rm to a maximum of dp decimal places.
@param dp Number of decimal places (0 to 1e+6 inclusive). If dp is omitted or is null or undefined, the return value is n rounded to a whole number.
@param rm Rounding mode. If rm is omitted or is null or undefined, the current Big.RM setting is used. */
round(dp: number, rm: RoundingMode): BigJS;
/** Returns a Big number whose value is the square root of this Big number. */
sqrt(): BigJS;
/** Returns a Big number whose value is the value of this Big number times n. */
times(n: number): BigJS;
/** Returns a Big number whose value is the value of this Big number times n. */
times(n: string): BigJS;
/** Returns a Big number whose value is the value of this Big number times n. */
times(n: BigJS): BigJS;
/** Returns a string representing the value of this Big number in exponential notation to a fixed number of decimal places dp. */
toExponential(): string;
/** Returns a string representing the value of this Big number in exponential notation to a fixed number of decimal places dp.
@param dp Number of decimal places (0 to 1e+6 inclusive). If dp is omitted, or is null or undefined, the number of digits after the decimal point defaults to the minimum number of digits necessary to represent the value exactly. */
toExponential(dp: number): string;
/** Returns a string representing the value of this Big number in normal notation to a fixed number of decimal places dp. */
toFixed(): string;
/** Returns a string representing the value of this Big number in normal notation to a fixed number of decimal places dp.
@param dp Number of decimal places (0 to 1e+6 inclusive). If dp is omitted, or is null or undefined, then the return value is simply the value in normal notation. This is also unlike Number.prototype.toFixed, which returns the value to zero decimal places. */
toFixed(dp: number): string;
/** Returns a string representing the value of this Big number to the specified number of significant digits sd. */
toPrecision(): string;
/** Returns a string representing the value of this Big number to the specified number of significant digits sd.
@param sd significant digits. If sd is omitted, or is null or undefined, then the return value is the same as .toString(). */
toPrecision(sd: number /** number of significant digits (0 to 1e+6 inclusive) */): string;
/** Returns a string representing the value of this Big number. */
toString(): string;
/** As toString. */
valueOf(): string;
/** As toString. */
toJSON(): string;
/** coefficient (significand) */
c: number[];
/** exponent (Integer, -1e+6 to 1e+6 inclusive) */
e: number;
/** sign (-1 or 1) */
s: number;
}
}
declare var Big: BigJsLibrary.BigJS;