arith-eval ~master

A minimal math expression evaluation library.


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

ArithEval

ArithEval is a minimal arithmetic expression evaluator library for the D programming language. In other words, define a math function as a string depending on as many variables as you want (including none), then evaluate that function giving those variables the values you want. It is NOT designed to be efficient, just easy to use. Bear this in mind if your application requires time-sensitive evaluations.

This library is licensed under the MIT software license.

How to use

Minimal the library, minimal the tutorial, really. Just instance the Evaluable struct from the arith_eval package and define your function:

import arith_eval;

auto constantExpression = Evaluable!()("2 + 5");
assert(constantExpression() == 7);

auto a = Evaluable!("x", "y")("(x + y) * x - 3 * 2 * y");
assert(a.eval(2, 2) == (2 + 2) * 2 - 3 * 2 * 2);
assert(a.eval(3, 5) == (3 + 5) * 3 - 3 * 2 * 5);

auto b = Evaluable!("x", "z")("x ^ (2 * z)");
assert(b.eval(1.5f, 1.3f).approxEqual(1.5f ^^ (2 * 1.3f));

Evaluable is a struct template that takes the name of its variables as its template parameters.

Evaluable will throw an InvalidExpressionException if it isn't able to understand the expression given, and an EvaluationException if something wrong happens during evaluation, such as the value reaching unreliably high values.

Note that currently ArithEval will not check your expressions for variables not specified in the template, and using those should be considered an error, so please be extra careful in that aspect. I'll implement the checking once I have some time.

Supported operations

Currently, supported math operations are the following:

  • x + y
  • x - y
  • - x
  • x * y
  • x / y
  • x ^ y (x to the power of y)
  • x E y (x times 10 to the power of y)

Parenthesis should work wherever you place them, respecting basic math operation priorities.

If you are missing a specific operation, open an issue or submit a PR.

Add as DUB dependency

Just add the arith-eval package as a dependency in your dub.json or dub.sdl file. For example:

"dependencies" : {
        "arith-eval": "~>0.5.0"
}

Attributions

The following pieces of work make this library possible:

  • Pegged, by Philippe Sigaud, released under the Boost license. Used for input parsing.
  • unit-threaded, by Atila Neves, released under the BSD-3-Clause license. Used for the testing of the library.
Authors:
  • Héctor Barreras Almarcha @Dechcaudron
Dependencies:
pegged
Versions:
0.5.1 2018-Jan-08
0.5.0 2018-Jan-07
0.4.1 2017-Dec-26
0.4.0 2016-Jun-27
0.3.2 2016-Jun-27
Show all 10 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 198 downloads total

Score:
0.6
Short URL:
arith-eval.dub.pm