33
44<img alt =" TinyExpr logo " src =" https://codeplea.com/public/content/tinyexpr_logo.png " align =" right " />
55
6- #TinyExpr
6+ # TinyExpr
77
88TinyExpr is a very small recursive descent parser and evaluation engine for
99math expressions. It's handy when you want to add the ability to evaluation
@@ -12,7 +12,7 @@ math expressions at runtime without adding a bunch of cruft to you project.
1212In addition to the standard math operators and precedence, TinyExpr also supports
1313the standard C math functions and runtime binding of variables.
1414
15- ##Features
15+ ## Features
1616
1717- ** ANSI C with no dependencies** .
1818- Single source file and header file.
@@ -25,12 +25,12 @@ the standard C math functions and runtime binding of variables.
2525- Easy to use and integrate with your code
2626- Thread-safe, provided that your * malloc* is.
2727
28- ##Building
28+ ## Building
2929
3030TinyExpr is self-contained in two files: ` tinyexpr.c ` and ` tinyexpr.h ` . To use
3131TinyExpr, simply add those two files to your project.
3232
33- ##Short Example
33+ ## Short Example
3434
3535Here is a minimal example to evaluate an expression at runtime.
3636
@@ -40,7 +40,7 @@ Here is a minimal example to evaluate an expression at runtime.
4040```
4141
4242
43- ##Usage
43+ ## Usage
4444
4545TinyExpr defines only four functions:
4646
@@ -51,7 +51,7 @@ TinyExpr defines only four functions:
5151 void te_free(te_expr *expr);
5252```
5353
54- ##te_interp
54+ ## te_interp
5555``` C
5656 double te_interp (const char * expression, int * error);
5757```
@@ -72,7 +72,7 @@ of the parse error on failure, and set `*error` to 0 on success.
7272 double c = te_interp("(5+5", &error); /* Returns NaN, error is set to 4. */
7373```
7474
75- ##te_compile, te_eval, te_free
75+ ## te_compile, te_eval, te_free
7676``` C
7777 te_expr *te_compile (const char * expression, const te_variable * lookup, int lookup_len, int * error);
7878 double te_eval(const te_expr * n);
@@ -117,7 +117,7 @@ After you're finished, make sure to call `te_free()`.
117117
118118```
119119
120- ##Longer Example
120+ ## Longer Example
121121
122122Here is a complete example that will evaluate an expression passed in from the command
123123line. It also does error checking and binds the variables ` x ` and ` y ` to * 3* and * 4* , respectively.
@@ -178,7 +178,7 @@ This produces the output:
178178 5.000000
179179
180180
181- ##Binding to Custom Functions
181+ ## Binding to Custom Functions
182182
183183TinyExpr can also call to custom functions implemented in C. Here is a short example:
184184
@@ -197,7 +197,7 @@ te_expr *n = te_compile("mysum(5, 6)", vars, 1, 0);
197197```
198198
199199
200- ##How it works
200+ ## How it works
201201
202202` te_compile() ` uses a simple recursive descent parser to compile your
203203expression into a syntax tree. For example, the expression ` "sin x + 1/4" `
@@ -216,7 +216,7 @@ and return the result of the expression.
216216` te_free() ` should always be called when you're done with the compiled expression.
217217
218218
219- ##Speed
219+ ## Speed
220220
221221
222222TinyExpr is pretty fast compared to C when the expression is short, when the
@@ -237,7 +237,7 @@ Here is some example performance numbers taken from the included
237237
238238
239239
240- ##Grammar
240+ ## Grammar
241241
242242TinyExpr parses the following grammar:
243243
@@ -262,33 +262,39 @@ notation (e.g. *1e3* for *1000*). A leading zero is not required (e.g. *.5*
262262for * 0.5* )
263263
264264
265- ##Functions supported
265+ ## Functions supported
266266
267267TinyExpr supports addition (+), subtraction/negation (-), multiplication (\* ),
268268division (/), exponentiation (^) and modulus (%) with the normal operator
269269precedence (the one exception being that exponentiation is evaluated
270270left-to-right, but this can be changed - see below).
271271
272- In addition, the following C math functions are also supported:
272+ The following C math functions are also supported:
273273
274274- abs (calls to * fabs* ), acos, asin, atan, atan2, ceil, cos, cosh, exp, floor, ln (calls to * log* ), log (calls to * log10* by default, see below), log10, pow, sin, sinh, sqrt, tan, tanh
275275
276+ The following functions are also built-in and provided by TinyExpr:
277+
278+ - fac (factorials e.g. ` fac 5 ` == 120)
279+ - ncr (combinations e.g. ` ncr(6,2) ` == 15)
280+ - npr (permutations e.g. ` npr(6,2) ` == 30)
281+
276282Also, the following constants are available:
277283
278284- ` pi ` , ` e `
279285
280286
281- ##Compile-time options
287+ ## Compile-time options
282288
283289
284- By default, TinyExpr does exponentation from left to right. For example:
290+ By default, TinyExpr does exponentiation from left to right. For example:
285291
286292` a^b^c == (a^b)^c ` and ` -a^b == (-a)^b `
287293
288294This is by design. It's the way that spreadsheets do it (e.g. Excel, Google Sheets).
289295
290296
291- If you would rather have exponentation work from right to left, you need to
297+ If you would rather have exponentiation work from right to left, you need to
292298define ` TE_POW_FROM_RIGHT ` when compiling ` tinyexpr.c ` . There is a
293299commented-out define near the top of that file. With this option enabled, the
294300behaviour is:
@@ -300,7 +306,7 @@ That will match how many scripting languages do it (e.g. Python, Ruby).
300306Also, if you'd like ` log ` to default to the natural log instead of ` log10 ` ,
301307then you can define ` TE_NAT_LOG ` .
302308
303- ##Hints
309+ ## Hints
304310
305311- All functions/types start with the letters * te* .
306312
0 commit comments