biginteger.js

1.0.1 • Public • Published

bignumber.js

Big Integer Library for Javascript.

Requirement

  1. node.js
  2. npm

How to install

Open your terminal and execute the following command

npm install biginteger.js

The name of the repo and npm module are different because the repo name has already been taken in npm registry :(.

Features

Currently, library is capable of handling integers with the following operations.

  1. Addition
  2. Substraction
  3. Multiplication
  4. Division
  5. Modulo
  6. Power function.

The floating point functionality will be added soon.

Usage

Once you install the library you can start using it by importing it through require function.

  var BigNumber = require('bignumber').BigNumber;
  • Creating an instance of BigNumber:

    You can create an instance of BigNumber with new operator by providing initial numbers as a string or another instance of BigNumber object.

  var a = new BigNumber("1024"),
      b = new BigNumber(a);
  • Adding numbers:

    You can add numbers using add method of BigNumber object. The function accepts two parameters, number to be added and the optional 'times'. The optional 'times' parameter is used to add 'number' by one or more times.

  var a = new BigNumber("1024"), 
      b = new BigNumber("1000");
      
  a.add(b); // 2024
  a.add(b, 10); // 11024
  
  // adding two negative numbers results into an addition.
  a = new BigNumber("-5");
  b = new BigNumber("-5");
  a.add(b); // -10
  a.add(b, 3); // -20
  
  // adding one positive and negative number results into a subtraction.
  a = new BigNumber("-5");
  b = new BigNumber("5");
  a.add(b); // 0
  a.add(b, 3); // 10
  
  • Subtracting numbers:

    The library provides subtract function to carry out subtraction. The signature of the function is same as add. The sign of the result is properly handled by the function.

  var a = new BigNumber("5"),
      b = new BigNumber("1");
  
  a.subtract(b); // 4
  a.subtract(b, 6); // -1
  
  // subtracting a positive and a negative number results into an addition.
  a = new BigNumber("5");
  b = new BigNumber("-6");
  a.subtract(b); // 11
  
  // if the sign of variable 'a' in the above example is negative, 
  // the resulting operation will be subtraction and the 
  // result will be '1'
  a = new BigNumber("-5");
  b = new BigNumber("-6");
  a.subtract(b); // 1
  • Multiplication:

    Multiplication of numbers is done through multiply method of BigNumber instance. The function takes same number of parameters as add and subtract.

   var a = new BigNumber("8"),
       b = new BigNumber("2");
   
   a.multiply(b); // 16
   a.multiply(b, 3); // 64
   
   a = new BigNumber("-4");
   b = new BigNumber("8");
   
   a.multiply(b); // -32

Readme

Keywords

none

Package Sidebar

Install

npm i biginteger.js

Weekly Downloads

2

Version

1.0.1

License

BSD-2-Clause

Last publish

Collaborators

  • bhimsen92