Git Product home page Git Product logo

formula's Introduction

formula

Formula is a parser and evaluator for mathematical and logical formula language.

Example

To parse a formula, instantiate the Parser and call the method parser(..).

Expression expression = new Parser().parse("=2*2^2");

To evaluate the expression, instantiate an Evaluator and call evaluate(..).

Evaluator evaluator = new Evaluator();
int result = evaluator.evaluate(expression);

Here is a bit more complicated example with user defined variables, functions und other formulas wich are used inside target one.

// Define variables
Map<String, Object> variables = new HashMap<>();
variables.put("a", 1);
variables.put("text1", "TEXT_1");
variables.put("text2", "TEXT_2");

// Define functions
Map<String, IFunction> functions = new HashMap<>();
functions.put("sqrt", value -> Math.sqrt((Double)value[0]));

// Define formulas
Map<String, Expression> formulas = new HashMap<>();
formulas.put("f1", new Parser().parse("=a*2"));
formulas.put("f2", new Parser().parse("=a*3"));

// Use it all
Evaluator evaluator = new Evaluator(functions, variables, formulas);

Expression expression = new Parser().parse("=5 * when(text1=text2;=f1;=f2)");
evaluator.evaluate(expression);

// The result is 15.0
double result = evaluator.evaluate(expression);

Description

The formula language supports a number of built-in mathematical and comparison operators and user defined variables, functions and references to other formulas.

  • A formula should be started with = character. Although it is optional.

  • Mathematical operators: +, -, *, /, ^

  • Comparison operators: <, <=, =, >=, > and <>

  • A variable is a string which consist letter and number characters and must begin with a letter.

  • A function is a string (name of the function) with subsequent ( and ) characters. The function may have parametrs. They must be placed between ( and ). The multiple parameters must be delimited by the ; character.

  • A formula reference begins with = character folowing by a string (name of the formula).

License

Apache License Version 2.0 (see https://raw.githubusercontent.com/eduardsdv/formula/master/LICENSE file for the full text)

formula's People

Contributors

eduardsdv avatar

Stargazers

 avatar

Watchers

 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.