https://github.com/sd0e/parsemath
🔢 Fast JS math equation parser
https://github.com/sd0e/parsemath
math parse-equation
Last synced: 3 months ago
JSON representation
🔢 Fast JS math equation parser
- Host: GitHub
- URL: https://github.com/sd0e/parsemath
- Owner: sd0e
- License: mit
- Created: 2023-01-21T15:58:23.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-24T21:36:09.000Z (over 2 years ago)
- Last Synced: 2025-03-20T05:32:24.949Z (about 1 year ago)
- Topics: math, parse-equation
- Language: HTML
- Homepage: https://git.sebdoe.com/parsemath/
- Size: 238 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# parsemath
[](https://git.sebdoe.com/parsemath/coverage/lcov-report/)
> Disclaimer: I am not an expert in number parsing and, although many unit tests are in place for this library, I cannot confirm that it will work for all use cases, so do not use this library if reliability is important. **I do not hold any liability if this code does not work as expected.**
> Not related to npm package of same name
A JavaScript library which parses an equation in string format to a number. It skips the traditional method of generating a syntax tree, instead evaluating the expression from the inside directly through a recursive method, resulting in code which often executes much more quickly than others (and sometimes orders of magnitude faster). This speed does not come at the cost of more advanced features; it supports implicit multiplication, multiple units of angles, and multiple custom variables.
## Current abilities
* Evaluate mathematical expressions with the following operators: `^`, `*`, `/`, `+`, `-`
* Evaluate mathematical expressions with the following operators containing a number inside the brackets, with **trigonometric values represented in radians by default**: `sqrt()`, `sin()`, `cos()`, `tan()`, `asin()`, `acos()`, `atan()`, `abs()`
* Evaluate mathematical expressions with brackets
* Evaluate mathematical expressions containing multi-digit positive and negative integers and floats
* Evaluate mathematical expressions involving the mathematical constants `e` and `Ï€` (the `enableConstants` parameter must be set to true for this to work)
* Evaluate mathematical expressions involving variables in the Roman and Greek alphabets
* Evaluate mathematical expressions with trigonometric functions using *either* radians or degrees
## Certain things that are known to not work
(This is not an exhaustive list)
* Placing a number or variable immediately before a function (e.g. `8cos(0.5)`)
* Modulus brackets (`|`) (please instead use `abs()`)
## Usage
The function accepts the following arguments in their respective order:
* **equation** (*string*): the equation to be parsed
* **enableConstants** (*boolean*, default is `true`): whether to enable the mathematical constants `e` and `Ï€` when parsing equation
* **variables** (*object*, default is `null`): any custom variables to be used when parsing equation
* **angleMode** (*string*, can be either `rad` or `deg`, default is `rad`): the mode to be used with the angles
#### Example `variables` object
```js
{
"x": 0,
"y": -4.3
}
```
## Examples
#### Simple Equation
```js
ParseMath('5 + 3 * 6 / 2') // 14
```
#### Equation with Constants
```js
Number(ParseMath('3e').toFixed(3)) // 8.155
```
#### Equation with Custom Variables
```js
ParseMath('3x^2 - 5x + 3', true, {"x": 6}) // 81
```
#### Equation with Trigonometric Function
```js
Number(ParseMath('5*1-(sin(2)*tan(2))').toFixed(3)) // 6.987
```
#### Equation with Trigonometric Function in Degrees
```js
Number(ParseMath('sin(arccos(0.5) + 1)', false, null, 'deg').toFixed(3)) // 0.875
```
Note that this library has the standard JavaScript floating point math issue where there are occasionally inaccurate results, such as `0.1 + 0.2 = 0.30000000000000004`. This can be resolved by the user by rounding the answer to an appropriate number of decimal places.
Again, this library has not been fully tested so may not work as expected with these abilities.