npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bigeval

v3.2.3

Published

Mathematical expression solving library

Downloads

299

Readme

BigEval.js

GitHub Action CI codecov 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.
If you want it to fallback into the global namespace (i.e window) when it cannot find a function, you should set bigEval.fallbackToGlobalFunctions = true;.
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