-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.cpp
More file actions
65 lines (52 loc) · 1.8 KB
/
example.cpp
File metadata and controls
65 lines (52 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include "mexce.h"
int main()
{
using std::cout;
using std::endl;
short x0 = 0;
float x = 0.0f;
double y = 0.1;
int z = 200;
mexce::evaluator eval;
// associate runtime variables with their aliases in the expression.
eval.bind(x0, "x", y, "y", z, "z");
// binding a variable as "log" will fail, as there is a function called "log"
cout << endl << "Attempting to bind 'x' as 'log'" << endl;
try {
eval.bind(x, "log");
}
catch (std::exception& e) {
cout << " " << e.what() << endl;
}
// binding a variable as "pi" will fail, as there is a built-in constant called "pi"
cout << endl << "Attempting to bind 'x' as 'pi'" << endl;
try {
eval.bind(x, "pi");
}
catch (std::exception& e) {
cout << " " << e.what() << endl;
}
// replaces previous binding of variable x0 to "x"
eval.bind(x, "x");
eval.set_expression("0.3+(-sin(2.33+x-logb((.3*pi+(88/y)/e),3.2+z)))/988.472e-02");
cout << endl << "Evaluation results:" << endl;
for (int i = 0; i < 10; i++, x-=0.1f, y+=0.212, z+=2) {
cout << " " << eval.evaluate() << endl; // evaluation will use bound variables x, y and z
}
// attempting to unbind an unknown variable, will throw an exception
cout << endl << "Attempting to unbind w" << endl;
try {
eval.unbind("w");
}
catch (std::exception& e) {
cout << " " << e.what() << endl;
}
// Releasing a bound variable which is contained in the assigned expression will
// invalidate the expression.
cout << endl << "Unbinding x" << endl;
eval.unbind("x");
cout << endl << "Evaluation result:" << endl;
cout << " " << eval.evaluate() << endl; // Now it will return 0
return 0;
}