aritheval 0.4.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 glutenfree. (Warning: beer might not be glutenfree)
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!(int, "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!(float, "x", "z")("x ** (2 * z)");
assert(b(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 ofy
)
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 aritheval
package as a dependency in your dub.json or dub.sdl file. For example:
"dependencies" : {
"aritheval": "~>0.4.0"
}
 Registered by Héctor Barreras Almarcha
 0.4.0 released 8 years ago
 Dechcaudron/ArithEval
 GPL3.0
 Authors:
 Dependencies:
 pegged
 Versions:

0.5.1 2018Jan08 0.5.0 2018Jan07 0.4.1 2017Dec26 0.4.0 2016Jun27 0.3.2 2016Jun27  Download Stats:


0 downloads today

0 downloads this week

0 downloads this month

198 downloads total

 Score:
 0.6
 Short URL:
 aritheval.dub.pm