Skip to content

Evaluates simple math expressions. Supports variables, parentheses, basic functions.

Notifications You must be signed in to change notification settings

l8nite/matheval

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

matheval is a node.js library that evaluates simple math expressions. Supports variables, parentheses, basic functions.

Example

var evaluate = require('matheval').evaluate;

evaluate('x = 1', console.log); // prints 1
evaluate('x + 1', console.log); // prints 2

Installation

$ npm install matheval

Variables

You can override Variables.js to supply your own variables (for example, you can load them from a database or something)

var evaluate = require('./matheval.js').evaluate;
var Variables = require('./matheval.js').Variables;

var v = new Variables();

evaluate('x = 1', v, function(result) {
    console.log(result);
    evaluate('x + 1', v, console.log);
});

Notes

unary minus is higher precedence than exponentiation, so -1**-2 === (-1)(-2), not -(1(-2))

exponentiation is right-associative, so 333 is 327, not 93

Testing

The tests rely on mocha and should

To run them:

$ npm test

About

Evaluates simple math expressions. Supports variables, parentheses, basic functions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published