1
1
/*
2
- /home/ed/bin/bin/g++ -std=c++17 -o test_lerp test_lerp.cpp
2
+ /home/ed/bin/bin/g++ -std=c++17 -g -Wall -Wextra - o test_lerp test_lerp.cpp
3
3
*/
4
4
5
5
#include < limits>
9
9
10
10
#include < iostream>
11
11
12
+ template <typename Real>
13
+ bool
14
+ test ()
15
+ {
16
+ const auto a = Real{5 };
17
+ const auto b = Real{10 };
18
+ const auto t = Real{0 .75L };
19
+ const auto x = std::lerp (a, b, t);
20
+
21
+ std::cout << " x = " << x << ' \n ' ;
22
+
23
+ return true ;
24
+ }
25
+
12
26
template <typename Real>
13
27
bool
14
28
test_ends ()
15
29
{
16
30
const auto a = Real{-3 };
17
31
const auto b = Real{+5 };
18
- const auto t = Real{ 3 } / Real{ 4 };
19
- auto ca = std::lerp (a, b, Real{0 });
32
+
33
+ const auto ca = std::lerp (a, b, Real{0 });
20
34
std::cout << " ca = " << ca << ' \n ' ;
21
- std::cout << std::boolalpha << " ca == a: " << (ca == a) << ' \n ' ;
22
- auto cb = std::lerp (a, b, Real{1 });
35
+ std::cout << " ca == a: " << std::boolalpha << (ca == a) << ' \n ' ;
36
+
37
+ const auto cb = std::lerp (a, b, Real{1 });
23
38
std::cout << " cb = " << cb << ' \n ' ;
24
- std::cout << std::boolalpha << " cb == b: " << (cb == b) << ' \n ' ;
39
+ std::cout << " cb == b: " << std::boolalpha << (cb == b) << ' \n ' ;
40
+
25
41
return ca == a && cb == b;
26
42
}
27
43
28
44
template <typename Real>
29
45
bool
30
- test_huge ()
46
+ test_nan ()
31
47
{
32
- const auto a = -std::numeric_limits<Real>::max ();
33
- const auto b = +std::numeric_limits<Real>::max ();
34
- const auto t = Real{3 } / Real{4 };
35
- auto c = std::lerp (a, b, t);
36
- std::cout << " c = " << c << ' \n ' ;
37
- std::cout << std::boolalpha << " c == b/2: " << (c == b / Real{2 }) << ' \n ' ;
38
- return c == b / Real{2 };
48
+ const auto a = Real{5 };
49
+ const auto b = Real{10 };
50
+ const auto t = Real{0 .75L };
51
+
52
+ const auto anan = std::numeric_limits<Real>::quiet_NaN ();
53
+ const auto bnan = std::numeric_limits<Real>::quiet_NaN ();
54
+ const auto tnan = std::numeric_limits<Real>::quiet_NaN ();
55
+
56
+ const auto nan = std::isnan (std::lerp (anan, b, t))
57
+ && std::isnan (std::lerp (a, bnan, t))
58
+ && std::isnan (std::lerp (a, b, tnan));
59
+
60
+ std::cout << " nan: " << std::boolalpha << nan << ' \n ' ;
61
+
62
+ return nan ;
63
+ }
64
+
65
+ template <typename Real>
66
+ bool
67
+ test_subnorm ()
68
+ {
69
+ const auto denorm = std::numeric_limits<Real>::denorm_min ();
70
+
71
+ const auto a = Real{5 } * denorm;
72
+ const auto b = Real{10 } * denorm;
73
+ const auto t = Real{0 .75L };
74
+ const auto x = std::lerp (a, b, t);
75
+ std::cout << " x: " << x / denorm << ' \n ' ;
76
+
77
+ const auto a00 = Real{500 } * denorm;
78
+ const auto b00 = Real{1000 } * denorm;
79
+ const auto x00 = std::lerp (a00, b00, t);
80
+ std::cout << " x00: " << x00 / denorm << ' \n ' ;
81
+ return true ;
39
82
}
40
83
41
84
template <typename Real>
@@ -45,28 +88,58 @@ template<typename Real>
45
88
const auto a = Real{5 };
46
89
const auto b = a;
47
90
const auto t = std::numeric_limits<Real>::infinity ();
48
- auto c = std::lerp (a, b, t);
91
+ const auto c = std::lerp (a, b, t);
49
92
std::cout << " c = " << c << ' \n ' ;
50
- std::cout << std::boolalpha << " c == a: " << (c == a) << ' \n ' ;
93
+ std::cout << " c == a: " << std::boolalpha << (c == a) << ' \n ' ;
51
94
return c == a;
52
95
}
53
96
97
+ template <typename Real>
98
+ bool
99
+ test_huge ()
100
+ {
101
+ const auto max = std::numeric_limits<Real>::max () / 16 ;
102
+ const auto a = Real{5 } * max;
103
+ const auto b = Real{10 } * max;
104
+ const auto t = Real{3 } / Real{4 };
105
+ const auto c = std::lerp (a, b, t);
106
+ std::cout << " c = " << c / max << ' \n ' ;
107
+ std::cout << " c == b/2: " << std::boolalpha << (c == Real{8.75 } * max) << ' \n ' ;
108
+ return c == b / Real{2 };
109
+ }
110
+
54
111
int
55
112
main ()
56
113
{
57
114
bool ok = true ;
58
115
116
+ std::cout << " \n test\n " ;
117
+ ok = ok && test<float >();
118
+ ok = ok && test<double >();
119
+ ok = ok && test<long double >();
120
+
121
+ std::cout << " \n test_nan\n " ;
122
+ ok = ok && test_nan<float >();
123
+ ok = ok && test_nan<double >();
124
+ ok = ok && test_nan<long double >();
125
+
126
+ std::cout << " \n test_ends\n " ;
59
127
ok = ok && test_ends<float >();
60
128
ok = ok && test_ends<double >();
61
129
ok = ok && test_ends<long double >();
62
130
63
- ok = ok && test_huge<float >();
64
- ok = ok && test_huge<double >();
65
- ok = ok && test_huge<long double >();
66
-
131
+ std::cout << " \n test_inf\n " ;
67
132
ok = ok && test_inf<float >();
68
133
ok = ok && test_inf<double >();
69
134
ok = ok && test_inf<long double >();
70
135
136
+ std::cout << " \n test_subnorm\n " ;
137
+ ok = ok && test_subnorm<float >();
138
+
139
+ std::cout << " \n test_huge\n " ;
140
+ ok = ok && test_huge<float >();
141
+ ok = ok && test_huge<double >();
142
+ ok = ok && test_huge<long double >();
143
+
71
144
return ok;
72
145
}
0 commit comments