GithubHelp home page GithubHelp logo

davidwatkins73 / bigeval.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aviaryan/bigeval.js

0.0 2.0 0.0 83 KB

Fully featured mathematical expression solving library

Home Page: http://aviaryan.in/BigEval.js/index.html

License: Apache License 2.0

JavaScript 86.77% HTML 13.23%

bigeval.js's Introduction

BigEval.js

Build Status Codecov Codacy Badge npm npm npm

An alternative to JavaScript's eval() for solving mathematical expressions. It can be extended to use the Big Number libraries available to provide results with maximum precision.

Installation

  • Node
npm install bigeval
> var BigEval = require('bigeval')
  • From browser
<script src="BigEval.js"></script>

Features

  • Full BODMAS/PEMDAS support (just like Eval).
  • Factorial, Power, Modulo, bitwise and logical operations supported. See Operators section for full list.
  • Support for numbers in scientific notation
  • Support for functions. (Math library functions, User functions)
  • Support for CONSTANTS/variables in expressions.

Using

After including BigEval.js, the first step is to create an instance of BigEval. Then we can use the exec() method to solve an expression. See project page for a working example.

var Obj = new BigEval();
var result = Obj.exec("5! + 1e3 * (PI + E)"); // 5979.874482048837
var result2 = Obj.exec("sin(45 * deg)**2 + cos(pi / 4)**2"); // 1
var result3 = Obj.exec("0 & -7 ^ -7 - 0%1 + 6%2"); //-7
var result4 = Obj.exec("sin( acos( ceil( tan(pi/6) ) ) )"); // sin(0) i.e. 0
var result5 = Obj.exec("((1 << 4) ^ (14 >> 1)) + pi"); // 26.141592653589793

The exec method returns the answer as string. If an error occurs, then Obj.err is set to true and the error message is returned by exec().

Operators

The operators currently supported in order of precedence are -

[
	['!'],  // Factorial
	['**'],  // power
	['/', '*', '%'],
	['+', '-'],
	['<<', '>>'],  // bit shifts
	['<', '<=', '>', '>='],
	['==', '=', '!='],   // equality comparisons
	['&'], ['^'], ['|'],   // bitwise operations
	['&&'], ['||']   // logical operations
]

Functions

BigEval supports functions like sin(), cos() ... When a function is used in an expression, BigEval first looks into its methods to see if such a function exist, then it looks into the JavaScript's Math library and in the end it looks into window's global namespace for the function. Please note that we use just sin() and not Math.sin() in expressions. Attaching a new function to BigEval is easy.

var Obj = new BigEval();
Obj.prototype.avg = function(a, b){
    return this.div( this.add(a,b) , "2");
};

Constants

Constants are nothing but properties of the BigEval object. To use a constant such as PI in an expression, we can simply write PI. Example - sin( PI / 4 ). To add a new constant, we do Obj.CONSTANT.NAME = VALUE. The VALUE should be in string format.

Obj.CONSTANT.INTOCM = '2.54'; // inch to cm
console.log(Obj.exec('12 * intocm')); // the case doesn't matter in expressions

A constant NAME should start with an alphabet and should only use [a-zA-Z0-9_] characters. Default constants include -

PI = 3.1415...
PI_2 = PI / 2
LOG2E = log(e) / log(2)
DEG = PI / 180
E = 2.718...
INFINITY = Infinity
NaN = NaN

Extending with Big Number libraries

BigEval can be extended with any of the big number libraries for JavaScript. To extend BigEval, we only need to change its methods that are responsible for adding, subtracting and so. For an example see the extension MikeMcl-decimal.js which is based on MikeMcl's Decimal.js library. The HTML page extended.html shows how to use this extension.

To use decimaljs extension with Node, download the file and then

var BigEval = require('./MikeMcl-decimal.js');
var b = new BigEval();
console.log(b.exec('-2 + 3'));

Ports

Contributors

bigeval.js's People

Contributors

aviaryan avatar danielgindi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.