Skip to content

morriswmz/MathEvaluator.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

A Simple Math Evaluator in JavaScript

As a practice in writing a tokenizer and parser. Currently support real math operations. Next version will include complex number operations. Inspired by http://effbot.org/zone/simple-top-down-parsing.htm

Usage

Simple pass a string to the evaluator and it will parse and evaluate the result. The result is an Object containing three properties:

  • success (Boolean) - whether evaluation is successful
  • msg (String) - error message
  • answer (Number) - actual result

It support almost all arithmetic operations including +, -, *, /, ^, %, and shift operations like >>, >>>, <<:

console>MathEvaluator.evaluate('(1+2^3)%3-4>>1')
console>{success:true, answer:-2}

It also support basic functions like sin, cos, tan, exp, log, etc:

console>MathEvaluator.evaluate('sin(pi/2)+1')
console>{success:true, answer:2}

About

A simple js library to evaluate math expressions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published