You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
8.0 KiB

4 years ago
  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/) and [decimal.js](https://github.com/MikeMcl/decimal.js/). See [here](https://github.com/MikeMcl/big.js/wiki) for some notes on the difference between them.
  4. ## Features
  5. - Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
  6. - Only 5.9 KB minified and 2.7 KB gzipped
  7. - Simple API
  8. - Replicates the `toExponential`, `toFixed` and `toPrecision` methods of JavaScript's Number type
  9. - Includes a `sqrt` method
  10. - Stores values in an accessible decimal floating point format
  11. - No dependencies
  12. - Comprehensive [documentation](http://mikemcl.github.io/big.js/) and test set
  13. ## Set up
  14. The library is the single JavaScript file *big.js* (or *big.min.js*, which is *big.js* minified).
  15. Browser:
  16. ```html
  17. <script src='path/to/big.js'></script>
  18. ```
  19. [Node.js](http://nodejs.org):
  20. ```bash
  21. $ npm install big.js
  22. ```
  23. ```javascript
  24. const Big = require('big.js');
  25. ```
  26. ES6 module:
  27. ```javascript
  28. import Big from 'big.mjs';
  29. ```
  30. ## Use
  31. *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.*
  32. The library exports a single function, `Big`, the constructor of Big number instances.
  33. It accepts a value of type number, string or Big number object.
  34. x = new Big(123.4567)
  35. y = Big('123456.7e-3') // 'new' is optional
  36. z = new Big(x)
  37. x.eq(y) && x.eq(z) && y.eq(z) // true
  38. A Big number is immutable in the sense that it is not changed by its methods.
  39. 0.3 - 0.1 // 0.19999999999999998
  40. x = new Big(0.3)
  41. x.minus(0.1) // "0.2"
  42. x // "0.3"
  43. The methods that return a Big number can be chained.
  44. x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
  45. x.sqrt().div(y).pow(3).gt(y.mod(z)) // true
  46. Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods.
  47. x = new Big(255.5)
  48. x.toExponential(5) // "2.55500e+2"
  49. x.toFixed(5) // "255.50000"
  50. x.toPrecision(5) // "255.50"
  51. The arithmetic methods always return the exact result except `div`, `sqrt` and `pow`
  52. (with negative exponent), as these methods involve division.
  53. The maximum number of decimal places and the rounding mode used to round the results of these methods is determined by the value of the `DP` and `RM` properties of the `Big` number constructor.
  54. Big.DP = 10
  55. Big.RM = 1
  56. x = new Big(2);
  57. y = new Big(3);
  58. z = x.div(y) // "0.6666666667"
  59. z.sqrt() // "0.8164965809"
  60. z.pow(-3) // "3.3749999995"
  61. z.times(z) // "0.44444444448888888889"
  62. z.times(z).round(10) // "0.4444444445"
  63. Multiple Big number constructors can be created, each with an independent configuration.
  64. The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
  65. x = new Big(-123.456);
  66. x.c // [1,2,3,4,5,6] coefficient (i.e. significand)
  67. x.e // 2 exponent
  68. x.s // -1 sign
  69. For further information see the [API](http://mikemcl.github.io/big.js/) reference from the *doc* folder.
  70. ## Test
  71. The *test* directory contains the test scripts for each Big number method.
  72. The tests can be run with Node.js or a browser.
  73. To run all the tests
  74. $ npm test
  75. To test a single method
  76. $ node test/toFixed
  77. For the browser, see *single-test.html* and *every-test.html* in the *test/browser* directory.
  78. *big-vs-number.html* is a simple application that enables some of the methods of big.js to be compared with those of JavaScript's Number type.
  79. ## Performance
  80. The *perf* directory contains two legacy applications and a *lib* directory containing the BigDecimal libraries used by both.
  81. *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.
  82. * [GWT: java.math.BigDecimal](https://github.com/iriscouch/bigdecimal.js)
  83. * [ICU4J: com.ibm.icu.math.BigDecimal](https://github.com/dtrebbien/BigDecimal.js)
  84. The BigDecimal in the npm registry is the GWT version. It has some bugs, see the Node.js script *perf/lib/bigdecimal_GWT/bugs.js* for examples of flaws in its *remainder*, *divide* and *compareTo* methods.
  85. *bigtime.js* is a Node.js command-line application which tests the performance of big.js against the GWT version of
  86. BigDecimal from the npm registry.
  87. For example, to compare the time taken by the big.js `plus` method and the BigDecimal `add` method
  88. $ node bigtime plus 10000 40
  89. This will time 10000 calls to each, using operands of up to 40 random digits and will check that the results match.
  90. For help
  91. $ node bigtime -h
  92. ## Build
  93. If [uglify-js](https://github.com/mishoo/UglifyJS2) is installed globally
  94. $ npm install uglify-js -g
  95. then
  96. $ npm run build
  97. will create *big.min.js*.
  98. ## TypeScript
  99. The [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) project has a Typescript type definitions file for big.js.
  100. $ npm install @types/big.js
  101. Any questions about the TypeScript type definitions file should be addressed to the DefinitelyTyped project.
  102. ## Feedback
  103. Bugs/comments/questions?
  104. Open an issue, or email <a href="mailto:M8ch88l@gmail.com">Michael</a>
  105. ## Licence
  106. [MIT](LICENCE)
  107. ## Contributors
  108. This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
  109. <a href="graphs/contributors"><img src="https://opencollective.com/bigjs/contributors.svg?width=890&button=false" /></a>
  110. ## Backers
  111. Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/bigjs#backer)]
  112. <a href="https://opencollective.com/bigjs#backers" target="_blank"><img src="https://opencollective.com/bigjs/backers.svg?width=890"></a>
  113. ## Sponsors
  114. Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/bigjs#sponsor)]
  115. <a href="https://opencollective.com/bigjs/sponsor/0/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/0/avatar.svg"></a>
  116. <a href="https://opencollective.com/bigjs/sponsor/1/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/1/avatar.svg"></a>
  117. <a href="https://opencollective.com/bigjs/sponsor/2/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/2/avatar.svg"></a>
  118. <a href="https://opencollective.com/bigjs/sponsor/3/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/3/avatar.svg"></a>
  119. <a href="https://opencollective.com/bigjs/sponsor/4/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/4/avatar.svg"></a>
  120. <a href="https://opencollective.com/bigjs/sponsor/5/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/5/avatar.svg"></a>
  121. <a href="https://opencollective.com/bigjs/sponsor/6/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/6/avatar.svg"></a>
  122. <a href="https://opencollective.com/bigjs/sponsor/7/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/7/avatar.svg"></a>
  123. <a href="https://opencollective.com/bigjs/sponsor/8/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/8/avatar.svg"></a>
  124. <a href="https://opencollective.com/bigjs/sponsor/9/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/9/avatar.svg"></a>