This commit is contained in:
Michael Mclaughlin 2014-12-29 00:24:38 +00:00
parent e8130405e4
commit bc66fcfed1
23 changed files with 3758 additions and 3328 deletions

View File

@ -1,2 +1,4 @@
test
perf
coverage

142
README.md
View File

@ -3,59 +3,61 @@
# bignumber.js #
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic.
*Note: This is version 2 of the library, for version 1.x.x see the tagged releases or switch to the 'original' branch. The advantages of version 2 are that it is considerably faster for numbers with many digits and that there are a few added methods (see Change Log below). The disadvantages are more lines of code and increased code complexity, and the loss of simplicity in no longer having the coefficient of a BigNumber stored in base 10. The 'original' version will continue to be supported.*
## Features
- Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
- 5 KB minified and gzipped
- 6 KB minified and gzipped
- Simple API but full-featured
- Works with numbers with or without fraction digits in bases from 2 to 64 inclusive
- Replicates the `toExponential`, `toFixed`, `toPrecision` and `toString` methods of JavaScript's Number type
- Includes a `toFraction` and a correctly-rounded `squareRoot` method
- No dependencies
- ECMAScript 3 compliant
- Comprehensive [documentation](http://mikemcl.github.io/bignumber.js/) and test set
- Comprehensive [documentation](http://mikemcl.github.io/bignumber.js/) and test set
If an even smaller and simpler library is required see [big.js](https://github.com/MikeMcl/big.js/).
It's half the size but only works with decimal numbers and only has half the methods.
If an even smaller and simpler library is required see [big.js](https://github.com/MikeMcl/big.js/).
It's half the size but only works with decimal numbers and only has half the methods.
It also does not allow `NaN` or `Infinity`, or have the configuration options of this library.
See also [decimal.js](https://github.com/MikeMcl/decimal.js/).
## Load
The library is the single JavaScript file *bignumber.js* (or minified, *bignumber.min.js*).
The library is the single JavaScript file *bignumber.js* (or minified, *bignumber.min.js*).
It can be loaded via a script tag in an HTML document for the browser
<script src='./relative/path/to/bignumber.js'></script>
or as a CommonJS, [Node.js](http://nodejs.org) or AMD module using `require`.
or as a CommonJS, [Node.js](http://nodejs.org) or AMD module using `require`.
For Node, put the *bignumber.js* file into the same directory as the file that is requiring it and use
var BigNumber = require('./bignumber.js');
var BigNumber = require('./bignumber.js');
or put it in a *node_modules* directory within the directory and use `require('bignumber.js')`.
or put it in a *node_modules* directory within the directory and use `require('bignumber.js')`.
The library is also available from the [npm](https://npmjs.org/) registry, so
$ npm install bignumber.js
will install this directory in a *node_modules* directory within the current directory.
To load with AMD loader libraries such as [requireJS](http://requirejs.org/):
require(['path/to/bignumber'], function(BigNumber) {
// Use BigNumber here in local scope. No global BigNumber.
// Use BigNumber here in local scope. No global BigNumber.
});
## Use
*In all examples below, `var`, semicolons and `toString` calls are not shown.
*In all examples below, `var`, semicolons and `toString` calls are not shown.
If a commented-out value is in quotes it means `toString` has been called on the preceding expression.*
The library exports a single function: BigNumber, the constructor of BigNumber instances.
It accepts a value of type Number *(up to 15 significant digits only)*, String or BigNumber Object,
The library exports a single function: BigNumber, the constructor of BigNumber instances.
It accepts a value of type Number *(up to 15 significant digits only)*, String or BigNumber Object,
x = new BigNumber(123.4567)
y = BigNumber('123456.7e-3') // 'new' is optional
@ -64,14 +66,14 @@ It accepts a value of type Number *(up to 15 significant digits only)*, String o
and a base from 2 to 64 inclusive can be specified.
x = new BigNumber(1011, 2) // "11"
x = new BigNumber(1011, 2) // "11"
y = new BigNumber('zz.9', 36) // "1295.25"
z = x.plus(y) // "1306.25"
A BigNumber is immutable in the sense that it is not changed by its methods.
0.3 - 0.1 // 0.19999999999999998
x = new BigNumber(0.3)
x = new BigNumber(0.3)
x.minus(0.1) // "0.2"
x // "0.3"
@ -80,14 +82,14 @@ The methods that return a BigNumber can be chained.
x.dividedBy(y).plus(z).times(9).floor()
x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil()
Method names over 5 letters in length have a shorter alias.
Many method names have a shorter alias.
x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3)) // true
x.cmp(y.mod(z).neg()) == 1 && x.comparedTo(y.modulo(z).negated()) == 1 // true
Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods
x = new BigNumber(255.5)
x = new BigNumber(255.5)
x.toExponential(5) // "2.55500e+2"
x.toFixed(5) // "255.50000"
x.toPrecision(5) // "255.50"
@ -97,14 +99,19 @@ Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPreci
x.toString(16) // "ff.8"
The maximum number of decimal places of, and the rounding mode applied to, the results of operations involving division (i.e. division, square root, base conversion, and negative power operations) is set by a configuration object passed to the `config` method of the `BigNumber` constructor.
There is also a `toFormat` method which may be useful for internationalisation
y = new BigNumber('1234567.898765')
y.toFormat(2) // "1,234,567.90"
The maximum number of decimal places of the result of an operation involving division (i.e. a division, square root, base conversion or negative power operation) is set using the `config` method of the `BigNumber` constructor.
The other arithmetic operations always give the exact result.
BigNumber.config({ DECIMAL_PLACES: 10, ROUNDING_MODE: 4 })
// Alternatively, BigNumber.config( 10, 4 );
x = new BigNumber(2);
y = new BigNumber(3);
y = new BigNumber(3);
z = x.div(y) // "0.6666666667"
z.sqrt() // "0.8164965809"
z.pow(-3) // "3.3749999995"
@ -127,57 +134,55 @@ and `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `BigNumber
The value of a BigNumber is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
x = new BigNumber(-123.456);
x.c // "1,2,3,4,5,6" coefficient (i.e. significand)
x.e // 2 exponent
x.s // -1 sign
x = new BigNumber(-123.456);
x.c // [ 123, 45600000000000 ] coefficient (i.e. significand)
x.e // 2 exponent
x.s // -1 sign
For futher information see the [API](http://mikemcl.github.io/bignumber.js/) reference from the *doc* folder.
## Test
The *test* directory contains the test scripts for each method.
The *test* directory contains the test scripts for each method.
The tests can be run with Node or a browser.
The tests can be run with Node or a browser. For Node use
For a quick test of all the methods, from a command-line shell at the *test/* directory
$ npm test
$ node quick-test
or
To test a single method in more depth, e.g.
$ node test/every-test
$ node toFraction
To test a single method
To test all the methods in more depth
$ node test/toFraction
$ node every-test
For the browser, see *every-test.html* and *single-test.html* in the *test/browser* directory.
For the browser, see *quick-test.html*, *single-test.html* and *every-test.html* in the *test/browser* directory.
*bignumber-vs-number.html* enables some of the methods of bignumber.js to be compared with those of JavaScript's Number type.
## Performance
The *perf* directory contains two applications and a *lib* directory containing the BigDecimal libraries used by both.
The *perf* directory contains two applications and a *lib* directory containing the BigDecimal libraries used by both.
*bignumber-vs-bigdecimal.html* tests the performance of bignumber.js against the JavaScript translations of two versions of BigDecimal, its use should be more or less self-explanatory.
(The GWT version doesn't work in IE 6.)
* GWT: java.math.BigDecimal
* GWT: java.math.BigDecimal
<https://github.com/iriscouch/bigdecimal.js>
* ICU4J: com.ibm.icu.math.BigDecimal
<https://github.com/dtrebbien/BigDecimal.js>
* ICU4J: com.ibm.icu.math.BigDecimal
<https://github.com/dtrebbien/BigDecimal.js>
The BigDecimal in Node's npm registry is the GWT version. Despite its seeming popularity I have found it to have some serious bugs, see the Node script *perf/lib/bigdecimal_GWT/bugs.js* for examples of flaws in its *remainder*, *divide* and *compareTo* methods.
The BigDecimal in Node's npm registry is the GWT version. Despite its seeming popularity I have found it to have some serious bugs, see the Node script *perf/lib/bigdecimal_GWT/bugs.js* for examples of flaws in its *remainder*, *divide* and *compareTo* methods.
*bigtime.js* is a Node command-line application which tests the performance of bignumber.js against the GWT version of BigDecimal from the npm registry.
For example, to compare the time taken by the bignumber.js `plus` method and the BigDecimal `add` method:
$ node bigtime plus 10000 40
This will time 10000 calls to each, using operands of up to 40 random digits and will check that the results match.
$ node bigtime plus 10000 40
This will time 10000 calls to each, using operands of up to 40 random digits and will check that the results match.
For help:
$ node bigtime -h
@ -188,20 +193,20 @@ See the README in the directory for more information.
I.e. minify.
For Node, if uglify-js is installed globally ( `npm install uglify-js -g` ) then
For Node, if uglify-js is installed globally ( `npm install uglify-js -g` ) then
npm run build
will create *bignumber.min.js*.
will create *bignumber.min.js*.
## Feedback
Open an issue, or email
Michael
Michael
<a href="mailto:M8ch88l@gmail.com">M8ch88l@gmail.com</a>
Bitcoin donation to:
Bitcoin donation
**1CauoGYrEoJFhcyxGVaiLTE6f3WCaSUjnm**
Thank you
@ -213,33 +218,50 @@ See LICENCE.
## Change Log
####2.0.0
* 29/12/2014
* Add `dividedToIntegerBy`, `isInteger` and `toFormat` methods.
* Remove the following short-forms: `isF`, `isZ`, `toE`, `toF`, `toFr`, `toN`, `toP`, `toS`.
* Store a BigNumber's coefficient in base 1e14, rather than base 10.
* Add fast path for integers to BigNumber constructor.
* Incorporate the library into the online documentation.
####1.5.0
* 13/11/2014 Added `toJSON` and `decimalPlaces` methods.
* 13/11/2014
* Add `toJSON` and `decimalPlaces` methods.
####1.4.1
* 08/06/2014 Amend README.
* 08/06/2014
* Amend README.
####1.4.0
* 08/05/2014 Added `toNumber`.
* 08/05/2014
* Add `toNumber`.
####1.3.0
* 08/11/2013 Ensure correct rounding of `sqrt` in all, rather than almost all, cases.
* 08/11/2013
* Ensure correct rounding of `sqrt` in all, rather than almost all, cases.
* Maximum radix to 64.
####1.2.1
* 17/10/2013 Sign of zero when x < 0 and x + (-x) = 0.
* 17/10/2013
* Sign of zero when x < 0 and x + (-x) = 0.
####1.2.0
* 19/9/2013 Throw Error objects for stack.
* 19/9/2013
* Throw Error objects for stack.
####1.1.1
* 22/8/2013 Show original value in constructor error message.
* 22/8/2013
* Show original value in constructor error message.
####1.1.0
* 1/8/2013 Allow numbers with trailing radix point.
* 1/8/2013
* Allow numbers with trailing radix point.
####1.0.1
* Bugfix: error messages with incorrect method name
* Bugfix: error messages with incorrect method name
####1.0.0
* 8/11/2012 Initial release
* 8/11/2012
* Initial release

File diff suppressed because it is too large Load Diff

4
bignumber.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{
"name": "bignumber.js",
"description": "A library for arbitrary-precision decimal and non-decimal arithmetic",
"version": "1.5.0",
"version": "2.0.0",
"keywords": [
"arbitrary",
"precision",
@ -31,6 +31,6 @@
"license": "MIT",
"scripts": {
"test": "node ./test/every-test.js",
"build": "uglifyjs bignumber.js -c -m -o bignumber.min.js --preamble '/* bignumber.js v1.5.0 https://github.com/MikeMcl/bignumber.js/LICENCE */'"
"build": "uglifyjs bignumber.js -c -m -o bignumber.min.js --preamble '/* bignumber.js v2.0.0 https://github.com/MikeMcl/bignumber.js/LICENCE */'"
}
}

View File

@ -370,6 +370,7 @@ var count = (function baseIn(BigNumber) {
BigNumber.config({DECIMAL_PLACES : 20});
assertException(function () {new BigNumber(1010101010101010, 2).toString()}, "(1010101010101010, 2)");
assertException(function () {new BigNumber('2', 0).toString()}, "('2', 0)");
assertException(function () {new BigNumber('2', 2).toString()}, "('2', 2)");
assertException(function () {new BigNumber('2', '-2').toString()}, "('2', '-2')");
@ -449,6 +450,11 @@ var count = (function baseIn(BigNumber) {
T('101725686101180', '101725686101180', undefined);
T('101725686101180', '101725686101180', 10);
BigNumber.config({ERRORS : true});
T('635356108986960269840155289238139379273453551922021969747464031737829471932451727645074552025138332075241803866047', '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_', 64);
T('64163.091552734375', 'fGz.5T', 64);
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];;
})(this.BigNumber);

View File

@ -105,51 +105,51 @@
}
try {
n = new BigNumber(2).toE(300.3);
n = new BigNumber(2).toExponential(300.3);
} catch (e) {
console.error(e + '')
} try {
n = new BigNumber(2).toE(1e99);
n = new BigNumber(2).toExponential(1e99);
} catch (e) {
console.error(e + '')
}
try {
n = new BigNumber(2).toF(300.3);
n = new BigNumber(2).toFixed(300.3);
} catch (e) {
console.error(e + '')
} try {
n = new BigNumber(2).toF(1e99);
n = new BigNumber(2).toFixed(1e99);
} catch (e) {
console.error(e + '')
}
try {
n = new BigNumber(2).toFr(300.3);
n = new BigNumber(2).toFraction(300.3);
} catch (e) {
console.error(e + '')
} try {
n = new BigNumber(2).toFr(-1);
n = new BigNumber(2).toFraction(-1);
} catch (e) {
console.error(e + '')
}
try {
n = new BigNumber(2).toP(300.3);
n = new BigNumber(2).toPrecision(300.3);
} catch (e) {
console.error(e + '')
} try {
n = new BigNumber(2).toP(0);
n = new BigNumber(2).toPrecision(0);
} catch (e) {
console.error(e + '')
}
try {
n = new BigNumber(2).toS(3.3);
n = new BigNumber(2).toString(3.3);
} catch (e) {
console.error(e + '')
} try {
n = new BigNumber(2).toS(1);
n = new BigNumber(2).toString(1);
} catch (e) {
console.error(e + '')
}

View File

@ -21,6 +21,7 @@
'cmp',
'config',
'div',
'divToInt',
'dp',
'floor',
'minus',
@ -50,7 +51,7 @@
document.body.scrollIntoView(false);
return;
}
script.onload = script.onreadystatechange = function () {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
if (!count) {

View File

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Testing bignumber.js</title>
<style> body {font-family: monospace; font-size: 12px; line-height: 14px;}</style>
<script src='../../bignumber.js'></script>
</head>
<body>
<script src='../quick-test.js'></script>
</body>
</html>

View File

@ -14,7 +14,8 @@
<!-- <script src='../config.js'></script> -->
<!-- <script src='../cmp.js'></script> -->
<!-- <script src='../div.js'></script> -->
<!-- <script src='../dp.js'></script> -->
<!-- <script src='../divToInt.js'></script> -->
<!-- <script src='../dp.js'></script> -->
<!-- <script src='../floor.js'></script> -->
<!-- <script src='../minus.js'></script> -->
<!-- <script src='../mod.js'></script> -->

View File

@ -203,6 +203,6 @@ var count = (function ceil(BigNumber) {
// ------------------------------------------------------------------ v8 end
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];;
return [passed, total];
})(this.BigNumber);
if (typeof module !== 'undefined' && module.exports) module.exports = count;

View File

@ -34,6 +34,25 @@ var count = (function cmp(BigNumber) {
}
}
function assertException(func, message) {
var actual;
total++;
try {
func();
} catch (e) {
actual = e;
}
if (actual && actual.name == 'BigNumber Error') {
passed++;
//log('\n Expected and actual: ' + actual);
} else {
error('\n Test number: ' + total + ' failed');
error('\n Expected: ' + message + ' to raise a BigNumber Error.');
error(' Actual: ' + (actual || 'no exception'));
//process.exit();
}
}
function T(a, b, expected) {
assert(String(expected), String(new BigNumber(a).cmp(b)));
assert(String(expected), String(new BigNumber(a).cmp(new BigNumber(b))));
@ -4121,6 +4140,9 @@ var count = (function cmp(BigNumber) {
T('-0.10021507', '-2049541544645617700923988306', 1);
T('6609143733354158875894', '-6609143733354158875894', 1);
BigNumber.config({ ERRORS : true });
assertException(function () {new BigNumber(1).cmp('one')}, "new BigNumber(1).cmp('one')");
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];;
})(this.BigNumber);

View File

@ -417,6 +417,31 @@ var count = (function config(BigNumber) {
assert(false, BigNumber.config({ERRORS : Boolean(false)}).ERRORS);
assert(true, BigNumber.config({ERRORS : Boolean('false')}).ERRORS);
// FORMAT
assertException(function () {BigNumber.config({FORMAT : ''})}, "FORMAT : ''");
assertException(function () {BigNumber.config({FORMAT : 1})}, "FORMAT : 1");
obj = {
decimalSeparator : '.',
groupSeparator : ',',
groupSize : 3,
secondaryGroupSize : 0,
fractionGroupSeparator : '\xA0',
fractionGroupSize : 0
};
assert(obj, BigNumber.config({FORMAT : obj}).FORMAT);
assert('.', BigNumber.config().FORMAT.decimalSeparator);
obj.decimalSeparator = ',';
assert(',', BigNumber.config().FORMAT.decimalSeparator);
assert(obj, BigNumber.config({FORMAT : null}).FORMAT);
BigNumber.config({ ERRORS : false });
assert(obj, BigNumber.config({FORMAT : 2}).FORMAT);
// Test constructor parsing.
BigNumber.config({
@ -427,19 +452,24 @@ var count = (function config(BigNumber) {
ERRORS : true
});
assert('NaN', new BigNumber(NaN).toS());
assert('NaN', new BigNumber('NaN').toS());
assert('NaN', new BigNumber(' NaN').toS());
assert('NaN', new BigNumber('NaN ').toS());
assert('NaN', new BigNumber(' NaN ').toS());
assert('NaN', new BigNumber('+NaN').toS());
assert('NaN', new BigNumber(' +NaN').toS());
assert('NaN', new BigNumber('+NaN ').toS());
assert('NaN', new BigNumber(' +NaN ').toS());
assert('NaN', new BigNumber('-NaN').toS());
assert('NaN', new BigNumber(' -NaN').toS());
assert('NaN', new BigNumber('-NaN ').toS());
assert('NaN', new BigNumber(' -NaN ').toS());
assert('Infinity', new BigNumber('1e10000000000').toString());
assert('-Infinity', new BigNumber('-1e10000000000').toString());
assert('0', new BigNumber('1e-10000000000').toString());
assert('0', new BigNumber('-1e-10000000000').toString());
assert('NaN', new BigNumber(NaN).toString());
assert('NaN', new BigNumber('NaN').toString());
assert('NaN', new BigNumber(' NaN').toString());
assert('NaN', new BigNumber('NaN ').toString());
assert('NaN', new BigNumber(' NaN ').toString());
assert('NaN', new BigNumber('+NaN').toString());
assert('NaN', new BigNumber(' +NaN').toString());
assert('NaN', new BigNumber('+NaN ').toString());
assert('NaN', new BigNumber(' +NaN ').toString());
assert('NaN', new BigNumber('-NaN').toString());
assert('NaN', new BigNumber(' -NaN').toString());
assert('NaN', new BigNumber('-NaN ').toString());
assert('NaN', new BigNumber(' -NaN ').toString());
assertException(function () {new BigNumber('+ NaN')}, "+ NaN");
assertException(function () {new BigNumber('- NaN')}, "- NaN");
@ -451,19 +481,19 @@ var count = (function config(BigNumber) {
assertException(function () {new BigNumber('-.NaN')}, "-.NaN");
assertException(function () {new BigNumber('+.NaN')}, "+.NaN");
assert('Infinity', new BigNumber(Infinity).toS());
assert('Infinity', new BigNumber('Infinity').toS());
assert('Infinity', new BigNumber(' Infinity').toS());
assert('Infinity', new BigNumber('Infinity ').toS());
assert('Infinity', new BigNumber(' Infinity ').toS());
assert('Infinity', new BigNumber('+Infinity').toS());
assert('Infinity', new BigNumber(' +Infinity').toS());
assert('Infinity', new BigNumber('+Infinity ').toS());
assert('Infinity', new BigNumber(' +Infinity ').toS());
assert('-Infinity', new BigNumber('-Infinity').toS());
assert('-Infinity', new BigNumber(' -Infinity').toS());
assert('-Infinity', new BigNumber('-Infinity ').toS());
assert('-Infinity', new BigNumber(' -Infinity ').toS());
assert('Infinity', new BigNumber(Infinity).toString());
assert('Infinity', new BigNumber('Infinity').toString());
assert('Infinity', new BigNumber(' Infinity').toString());
assert('Infinity', new BigNumber('Infinity ').toString());
assert('Infinity', new BigNumber(' Infinity ').toString());
assert('Infinity', new BigNumber('+Infinity').toString());
assert('Infinity', new BigNumber(' +Infinity').toString());
assert('Infinity', new BigNumber('+Infinity ').toString());
assert('Infinity', new BigNumber(' +Infinity ').toString());
assert('-Infinity', new BigNumber('-Infinity').toString());
assert('-Infinity', new BigNumber(' -Infinity').toString());
assert('-Infinity', new BigNumber('-Infinity ').toString());
assert('-Infinity', new BigNumber(' -Infinity ').toString());
assertException(function () {new BigNumber('+ Infinity')}, "+ Infinity");
assertException(function () {new BigNumber(' + Infinity')}, " + Infinity");
@ -476,24 +506,24 @@ var count = (function config(BigNumber) {
assertException(function () {new BigNumber('-.Infinity')}, "-.Infinity");
assertException(function () {new BigNumber('+.Infinity')}, "+.Infinity");
assert('0', new BigNumber(0).toS());
assert('0', new BigNumber(-0).toS());
assert('0', new BigNumber('.0').toS());
assert('0', new BigNumber('0.').toS());
assert('0', new BigNumber('-0.').toS());
assert('0', new BigNumber('+0.').toS());
assert('0', new BigNumber('+0').toS());
assert('0', new BigNumber('-0').toS());
assert('0', new BigNumber(' +0').toS());
assert('0', new BigNumber(' -0').toS());
assert('0', new BigNumber(' +0 ').toS());
assert('0', new BigNumber(' -0 ').toS());
assert('0', new BigNumber('+.0').toS());
assert('0', new BigNumber('-.0').toS());
assert('0', new BigNumber(' +.0').toS());
assert('0', new BigNumber(' -.0').toS());
assert('0', new BigNumber(' +.0 ').toS());
assert('0', new BigNumber(' -.0 ').toS());
assert('0', new BigNumber(0).toString());
assert('0', new BigNumber(-0).toString());
assert('0', new BigNumber('.0').toString());
assert('0', new BigNumber('0.').toString());
assert('0', new BigNumber('-0.').toString());
assert('0', new BigNumber('+0.').toString());
assert('0', new BigNumber('+0').toString());
assert('0', new BigNumber('-0').toString());
assert('0', new BigNumber(' +0').toString());
assert('0', new BigNumber(' -0').toString());
assert('0', new BigNumber(' +0 ').toString());
assert('0', new BigNumber(' -0 ').toString());
assert('0', new BigNumber('+.0').toString());
assert('0', new BigNumber('-.0').toString());
assert('0', new BigNumber(' +.0').toString());
assert('0', new BigNumber(' -.0').toString());
assert('0', new BigNumber(' +.0 ').toString());
assert('0', new BigNumber(' -.0 ').toString());
assertException(function () {new BigNumber('+-0')}, "+-0");
assertException(function () {new BigNumber('-+0')}, "-+0");
@ -509,22 +539,22 @@ var count = (function config(BigNumber) {
assertException(function () {new BigNumber('+. 0')}, "+. 0");
assertException(function () {new BigNumber('-. 0')}, "-. 0");
assert('2', new BigNumber('+2').toS());
assert('-2', new BigNumber('-2').toS());
assert('2', new BigNumber(' +2').toS());
assert('-2', new BigNumber(' -2').toS());
assert('2', new BigNumber(' +2 ').toS());
assert('-2', new BigNumber(' -2 ').toS());
assert('0.2', new BigNumber('.2').toS());
assert('2', new BigNumber('2.').toS());
assert('-2', new BigNumber('-2.').toS());
assert('2', new BigNumber('+2.').toS());
assert('0.2', new BigNumber('+.2').toS());
assert('-0.2', new BigNumber('-.2').toS());
assert('0.2', new BigNumber(' +.2').toS());
assert('-0.2', new BigNumber(' -.2').toS());
assert('0.2', new BigNumber(' +.2 ').toS());
assert('-0.2', new BigNumber(' -.2 ').toS());
assert('2', new BigNumber('+2').toString());
assert('-2', new BigNumber('-2').toString());
assert('2', new BigNumber(' +2').toString());
assert('-2', new BigNumber(' -2').toString());
assert('2', new BigNumber(' +2 ').toString());
assert('-2', new BigNumber(' -2 ').toString());
assert('0.2', new BigNumber('.2').toString());
assert('2', new BigNumber('2.').toString());
assert('-2', new BigNumber('-2.').toString());
assert('2', new BigNumber('+2.').toString());
assert('0.2', new BigNumber('+.2').toString());
assert('-0.2', new BigNumber('-.2').toString());
assert('0.2', new BigNumber(' +.2').toString());
assert('-0.2', new BigNumber(' -.2').toString());
assert('0.2', new BigNumber(' +.2 ').toString());
assert('-0.2', new BigNumber(' -.2 ').toString());
assertException(function () {new BigNumber('+-2')}, "+-2");
assertException(function () {new BigNumber('-+2')}, "-+2");

View File

@ -2010,6 +2010,15 @@ var count = (function div(BigNumber) {
T('45573.038', '-1951245006.57', '-0.0000233558768102170098357070718333190030237587540949009', 55, 8);
T('11697035101999953412797412955.51', '-0.00002645182103613', '-4.422015061278092346231915492159911911859012754718621042083122434161972684365584014570167e+32', 57, 8);
T('1', '1e+1000000000', '1', 0, 0);
T('1', '1e+1000000000', '0', 0, 4);
T('10', '1e+1000000000', '0', 0, 4);
T('1', '1e-1000000000', '1e+1000000000');
T('10', '1e-1000000000', 'Infinity');
var x = new BigNumber(1e-9);
BigNumber.config({ RANGE: [-8, 1e9], ERRORS: true });
T(x, 1, '0', 9, 4);
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];;
})(this.BigNumber);

1055
test/divToInt.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -14,6 +14,7 @@ console.log( '\n STARTING TESTS...\n' );
'config',
'dp',
'div',
'divToInt',
'floor',
'minus',
'mod',
@ -26,6 +27,7 @@ console.log( '\n STARTING TESTS...\n' );
'times',
'toExponential',
'toFixed',
'toFormat',
'toFraction',
'toNumber',
'toPrecision',

View File

@ -31,11 +31,31 @@ var count = (function others(BigNumber) {
}
}
function assertException(func, message) {
var actual;
total++;
try {
func();
} catch (e) {
actual = e;
}
if (actual && actual.name == 'BigNumber Error') {
passed++;
//log('\n Expected and actual: ' + actual);
} else {
error('\n Test number: ' + total + ' failed');
error('\n Expected: ' + message + ' to raise a BigNumber Error.');
error(' Actual: ' + (actual || 'no exception'));
//process.exit();
}
}
log('\n Testing others...');
/*
*
* isFinite
* isInteger
* isNaN
* isNegative
* isZero
@ -58,6 +78,7 @@ var count = (function others(BigNumber) {
n = new BigNumber(1);
assert(true, n.isFinite());
assert(true, n.isInteger());
assert(false, n.isNaN());
assert(false, n.isNegative());
assert(false, n.isZero());
@ -104,10 +125,11 @@ var count = (function others(BigNumber) {
assert(n.toString(), n.valueOf());
n = new BigNumber('-0.1');
assert(true, n.isF());
assert(true, n.isFinite());
assert(false, n.isInt());
assert(false, n.isNaN());
assert(true, n.isNeg());
assert(false, n.isZ());
assert(false, n.isZero());
assert(false, n.equals(0.1));
assert(false, n.greaterThan(-0.1));
assert(true, n.greaterThanOrEqualTo(-1));
@ -117,6 +139,7 @@ var count = (function others(BigNumber) {
n = new BigNumber(Infinity);
assert(false, n.isFinite());
assert(false, n.isInteger());
assert(false, n.isNaN());
assert(false, n.isNegative());
assert(false, n.isZero());
@ -129,10 +152,11 @@ var count = (function others(BigNumber) {
assert(n.toString(), n.valueOf());
n = new BigNumber('-Infinity');
assert(false, n.isF());
assert(false, n.isFinite());
assert(false, n.isInt());
assert(false, n.isNaN());
assert(true, n.isNeg());
assert(false, n.isZ());
assert(false, n.isZero());
assert(false, n.equals(Infinity));
assert(true, n.equals(-1/0));
assert(false, n.greaterThan(-Infinity));
@ -143,6 +167,7 @@ var count = (function others(BigNumber) {
n = new BigNumber('0.0000000');
assert(true, n.isFinite());
assert(true, n.isInteger());
assert(false, n.isNaN());
assert(false, n.isNegative());
assert(true, n.isZero());
@ -154,10 +179,11 @@ var count = (function others(BigNumber) {
assert(n.toString(), n.valueOf());
n = new BigNumber(-0);
assert(true, n.isF());
assert(true, n.isFinite());
assert(true, n.isInt());
assert(false, n.isNaN());
assert(true, n.isNeg());
assert(true, n.isZ());
assert(true, n.isZero());
assert(true, n.equals('0.000'));
assert(true, n.greaterThan(-1));
assert(false, n.greaterThanOrEqualTo(0.1));
@ -169,6 +195,7 @@ var count = (function others(BigNumber) {
n = new BigNumber('NaN');
assert(false, n.isFinite());
assert(false, n.isInteger());
assert(true, n.isNaN());
assert(false, n.isNegative());
assert(false, n.isZero());
@ -185,6 +212,7 @@ var count = (function others(BigNumber) {
n = new BigNumber('hiya');
assert(false, n.isFinite());
assert(false, n.isInteger());
assert(true, n.isNaN());
assert(false, n.isNegative());
assert(false, n.isZero());
@ -198,10 +226,11 @@ var count = (function others(BigNumber) {
BigNumber.config({ ERRORS : true });
n = new BigNumber('-1.234e+2');
assert(true, n.isF());
assert(true, n.isFinite());
assert(false, n.isInt());
assert(false, n.isNaN());
assert(true, n.isNeg());
assert(false, n.isZ());
assert(false, n.isZero());
assert(true, n.eq(-123.4, 10));
assert(true, n.gt('-ff', 16));
assert(true, n.gte('-1.234e+3'));
@ -211,6 +240,7 @@ var count = (function others(BigNumber) {
n = new BigNumber('5e-200');
assert(true, n.isFinite());
assert(false, n.isInteger());
assert(false, n.isNaN());
assert(false, n.isNegative());
assert(false, n.isZero());
@ -223,7 +253,7 @@ var count = (function others(BigNumber) {
n = new BigNumber('1');
assert(true, n.equals(n));
assert(true, n.equals(n.toS()));
assert(true, n.equals(n.toString()));
assert(true, n.equals(n.toString()));
assert(true, n.equals(n.valueOf()));
assert(true, n.equals(n.toFixed()));
@ -355,6 +385,9 @@ var count = (function others(BigNumber) {
assert(false, new BigNumber(9.999999e+2).greaterThanOrEqualTo(1e+3));
assert(true, new BigNumber(1e+3).gte(9.9999999e+2));
assertException(function () {new BigNumber(1).lt(null)}, "new BigNumber(1).lt(null)");
assertException(function () {new BigNumber(1).gt('one')}, "new BigNumber(1).gt('one')");
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];;
})(this.BigNumber);

File diff suppressed because it is too large Load Diff

View File

@ -446,6 +446,16 @@ var count = (function sqrt(BigNumber) {
T('0.0000000000000000000000000000000000000000008', '6.60007159131152131407609999E-85', 43, 4);
T('0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002598902022257132166703490111187879281161770014282895', '6.7542917212922111E-232', 167, 5);
T('56234132519034908039495103977648123146825104309869166408168942373588356864306284890585798452622030592867610732010032521800922849757565578997762493460810298', '3162277660168379331998893544432718533719555139325216826857504852792594438639238221344248108379300295187347284152840055148548856030453880014690519596700153931622776601683793319988935444327185337195551393252168268575048527925944386392382213442481083793002951873472841528400551485488560304538800146905195967001539', 0, 4);
T('31622776601683793319988935444327185337195551393252168268575048527925944386392382213442481083793002951873472841528400551485488560304538800146905195967001539', '1e309', 0, 4);
T('0', '1e-324', 0, 4);
T('1', '1e-324', 0, 0);
var x = new BigNumber('0.000000009');
BigNumber.config({ RANGE : [-4, 1e9] });
// sqrt(0.000000009) = 0.00009486...
T('0', x, 5, 4);
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];
})(this.BigNumber);

View File

@ -36,7 +36,7 @@ var count = (function times(BigNumber) {
function T(multiplicand, multiplier, expected) {
assert(String(expected), String(new BigNumber(multiplicand).times(multiplier)));
assert(String(expected), String(new BigNumber(multiplicand).times(new BigNumber(multiplier))));
//assert(String(expected), String(new BigNumber(multiplicand).times(new BigNumber(multiplier))));
}
function isMinusZero(n) {
@ -98,6 +98,12 @@ var count = (function times(BigNumber) {
T(I, -I, -I);
T(-I, I, -I);
T(-I, -I, I);
T('1e1000000000', 10, I);
T('5e500000000', '2e500000000', I);
T('-1e1000000000', 10, -I);
T('5e500000000', '-2e500000000', -I);
T('1e-1000000000', 0.1, 0);
T('-1e-1000000000', 0.1, 0);
T(1, '1','1');
T(1, '-45', '-45');

242
test/toFormat.js Normal file
View File

@ -0,0 +1,242 @@
var count = (function toFormat(BigNumber) {
var start = +new Date(),
log,
error,
format,
u,
passed = 0,
total = 0;
if (typeof window === 'undefined') {
log = console.log;
error = console.error;
} else {
log = function (str) { document.body.innerHTML += str.replace('\n', '<br>') };
error = function (str) { document.body.innerHTML += '<div style="color: red">' +
str.replace('\n', '<br>') + '</div>' };
}
if (!BigNumber && typeof require === 'function') BigNumber = require('../bignumber');
function assert(expected, actual) {
total++;
if (expected !== actual) {
error('\n Test number: ' + total + ' failed');
error(' Expected: ' + expected);
error(' Actual: ' + actual);
//process.exit();
} else {
passed++;
//log('\n Expected and actual: ' + actual);
}
}
function assertException(func, message) {
var actual;
total++;
try {
func();
} catch (e) {
actual = e;
}
if (actual && actual.name == 'BigNumber Error') {
passed++;
//log('\n Expected and actual: ' + actual);
} else {
error('\n Test number: ' + total + ' failed');
error('\n Expected: ' + message + ' to raise a BigNumber Error.');
error(' Actual: ' + (actual || 'no exception'));
//process.exit();
}
}
function T(expected, value, dp){
assert(expected, new BigNumber(value).toFormat(dp));
}
log('\n Testing toFormat...');
format = {
decimalSeparator : '.',
groupSeparator : ',',
groupSize : 3,
secondaryGroupSize : 0,
fractionGroupSeparator : ' ',
fractionGroupSize : 0
};
BigNumber.config({
DECIMAL_PLACES : 20,
ROUNDING_MODE : 4,
ERRORS : true,
RANGE : 1E9,
EXPONENTIAL_AT : [-7, 21],
FORMAT: format
});
T('0', 0);
T('1', 1);
T('-1', -1);
T('123.456', 123.456);
T('NaN', NaN);
T('Infinity', 1/0);
T('-Infinity', -1/0);
T('0', 0, null);
T('1', 1, undefined);
T('-1', -1, 0);
T('123.456', 123.456, 3);
T('NaN', NaN, 0);
T('Infinity', 1/0, 3);
T('-Infinity', -1/0, 0);
T('0.0', 0, 1);
T('1.00', 1, 2);
T('-1.000', -1, 3);
T('123.4560', 123.456, 4);
T('NaN', NaN, 5);
T('Infinity', 1/0, 6);
T('-Infinity', -1/0, 7);
T('9,876.54321', 9876.54321);
T('4,018,736,400,000,000,000,000', '4.0187364e+21');
T('999,999,999,999,999', 999999999999999);
T('99,999,999,999,999', 99999999999999);
T('9,999,999,999,999', 9999999999999);
T('999,999,999,999', 999999999999);
T('99,999,999,999', 99999999999);
T('9,999,999,999', 9999999999);
T('999,999,999', 999999999);
T('99,999,999', 99999999);
T('9,999,999', 9999999);
T('999,999', 999999);
T('99,999', 99999);
T('9,999', 9999);
T('999', 999);
T('99', 99);
T('9', 9);
T('76,852.342091', '7.6852342091e+4');
format.groupSeparator = ' ';
T('76 852.34', '7.6852342091e+4', 2);
T('76 852.342091', '7.6852342091e+4');
T('76 852.3420910871', '7.6852342091087145832640897e+4', 10);
format.fractionGroupSize = 5;
T('4 018 736 400 000 000 000 000', '4.0187364e+21');
T('76 852.34209 10871 45832 64089', '7.685234209108714583264089e+4', 20);
T('76 852.34209 10871 45832 64089 7', '7.6852342091087145832640897e+4', 21);
T('76 852.34209 10871 45832 64089 70000', '7.6852342091087145832640897e+4', 25);
T('999 999 999 999 999', 999999999999999, 0);
T('99 999 999 999 999.0', 99999999999999, 1);
T('9 999 999 999 999.00', 9999999999999, 2);
T('999 999 999 999.000', 999999999999, 3);
T('99 999 999 999.0000', 99999999999, 4);
T('9 999 999 999.00000', 9999999999, 5);
T('999 999 999.00000 0', 999999999, 6);
T('99 999 999.00000 00', 99999999, 7);
T('9 999 999.00000 000', 9999999, 8);
T('999 999.00000 0000', 999999, 9);
T('99 999.00000 00000', 99999, 10);
T('9 999.00000 00000 0', 9999, 11);
T('999.00000 00000 00', 999, 12);
T('99.00000 00000 000', 99, 13);
T('9.00000 00000 0000', 9, 14);
T('1.00000 00000 00000', 1, 15);
T('1.00000 00000 0000', 1, 14);
T('1.00000 00000 000', 1, 13);
T('1.00000 00000 00', 1, 12);
T('1.00000 00000 0', 1, 11);
T('1.00000 00000', 1, 10);
T('1.00000 0000', 1, 9);
format.fractionGroupSize = 0;
T('4 018 736 400 000 000 000 000', '4.0187364e+21');
T('76 852.34209108714583264089', '7.685234209108714583264089e+4', 20);
T('76 852.342091087145832640897', '7.6852342091087145832640897e+4', 21);
T('76 852.3420910871458326408970000', '7.6852342091087145832640897e+4', 25);
T('999 999 999 999 999', 999999999999999, 0);
T('99 999 999 999 999.0', 99999999999999, 1);
T('9 999 999 999 999.00', 9999999999999, 2);
T('999 999 999 999.000', 999999999999, 3);
T('99 999 999 999.0000', 99999999999, 4);
T('9 999 999 999.00000', 9999999999, 5);
T('999 999 999.000000', 999999999, 6);
T('99 999 999.0000000', 99999999, 7);
T('9 999 999.00000000', 9999999, 8);
T('999 999.000000000', 999999, 9);
T('99 999.0000000000', 99999, 10);
T('9 999.00000000000', 9999, 11);
T('999.000000000000', 999, 12);
T('99.0000000000000', 99, 13);
T('9.00000000000000', 9, 14);
T('1.000000000000000', 1, 15);
T('1.00000000000000', 1, 14);
T('1.0000000000000', 1, 13);
T('1.000000000000', 1, 12);
T('1.00000000000', 1, 11);
T('1.0000000000', 1, 10);
T('1.000000000', 1, 9);
format = {
decimalSeparator : '.',
groupSeparator : ',',
groupSize : 3,
secondaryGroupSize : 2
};
BigNumber.config({ FORMAT: format });
T('9,876.54321', 9876.54321);
T('10,00,037.123', '1000037.123456789', 3);
T('4,01,87,36,40,00,00,00,00,00,000', '4.0187364e+21');
T('99,99,99,99,99,99,999', 999999999999999);
T('9,99,99,99,99,99,999', 99999999999999);
T('99,99,99,99,99,999', 9999999999999);
T('9,99,99,99,99,999', 999999999999);
T('99,99,99,99,999', 99999999999);
T('9,99,99,99,999', 9999999999);
T('99,99,99,999', 999999999);
T('9,99,99,999', 99999999);
T('99,99,999', 9999999);
T('9,99,999', 999999);
T('99,999', 99999);
T('9,999', 9999);
T('999', 999);
T('99', 99);
T('9', 9);
format.decimalSeparator = ',';
format.groupSeparator = '.';
T('1.23.45.60.000,000000000008', '1.23456000000000000000789e+9', 12);
format.groupSeparator = '';
T('10000000000123456789000000,0000000001', '10000000000123456789000000.000000000100000001', 10);
format.groupSeparator = ' ';
format.groupSize = 1;
format.secondaryGroupSize = 4;
T('4658 0734 6509 8347 6580 3645 0,6', '4658073465098347658036450.59764985763489569875659876459', 1);
format.fractionGroupSize = 2;
format.fractionGroupSeparator = ':';
format.secondaryGroupSize = null;
T('4 6 5 8 0 7 3 4 6 5 0 9 8 3 4 7 6 5 8 0 3 6 4 5 0,59:76:49:85:76:34:89:56:98:75:65:98:76:45:9', '4658073465098347658036450.59764985763489569875659876459' );
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];
})(this.BigNumber);
if (typeof module !== 'undefined' && module.exports) module.exports = count;

View File

@ -64,6 +64,11 @@ var count = (function toFraction(BigNumber) {
EXPONENTIAL_AT : 1E9
});
T('Infinity', 'Infinity', u);
T('-Infinity', -Infinity, u);
T('NaN', 'NaN', u);
T('NaN', NaN, u);
// Tests generated using Python's fraction module.
T('1,10', '0.1', u);