C++ Library Extensions 2022.12.09
To help learn modern C++ programming
binary_operations.cpp
Go to the documentation of this file.
1/*
2 Author: Thomas Kim
3 First Edit: Feb. 03, 2021
4
5 Requirements: C++ compilers that supports C++17 Standards
6
7 clang++ -std=c++17 filename.cpp -ltbb -o c.exe
8 g++ -std=c++17 binary_operations.cpp -ltbb -o g.exe
9 cl /EHsc /std:c++17 binary_operations.cpp /Fe: m.exe
10 dpcpp -Qstd=c++17 /EHsc binary_operations.cpp -o d.exe
11*/
12
13#include <tpf_output.hpp>
15
16namespace types = tpf::types;
17
19auto& endl = tpf::endl; // one carriage-return and flush out to console
20auto& endL = tpf::endL; // two carriage-returns and flush out to console
21
23{
24 stream <<"\n--- Summary for Binary Operators for std::array and std::tuple --- "<<endL;
25
26 stream <<"1. tuple-tuple binary operators +, -, *, and / are defined !!" << endL;
27
28 stream <<"2. scalar-tuple, tuple-scalar binary operators +, -, *, and / are defined !!" << endL;
29
30 stream <<"3. array-array binary operators +, -, *, and / are defined !!" << endL;
31
32 stream <<"4. scalar-array, array-scalar binary operators +, -, *, and / are defined !!" << endL;
33
34 stream <<"5. operators ==, !=, >, <, >=, <= are defined for the same type of std::array<T, Size> in C++ Standard."<<endl;
35 stream <<" But I implemented them for differnt types of std::array< T, Size1 > and std::array< S, Size2 >."<<endL;
36}
37
39{
40 stream <<"--- Array Binary Operations ---" << endL;
41
42 std::array a{1.0, 2.5, 3.5};
43 std::array b{1, 2, 3};
44 std::array c{2.0f, 2.5f, 3.2f};
45
46 stream <<"std::array a = " << a << endl;
47 stream <<"std::array b = " << b << endl;
48 stream <<"std::array c = " << c << endL;
49
50 stream<<" 1. Simple Binary Operations with Different Types" << endL;
51 stream << a << " + " << b << " = " << (a + b) << endl;
52 stream << a << " - " << b << " = " << (a - b) << endl;
53 stream << a << " * " << b << " = " << (a * b) << endl;
54 stream << a << " / " << b << " = " << (a / b) << endL;
55
56 std::array aa{2.0, 3.5, 7.5};
57 std::array bb{3, 1, 2};
58 std::array cc{3.0f, 1.5f, 2.2f};
59
60 std::array d{a, aa};
61 std::array e{b, bb};
62 std::array f{c, cc};
63
64 stream <<" d = " << d << endl;
65 stream <<" e = " << e << endl;
66 stream <<" f = " << f << endL;
67
68 stream <<" 2. Nested Binary Operation with Different Types" << endL;
69 stream << d << " + " << e << " = " <<(d + e) << endl;
70 stream << d << " - " << e << " = " <<(d - e) << endl;
71 stream << d << " * " << f << " = " <<(d * f) << endl;
72 stream << d << " / " << f << " = " <<(d / f) << endL;
73
74 stream <<" 3. Simple Scalar-Tuple, Tuple-Scalar Operations" << endL;
75 stream <<" 2 + "<< a <<" = " << (2 + a) << endL;
76 stream <<" " << a <<" + "<< 3 << " = " << (a + 3) << endL;
77 stream <<" 2 - "<< a <<" = " << (2 - a) << endL;
78 stream <<" " << a <<" - "<< 3 << " = " << (a - 3) << endL;
79 stream <<" 2 * "<< a <<" = " << (2 * a) << endL;
80 stream <<" " << a <<" * "<< 3 << " = " << (a * 3) << endL;
81 stream <<" 2 / "<< a <<" = " << (2 / a) << endL;
82 stream <<" " << a <<" / "<< 3 << " = " << (a / 3) << endL;
83
84 stream <<" 4. Nested Scalar-Tuple, Tuple-Scalar Operations" << endL;
85 stream <<" 2 + "<< d <<" = " << (2 + d) << endL;
86 stream <<" " << d <<" + "<< 3 << " = " << (d + 3) << endL;
87 stream <<" 2 - "<< d <<" = " << (2 - d) << endL;
88 stream <<" " << d <<" - "<< 3 << " = " << (d - 3) << endL;
89 stream <<" 2 * "<< d <<" = " << (2 * d) << endL;
90 stream <<" " << d <<" * "<< 3 << " = " << (d * 3) << endL;
91 stream <<" 2 / "<< d <<" = " << (2 / d) << endL;
92 stream <<" " << d <<" / "<< 3 << " = " << (d / 3) << endL;
93}
94
96{
97 stream << "--- Tuple Binary Operations ---" << endL;
98
99 std::tuple a{1, 2.5, 3.5};
100 std::tuple b{1.5, 2, 3};
101 std::tuple c{1, 2, 3 };
102
103 stream <<" a = " << a << endl;
104 stream <<" b = " << b << endl;
105 stream <<" c = " << c << endL;
106
107 stream<<" 1. Simple Binary Operations with Different Types" << endL;
108 stream << a << " + " << b << " = " << (a + b) << endl;
109 stream << a << " - " << b << " = " << (a - b) << endl;
110 stream << a << " * " << b << " = " << (a * b) << endl;
111 stream << a << " / " << b << " = " << (a / b) << endL;
112
113 std::tuple d{a, b};
114 std::tuple e{a, c};
115 std::tuple f{b, c};
116
117 stream <<" d = " << d << endl;
118 stream <<" e = " << e << endl;
119 stream <<" f = " << f << endL;
120
121 stream<<" 2. Nested Binary Operation with Different Types" << endL;
122 stream <<" d + e = "<<(d + e) << endl;
123 stream <<" d - e = "<<(d - e) << endl;
124 stream <<" e * f = "<<(e * f) << endl;
125 stream <<" e / f = "<<(e / f) << endL;
126
127 stream <<" 3. Simple Scalar-Tuple, Tuple-Scalar Operation" << endL;
128 stream <<" 2 + "<< a <<" = " << (2 + a) << endL;
129 stream <<" "<< a <<" + "<< 3 << " = " << (a + 3) << endL;
130 stream <<" 2 - "<< a <<" = " << (2 - a) << endL;
131 stream <<" "<< a <<" - "<< 3 << " = " << (a - 3) << endL;
132 stream <<" 2 * "<< a <<" = " << (2 * a) << endL;
133 stream <<" "<< a <<" * "<< 3 << " = " << (a * 3) << endL;
134 stream <<" 2 / "<< a <<" = " << (2 / a) << endL;
135 stream <<" "<< a <<" / "<< 3 << " = " << (a / 3) << endL;
136
137 stream <<" 4. Nested Scalar-Tuple, Tuple-Scalar Operation" << endL;
138 stream <<" 2 + "<< d <<" = " << (2 + d) << endL;
139 stream <<" "<< d <<" + "<< 3 << " = " << (d + 3) << endL;
140 stream <<" 2 - "<< d <<" = " << (2 - d) << endL;
141 stream <<" "<< d <<" - "<< 3 << " = " << (d - 3) << endL;
142 stream <<" 2 * "<< d <<" = " << (2 * d) << endL;
143 stream <<" "<< d <<" * "<< 3 << " = " << (a * 3) << endL;
144 stream <<" 2 / "<< d <<" = " << (2 / d) << endL;
145 stream <<" "<< d <<" / "<< 3 << " = " << (d / 3) << endL;
146}
147
149{
150 stream << "--- File Contents Summary ---\n\n--- Tuple Equality Comparison Operators - defined in Standard C++! " << endL;
151
152 std::tuple a{1, 2.5, 3.5};
153 std::tuple b{1.5, 2, 3};
154 std::tuple<double, double, double> c{1, 2.5, 3.5};
155
156 stream <<" std::tuple<int, double, double> a = " << a << endL;
157 stream <<" std::tuple<double, int, int> b = " << b << endL;
158 stream <<" std::tuple<double, double, double> c = " << c << endL;
159
160 stream<<" 1. Simple Equality Operators" << endL;
161 stream << " (a == a) ? " << (a == a) << endl;
162 stream << " (a == b) ? " << (a == b) << " <- Comparison Between Differet Types" << endl;
163 stream << " (a != a) ? " << (a != a) << endl;
164 stream << " (a != b) ? " << (a != b) << " <- Comparison Between Differet Types" << endl;
165 stream << " (a == c) ? " << (a == c) << " <- Comparison Between Differet Types" << endL;
166
167 stream<<" 2. Nested Equality Operators" << endL;
168
169 std::tuple d{a, b};
170 std::tuple e{a, c};
171
172 stream <<" d = " << d << endl;
173 stream <<" e = " << e << endl;
174
175 stream <<" (d == d) ? " << (d == d) << endl;
176 stream <<" (d == e) ? " << (d == e) << endl;
177 stream <<" (d != d) ? " << (d != d) << endl;
178 stream <<" (d != e) ? " << (d != e) << endL;
179
180 stream<<" 3. Inequality Operators" << endL;
181
182 stream <<" d = " << d << endl;
183 stream <<" e = " << e << endl;
184
185 stream <<" (d > d) ? " << (d > d) << endl;
186 stream <<" (d > e) ? " << (d > e) << endl;
187 stream <<" (d < e) ? " << (d < e) << endL;
188}
189
191{
192 stream <<"--- Array Equality Comparison Operators With Different Types With the Same Size! " << endL;
193
194 std::array a{1, 2, 3};
195 std::array b{1.0, 2.0, 3.0};
196 std::array c{1.0, 2.5, 3.0};
197
198 stream <<" std::array<int, 3> a = " << a << endl;
199 stream <<" std::array<double, 3> b = " << b << endl;
200 stream <<" std::array<double, 3> c = " << c << endL;
201
202 stream << "1. Array Equality with Different Type, of Same Size! " << endL;
203
204 stream <<"<int, 3>"<< a <<" == "<< b << " <double, 3> (all elements pairwise equal) ? "
205 << (a == b) << endl;
206
207 stream <<"<int, 3>"<< a <<" == "<< c << "<double, 3> (all elements pairwise equal) ? "
208 << (a == c) << endl;
209
210 stream <<"<int, 3>"<< a <<" != "<< b << " <double, 3> !(all elements pairwise equal) ? "
211 << (a != b) << endl;
212
213 stream <<"<int, 3>"<< a <<" != "<< c << "<double, 3> !(all elements pairwise equal) ? "
214 << (a != c) << endL;
215
216 stream << "2. Array Comparison with Different Type, of Same Size! " << endL;
217
218 stream <<"<int, 3> "<< a << " > " << b <<" <double, 3> (all elements pairwise > ) ? "
219 << (a > b) << endl;
220
221 stream <<"<int, 3> "<< a << " >= " << b <<" <double, 3> (all elements pairwise >= ) ? "
222 << (a >= b) << endl;
223
224 stream <<"<int, 3> "<< a << " < " << b <<" <double, 3> (all elements pairwise < ) ? "
225 << (a < b) << endl;
226
227 stream <<"<int, 3> "<< a << " <= " << b <<" <double, 3> (all elements pairwise <= ) ? "
228 << (a <= b) << endl;
229
230 stream <<"<int, 3> "<< a << " < " << c <<"<double, 3> (all elements pairwise < ) ? "
231 << (a < c) << endl;
232
233 stream <<"<int, 3> "<< a << " <= " << c <<"<double, 3> (all elements pairwise <= ) ? "
234 << (a <= c) << endl;
235
236 stream <<"<int, 3> "<< a << " > " << c <<"<double, 3> (all elements pairwise > ) ? "
237 << (a > c) << endl;
238
239 stream <<"<int, 3> "<< a << " >= " << c <<"<double, 3> (all elements pairwise >= ) ? "
240 << (a >= c) << endL;
241}
242
244{
245 stream << "--- Array Equality Comparison Operators With Same Types With DIFFERENT Size! " << endL;
246
247 std::array a{1, 2, 3};
248 std::array b{1, 2, 3, 4};
249
250 stream <<" std::array<int, 3> a = " << a << endl;
251 stream <<" std::array<int, 4> b = " << b << endL;
252
253 stream << a << " == " << b << " ? " << (a == b) << endl;
254 stream << a << " != " << b << " ? " << (a != b) << endl;
255
256 stream << a << " < " << b << " ? " << (a < b) << endl;
257 stream << a << " <= " << b << " ? " << (a <= b) << endl;
258 stream << a << " > " << b << " ? " << (a > b) << endl;
259 stream << a << " >= " << b << " ? " << (a >= b) << endL;
260
261 stream <<"\t\t--- By Thomas Kim, Feb. 05, 2020. ---" << endl;
262}
263
264int main()
265{
267
269
271
273
275
277}
auto & endl
tpf::sstream stream
void test_array_binary_operations()
void test_tuple_binary_operations()
auto & endL
void test_array_equality_comparison_different_size_operations()
void test_tuple_equality_comparison_binary_operations()
void test_array_equality_comparison_same_size_operations()
void summary_for_binary_operators_for_array_tuple()
int main()
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.
Type functions are implemented.