-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_op_binary.cpp
80 lines (72 loc) · 2.55 KB
/
test_op_binary.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "minifadtest.hpp"
template <typename ad_type> static
void test_binary_op() {
ad_type x(4,0),y(2,1),f;
typedef typename ad_type::value_type value_type; typedef value_type T; float h = 0.5f;
f = x+y; assertclose(f.val(),6); assertclose(f.grd(0),1); assertclose(f.grd(1), 1);
f = x-y; assertclose(f.val(),2); assertclose(f.grd(0),1); assertclose(f.grd(1),-1);
f = x*y; assertclose(f.val(),8); assertclose(f.grd(0),2); assertclose(f.grd(1), 4);
f = x/y; assertclose(f.val(),2); assertclose(f.grd(0),h); assertclose(f.grd(1),-1);
const T a=2;
f = x+a; assertclose(f.val(),T(4)+a); assertclose(f.grd(0),1); assertclose(f.grd(1),0);
f = x-a; assertclose(f.val(),T(4)-a); assertclose(f.grd(0),1); assertclose(f.grd(1),0);
f = x*a; assertclose(f.val(),T(4)*a); assertclose(f.grd(0),a); assertclose(f.grd(1),0);
f = x/a; assertclose(f.val(),T(4)/a); assertclose(f.grd(0),h); assertclose(f.grd(1),0);
const T b=2;
f = b+y; assertclose(f.val(),b+T(2)); assertclose(f.grd(0),0); assertclose(f.grd(1), 1);
f = b-y; assertclose(f.val(),b-T(2)); assertclose(f.grd(0),0); assertclose(f.grd(1),-1);
f = b*y; assertclose(f.val(),b*T(2)); assertclose(f.grd(0),0); assertclose(f.grd(1), b);
f = b/y; assertclose(f.val(),b/T(2)); assertclose(f.grd(0),0); assertclose(f.grd(1),-h);
}
template <typename value_type> static
void test_common() {
typedef MiniFAD::Fad<value_type,0> ad_type_0;
typedef MiniFAD::Fad<value_type,2> ad_type_2;
typedef MiniFAD::Fad<value_type,3> ad_type_3;
test_binary_op<ad_type_0>();
test_binary_op<ad_type_2>();
test_binary_op<ad_type_3>();
}
template <typename float_type> static
void test_real() {
test_common<float_type>();
}
template <typename float_type> static
void test_cxx_complex() {
#if MINIFAD_HAVE_COMPLEX_CXX
typedef std::complex<float_type> complex_type;
test_common<complex_type>();
#endif
}
template <typename float_type> static
void test_c99_complex() {
#if MINIFAD_HAVE_COMPLEX_C99
typedef typename c99complex<float_type>::type complex_type;
test_common<complex_type>();
#endif
}
template <typename float_type> static
void test_complex() {
test_cxx_complex<float_type>();
test_c99_complex<float_type>();
}
#if MINIFAD_HAVE_FLOAT128
template <>
inline void test_complex<__float128>()
{test_c99_complex<__float128>();}
#endif
template <typename float_type> static
void test() {
test_real<float_type>();
test_complex<float_type>();
}
int main(int,char *[])
{
test< float >();
test< double >();
test< long double >();
#if MINIFAD_HAVE_FLOAT128
test< __float128 >();
#endif
return 0;
}