Does C++11, 14, 17 or 20 introduce a standard constant for pi?

前端 未结 6 1232
失恋的感觉
失恋的感觉 2020-12-08 08:56

There is a rather silly problem with the number pi in C and C++. As far as I know M_PI defined in math.h is not required by any standard.

N

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 09:35

    Up to C++20, no, none of the standards introduces the constant that would represent the number pi (π). You can approximate the number in your code:

    constexpr double pi = 3.14159265358979323846;
    

    Other languages such as C# have the constant declared in their libraries.

    Update: Starting with the C++20, there indeed is a pi constant declared inside the header. It is accessed via: std::numbers::pi.

提交回复
热议问题