3
3
4
4
<img alt =" TinyExpr logo " src =" https://codeplea.com/public/content/tinyexpr_logo.png " align =" right " />
5
5
6
- #TinyExpr
6
+ # TinyExpr
7
7
8
8
TinyExpr is a very small recursive descent parser and evaluation engine for
9
9
math 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.
12
12
In addition to the standard math operators and precedence, TinyExpr also supports
13
13
the standard C math functions and runtime binding of variables.
14
14
15
- ##Features
15
+ ## Features
16
16
17
17
- ** ANSI C with no dependencies** .
18
18
- Single source file and header file.
@@ -25,12 +25,12 @@ the standard C math functions and runtime binding of variables.
25
25
- Easy to use and integrate with your code
26
26
- Thread-safe, provided that your * malloc* is.
27
27
28
- ##Building
28
+ ## Building
29
29
30
30
TinyExpr is self-contained in two files: ` tinyexpr.c ` and ` tinyexpr.h ` . To use
31
31
TinyExpr, simply add those two files to your project.
32
32
33
- ##Short Example
33
+ ## Short Example
34
34
35
35
Here is a minimal example to evaluate an expression at runtime.
36
36
@@ -40,7 +40,7 @@ Here is a minimal example to evaluate an expression at runtime.
40
40
```
41
41
42
42
43
- ##Usage
43
+ ## Usage
44
44
45
45
TinyExpr defines only four functions:
46
46
@@ -51,7 +51,7 @@ TinyExpr defines only four functions:
51
51
void te_free(te_expr *expr);
52
52
```
53
53
54
- ##te_interp
54
+ ## te_interp
55
55
``` C
56
56
double te_interp (const char * expression, int * error);
57
57
```
@@ -72,7 +72,7 @@ of the parse error on failure, and set `*error` to 0 on success.
72
72
double c = te_interp("(5+5", &error); /* Returns NaN, error is set to 4. */
73
73
```
74
74
75
- ##te_compile, te_eval, te_free
75
+ ## te_compile, te_eval, te_free
76
76
``` C
77
77
te_expr *te_compile (const char * expression, const te_variable * lookup, int lookup_len, int * error);
78
78
double te_eval(const te_expr * n);
@@ -117,7 +117,7 @@ After you're finished, make sure to call `te_free()`.
117
117
118
118
```
119
119
120
- ##Longer Example
120
+ ## Longer Example
121
121
122
122
Here is a complete example that will evaluate an expression passed in from the command
123
123
line. 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:
178
178
5.000000
179
179
180
180
181
- ##Binding to Custom Functions
181
+ ## Binding to Custom Functions
182
182
183
183
TinyExpr can also call to custom functions implemented in C. Here is a short example:
184
184
@@ -197,7 +197,7 @@ te_expr *n = te_compile("mysum(5, 6)", vars, 1, 0);
197
197
```
198
198
199
199
200
- ##How it works
200
+ ## How it works
201
201
202
202
` te_compile() ` uses a simple recursive descent parser to compile your
203
203
expression into a syntax tree. For example, the expression ` "sin x + 1/4" `
@@ -216,7 +216,7 @@ and return the result of the expression.
216
216
` te_free() ` should always be called when you're done with the compiled expression.
217
217
218
218
219
- ##Speed
219
+ ## Speed
220
220
221
221
222
222
TinyExpr 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
237
237
238
238
239
239
240
- ##Grammar
240
+ ## Grammar
241
241
242
242
TinyExpr parses the following grammar:
243
243
@@ -262,33 +262,39 @@ notation (e.g. *1e3* for *1000*). A leading zero is not required (e.g. *.5*
262
262
for * 0.5* )
263
263
264
264
265
- ##Functions supported
265
+ ## Functions supported
266
266
267
267
TinyExpr supports addition (+), subtraction/negation (-), multiplication (\* ),
268
268
division (/), exponentiation (^) and modulus (%) with the normal operator
269
269
precedence (the one exception being that exponentiation is evaluated
270
270
left-to-right, but this can be changed - see below).
271
271
272
- In addition, the following C math functions are also supported:
272
+ The following C math functions are also supported:
273
273
274
274
- 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
275
275
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
+
276
282
Also, the following constants are available:
277
283
278
284
- ` pi ` , ` e `
279
285
280
286
281
- ##Compile-time options
287
+ ## Compile-time options
282
288
283
289
284
- By default, TinyExpr does exponentation from left to right. For example:
290
+ By default, TinyExpr does exponentiation from left to right. For example:
285
291
286
292
` a^b^c == (a^b)^c ` and ` -a^b == (-a)^b `
287
293
288
294
This is by design. It's the way that spreadsheets do it (e.g. Excel, Google Sheets).
289
295
290
296
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
292
298
define ` TE_POW_FROM_RIGHT ` when compiling ` tinyexpr.c ` . There is a
293
299
commented-out define near the top of that file. With this option enabled, the
294
300
behaviour is:
@@ -300,7 +306,7 @@ That will match how many scripting languages do it (e.g. Python, Ruby).
300
306
Also, if you'd like ` log ` to default to the natural log instead of ` log10 ` ,
301
307
then you can define ` TE_NAT_LOG ` .
302
308
303
- ##Hints
309
+ ## Hints
304
310
305
311
- All functions/types start with the letters * te* .
306
312
0 commit comments