This commit is contained in:
Michael Mclaughlin 2013-08-01 18:57:00 +01:00
parent 4473a6c888
commit a977d784a1
4 changed files with 32 additions and 23 deletions

View File

@ -1,15 +1,15 @@
# bignumber.js # # bignumber.js #
A Javascript library for arbitrary-precision decimal and non-decimal arithmetic. A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic.
## Features ## Features
- Faster, smaller, and perhaps easier to use than Javascript versions of Java's BigDecimal - Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
- 5 KB minified and gzipped - 5 KB minified and gzipped
- Simple API but full-featured - Simple API but full-featured
- Works with numbers with or without fraction digits in bases from 2 to 36 inclusive - Works with numbers with or without fraction digits in bases from 2 to 36 inclusive
- Replicates the `toExponential`, `toFixed`, `toPrecision` and `toString` methods of Javascript's Number type - Replicates the `toExponential`, `toFixed`, `toPrecision` and `toString` methods of JavaScript's Number type
- Includes a `toFraction` and a `squareRoot` method - Includes a `toFraction` and a `squareRoot` method
- Stores values in an accessible decimal floating point format - Stores values in an accessible decimal floating point format
- No dependencies - No dependencies
@ -21,8 +21,7 @@ It also does not allow `NaN` or `Infinity`, or have the configuration options of
## Load ## Load
The library is the single Javascript file *bignumber.js* The library is the single JavaScript file *bignumber.js* (or minified, *bignumber.min.js*).
(or *bignumber.min.js*, which is *bignumber.js* minified using uglify-js).
It can be loaded via a script tag in an HTML document for the browser It can be loaded via a script tag in an HTML document for the browser
@ -41,7 +40,6 @@ The library is also available from the [npm](https://npmjs.org/) registry, so
$ npm install bignumber.js $ npm install bignumber.js
will install this entire directory in a *node_modules* directory within the current directory. will install this entire directory in a *node_modules* directory within the current directory.
It can then be loaded with `require('bignumber.js')`.
To load with AMD loader libraries such as [requireJS](http://requirejs.org/): To load with AMD loader libraries such as [requireJS](http://requirejs.org/):
@ -85,7 +83,7 @@ Method names over 5 letters in length have a shorter alias.
x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3)) // true 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 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 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.toExponential(5) // "2.55500e+2"
@ -153,13 +151,13 @@ To test all the methods in more depth
For the browser, see *quick-test.html*, *single-test.html* and *every-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. *bignumber-vs-number.html* enables some of the methods of bignumber.js to be compared with those of JavaScript's Number type.
## Performance ## 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. *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.) (The GWT version doesn't work in IE 6.)
* GWT: java.math.BigDecimal * GWT: java.math.BigDecimal
@ -198,7 +196,7 @@ will create *bignumber.min.js*.
Bugs: surely not! Open an issue, please. Bugs: surely not! Open an issue, please.
Other feedback to: Other feedback to:
Michael Mclaughlin Michael
<a href="mailto:M8ch88l@gmail.com">M8ch88l@gmail.com</a> <a href="mailto:M8ch88l@gmail.com">M8ch88l@gmail.com</a>
Bitcoin donation to: Bitcoin donation to:
@ -211,12 +209,13 @@ See LICENCE.
## Change Log ## Change Log
####1.1.0
* 1/8/2013 Allow numbers with trailing radix point.
####1.0.1 ####1.0.1
* Bugfix: error messages with incorrect method name * Bugfix: error messages with incorrect method name
* Corrected a couple of spelling mistakes in comments
* Very minor regex tweaks
####1.0.0 ####1.0.0
* 8/11/2012 Initial release * 8/11/2012 Initial release
[![githalytics.com alpha](https://cruel-carlota.pagodabox.com/50b85fd919b406ef9312551092a95fb7 "githalytics.com")](http://githalytics.com/MikeMcl/bignumber.js) [![githalytics.com alpha](https://cruel-carlota.pagodabox.com/50b85fd919b406ef9312551092a95fb7 "githalytics.com")](http://githalytics.com/MikeMcl/bignumber.js)

View File

@ -145,7 +145,7 @@ code,pre{font-family:Monaco,Consolas,"Lucida Console",monospace;
<h1>bignumber.js</h1> <h1>bignumber.js</h1>
<p>A Javascript library for arbitrary-precision arithmetic.</p> <p>A JavaScript library for arbitrary-precision arithmetic.</p>
<p> <p>
<a href="https://github.com/MikeMcl/bignumber.js">Hosted on GitHub</a>. <a href="https://github.com/MikeMcl/bignumber.js">Hosted on GitHub</a>.
@ -190,7 +190,7 @@ code,pre{font-family:Monaco,Consolas,"Lucida Console",monospace;
</dd> </dd>
<dd> <dd>
There is no limit to the number of digits of a value of type There is no limit to the number of digits of a value of type
<em>string</em> (other than that of Javascript's maximum array size). <em>string</em> (other than that of JavaScript's maximum array size).
</dd> </dd>
<dd> <dd>
Decimal string values may be in exponential, as well as normal Decimal string values may be in exponential, as well as normal
@ -351,7 +351,7 @@ BigNumber.config(null, BigNumber.ROUND_UP) // equivalent</pre>
above which the same. above which the same.
</dd> </dd>
<dd> <dd>
For example, to emulate Javascript numbers in terms of the exponent For example, to emulate JavaScript numbers in terms of the exponent
values at which they begin to use exponential notation, use values at which they begin to use exponential notation, use
<code>[-7, 20]</code>. <code>[-7, 20]</code>.
</dd> </dd>
@ -408,7 +408,7 @@ BigNumber.config({ EXPONENTIAL_AT : 0 })</pre>
exponent limit. exponent limit.
</dd> </dd>
<dd> <dd>
For example, to emulate Javascript numbers in terms of the exponent For example, to emulate JavaScript numbers in terms of the exponent
values at which they become zero and Infinity, use values at which they become zero and Infinity, use
<code>[-324, 308]</code>. <code>[-324, 308]</code>.
</dd> </dd>
@ -575,7 +575,7 @@ BigNumber.config({ ROUNDING_MODE : 2 }) // equivalent</pre>
</p> </p>
<p> <p>
The treatment of &plusmn;<code>0</code>, &plusmn;<code>Infinity</code> and The treatment of &plusmn;<code>0</code>, &plusmn;<code>Infinity</code> and
<code>NaN</code> is consistent with how Javascript treats these values. <code>NaN</code> is consistent with how JavaScript treats these values.
</p> </p>
<p> <p>
Method names over 5 letters in length have a shorter alias (except Method names over 5 letters in length have a shorter alias (except
@ -850,7 +850,7 @@ x.minus(0.6, 20) // '0'</pre>
</p> </p>
<p> <p>
The result will have the same sign as this BigNumber, and it will match The result will have the same sign as this BigNumber, and it will match
that of Javascript's % operator (within the limits of its precision) and that of JavaScript's % operator (within the limits of its precision) and
BigDecimal's remainder method. BigDecimal's remainder method.
</p> </p>
<pre> <pre>
@ -961,7 +961,7 @@ new BigNumber(2).pow(1e+6) // Time taken (Node.js): 9 minutes 34 secs.</pre>
<p> <p>
Returns <code>true</code> if the value of this BigNumber equals the value Returns <code>true</code> if the value of this BigNumber equals the value
of <code>n</code>, otherwise returns <code>false</code>.<br /> of <code>n</code>, otherwise returns <code>false</code>.<br />
As with Javascript, NaN does not equal NaN. As with JavaScript, NaN does not equal NaN.
<br />Note : This method uses the <code>comparedTo</code> method <br />Note : This method uses the <code>comparedTo</code> method
internally. internally.
</p> </p>
@ -1383,7 +1383,7 @@ y // '1234.56'</pre>
(left-most) digit of the coefficient. (left-most) digit of the coefficient.
</p> </p>
<p> <p>
Note that, as with Javascript numbers, the original exponent and Note that, as with JavaScript numbers, the original exponent and
fractional trailing zeros are not preserved. fractional trailing zeros are not preserved.
</p> </p>
<pre>x = new BigNumber(0.123) // '0.123' <pre>x = new BigNumber(0.123) // '0.123'
@ -1691,7 +1691,7 @@ z = x.multiply(y) // 4.1400000</pre>
misleading. misleading.
</p> </p>
<p> <p>
This library, like Javascript and most calculators, does not This library, like binary floating point and most calculators, does not
retain trailing fractional zeros. Instead, the <code>toExponential</code>, retain trailing fractional zeros. Instead, the <code>toExponential</code>,
<code>toFixed</code> and <code>toPrecision</code> methods enable trailing <code>toFixed</code> and <code>toPrecision</code> methods enable trailing
zeros to be added if and when required. zeros to be added if and when required.

View File

@ -1,7 +1,7 @@
{ {
"name": "bignumber.js", "name": "bignumber.js",
"description": "A library for arbitrary-precision decimal and non-decimal arithmetic", "description": "A library for arbitrary-precision decimal and non-decimal arithmetic",
"version": "1.0.1", "version": "1.1.0",
"keywords": [ "keywords": [
"arbitrary", "arbitrary",
"precision", "precision",

View File

@ -478,6 +478,10 @@ var count = (function config(BigNumber) {
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());
@ -497,6 +501,7 @@ var count = (function config(BigNumber) {
assertException(function () {new BigNumber('++0')}, "++0"); assertException(function () {new BigNumber('++0')}, "++0");
assertException(function () {new BigNumber('.-0')}, ".-0"); assertException(function () {new BigNumber('.-0')}, ".-0");
assertException(function () {new BigNumber('.+0')}, ".+0"); assertException(function () {new BigNumber('.+0')}, ".+0");
assertException(function () {new BigNumber('0 .')}, "0 .");
assertException(function () {new BigNumber('. 0')}, ". 0"); assertException(function () {new BigNumber('. 0')}, ". 0");
assertException(function () {new BigNumber('..0')}, "..0"); assertException(function () {new BigNumber('..0')}, "..0");
assertException(function () {new BigNumber('+.-0')}, "+.-0"); assertException(function () {new BigNumber('+.-0')}, "+.-0");
@ -510,6 +515,10 @@ var count = (function config(BigNumber) {
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());
@ -523,6 +532,7 @@ var count = (function config(BigNumber) {
assertException(function () {new BigNumber('++2')}, "++2"); assertException(function () {new BigNumber('++2')}, "++2");
assertException(function () {new BigNumber('.-2')}, ".-2"); assertException(function () {new BigNumber('.-2')}, ".-2");
assertException(function () {new BigNumber('.+2')}, ".+2"); assertException(function () {new BigNumber('.+2')}, ".+2");
assertException(function () {new BigNumber('2 .')}, "2 .");
assertException(function () {new BigNumber('. 2')}, ". 2"); assertException(function () {new BigNumber('. 2')}, ". 2");
assertException(function () {new BigNumber('..2')}, "..2"); assertException(function () {new BigNumber('..2')}, "..2");
assertException(function () {new BigNumber('+.-2')}, "+.-2"); assertException(function () {new BigNumber('+.-2')}, "+.-2");