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

前端 未结 6 1222
失恋的感觉
失恋的感觉 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:17

    M_PI is defined by "a standard", if not a language standard: POSIX with the X/Open System Interfaces extension (which is very commonly supported and required for official UNIX branding).

    It's (still) not certain what will be in C++20, but since you asked: it probably will have such constants. The paper was merged in the last round of C++20 features (for the Committee Draft in August 2019).

    Specifically, there will both be std::numbers::pi (of type double) and a variable template that you can use if you want a different floating point type, e.g. std::numbers::pi_v. The full list of constants can be see in [numbers.syn].

提交回复
热议问题