All Projects → MikeMcl → Big.js

MikeMcl / Big.js

Licence: mit
A small, fast JavaScript library for arbitrary-precision decimal arithmetic.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Big.js

Bignumber.js
A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
Stars: ✭ 5,174 (+38.71%)
Mutual labels:  arbitrary-precision, bigdecimal, bignumber, decimal-places
Decimal.js
An arbitrary-precision Decimal type for JavaScript
Stars: ✭ 4,585 (+22.92%)
Mutual labels:  arbitrary-precision, bigdecimal, bignumber
break infinity.js
A replacement for decimal.js for incremental games who want to deal with very large numbers (bigger in magnitude than 1e308, up to as much as 1e(9e15) ) and want to prioritize speed over accuracy.
Stars: ✭ 145 (-96.11%)
Mutual labels:  bigdecimal, bignumber
bcmath-extended
Extends php BCMath lib for missing functions like floor, ceil, round, abs, min, max, rand for big numbers. Also wraps existing BCMath functions.
Stars: ✭ 59 (-98.42%)
Mutual labels:  arbitrary-precision, bignumber
hebimath
arbitrary precision arithmetic library
Stars: ✭ 37 (-99.01%)
Mutual labels:  arbitrary-precision, bignumber
cldr-engine
Internationalization and localization in Typescript with Unicode CLDR, batteries included
Stars: ✭ 34 (-99.09%)
Mutual labels:  arbitrary-precision
BhimIntegers
BhimIntegers🚀 is a C++ library that is useful when we are dealing with BigIntegers💥💥. We can handle big integers (integers having a size bigger than the long long int data type) and we can perform arithmetic operations📘 like addition, multiplication, subtraction, division, equality check, etc📐📐. Also, there are several functions like factorial, …
Stars: ✭ 43 (-98.85%)
Mutual labels:  bignumber
as-bignum
Fixed length big numbers for AssemblyScript 🚀
Stars: ✭ 49 (-98.69%)
Mutual labels:  bigdecimal
decimal
An arbitrary-precision decimal floating-point arithmetic package for Go
Stars: ✭ 28 (-99.25%)
Mutual labels:  arbitrary-precision
Decimal
Arbitrary-precision fixed-point decimal numbers in go
Stars: ✭ 3,588 (-3.81%)
Mutual labels:  bignumber
imath
Arbitrary precision integer and rational arithmetic library
Stars: ✭ 92 (-97.53%)
Mutual labels:  arbitrary-precision
klibcpp
kedixa's Cplusplus Library(timer, multiarray, unsigned_bigint, bigint, rational)
Stars: ✭ 17 (-99.54%)
Mutual labels:  bignumber
intx
intx – extended precision integer library
Stars: ✭ 83 (-97.77%)
Mutual labels:  arbitrary-precision
decimal
An object-oriented implementation of basic math operation with arbitrary precision, using BC Math if available.
Stars: ✭ 14 (-99.62%)
Mutual labels:  arbitrary-precision
eapa
Erlang/Elixir Arbitrary-Precision Arithmetic (EAPA)
Stars: ✭ 36 (-99.03%)
Mutual labels:  arbitrary-precision
wide-integer
Wide-Integer implements a generic C++ template for uint128_t, uint256_t, uint512_t, uint1024_t, etc.
Stars: ✭ 83 (-97.77%)
Mutual labels:  arbitrary-precision
Euler
The open-source computational framework for the Swift language
Stars: ✭ 37 (-99.01%)
Mutual labels:  bignumber
goff
goff (go finite field) is a unix-like tool that generates fast field arithmetic in Go.
Stars: ✭ 71 (-98.1%)
Mutual labels:  bignumber
BigNumber
A really long long long long long long number in C++
Stars: ✭ 37 (-99.01%)
Mutual labels:  bignumber
evm-bn
Convert fixed-point numbers to ethers big numbers and vice-versa.
Stars: ✭ 33 (-99.12%)
Mutual labels:  bignumber

big.js

A small, fast JavaScript library for arbitrary-precision decimal arithmetic.

npm version npm downloads

Features

  • Simple API
  • Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
  • Only 6 KB minified
  • Replicates the toExponential, toFixed and toPrecision methods of JavaScript Numbers
  • Stores values in an accessible decimal floating point format
  • Comprehensive documentation and test set
  • No dependencies
  • Uses ECMAScript 3 only, so works in all browsers

The little sister to bignumber.js and decimal.js. See here for some notes on the difference between them.

Install

The library is the single JavaScript file big.js or the ES module big.mjs.

Browsers

Add Big to global scope:

<script src='path/to/big.js'></script>

ES module:

<script type='module'>
import Big from './path/to/big.mjs';

Get a minified version from a CDN:

<script src='https://cdn.jsdelivr.net/npm/[email protected]/big.min.js'></script>

Node.js

$ npm install big.js

CommonJS:

const Big = require('big.js');

ES module:

import Big from 'big.js';

Deno

import Big from 'https://raw.githubusercontent.com/mikemcl/big.js/v6.0.0/big.mjs';
import Big from 'https://unpkg.com/[email protected]/big.mjs';

Use

In the code examples below, semicolons and toString calls are not shown.

The library exports a single constructor function, Big.

A Big number is created from a primitive number, string, or other Big number.

x = new Big(123.4567)
y = Big('123456.7e-3')                 // 'new' is optional
z = new Big(x)
x.eq(y) && x.eq(z) && y.eq(z)          // true

In Big strict mode, creating a Big number from a primitive number is disallowed.

Big.strict = true
x = new Big(1)                         // TypeError: [big.js] Invalid number
y = new Big('1.0000000000000001')
y.toNumber()                           // Error: [big.js] Imprecise conversion

A Big number is immutable in the sense that it is not changed by its methods.

0.3 - 0.1                              // 0.19999999999999998
x = new Big(0.3)
x.minus(0.1)                           // "0.2"
x                                      // "0.3"

The methods that return a Big number can be chained.

x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
x.sqrt().div(y).pow(3).gt(y.mod(z))    // true

Like JavaScript's Number type, there are toExponential, toFixed and toPrecision methods.

x = new Big(255.5)
x.toExponential(5)                     // "2.55500e+2"
x.toFixed(5)                           // "255.50000"
x.toPrecision(5)                       // "255.50"

The arithmetic methods always return the exact result except div, sqrt and pow (with negative exponent), as these methods involve division.

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.

Big.DP = 10
Big.RM = Big.roundHalfUp

x = new Big(2);
y = new Big(3);
z = x.div(y)                           // "0.6666666667"
z.sqrt()                               // "0.8164965809"
z.pow(-3)                              // "3.3749999995"
z.times(z)                             // "0.44444444448888888889"
z.times(z).round(10)                   // "0.4444444445"

The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.

x = new Big(-123.456);
x.c                                    // [1,2,3,4,5,6]    coefficient (i.e. significand)
x.e                                    // 2                exponent
x.s                                    // -1               sign

For advanced usage, multiple Big number constructors can be created, each with an independent configuration.

For further information see the API reference documentation.

Minify

To minify using, for example, npm and terser

$ npm install -g terser
$ terser big.js -c -m -o big.min.js

Test

The test directory contains the test scripts for each Big number method.

The tests can be run with Node.js or a browser.

Run all the tests:

$ npm test

Test a single method:

$ node test/toFixed

For the browser, see runner.html and test.html in the test/browser directory.

big-vs-number.html is a old application that enables some of the methods of big.js to be compared with those of JavaScript's Number type.

TypeScript

The DefinitelyTyped project has a Typescript type definitions file for big.js.

$ npm install --save-dev @types/big.js

Any questions about the TypeScript type definitions file should be addressed to the DefinitelyTyped project.

Licence

MIT

Contributors

Financial supporters

Thank you to all who have supported this project via Open Collective, particularly Coinbase.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].