7fd35c5a0998a36930aac839b8bf4ea3ab5b78a35e3bac7c301625ec914e16d8cb9446aabf76b88f3b5baec41cc34b3978dbfdf45b49971ff09a67f819a50c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # big.js #
  2. A small, fast JavaScript library for arbitrary-precision decimal arithmetic.
  3. The little sister to [bignumber.js](https://github.com/MikeMcl/bignumber.js/).
  4. See also [decimal.js](https://github.com/MikeMcl/decimal.js/), and [here](https://github.com/MikeMcl/big.js/wiki) for the difference between them.
  5. ## Features
  6. - Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
  7. - Only 2.7 KB minified and gzipped
  8. - Simple API
  9. - Replicates the `toExponential`, `toFixed` and `toPrecision` methods of JavaScript's Number type
  10. - Includes a `sqrt` method
  11. - Stores values in an accessible decimal floating point format
  12. - No dependencies
  13. - Comprehensive [documentation](http://mikemcl.github.io/big.js/) and test set
  14. ## Load
  15. The library is the single JavaScript file *big.js* (or *big.min.js*, which is *big.js* minified).
  16. It can be loaded via a script tag in an HTML document for the browser
  17. <script src='./relative/path/to/big.js'></script>
  18. or as a CommonJS, [Node.js](http://nodejs.org) or AMD module using `require`.
  19. var Big = require('big.js');
  20. For Node.js, the library is available from the npm registry:
  21. $ npm install big.js
  22. ## Use
  23. *In all examples below, `var`, semicolons and `toString` calls are not shown.
  24. If a commented-out value is in quotes it means `toString` has been called on the preceding expression.*
  25. The library exports a single function: Big, the constructor of Big number instances.
  26. It accepts a value of type Number, String or Big number Object.
  27. x = new Big(123.4567)
  28. y = Big('123456.7e-3') // 'new' is optional
  29. z = new Big(x)
  30. x.eq(y) && x.eq(z) && y.eq(z) // true
  31. A Big number is immutable in the sense that it is not changed by its methods.
  32. 0.3 - 0.1 // 0.19999999999999998
  33. x = new Big(0.3)
  34. x.minus(0.1) // "0.2"
  35. x // "0.3"
  36. The methods that return a Big number can be chained.
  37. x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
  38. x.sqrt().div(y).pow(3).gt(y.mod(z)) // true
  39. Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods.
  40. x = new Big(255.5)
  41. x.toExponential(5) // "2.55500e+2"
  42. x.toFixed(5) // "255.50000"
  43. x.toPrecision(5) // "255.50"
  44. The maximum number of decimal places and the rounding mode used to round the results of the `div`, `sqrt` and `pow`
  45. (with negative exponent) methods is determined by the value of the `DP` and `RM` properties of the `Big` number constructor.
  46. The other methods always give the exact result.
  47. (From *v3.0.0*, multiple Big number constructors can be created, see Change Log below.)
  48. Big.DP = 10
  49. Big.RM = 1
  50. x = new Big(2);
  51. y = new Big(3);
  52. z = x.div(y) // "0.6666666667"
  53. z.sqrt() // "0.8164965809"
  54. z.pow(-3) // "3.3749999995"
  55. z.times(z) // "0.44444444448888888889"
  56. z.times(z).round(10) // "0.4444444445"
  57. The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
  58. x = new Big(-123.456);
  59. x.c // [1,2,3,4,5,6] coefficient (i.e. significand)
  60. x.e // 2 exponent
  61. x.s // -1 sign
  62. For further information see the [API](http://mikemcl.github.io/big.js/) reference from the *doc* folder.
  63. ## Test
  64. The *test* directory contains the test scripts for each Big number method.
  65. The tests can be run with Node or a browser.
  66. To test a single method, from a command-line shell at the *test* directory, use e.g.
  67. $ node toFixed
  68. To test all the methods
  69. $ node every-test
  70. For the browser, see *single-test.html* and *every-test.html* in the *test/browser* directory.
  71. *big-vs-number.html* enables some of the methods of big.js to be compared with those of JavaScript's Number type.
  72. ## Performance
  73. The *perf* directory contains two applications and a *lib* directory containing the BigDecimal libraries used by both.
  74. *big-vs-bigdecimal.html* tests the performance of big.js against the JavaScript translations of two versions of BigDecimal, its use should be more or less self-explanatory.
  75. (The GWT version doesn't work in IE 6.)
  76. * GWT: java.math.BigDecimal
  77. <https://github.com/iriscouch/bigdecimal.js>
  78. * ICU4J: com.ibm.icu.math.BigDecimal
  79. <https://github.com/dtrebbien/BigDecimal.js>
  80. 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.
  81. *bigtime.js* is a Node command-line application which tests the performance of big.js against the GWT version of
  82. BigDecimal from the npm registry.
  83. For example, to compare the time taken by the big.js `plus` method and the BigDecimal `add` method:
  84. $ node bigtime plus 10000 40
  85. This will time 10000 calls to each, using operands of up to 40 random digits and will check that the results match.
  86. For help:
  87. $ node bigtime -h
  88. ## Build
  89. I.e. minify.
  90. For Node, if uglify-js is installed globally ( `npm install uglify-js -g` ) then
  91. uglifyjs -o ./big.min.js ./big.js
  92. will create *big.min.js*.
  93. The *big.min.js* already present was created with *Microsoft Ajax Minifier 5.11*.
  94. ## TypeScript
  95. The [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) project has a TypeScript [definitions file](https://github.com/borisyankov/DefinitelyTyped/blob/master/big.js/big.js.d.ts) for big.js.
  96. The definitions file can be added to your project via the [big.js.TypeScript.DefinitelyTyped](https://www.nuget.org/packages/big.js.TypeScript.DefinitelyTyped/0.0.1) NuGet package or via [tsd](http://definitelytyped.org/tsd/).
  97. tsd query big.js --action install
  98. Any questions about the TypeScript definitions file should be addressed to the DefinitelyTyped project.
  99. ## Feedback
  100. Feedback is welcome.
  101. Bugs/comments/questions?
  102. Open an issue, or email
  103. Michael
  104. <a href="mailto:M8ch88l@gmail.com">M8ch88l@gmail.com</a>
  105. Bitcoin donation to:
  106. **1DppGRQSjVSMgGxuygDEHQuWEdTiVEzJYG**
  107. Thank you
  108. ## Licence
  109. See LICENCE.
  110. ## Change Log
  111. ####3.2.0
  112. * 14/09/17 Aid ES6 import.
  113. ####3.1.3
  114. * Minor documentation updates.
  115. ####3.1.2
  116. * README typo.
  117. ####3.1.1
  118. * API documentation update, including FAQ additions.
  119. ####3.1.0
  120. * Renamed and exposed `TO_EXP_NEG` and `TO_EXP_POS` as `Big.E_NEG` and
  121. `Big.E_POS`.
  122. ####3.0.2
  123. * Remove *.npmignore*, use `files` field in *package.json* instead.
  124. ####3.0.1
  125. * Added `sub`, `add` and `mul` aliases.
  126. * Clean-up after lint.
  127. ####3.0.0
  128. * 10/12/14 Added [multiple constructor functionality](http://mikemcl.github.io/big.js/#faq).
  129. * No breaking changes or other additions, but a major code reorganisation,
  130. so *v3* seemed appropriate.
  131. ####2.5.2
  132. * 1/11/14 Added bower.json.
  133. ####2.5.1
  134. * 8/06/14 Amend README requires.
  135. ####2.5.0
  136. * 26/01/14 Added `toJSON` method so serialization uses `toString`.
  137. ####2.4.1
  138. * 17/10/13 Conform signed zero to IEEEE 754 (2008).
  139. ####2.4.0
  140. * 19/09/13 Throw instances of `Error`.
  141. ####2.3.0
  142. * 16/09/13 Added `cmp` method.
  143. ####2.2.0
  144. * 11/07/13 Added 'round up' mode.
  145. ####2.1.0
  146. * 26/06/13 Allow e.g. `.1` and `2.`.
  147. ####2.0.0
  148. * 12/05/13 Added `abs` method and replaced `cmp` with `eq`, `gt`, `gte`, `lt`, and `lte` methods.
  149. ####1.0.1
  150. * Changed default value of MAX_DP to 1E6
  151. ####1.0.0
  152. * 7/11/2012 Initial release