arith-eval 0.4.1

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 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.

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 package and define your function:

import arith_eval;

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

auto intEvaluable = Evaluable!(int, "x", "y")("(x + y) * x - 3 * 2 * y");
assert(intEvaluable(2, 2) == (2 + 2) * 2 - 3 * 2 * 2);
assert(intEvaluable(3, 5) == (3 + 5) * 3 - 3 * 2 * 5);

import std.math: pow;

auto floatEvaluable = Evaluable!(float, "x", "z")("x ** (2 * z)");
assert(floatEvaluable(1.5f, 1.3f) == pow(1.5f, 2 * 1.3f));

Evaluable, as you can see, is a template struct that takes the evaluation type and 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 OverflowException if it overflows during calculation at a given point for the specified evaluation type.

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.

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.4.1"
  • Héctor Barreras Almarcha @Dechcaudron
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

  • 194 downloads total

Short URL: