Issue 131937
Summary [clang-tidy] Check request: boost-use-math-constants
Labels clang-tidy
Assignees
Reporter denzor200
    
Needs a check that will find constants and function calls to math functions that can be replaced with Boost's mathematical constants from the Math library and offers fix-it hints(the same check as modernize-use-std-numbers, but for earlier C++ standarts).

Here is table of all available constants:
https://www.boost.org/doc/libs/1_87_0/libs/math/doc/html/math_toolkit/constants.html

BEFORE
```
double sqrt(double);
double log2(double);
template<typename T>
void sink(T&& val) {}

#define MY_PI 3.1415926

void foo() {
    const double Pi = 3.141592653589;
    const auto Use = Pi / 2;
    static constexpr double Euler = 2.7182818;

    log2(exp(1));
    log2(Euler);
    1 / sqrt(MY_PI);
    sink(MY_PI);
 sink(static_cast<float>(MY_PI));
}
```

AFTER
```
#include <boost/math/constants/constants.hpp>

double sqrt(double);
double log2(double);
template<typename T>
void sink(T&& val) {}

#define MY_PI 3.1415926

void foo() {
    const double Pi = boost::math::double_constants::pi;
    const auto Use = Pi / 2; // no match for Pi
    static constexpr double Euler = boost::math::double_constants::e;

 boost::math::double_constants::log2_e;
 boost::math::double_constants::log2_e;
 boost::math::double_constants::one_div_root_pi;
 sink(boost::math::double_constants::pi);
 sink(boost::math::float_constants::pi);
}
```

This check doesn't require C++11 in Boost 1.75.0 and older.
This check requires C++11 in Boost 1.86.0 and older, and C++14 in Boost 1.87.0 and newer.

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to