How to use Math.js
Step 1: Installation Using npm
If you’re using npm for package management, you can install Math.js as a dependency:
# Using npm
npm install mathjs
# Using yarn
yarn add mathjs
Step 2: Import Math.js in program
const math = require('mathjs');
Example: In this example we will perform different mathematical operations using math.js.
// Import the math.js library
const math = require('mathjs');
// Define some mathematical expressions
const expression1 = '2 * (3 + 4)';
const expression2 = 'sin(45 deg) ^ 2 + cos(45 deg) ^ 2';
// Evaluate the expressions
const result1 = math.evaluate(expression1);
const result2 = math.evaluate(expression2);
// Print the results
console.log('Result of expression 1: ', result1);
console.log('Result of expression 2: ', result2);
Output:
Result of expression 1: 14
Result of expression 2: 1
Math.JS
Math.js is an all-inclusive, user-friendly math library that is made to give JavaScript developers increased power in terms of their math capabilities and it is applicable both on web browsers and Node.js environment. It rises above all kinds of restrictions set by the built-in Math object of JavaScript by offering more useful features and functions.