C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_simple_example_09.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Simple Example 09 *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
#include <cstdio>
21
#include <string>
22
23
#include "
exprtk.hpp
"
24
25
26
template
<
typename
T>
27
void
primes
()
28
{
29
typedef
exprtk::symbol_table<T>
symbol_table_t;
30
typedef
exprtk::expression<T>
expression_t;
31
typedef
exprtk::parser<T>
parser_t;
32
typedef
exprtk::function_compositor<T>
compositor_t;
33
typedef
typename
compositor_t::function function_t;
34
35
T x = T(0);
36
37
symbol_table_t symbol_table;
38
39
symbol_table.add_constants();
40
symbol_table.add_variable(
"x"
,x);
41
42
compositor_t compositor(symbol_table);
43
44
//Mode 1 - if statement based
45
compositor.add(
46
function_t(
"is_prime_impl1"
)
47
.vars(
"x"
,
"y"
)
48
.expression
49
(
50
" if (y == 1,true, "
51
" if (0 == (x % y),false, "
52
" is_prime_impl1(x,y - 1))) "
53
));
54
55
compositor.add(
56
function_t(
"is_prime1"
)
57
.var(
"x"
)
58
.expression
59
(
60
" if (frac(x) != 0) "
61
" return [false]; "
62
" else if (x <= 0) "
63
" return [false]; "
64
" else "
65
" is_prime_impl1(x,min(x - 1,trunc(sqrt(x)) + 1)); "
66
));
67
68
//Mode 2 - switch statement based
69
compositor.add(
70
function_t(
"is_prime_impl2"
)
71
.vars(
"x"
,
"y"
)
72
.expression
73
(
74
" switch "
75
" { "
76
" case y == 1 : true; "
77
" case (x % y) == 0 : false; "
78
" default : is_prime_impl2(x,y - 1); "
79
" } "
80
));
81
82
compositor.add(
83
function_t(
"is_prime2"
)
84
.var(
"x"
)
85
.expression
86
(
87
" switch "
88
" { "
89
" case x <= 0 : false; "
90
" case frac(x) != 0 : false; "
91
" default : is_prime_impl2(x,min(x - 1,trunc(sqrt(x)) + 1)); "
92
" } "
93
));
94
95
//Mode 3 - switch statement and for-loop based
96
compositor.add(
97
function_t(
"is_prime3"
)
98
.var(
"x"
)
99
.expression
100
(
101
" switch "
102
" { "
103
" case x <= 1 : return [false]; "
104
" case frac(x) != 0 : return [false]; "
105
" case x == 2 : return [true ]; "
106
" }; "
107
" "
108
" var prime_lut[27] := "
109
" { "
110
" 2, 3, 5, 7, 11, 13, 17, 19, 23, "
111
" 29, 31, 37, 41, 43, 47, 53, 59, 61, "
112
" 67, 71, 73, 79, 83, 89, 97, 101, 103 "
113
" }; "
114
" "
115
" var upper_bound := min(x - 1, trunc(sqrt(x)) + 1); "
116
" "
117
" for (var i := 0; i < prime_lut[]; i += 1) "
118
" { "
119
" if (prime_lut[i] >= upper_bound) "
120
" return [true]; "
121
" else if ((x % prime_lut[i]) == 0) "
122
" return [false]; "
123
" }; "
124
" "
125
" var lower_bound := prime_lut[prime_lut[] - 1] + 2; "
126
" "
127
" for (var i := lower_bound; i < upper_bound; i += 2) "
128
" { "
129
" if ((x % i) == 0) "
130
" { "
131
" return [false]; "
132
" } "
133
" }; "
134
" "
135
" return [true]; "
136
));
137
138
std::string expression_str1 =
"is_prime1(x)"
;
139
std::string expression_str2 =
"is_prime2(x)"
;
140
std::string expression_str3 =
"is_prime3(x)"
;
141
142
expression_t expression1;
143
expression_t expression2;
144
expression_t expression3;
145
expression1.register_symbol_table(symbol_table);
146
expression2.register_symbol_table(symbol_table);
147
expression3.register_symbol_table(symbol_table);
148
149
parser_t parser;
150
151
parser.compile(expression_str1, expression1);
152
parser.compile(expression_str2, expression2);
153
parser.compile(expression_str3, expression3);
154
155
for
(std::size_t i = 0; i < 15000; ++i)
156
{
157
x =
static_cast<
T
>
(i);
158
159
const
T result1 = expression1.value();
160
const
T result2 = expression2.value();
161
const
T result3 = expression3.value();
162
163
const
bool
results_concur = (result1 == result2) &&
164
(result1 == result3) ;
165
166
printf(
"%03d Result1: %c Result2: %c Result3: %c "
167
"Results Concur: %c\n"
,
168
static_cast<
unsigned
int
>
(i),
169
(result1 == T(1)) ?
'T'
:
'F'
,
170
(result2 == T(1)) ?
'T'
:
'F'
,
171
(result3 == T(1)) ?
'T'
:
'F'
,
172
(results_concur) ?
'T'
:
'F'
);
173
}
174
}
175
176
int
main
()
177
{
178
primes<double>
();
179
return
0;
180
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
primes
void primes()
Definition
exprtk_simple_example_09.cpp:27
main
int main()
Definition
exprtk_simple_example_09.cpp:176
Generated by
1.17.0