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

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