我想在一些c++程序中使用PI常数和三角函数。我得到三角函数包含<math。h>。然而,在这个头文件中似乎没有PI的定义。

如何获得PI而不手动定义它?


当前回答

而不是写作

#define _USE_MATH_DEFINES

我建议使用- use_math_definitions或/ d_use_math_definitions,这取决于你的编译器。

通过这种方式,即使有人在您之前包含了头文件(并且没有使用#define),您仍然可以得到常量,而不是一个晦涩的编译器错误,您需要花费很长时间来查找。

其他回答

c++ 14允许你执行静态constexpr auto pi = acos(-1);

来自math.h的Posix手册页:

   The  <math.h>  header  shall  provide for the following constants.  The
   values are of type double and are accurate within the precision of  the
   double type.

   M_PI   Value of pi

   M_PI_2 Value of pi/2

   M_PI_4 Value of pi/4

   M_1_PI Value of 1/pi

   M_2_PI Value of 2/pi

   M_2_SQRTPI
          Value of 2/ sqrt pi

C + + 20 std::数字pi

最后,它来了:http://eel.is/c++draft/numbers

main.cpp

#include <numbers> // std::numbers
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::fixed << std::setprecision(20);
    std::cout << "float       " << std::numbers::pi_v<float> << std::endl;
    std::cout << "double      " << std::numbers::pi << std::endl;
    std::cout << "long double " << std::numbers::pi_v<long double> << std::endl;
    std::cout << "exact       " << "3.141592653589793238462643383279502884197169399375105820974944" << std::endl;
}

其中,精确的计算结果为:

echo "scale=60; 4*a(1)" | BC_LINE_LENGTH=0 bc -l

如何使用Bash命令计算pi

编译并运行:

g++-10 -ggdb3 -O0 -std=c++20 -Wall -Wextra -pedantic -o main.out main.cpp
./main.out

输出:

float       3.14159274101257324219
double      3.14159265358979311600
long double 3.14159265358979323851
exact       3.141592653589793238462643383279502884197169399375105820974944

在Ubuntu 20.04 amd64, GCC 10.2.0上测试

已接受的建议如下:

5.0. “头”(头) 在表[tab: cppp .library.]Headers],需要添加一个新的<math>头。 […] 命名空间STD { 命名空间math { template<typename T > inline constexpr T pi_v = undefined; Inline constexpr double PI = pi_v<double>;

还有一个std::numbers::e当然:-)如何计算欧拉常数或欧拉驱动在c++ ?

这些常量使用c++ 14变量模板特性:有什么使用例子吗?

在草案的早期版本中,常量位于std::math::pi: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0631r7.pdf之下

我会这么做

template<typename T>
T const pi = std::acos(-T(1));

or

template<typename T>
T const pi = std::arg(-std::log(T(2)));

我不会把π输入到你需要的精度。这到底是什么意思?你需要的精度是T的精度,但是我们对T一无所知。

你可能会说:What are You talking about?T是float, double或long double。因此,只需输入long double的精度,即。

template<typename T>
T const pi = static_cast<T>(/* long double precision π */);

但是你真的知道在未来的标准中不会有比long double精度更高的新的浮点类型吗?你不。

这就是为什么第一个解很漂亮。可以肯定的是,这个标准将会使三角函数过载而产生一种新的类型。

请不要说三角函数在初始化时的计算是性能损失。

在c++ 20标准库中,π被定义为std::numbers::pi_v,用于浮点数、双精度数和长双精度数。

#include <numbers>
auto n = std::numbers::pi_v<float>;

并且可以专门化为用户定义的类型。