8
8
#include < limits>
9
9
#include < complex>
10
10
#include < iostream>
11
+ #include < type_traits>
11
12
#include < cmath>
12
13
13
14
using namespace boost ::decimal;
14
15
15
16
template <typename T>
16
- bool test_equal (T lhs, T rhs, int tol = 10 ) noexcept
17
+ bool test_equal (T lhs, T rhs, int tol = 15 ) noexcept
17
18
{
18
19
using std::fabs ;
19
20
const bool res = fabs (lhs - rhs) < static_cast <T>(tol) * std::numeric_limits<T>::epsilon ();
@@ -51,7 +52,6 @@ void test_construction()
51
52
BOOST_TEST (test_equal (v2.imag (), T{2 }));
52
53
}
53
54
54
- /*
55
55
template <typename T>
56
56
void test_unary_operators ()
57
57
{
@@ -61,12 +61,12 @@ void test_unary_operators()
61
61
62
62
const complex_scalar val {T{2 }, T{-2 }};
63
63
complex_scalar pos_val = +val;
64
- BOOST_TEST_EQ( val.real(), pos_val.real());
65
- BOOST_TEST_EQ( val.imag(), pos_val.imag());
64
+ BOOST_TEST ( test_equal ( val.real (), pos_val.real () ));
65
+ BOOST_TEST ( test_equal ( val.imag (), pos_val.imag () ));
66
66
67
67
complex_scalar neg_val = -val;
68
- BOOST_TEST_EQ( neg_val.real(), T{-2});
69
- BOOST_TEST_EQ( neg_val.imag(), T{2});
68
+ BOOST_TEST ( test_equal ( neg_val.real (), T{-2 }) );
69
+ BOOST_TEST ( test_equal ( neg_val.imag (), T{2 }) );
70
70
}
71
71
72
72
template <typename T>
@@ -80,8 +80,8 @@ void test_addition()
80
80
complex_scalar rhs_1 {T{2 }, T{2 }};
81
81
complex_scalar res_1 = lhs_1 + rhs_1;
82
82
83
- BOOST_TEST_EQ( res_1.real(), T{3});
84
- BOOST_TEST_EQ( res_1.imag(), T{3});
83
+ BOOST_TEST ( test_equal ( res_1.real (), T{3 }) );
84
+ BOOST_TEST ( test_equal ( res_1.imag (), T{3 }) );
85
85
}
86
86
87
87
template <typename T>
@@ -95,8 +95,8 @@ void test_subtraction()
95
95
complex_scalar rhs_1 {T{2 }, T{2 }};
96
96
complex_scalar res_1 = lhs_1 - rhs_1;
97
97
98
- BOOST_TEST_EQ( res_1.real(), T{-1});
99
- BOOST_TEST_EQ( res_1.imag(), T{-1});
98
+ BOOST_TEST ( test_equal ( res_1.real (), T{-1 }) );
99
+ BOOST_TEST ( test_equal ( res_1.imag (), T{-1 }) );
100
100
}
101
101
102
102
template <typename T>
@@ -110,8 +110,8 @@ void test_multiplication()
110
110
complex_scalar rhs_1 {T{2 }, T{2 }};
111
111
complex_scalar res_1 = lhs_1 * rhs_1;
112
112
113
- BOOST_TEST_EQ( res_1.real(), T{-12});
114
- BOOST_TEST_EQ( res_1.imag(), T{0});
113
+ BOOST_TEST ( test_equal ( res_1.real (), T{-12 }) );
114
+ BOOST_TEST ( test_equal ( res_1.imag (), T{0 }) );
115
115
}
116
116
117
117
template <typename T>
@@ -125,8 +125,8 @@ void test_division()
125
125
complex_scalar rhs_1 {T{2 }, T{2 }};
126
126
complex_scalar res_1 = lhs_1 / rhs_1;
127
127
128
- BOOST_TEST_EQ( res_1.real(), T{2});
129
- BOOST_TEST_EQ( res_1.imag(), T{-1});
128
+ BOOST_TEST ( test_equal ( res_1.real (), T{2 }) );
129
+ BOOST_TEST ( test_equal ( res_1.imag (), T{-1 }) );
130
130
}
131
131
132
132
template <typename T>
@@ -162,8 +162,8 @@ void test_non_member_real_imag()
162
162
163
163
complex_scalar lhs {T{2 }, T{-1 }};
164
164
165
- BOOST_TEST_EQ( real(lhs), lhs.real());
166
- BOOST_TEST_EQ( imag(lhs), lhs.imag());
165
+ BOOST_TEST ( test_equal ( real (lhs), lhs.real () ));
166
+ BOOST_TEST ( test_equal ( imag (lhs), lhs.imag () ));
167
167
}
168
168
169
169
template <typename T>
@@ -179,7 +179,7 @@ void test_abs()
179
179
BOOST_TEST (test_equal (static_cast <T>(abs (lhs)), static_cast <T>(sqrt (T{2 }))));
180
180
}
181
181
182
- template <typename T>
182
+ template <typename T, std:: enable_if_t <std::is_floating_point<T>::value, bool > = true >
183
183
void test_arg ()
184
184
{
185
185
using std::complex;
@@ -195,6 +195,21 @@ void test_arg()
195
195
BOOST_TEST (test_equal (arg (complex_scalar{T{-1 }, T{0 }}), pi <T>()));
196
196
}
197
197
198
+ template <typename T, std::enable_if_t <!std::is_floating_point<T>::value, bool > = true >
199
+ void test_arg ()
200
+ {
201
+ using std::complex;
202
+ using std::polar;
203
+ using std::arg;
204
+ using complex_scalar = decltype (polar (T (), T ()));
205
+
206
+ BOOST_TEST (test_equal (arg (complex_scalar{T{1 }, T{0 }}), T{0 }));
207
+ BOOST_TEST (test_equal (arg (complex_scalar{T{0 }, T{0 }}), T{0 }));
208
+ BOOST_TEST (test_equal (arg (complex_scalar{T{0 }, T{1 }}), numbers::pi_v<T> / 2 ));
209
+ BOOST_TEST (test_equal (arg (complex_scalar{T{-1 }, T{0 }}), numbers::pi_v<T>));
210
+ }
211
+
212
+
198
213
template <typename T>
199
214
void test_norm ()
200
215
{
@@ -236,13 +251,9 @@ void test_proj()
236
251
lhs = complex_scalar{T{std::numeric_limits<T>::infinity ()}, T{1 }};
237
252
complex_scalar rhs = complex_scalar{T{std::numeric_limits<T>::infinity ()}, T{0 }};
238
253
BOOST_TEST_EQ (proj (lhs), rhs);
239
-
240
- lhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{-1}};
241
- rhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{-0}};
242
- BOOST_TEST_EQ(proj(lhs), rhs);
243
254
}
244
255
245
- template <typename T>
256
+ template <typename T, std:: enable_if_t <std::is_floating_point<T>::value, bool > = true >
246
257
void test_exp ()
247
258
{
248
259
using std::complex;
@@ -258,7 +269,22 @@ void test_exp()
258
269
BOOST_TEST (test_equal (lhs.imag (), rhs.imag ()));
259
270
}
260
271
261
- template <typename T>
272
+ template <typename T, std::enable_if_t <!std::is_floating_point<T>::value, bool > = true >
273
+ void test_exp ()
274
+ {
275
+ using std::complex;
276
+ using std::polar;
277
+ using std::exp ;
278
+ using complex_scalar = decltype (polar (T (), T ()));
279
+
280
+ complex_scalar lhs {T{0 }, numbers::pi_v<T>};
281
+ lhs = exp (lhs);
282
+ complex_scalar rhs {T{-1 }, T{0 }};
283
+ BOOST_TEST (test_equal (lhs.real (), rhs.real ()));
284
+ BOOST_TEST (test_equal (lhs.imag (), rhs.imag ()));
285
+ }
286
+
287
+ template <typename T, std::enable_if_t <std::is_floating_point<T>::value, bool > = true >
262
288
void test_log ()
263
289
{
264
290
using std::complex;
@@ -291,6 +317,41 @@ void test_log()
291
317
BOOST_TEST (test_equal (lhs.imag (), rhs.imag ()));
292
318
}
293
319
320
+ /*
321
+ template <typename T, std::enable_if_t<!std::is_floating_point<T>::value, bool> = true>
322
+ void test_log()
323
+ {
324
+ using std::complex;
325
+ using std::polar;
326
+ using std::log;
327
+ using boost::math::constants::half_pi;
328
+ using boost::math::constants::pi;
329
+ using complex_scalar = decltype(polar(T(), T()));
330
+
331
+ complex_scalar lhs {T{0}, T{1}};
332
+ lhs = log(lhs);
333
+ complex_scalar rhs {T{0}, numbers::pi_v<T> / 2};
334
+
335
+ BOOST_TEST(test_equal(lhs.real(), rhs.real()));
336
+ BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
337
+
338
+ lhs = {T{-1}, T{0}};
339
+ lhs = log(lhs);
340
+ rhs = {T{0}, numbers::pi_v<T>};
341
+
342
+ BOOST_TEST(test_equal(lhs.real(), rhs.real()));
343
+ BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
344
+
345
+ // Other side of the cut line
346
+ lhs = {T {-1}, -T {0}};
347
+ lhs = log(lhs);
348
+ rhs = {T {0}, -numbers::pi_v<T>};
349
+
350
+ BOOST_TEST(test_equal(lhs.real(), rhs.real()));
351
+ BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
352
+ }
353
+ */
354
+
294
355
template <typename T>
295
356
void test_scalar_addition ()
296
357
{
@@ -302,8 +363,8 @@ void test_scalar_addition()
302
363
T rhs_1 {T{2 }};
303
364
complex_scalar res_1 = lhs_1 + rhs_1;
304
365
305
- BOOST_TEST_EQ( res_1.real(), T{3});
306
- BOOST_TEST_EQ( res_1.imag(), T{1});
366
+ BOOST_TEST ( test_equal ( res_1.real (), T{3 }) );
367
+ BOOST_TEST ( test_equal ( res_1.imag (), T{1 }) );
307
368
}
308
369
309
370
template <typename T>
@@ -317,8 +378,8 @@ void test_scalar_subtraction()
317
378
T rhs_1 {T{2 }};
318
379
complex_scalar res_1 = lhs_1 - rhs_1;
319
380
320
- BOOST_TEST_EQ( res_1.real(), T{-1});
321
- BOOST_TEST_EQ( res_1.imag(), T{1});
381
+ BOOST_TEST ( test_equal ( res_1.real (), T{-1 }) );
382
+ BOOST_TEST ( test_equal ( res_1.imag (), T{1 }) );
322
383
}
323
384
324
385
template <typename T>
@@ -332,8 +393,8 @@ void test_scalar_multiplication()
332
393
T rhs_1 {T{2 }};
333
394
complex_scalar res_1 = lhs_1 * rhs_1;
334
395
335
- BOOST_TEST_EQ( res_1.real(), T{6});
336
- BOOST_TEST_EQ( res_1.imag(), T{4});
396
+ BOOST_TEST ( test_equal ( res_1.real (), T{6 }) );
397
+ BOOST_TEST ( test_equal ( res_1.imag (), T{4 }) );
337
398
}
338
399
339
400
template <typename T>
@@ -347,10 +408,11 @@ void test_scalar_division()
347
408
T rhs_1 {T{2 }};
348
409
complex_scalar res_1 = lhs_1 / rhs_1;
349
410
350
- BOOST_TEST_EQ( res_1.real(), T{2});
351
- BOOST_TEST_EQ( res_1.imag(), T{1});
411
+ BOOST_TEST ( test_equal ( res_1.real (), T{2 }) );
412
+ BOOST_TEST ( test_equal ( res_1.imag (), T{1 }) );
352
413
}
353
414
415
+ /*
354
416
template <typename T>
355
417
void test_log10()
356
418
{
@@ -364,7 +426,9 @@ void test_log10()
364
426
complex_scalar rhs {T{2}, static_cast<T>(1.36438)};
365
427
BOOST_TEST(test_equal(lhs.real(), rhs.real()));
366
428
}
429
+ */
367
430
431
+ /*
368
432
template <typename T>
369
433
void test_pow()
370
434
{
@@ -395,6 +459,7 @@ void test_pow()
395
459
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
396
460
#endif
397
461
}
462
+ */
398
463
399
464
template <typename T>
400
465
void test_sqrt ()
@@ -480,7 +545,6 @@ void test_tanh()
480
545
BOOST_TEST (test_equal (lhs.real (), rhs.real ()));
481
546
BOOST_TEST (test_equal (lhs.imag (), rhs.imag ()));
482
547
}
483
- */
484
548
485
549
int main ()
486
550
{
@@ -489,7 +553,6 @@ int main()
489
553
test_construction<decimal32>();
490
554
test_construction<decimal64>();
491
555
492
- /*
493
556
test_unary_operators<float >();
494
557
test_unary_operators<double >();
495
558
test_unary_operators<decimal32>();
@@ -555,10 +618,12 @@ int main()
555
618
test_exp<decimal32>();
556
619
test_exp<decimal64>();
557
620
621
+ /*
558
622
test_log<float>();
559
623
test_log<double>();
560
624
test_log<decimal32>();
561
625
test_log<decimal64>();
626
+ */
562
627
563
628
test_scalar_addition<float >();
564
629
test_scalar_addition<double >();
@@ -580,6 +645,7 @@ int main()
580
645
test_scalar_division<decimal32>();
581
646
test_scalar_division<decimal64>();
582
647
648
+ /*
583
649
test_log10<float>();
584
650
test_log10<double>();
585
651
test_log10<decimal32>();
@@ -589,6 +655,7 @@ int main()
589
655
test_pow<double>();
590
656
test_pow<decimal32>();
591
657
test_pow<decimal64>();
658
+ */
592
659
593
660
test_sqrt<float >();
594
661
test_sqrt<double >();
@@ -609,7 +676,6 @@ int main()
609
676
test_tanh<double >();
610
677
test_tanh<decimal32>();
611
678
test_tanh<decimal64>();
612
- */
613
679
614
680
return boost::report_errors ();
615
681
}
0 commit comments