Open In App

How to Work with Big Numbers in MathJS?

Last Updated : 08 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Big numbers are the numerical values that exceed the precision limits of standard JavaScript numbers, often requiring special handling to avoid precision loss. In math.js, big numbers are managed using the BigNumber type, which provides precise arithmetic operations for very large or very small numbers.

First, install math.js library

npm install math.js

There are different approaches to working with Big Numbers in MathJS:

Using math.bignumber To handle Big Numbers

In this approach, we are using math.bignumber from the mathjs library to handle very large numbers accurately. We perform arithmetic operations such as addition, subtraction, multiplication, and division on these big numbers and then convert the results to strings for display.

Example: The below example uses math.bignumber to Handle Big Numbers.

JavaScript
const math = require('mathjs');
const bigNumber1 = math.bignumber('123456789012345678901234567890');
const bigNumber2 = math.bignumber('987654321098765432109876543210');
const sum = math.add(bigNumber1, bigNumber2);
const difference = math.subtract(bigNumber1, bigNumber2);
const product = math.multiply(bigNumber1, bigNumber2);
const quotient = math.divide(bigNumber1, bigNumber2)
console.log('Sum:', sum.toString());
console.log('Difference:', difference.toString());
console.log('Product:', product.toString());
console.log('Quotient:', quotient.toString());

Output:

Sum: 1.1111111101111111110111111111e+30
Difference: -8.6419753208641975320864197532e+29
Product: 1.219326311370217952261850327336229233322374638011112635269e+59
Quotient: 0.1249999988609375000142382812498220214843772247314452846908569339

Creating a MathJS Instance with BigNumber Configuration

In this approach, we are using a custom instance of math.js created with the create function to handle big numbers with high precision. We configure this instance to use BigNumber as the default number type and perform arithmetic operations on large numbers, ensuring accurate results and full precision.

Example: The below example configures math.js to Use BigNumber as Default Number Type.

JavaScript
const { create, all } = require('mathjs')
const math = create(all);
math.config({
  number: 'BigNumber',
  precision: 64 
})
const bigNumber1 = '123456789012345678901234567890';
const bigNumber2 = '987654321098765432109876543210';
const sum = math.add(bigNumber1, bigNumber2);
const difference = math.subtract(bigNumber1, bigNumber2);
const product = math.multiply(bigNumber1, bigNumber2);
const quotient = math.divide(bigNumber1, bigNumber2);
console.log('Sum:', sum.toString());
console.log('Difference:', difference.toString());
console.log('Product:', product.toString());
console.log('Quotient:', quotient.toString());

Output:

Sum: 1.1111111101111111e+30
Difference: -8.641975320864197e+29
Product: 1.2193263113702178e+59
Quotient: 0.1249999988609375

Next Article

Similar Reads