Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
math-expression-evaluator
Advanced tools
The math-expression-evaluator is an npm package that allows for parsing and evaluating mathematical expressions. It can handle various mathematical operations, including basic arithmetic, logical operations, and functions.
Evaluating Mathematical Expressions
This feature allows for the evaluation of a string-based mathematical expression, including support for functions like square root.
var mexp = require('math-expression-evaluator');
var result = mexp.eval('2+3*sqrt(4)');
Support for Variables in Expressions
This feature enables the evaluation of expressions with variables, where the values of the variables can be provided as an object.
var mexp = require('math-expression-evaluator');
var result = mexp.eval('x^2 + y', {x: 3, y: 4});
Custom Functions
This feature allows users to add custom functions to the evaluator, which can then be used in expressions.
var mexp = require('math-expression-evaluator');
mexp.addToken([
{type: 3, token: 'sum', show: 'sum', value: function(a, b) { return a + b; }}
]);
var result = mexp.eval('sum(1,2)');
Logical Operations
This feature supports logical operations within expressions, enabling the evaluation of expressions that include logical comparisons.
var mexp = require('math-expression-evaluator');
var result = mexp.eval('1 < 2 && 3 > 2');
Math.js is an extensive math library for JavaScript and Node.js. It provides a wide range of features including support for units, complex numbers, matrices, and a large number of mathematical functions. Math.js is more feature-rich and can be considered more advanced than math-expression-evaluator.
Algebra.js allows you to define algebraic expressions and equations and perform operations on them. It is more focused on algebraic structures like fractions and equations compared to math-expression-evaluator, which is more about evaluating general mathematical expressions.
Nerdamer is a JavaScript library for symbolic mathematics. It is capable of handling symbolic expressions, calculus, algebra, and solving equations. Nerdamer provides more advanced features for symbolic computation compared to math-expression-evaluator.
An extremely efficient, flexible and amazing evaluator for Math expression in Javascript.(Documentation)
Input | Result | Explanation |
---|---|---|
2+3-1 | 4 | Addition and Subtraction operator |
2*5/10 | 1 | Multiplication and Division operator |
tan45 or tan(45) | 1 | Trigonometric Function ( tan in Degree mode) |
tan45 or tan(45) | 1.619775190543862 | Trigonometric Function ( tan in Radian mode) |
Pi1,15,n or Pi(1,15,n) | 1307674368000 | Product of Sequence |
Sigma1,15,n or Sigma(1,15,n) | 120 | Sum of Sequence( also called summation) |
2^3 | 8 | Exponent( note this operator is left associative like MS Office) |
5P3 | 60 | Permutaion Method to calculate all the permutaions |
sincostan90 or sin(cos(tan(90))) | 0.017261434031253 | Multiple functions with or without parenthesis (both works) |
Using npm
npm install math-expression-evaluator
Using bower
bower install math-expression-evaluator
npm test
Symbol | Explanation |
---|---|
+ | Addition Operator eg. 2+3 results 5 |
- | Subtraction Operator eg. 2-3 results -1 |
/ | Division operator eg 3/2 results 1.5 |
* | Multiplication Operator eg. 2*3 results 6 |
Mod | Modulus Operator eg. 3 Mod 2 results 1 |
( | Opening Parenthesis |
) | Closing Parenthesis |
Sigma | Summation eg. Sigma(1,100,n) results 5050 |
Pi | Product eg. Pi(1,10,n) results 3628800 |
n | Variable for Summation or Product |
pi | Math constant pi returns 3.14 |
e | Math constant e returns 2.71 |
C | Combination operator eg. 4C2 returns 6 |
P | Permutation operator eg. 4P2 returns 12 |
! | factorial operator eg. 4! returns 24 |
log | logarithmic function with base 10 eg. log 1000 returns 3 |
ln | natural log function with base e eg. ln 2 returns .3010 |
pow | power function with two operator pow(2,3) returns 8 |
^ | power operator eg. 2^3 returns 8 |
root | underroot function root 4 returns 2 |
sin | Sine function |
cos | Cosine function |
tan | Tangent function |
asin | Inverse Sine funtion |
acos | Inverse Cosine funtion |
atan | Inverse Tangent funtion |
sinh | Hyperbolic Sine funtion |
cosh | Hyperbolic Cosine funtion |
tanh | Hyperbolic Tangent funtion |
asinh | Inverse Hyperbolic Sine funtion |
acosh | Inverse Hyperbolic Cosine funtion |
atanh | Inverse Hyperbolic Tangent funtion |
This is a fantastic feature of this calculator that it is capable of evaluating expressions containing Sigma and Pi.
Passing Sigma(1,100,n)
will evaluate to 5050 as n is summationed from 1 to 100.
and Pi(1,15,n) will evaluate to 1307674368000 as n is multiplied from 1 to 15 which is equal to 15!
If a expression is readable by human then it is readable by this evaluator. There is no need to wrap every function inside parenthesis. For eg. sin90 will work totally fine instead of sin(90)
##Changelog
This will reflect in next release named v1.2.16
FAQs
A flexible math expression evaluator
The npm package math-expression-evaluator receives a total of 943,020 weekly downloads. As such, math-expression-evaluator popularity was classified as popular.
We found that math-expression-evaluator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.