arith-eval 0.2.0

A minimal math evaluator 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, then evaluate that function giving those variables the values you want.

This library is licensed under the terms of the GNU GPL3 free software license. Free as in freedom. Also as in free beer. Also as in gluten-free. (Warning: beer might not be gluten-free)

Currently, ArithEval uses Pegged as its base for parsing math expressions, although this might change in the future.

How to use

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

import arith_eval.evaluable;

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

import std.math: pow;

auto b = evaluable!("x", "z")("x ** (2 * z)");
assert(b(1.5f, 1.3f) == pow(1.5f, 2 * 1.3f));

evaluable is a public alias for immutable Evaluable (it is an immutable struct).

Evaluable, as you can see, is a template struct that takes the name of its variables as its template parameters, and defines the eval method to later substitute those variables in the same order in the original math expression.

Evaluable will throw an InvalidExpressionException if it isn't able to understand the expression given.

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)

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

If you are missing a specific operation, open an issue or, even better, implement it yourself and submit a PR.

Limitations

opCall currently takes floats and returns a float. No double or real so far, sorry. If you need it, open issue or 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.2.0"
}
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