1// RUN: %check_clang_tidy -check-suffix=ALL -std=c++20 %s modernize-use-std-numbers %t
2// RUN: %check_clang_tidy -check-suffix=ALL,IMPRECISE -std=c++20 %s modernize-use-std-numbers %t -- -config="{CheckOptions: { modernize-use-std-numbers.DiffThreshold: 0.01 }}"
3
4// CHECK-FIXES-ALL: #include <numbers>
5
6namespace bar {
7 double sqrt(double Arg);
8 float sqrt(float Arg);
9 template <typename T>
10 auto sqrt(T val) { return sqrt(Arg: static_cast<double>(val)); }
11
12 float sqrtf(float Arg);
13 long double sqrtl(long double Arg);
14
15 static constexpr double e = 2.718281828459045235360287471352662497757247093;
16 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:33: warning: prefer 'std::numbers::e' to this literal, differs by '0.00e+00' [modernize-use-std-numbers]
17 // CHECK-FIXES-ALL: static constexpr double e = std::numbers::e;
18}
19
20float expf(float Arg);
21double exp(double Arg);
22long double expl(long double Arg);
23double log(double Arg);
24
25double log2(double Arg);
26float log2(float Arg);
27template <typename T>
28auto log2(T val) { return log2(Arg: static_cast<double>(val)); }
29
30double log10(double Arg);
31
32template<typename T>
33void sink(T&&) { }
34
35void floatSink(float) {}
36
37#define MY_PI 3.1415926
38
39#define INV_SQRT3 1 / bar::sqrt(3)
40#define NOT_INV_SQRT3 1 / bar::sqrt(3) + 1
41
42using my_double = double;
43using my_float = float;
44
45void foo(){
46 static constexpr double Pi = 3.1415926;
47 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:34: warning: prefer 'std::numbers::pi' to this literal, differs by '5.36e-08' [modernize-use-std-numbers]
48 // CHECK-FIXES-ALL: static constexpr double Pi = std::numbers::pi;
49
50 static constexpr double Euler = 2.7182818;
51 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:37: warning: prefer 'std::numbers::e' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
52 // CHECK-FIXES-ALL: static constexpr double Euler = std::numbers::e;
53
54 static constexpr double Phi = 1.6180339;
55 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:35: warning: prefer 'std::numbers::phi' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
56 // CHECK-FIXES-ALL: static constexpr double Phi = std::numbers::phi;
57
58 static constexpr double PiCopy = Pi;
59 static constexpr double PiDefineFromMacro = MY_PI;
60 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:49: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers]
61 // CHECK-FIXES-ALL: static constexpr double PiDefineFromMacro = std::numbers::pi;
62
63 static constexpr double Pi2 = 3.14;
64 // CHECK-MESSAGES-IMPRECISE: :[[@LINE-1]]:35: warning: prefer 'std::numbers::pi' to this literal, differs by '1.59e-03' [modernize-use-std-numbers]
65 // CHECK-FIXES-IMPRECISE: static constexpr double Pi2 = std::numbers::pi;
66 static constexpr double Euler2 = 2.71;
67 // CHECK-MESSAGES-IMPRECISE: :[[@LINE-1]]:38: warning: prefer 'std::numbers::e' to this literal, differs by '8.28e-03' [modernize-use-std-numbers]
68 // CHECK-FIXES-IMPRECISE: static constexpr double Euler2 = std::numbers::e;
69 static constexpr double Phi2 = 1.61;
70 // CHECK-MESSAGES-IMPRECISE: :[[@LINE-1]]:36: warning: prefer 'std::numbers::phi' to this literal, differs by '8.03e-03' [modernize-use-std-numbers]
71 // CHECK-FIXES-IMPRECISE: static constexpr double Phi2 = std::numbers::phi;
72
73 static constexpr double Pi3 = 3.1415926L;
74 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:35: warning: prefer 'std::numbers::pi_v<long double>' to this literal, differs by '5.36e-08' [modernize-use-std-numbers]
75 // CHECK-FIXES-ALL: static constexpr double Pi3 = std::numbers::pi_v<long double>;
76
77 static constexpr double Euler3 = 2.7182818L;
78 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:38: warning: prefer 'std::numbers::e_v<long double>' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
79 // CHECK-FIXES-ALL: static constexpr double Euler3 = std::numbers::e_v<long double>;
80
81 static constexpr double Phi3 = 1.6180339L;
82 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:36: warning: prefer 'std::numbers::phi_v<long double>' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
83 // CHECK-FIXES-ALL: static constexpr double Phi3 = std::numbers::phi_v<long double>;
84
85 static constexpr long double Pi4 = 3.1415926L;
86 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:40: warning: prefer 'std::numbers::pi_v<long double>' to this literal, differs by '5.36e-08' [modernize-use-std-numbers]
87 // CHECK-FIXES-ALL: static constexpr long double Pi4 = std::numbers::pi_v<long double>;
88
89 static constexpr long double Euler4 = 2.7182818L;
90 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:43: warning: prefer 'std::numbers::e_v<long double>' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
91 // CHECK-FIXES-ALL: static constexpr long double Euler4 = std::numbers::e_v<long double>;
92
93 static constexpr long double Phi4 = 1.6180339L;
94 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:41: warning: prefer 'std::numbers::phi_v<long double>' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
95 // CHECK-FIXES-ALL: static constexpr long double Phi4 = std::numbers::phi_v<long double>;
96
97 static constexpr my_double Euler5 = 2.7182818;
98 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:41: warning: prefer 'std::numbers::e' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
99 // CHECK-FIXES-ALL: static constexpr my_double Euler5 = std::numbers::e;
100
101 static constexpr my_float Euler6 = 2.7182818;
102 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:40: warning: prefer 'std::numbers::e' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
103 // CHECK-FIXES-ALL: static constexpr my_float Euler6 = std::numbers::e;
104
105 static constexpr int NotEuler7 = 2.7182818;
106 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:38: warning: prefer 'std::numbers::e' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
107 // CHECK-FIXES-ALL: static constexpr int NotEuler7 = std::numbers::e;
108
109 static constexpr double InvPi = 1.0 / Pi;
110 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:37: warning: prefer 'std::numbers::inv_pi' to this formula [modernize-use-std-numbers]
111 // CHECK-FIXES-ALL: static constexpr double InvPi = std::numbers::inv_pi;
112
113 static constexpr my_float Actually2MyFloat = 2;
114 bar::sqrt(Arg: Actually2MyFloat);
115 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<float>' to this formula [modernize-use-std-numbers]
116 // CHECK-FIXES-ALL: std::numbers::sqrt2_v<float>;
117
118 bar::sqrtf(Arg: 2.0F);
119 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<float>' to this formula [modernize-use-std-numbers]
120 // CHECK-FIXES-ALL: std::numbers::sqrt2_v<float>;
121
122 bar::sqrtl(Arg: 2.0);
123 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<long double>' to this formula [modernize-use-std-numbers]
124 // CHECK-FIXES-ALL: std::numbers::sqrt2_v<long double>;
125
126 sink(MY_PI);
127 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers]
128 // CHECK-FIXES-ALL: sink(std::numbers::pi);
129
130 auto X = 42.0;
131 auto Y = X * 3.14;
132 // CHECK-MESSAGES-IMPRECISE: :[[@LINE-1]]:18: warning: prefer 'std::numbers::pi' to this literal, differs by '1.59e-03' [modernize-use-std-numbers]
133 // CHECK-FIXES-IMPRECISE: auto Y = X * std::numbers::pi;
134
135 constexpr static auto One = 1;
136 constexpr static auto Two = 2;
137
138 bar::sqrt(val: 2);
139 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' to this formula [modernize-use-std-numbers]
140 // CHECK-FIXES-ALL: std::numbers::sqrt2;
141
142 bar::sqrt(val: Two);
143 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' to this formula [modernize-use-std-numbers]
144 // CHECK-FIXES-ALL: std::numbers::sqrt2;
145
146 bar::sqrt(Arg: 2.0);
147 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' to this formula [modernize-use-std-numbers]
148 // CHECK-FIXES-ALL: std::numbers::sqrt2;
149
150 auto Not2 = 2;
151 Not2 = 42;
152 bar::sqrt(val: Not2);
153
154 const auto Actually2 = 2;
155 bar::sqrt(val: Actually2);
156 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' to this formula [modernize-use-std-numbers]
157 // CHECK-FIXES-ALL: std::numbers::sqrt2;
158
159 exp(Arg: 1);
160 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
161 // CHECK-FIXES-ALL: std::numbers::e;
162
163 exp(Arg: One);
164 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
165 // CHECK-FIXES-ALL: std::numbers::e;
166
167 exp(Arg: 1.00000000000001);
168 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
169 // CHECK-FIXES-ALL: std::numbers::e;
170
171 expf(Arg: 1);
172 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v<float>' to this formula [modernize-use-std-numbers]
173 // CHECK-FIXES-ALL: std::numbers::e_v<float>;
174
175 expl(Arg: 1);
176 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e_v<long double>' to this formula [modernize-use-std-numbers]
177 // CHECK-FIXES-ALL: std::numbers::e_v<long double>;
178
179 log2(Arg: exp(Arg: 1));
180 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
181 // CHECK-MESSAGES-ALL: :[[@LINE-2]]:10: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
182 // CHECK-FIXES-ALL: std::numbers::log2e;
183
184 log2(Arg: Euler);
185 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
186 // CHECK-FIXES-ALL: std::numbers::log2e;
187
188 log2(Arg: bar::e);
189 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
190 // CHECK-FIXES-ALL: std::numbers::log2e;
191
192 log2(Arg: Euler5);
193 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
194 // CHECK-FIXES-ALL: std::numbers::log2e;
195
196 log2(Arg: Euler6);
197 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e_v<float>' to this formula [modernize-use-std-numbers]
198 // CHECK-FIXES-ALL: std::numbers::log2e_v<float>;
199
200 log2(val: NotEuler7);
201
202 auto log2e = 1.4426950;
203 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:18: warning: prefer 'std::numbers::log2e' to this literal, differs by '4.09e-08' [modernize-use-std-numbers]
204 // CHECK-FIXES-ALL: auto log2e = std::numbers::log2e;
205
206 floatSink(log2(Arg: Euler));
207 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
208 // CHECK-FIXES-ALL: floatSink(std::numbers::log2e);
209
210 floatSink(static_cast<float>(log2(Arg: Euler)));
211 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this formula [modernize-use-std-numbers]
212 // CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
213
214 floatSink(1.4426950);
215 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e' to this literal, differs by '4.09e-08' [modernize-use-std-numbers]
216 // CHECK-FIXES-ALL: floatSink(std::numbers::log2e);
217
218 floatSink(static_cast<float>(1.4426950));
219 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this literal, differs by '4.09e-08' [modernize-use-std-numbers]
220 // CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
221
222 floatSink(log2(Arg: static_cast<float>(Euler)));
223 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this formula [modernize-use-std-numbers]
224 // CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
225
226 floatSink(static_cast<float>(log2(Arg: static_cast<float>(Euler))));
227 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this formula [modernize-use-std-numbers]
228 // CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
229
230 floatSink(static_cast<float>(log2(val: static_cast<int>(Euler))));
231
232 floatSink(static_cast<int>(log2(Arg: static_cast<float>(Euler))));
233 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:32: warning: prefer 'std::numbers::log2e_v<float>' to this formula [modernize-use-std-numbers]
234 // CHECK-FIXES-ALL: floatSink(static_cast<int>(std::numbers::log2e_v<float>));
235
236 floatSink(1.4426950F);
237 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e_v<float>' to this literal, differs by '1.93e-08' [modernize-use-std-numbers]
238 // CHECK-FIXES-ALL: floatSink(std::numbers::log2e_v<float>);
239
240 floatSink(static_cast<double>(1.4426950F));
241 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e' to this literal, differs by '1.93e-08' [modernize-use-std-numbers]
242 // CHECK-FIXES-ALL: floatSink(std::numbers::log2e);
243
244 floatSink(static_cast<int>(1.4426950F));
245 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:32: warning: prefer 'std::numbers::log2e_v<float>' to this literal, differs by '1.93e-08' [modernize-use-std-numbers]
246 // CHECK-FIXES-ALL: floatSink(static_cast<int>(std::numbers::log2e_v<float>));
247
248 log10(Arg: exp(Arg: 1));
249 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log10e' to this formula [modernize-use-std-numbers]
250 // CHECK-MESSAGES-ALL: :[[@LINE-2]]:11: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
251 // CHECK-FIXES-ALL: std::numbers::log10e;
252
253 log10(Arg: Euler);
254 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log10e' to this formula [modernize-use-std-numbers]
255 // CHECK-FIXES-ALL: std::numbers::log10e;
256
257 log10(Arg: bar::e);
258 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log10e' to this formula [modernize-use-std-numbers]
259 // CHECK-FIXES-ALL: std::numbers::log10e;
260
261 auto log10e = .434294;
262 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:19: warning: prefer 'std::numbers::log10e' to this literal, differs by '4.82e-07' [modernize-use-std-numbers]
263 // CHECK-FIXES-ALL: auto log10e = std::numbers::log10e;
264
265 auto egamma = 0.5772156 * 42;
266 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:19: warning: prefer 'std::numbers::egamma' to this literal, differs by '6.49e-08' [modernize-use-std-numbers]
267 // CHECK-FIXES-ALL: auto egamma = std::numbers::egamma * 42;
268
269 sink(InvPi);
270
271 sink(1 / Pi);
272 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::inv_pi' to this formula [modernize-use-std-numbers]
273 // CHECK-FIXES-ALL: sink(std::numbers::inv_pi);
274
275 sink(1 / bar::sqrt(Arg: Pi));
276 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::inv_sqrtpi' to this formula [modernize-use-std-numbers]
277 // CHECK-FIXES-ALL: sink(std::numbers::inv_sqrtpi);
278
279 sink(1 / bar::sqrt(MY_PI));
280 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::inv_sqrtpi' to this formula [modernize-use-std-numbers]
281 // CHECK-MESSAGES-ALL: :[[@LINE-2]]:24: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers]
282 // CHECK-FIXES-ALL: sink(std::numbers::inv_sqrtpi);
283
284 log(Arg: 2);
285 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::ln2' to this formula [modernize-use-std-numbers]
286 // CHECK-FIXES-ALL: std::numbers::ln2;
287
288 log(Arg: 10);
289 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::ln10' to this formula [modernize-use-std-numbers]
290 // CHECK-FIXES-ALL: std::numbers::ln10;
291
292 bar::sqrt(val: 2);
293 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' to this formula [modernize-use-std-numbers]
294 // CHECK-FIXES-ALL: std::numbers::sqrt2;
295
296 sink(1 / bar::sqrt(val: 3));
297 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::inv_sqrt3' to this formula [modernize-use-std-numbers]
298 // CHECK-MESSAGES-ALL: :[[@LINE-2]]:14: warning: prefer 'std::numbers::sqrt3' to this formula [modernize-use-std-numbers]
299 // CHECK-FIXES-ALL: sink(std::numbers::inv_sqrt3);
300
301 sink(INV_SQRT3);
302 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::inv_sqrt3' to this macro [modernize-use-std-numbers]
303 // CHECK-FIXES-ALL: sink(std::numbers::inv_sqrt3);
304
305 sink(NOT_INV_SQRT3);
306
307 const auto inv_sqrt3f = .577350269F;
308 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:29: warning: prefer 'std::numbers::inv_sqrt3_v<float>' to this literal, differs by '1.04e-08' [modernize-use-std-numbers]
309 // CHECK-FIXES-ALL: const auto inv_sqrt3f = std::numbers::inv_sqrt3_v<float>;
310
311 bar::sqrt(val: 3);
312 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt3' to this formula [modernize-use-std-numbers]
313 // CHECK-FIXES-ALL: std::numbers::sqrt3;
314
315 auto somePhi = 1.6180339;
316 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:20: warning: prefer 'std::numbers::phi' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
317 // CHECK-FIXES-ALL: auto somePhi = std::numbers::phi;
318
319 sink(Phi);
320
321 sink((42 + bar::sqrt(val: 5)) / 2);
322
323 sink((1 + bar::sqrt(val: 5)) / 2);
324 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::phi' to this formula [modernize-use-std-numbers]
325 // CHECK-FIXES-ALL: sink(std::numbers::phi);
326
327 sink((bar::sqrt(Arg: 5.0F) + 1) / 2);
328 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::phi_v<float>' to this formula [modernize-use-std-numbers]
329 // CHECK-FIXES-ALL: sink(std::numbers::phi_v<float>);
330}
331
332
333
334template <typename T>
335void baz(){
336 static constexpr T Pi = 3.1415926;
337 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:29: warning: prefer 'std::numbers::pi' to this literal, differs by '5.36e-08' [modernize-use-std-numbers]
338 // CHECK-FIXES-ALL: static constexpr T Pi = std::numbers::pi;
339
340 static constexpr T Euler = 2.7182818;
341 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:32: warning: prefer 'std::numbers::e' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
342 // CHECK-FIXES-ALL: static constexpr T Euler = std::numbers::e;
343
344 static constexpr T Phi = 1.6180339;
345 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:30: warning: prefer 'std::numbers::phi' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
346 // CHECK-FIXES-ALL: static constexpr T Phi = std::numbers::phi;
347
348 static constexpr T PiCopy = Pi;
349 static constexpr T PiDefineFromMacro = MY_PI;
350 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:44: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers]
351 // CHECK-FIXES-ALL: static constexpr T PiDefineFromMacro = std::numbers::pi;
352
353 static constexpr T Pi2 = 3.14;
354 // CHECK-MESSAGES-IMPRECISE: :[[@LINE-1]]:30: warning: prefer 'std::numbers::pi' to this literal, differs by '1.59e-03' [modernize-use-std-numbers]
355 // CHECK-FIXES-IMPRECISE: static constexpr T Pi2 = std::numbers::pi;
356 static constexpr T Euler2 = 2.71;
357 // CHECK-MESSAGES-IMPRECISE: :[[@LINE-1]]:33: warning: prefer 'std::numbers::e' to this literal, differs by '8.28e-03' [modernize-use-std-numbers]
358 // CHECK-FIXES-IMPRECISE: static constexpr T Euler2 = std::numbers::e;
359 static constexpr T Phi2 = 1.61;
360 // CHECK-MESSAGES-IMPRECISE: :[[@LINE-1]]:31: warning: prefer 'std::numbers::phi' to this literal, differs by '8.03e-03' [modernize-use-std-numbers]
361 // CHECK-FIXES-IMPRECISE: static constexpr T Phi2 = std::numbers::phi;
362
363 static constexpr T Pi3 = 3.1415926L;
364 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:30: warning: prefer 'std::numbers::pi_v<long double>' to this literal, differs by '5.36e-08' [modernize-use-std-numbers]
365 // CHECK-FIXES-ALL: static constexpr T Pi3 = std::numbers::pi_v<long double>;
366
367 static constexpr T Euler3 = 2.7182818L;
368 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:33: warning: prefer 'std::numbers::e_v<long double>' to this literal, differs by '2.85e-08' [modernize-use-std-numbers]
369 // CHECK-FIXES-ALL: static constexpr T Euler3 = std::numbers::e_v<long double>;
370
371 static constexpr T Phi3 = 1.6180339L;
372 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:31: warning: prefer 'std::numbers::phi_v<long double>' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
373 // CHECK-FIXES-ALL: static constexpr T Phi3 = std::numbers::phi_v<long double>;
374
375 static constexpr my_float Actually2MyFloat = 2;
376 bar::sqrt(Arg: Actually2MyFloat);
377 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2_v<float>' to this formula [modernize-use-std-numbers]
378 // CHECK-FIXES-ALL: std::numbers::sqrt2_v<float>;
379
380 constexpr static T One = 1;
381 constexpr static T Two = 2;
382
383 bar::sqrt(val: 2);
384 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' to this formula [modernize-use-std-numbers]
385 // CHECK-FIXES-ALL: std::numbers::sqrt2;
386
387 bar::sqrt(Two);
388
389 bar::sqrt(Arg: 2.0);
390 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' to this formula [modernize-use-std-numbers]
391 // CHECK-FIXES-ALL: std::numbers::sqrt2;
392
393 T Not2 = 2;
394 Not2 = 42;
395 bar::sqrt(Not2);
396
397 const T Actually2 = 2;
398 bar::sqrt(Actually2);
399
400 exp(Arg: 1);
401 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
402 // CHECK-FIXES-ALL: std::numbers::e;
403
404 exp(One);
405
406 exp(Arg: 1.00000000000001);
407 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
408 // CHECK-FIXES-ALL: std::numbers::e;
409
410 log2(Arg: exp(Arg: 1));
411 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
412 // CHECK-MESSAGES-ALL: :[[@LINE-2]]:10: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
413 // CHECK-FIXES-ALL: std::numbers::log2e;
414
415 log2(Euler);
416
417 log2(Arg: bar::e);
418 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log2e' to this formula [modernize-use-std-numbers]
419 // CHECK-FIXES-ALL: std::numbers::log2e;
420
421 T log2e = 1.4426950;
422 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:15: warning: prefer 'std::numbers::log2e' to this literal, differs by '4.09e-08' [modernize-use-std-numbers]
423 // CHECK-FIXES-ALL: T log2e = std::numbers::log2e;
424
425 log10(Arg: exp(Arg: 1));
426 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log10e' to this formula [modernize-use-std-numbers]
427 // CHECK-MESSAGES-ALL: :[[@LINE-2]]:11: warning: prefer 'std::numbers::e' to this formula [modernize-use-std-numbers]
428 // CHECK-FIXES-ALL: std::numbers::log10e;
429
430 log10(Euler);
431
432 log10(Arg: bar::e);
433 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::log10e' to this formula [modernize-use-std-numbers]
434 // CHECK-FIXES-ALL: std::numbers::log10e;
435
436 T log10e = .434294;
437 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:16: warning: prefer 'std::numbers::log10e' to this literal, differs by '4.82e-07' [modernize-use-std-numbers]
438 // CHECK-FIXES-ALL: T log10e = std::numbers::log10e;
439
440 T egamma = 0.5772156 * 42;
441 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:16: warning: prefer 'std::numbers::egamma' to this literal, differs by '6.49e-08' [modernize-use-std-numbers]
442 // CHECK-FIXES-ALL: T egamma = std::numbers::egamma * 42;
443
444 sink(1 / Pi);
445
446 sink(1 / bar::sqrt(Pi));
447
448 sink(1 / bar::sqrt(MY_PI));
449 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::inv_sqrtpi' to this formula [modernize-use-std-numbers]
450 // CHECK-MESSAGES-ALL: :[[@LINE-2]]:24: warning: prefer 'std::numbers::pi' to this macro, differs by '5.36e-08' [modernize-use-std-numbers]
451 // CHECK-FIXES-ALL: sink(std::numbers::inv_sqrtpi);
452
453
454 log(Arg: 2);
455 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::ln2' to this formula [modernize-use-std-numbers]
456 // CHECK-FIXES-ALL: std::numbers::ln2;
457
458 log(Arg: 10);
459 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::ln10' to this formula [modernize-use-std-numbers]
460 // CHECK-FIXES-ALL: std::numbers::ln10;
461
462 bar::sqrt(val: 2);
463 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt2' to this formula [modernize-use-std-numbers]
464 // CHECK-FIXES-ALL: std::numbers::sqrt2;
465
466 sink(1 / bar::sqrt(val: 3));
467 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::inv_sqrt3' to this formula [modernize-use-std-numbers]
468 // CHECK-MESSAGES-ALL: :[[@LINE-2]]:14: warning: prefer 'std::numbers::sqrt3' to this formula [modernize-use-std-numbers]
469 // CHECK-FIXES-ALL: sink(std::numbers::inv_sqrt3);
470
471 bar::sqrt(val: 3);
472 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:5: warning: prefer 'std::numbers::sqrt3' to this formula [modernize-use-std-numbers]
473 // CHECK-FIXES-ALL: std::numbers::sqrt3;
474
475 T phi = 1.6180339;
476 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:13: warning: prefer 'std::numbers::phi' to this literal, differs by '8.87e-08' [modernize-use-std-numbers]
477 // CHECK-FIXES-ALL: T phi = std::numbers::phi;
478
479 sink((42 + bar::sqrt(val: 5)) / 2);
480
481 sink((1 + bar::sqrt(val: 5)) / 2);
482 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::phi' to this formula [modernize-use-std-numbers]
483 // CHECK-FIXES-ALL: sink(std::numbers::phi);
484
485 sink((bar::sqrt(Arg: 5.0F) + 1) / 2);
486 // CHECK-MESSAGES-ALL: :[[@LINE-1]]:10: warning: prefer 'std::numbers::phi_v<float>' to this formula [modernize-use-std-numbers]
487 // CHECK-FIXES-ALL: sink(std::numbers::phi_v<float>);
488}
489
490template <typename T>
491void foobar(){
492 const T Two = 2;
493 bar::sqrt(Two);
494}
495void use_templates() {
496 foobar<float>();
497 foobar<double>();
498
499 baz<float>();
500 baz<double>();
501}
502
503#define BIG_MARCO \
504 struct InvSqrt3 { \
505 template <typename T> static T get() { return 1 / bar::sqrt(3); } \
506 }
507
508BIG_MARCO;
509
510void use_BIG_MACRO() {
511InvSqrt3 f{};
512f.get<float>();
513f.get<double>();
514}
515

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-numbers.cpp