How to Work with Big Numbers in MathJS?
Last Updated :
08 Aug, 2024
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
Similar Reads
How to Work with Fractions in Math.js? Math.js is a JavaScript library that can handle various mathematical operations, including fractions. It makes it easy to perform calculations with fractions, convert them to decimals, and simplify them. This guide will show you how to work with fractions using math.js.To work with fractions, we nee
2 min read
How to Deal with Large Numbers in JavaScript ? Large numbers are the numbers that can hold huge memory and evaluation time is more than exceeds space and time to process. We can deal with large numbers in JavaScript using the data type BigInt. Advantages:It can hold numbers of large sizes.It performs arithmetic operations.Disadvantages:Consumes
3 min read
How to Format a Number with Two Decimals in JavaScript? In javascript, formatting numbers to a fixed number of decimal places is a common requirement, particularly in financial calculations, statistical data presentations, and to show in user interfaces. In this article we will explore different approaches to format a number with two decimals in JavaScri
2 min read
How to Remove the Decimal Part of a Number in JavaScript ? The decimal portion of a number in JavaScript can be removed through various methods, including the Math. trunc( ) method, Math. floor( ) method, Math. ceil( ) method, and Math. round( ) method. SyntaxMath.trunc(11.5) // Output: 11Math.floor(12.5) // Output: 12Math.ceil(15.5) // Output: 15Math.round
2 min read
How to Use math.js in NodeJS And Browser? Math.js can be used to perform complex mathematical computations in both NodeJS and browser environments. It is a powerful library that supports a wide range of mathematical functions, including algebra, calculus, and statistical operations. By integrating math.js into your projects, you can handle
3 min read
Maximum of Two Numbers in JavaScript Finding the maximum of two numbers is a popular JavaScript operation used in many different programming tasks. This can be accomplished in a variety of ways, each having unique benefits and applications. There are several approaches for determining the maximum of two numbers in JavaScript which are
2 min read