[clang-tidy] feature request: modernize-use-std-numbers to replace macros like `M_PI` defined in `<math.h>` with the correspondings math constants defined in `std::numbers` (C++20)
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Prepare compile_commands.json
```json
[{
"directory": "/tmp",
"command": "clang++ -c -std=c++20 -o a.o a.cpp",
"file": "a.cpp",
"output": "a.o"
}]
```
and a.cpp
```cpp
#include
int main() {
double x = std::sin(0.20 * M_PI);
}
```
I would like to suggest something like
```
a.cpp:4:30: warning: use std::numbers::pi [modernize-use-numbers]
4 | double x = std::sin(0.20 * M_PI);
^~~~
std::numbers::pi
```
which modifies as follows:
```diff
@@ -1,5 +1,6 @@
#include
+#include
int main() {
- double x = std::sin(0.20 * M_PI);
+ double x = std::sin(0.20 * std::numbers::pi);
}
```
The targeted macros are
- `M_PI` → `std::numbers::pi`, `M_PIf` → `std::numbers::pi_v`, `M_PIl` → `std::numbers::pi_v`
- `M_E` → `std::numbers::e`
- `M_LOG2E` → `std::numbers::log2e`
- `M_SQRT2` → `std::numbers::sqrt2`, and so forth
and constant expressions if detectable
- `std::exp(1.0)`, `std::log10(2.0)`, `std::sqrt(2.0)`,…
already supported
Contributor guide
Assessment
This issue has not been assessed yet.