All Projects → peakchen90 → decimal-eval

peakchen90 / decimal-eval

Licence: MIT license
A tiny, safe, fast JavaScript library for decimal arithmetic expressions.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to decimal-eval

math eval
✖️➕➖➗ `math_eval` safely evaluates mathematical expressions
Stars: ✭ 33 (+83.33%)
Mutual labels:  math, eval, math-eval
eval-estree-expression
Safely evaluate JavaScript (estree) expressions, sync and async.
Stars: ✭ 22 (+22.22%)
Mutual labels:  eval, expression-evaluator
DecFP.jl
Julia IEEE decimal floating-point via the Intel decimal-float library
Stars: ✭ 49 (+172.22%)
Mutual labels:  math, decimal
cldr-engine
Internationalization and localization in Typescript with Unicode CLDR, batteries included
Stars: ✭ 34 (+88.89%)
Mutual labels:  math, decimal
expression-evaluator
A math expression evaluator built with JavaScript
Stars: ✭ 25 (+38.89%)
Mutual labels:  math, expression-evaluator
Fasteval
Fast and safe evaluation of algebraic expressions
Stars: ✭ 177 (+883.33%)
Mutual labels:  math, eval
string-math
Evaluates a math expression from a string. Supports variables and custom operators.
Stars: ✭ 14 (-22.22%)
Mutual labels:  math, expression-evaluator
Mathjs
An extensive math library for JavaScript and Node.js
Stars: ✭ 11,861 (+65794.44%)
Mutual labels:  math, expression-evaluator
Expressionevaluator
A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts
Stars: ✭ 194 (+977.78%)
Mutual labels:  math, eval
java.math.expression.parser
java math expression parser is faster than JEP
Stars: ✭ 25 (+38.89%)
Mutual labels:  math, expression-evaluator
racket-cas
Simple computer algebra system
Stars: ✭ 58 (+222.22%)
Mutual labels:  math
fcal
Extensive math expression evaluator library for JavaScript and Node.js
Stars: ✭ 86 (+377.78%)
Mutual labels:  math
cas
Cellular Automata Simulator
Stars: ✭ 22 (+22.22%)
Mutual labels:  math
stockholm
💵 Modern Python library for working with money and monetary amounts. Human friendly and flexible approach for development. 100% test coverage + built-in support for GraphQL and Protocol Buffers transports using current best-practices.
Stars: ✭ 26 (+44.44%)
Mutual labels:  calculation
MO-Problem-Journal
A journal of theorems, lemmas and problems for Mathematical Olympiads.
Stars: ✭ 45 (+150%)
Mutual labels:  math
pyGameMath
Math library for game programming in python.
Stars: ✭ 20 (+11.11%)
Mutual labels:  math
grafar
Reactive multidimensional math & data visualization for the web.
Stars: ✭ 548 (+2944.44%)
Mutual labels:  math
Mathematical-Modeling
数学建模算法学习
Stars: ✭ 189 (+950%)
Mutual labels:  math
AbstractFFTs.jl
A Julia framework for implementing FFTs
Stars: ✭ 72 (+300%)
Mutual labels:  math
Machine-Learning-Explained
Learn the theory, math and code behind different machine learning algorithms and techniques.
Stars: ✭ 30 (+66.67%)
Mutual labels:  math

decimal-eval

A tiny, safe, fast JavaScript library for decimal arithmetic expressions.

Build Status Codecov npm BUNDLE SIZE GitHub

English | 简体中文

Features

  • ✌️ Automatically deal with the JavaScript decimal precision problem by bignumber.js, and supports big number
  • 🚀 Fast and tiny, only 28.7 KB minified and 11.2 KB gzipped (pure only 10.5 KB minified and 3.3 KB gzipped without bignumber.js)
  • ✍️ Easy to extend custom operator
  • 🖖 Supports expression scope variables

Get Started

Installation

# use npm
npm i -S decimal-eval

# or use yarn
yarn add decimal-eval

Usage

Supports the four arithmetic operations (+, -, *, /), and automatically deal with JavaScript decimal precision by bignumber.js, and supports big number.

import {evaluate} from 'decimal-eval';

evaluate('0.1 + 0.2') // '0.3'
evaluate('100 * (0.08 - 0.01)'); // '7'
evaluate('9007199254740992 + 1'); // '9007199254740993'
evaluate('1 + abc', { abc: 2 }); // '3'

In addition to the above operators, it also supports custom operator expansion, and supports unary operators and binary operators. See also reference document for the precedence of built-in operators.

import {evaluate, Parser} from 'decimal-eval';

// create binary operator `add`, the precedence is 13
const addOp = Parser.createBinaryOperator('add', 13, (left, right) => {
  return String(Number(left) + Number(right));
});

// create unary operator `sin`, the precedence is 16
const sinOp = Parser.createUnaryOperator('sin', 16, (value) => {
  return String(Math.sin(value));
});

// install custom operators
Parser.useOperator(addOp);
Parser.useOperator(sinOp);

// same as: `1 + Math.sin(-2)`
evaluate('1 add sin -2') // '0.09070257317431829'

API

evaluate(expression: string, scope?: Record<string, number>): number

Parse the arithmetic expression and then calculate the result. The name of scope has the following restrictions:

  • muse be start with a-z or A-Z
  • only includes a-zA-Z0-9 and _
import {evaluate} from 'decimal-eval';

evaluate('1 + 2'); // '3'
evaluate('1 + abc', { abc: 2 }); // '3'

Parser

new Parser(expression: string).parse(): Node | null

Parse arithmetic expressions.

import {Parser} from 'decimal-eval';

const node = new Parser('1 + 2').parse();

new Parser(expression: string).compile(): (scope: Record<string, number | string>) => string

Compile and cache the arithmetic expression.

import {Parser} from 'decimal-eval';

const evaluate = new Parser('1 + abc').compile();
evaluate({ abc: 2 }); // '3'
evaluate({ abc: 9 }); // '10'
evaluate({ def: 1 }); // throw error, the scope name `abc` is not initialized

Parser.createBinaryOperator(value: string, precedence: number, calc: (left: string, right: string) => string): Operator

Create a binary operator.

Parser.createUnaryOperator(value: string, precedence: number, calc: (value: string) => string): Operator

Create a unary operator.

Parser.useOperator(operator: Operator): void

Install an operator which created by the Parser.createBinaryOperator() or Parser.createUnaryOperator() method.

Parser.useAdapter(adapter)

Set custom calculation adapter methods for four arithmetic (+, -, *, /). bignumber.js is used by default.

Parser.useAdapter({
  '+': (left, right) => String(Number(left) + Number(right)),
  '-': (left, right) => String(Number(left) - Number(right)),
  '*': (left, right) => String(Number(left) * Number(right)),
  '/': (left, right) => String(Number(left) / Number(right))
})

Advanced

Use pure package (no dependencies)

When using a custom method to deal with the decimal precision problem, you can use a pure package, which can reduce the size by about 60%. It doesn't include bignumber.js.

import {evaluate, Parser} from 'decimal-eval/dist/pure';

// set custom calculation adapter
// Parser.useAdapter(adapter);

// Does not deal with precision by default
evaluate('0.1 + 0.2'); // '0.30000000000000004'

// not supports big number by default
evaluate('9007199254740992 + 1'); // '9007199254740992'

Re-export bignumber.js

Useful for deal JavaScript decimal precision problem without having to install bignumber.js again.

import {BigNumber} from 'decimal-eval';
const val = new BigNumber(0.1).plus(0.2);
console.log(String(val)); // '0.3'

Precedence of built-in operators

operator precedence
... + ... 13
... - ... 13
... * ... 14
... / ... 14
+ ... 16
- ... 16
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].