C++11、14、17 或 20 是否为 pi 引入了标准常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49778240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does C++11, 14, 17 or 20 introduce a standard constant for pi?
提问by Amomum
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.
C 和 C++ 中的数字 pi 有一个相当愚蠢的问题。据我所知,任何标准都不需要M_PI
定义 in math.h
。
New C++ standards introduced a lot of complicated math in the standard library - hyperbolic functions, std::hermite
and std::cyl_bessel_i
, different random number generators and so on and so forth.
新的 C++ 标准在标准库中引入了很多复杂的数学——双曲函数,std::hermite
以及std::cyl_bessel_i
不同的随机数生成器等等。
Did any of the 'new' standards bring in a constant for pi? If not - why? How does all this complicated math work without it?
是否有任何“新”标准为 pi 带来了常数?如果不是 - 为什么?没有它,所有这些复杂的数学如何运作?
I am aware of similar questions about pi in C++ (they are several years and standards old); I would like to know the current state of the problem.
我知道 C++ 中关于 pi 的类似问题(它们已经有好几年了,而且标准已经过时了);我想知道问题的当前状态。
I am also very interested in why oh whyC++ still doesn't have a pi constant but has a lot of more complicated math.
我也对为什么C++ 仍然没有 pi 常数但有很多更复杂的数学很感兴趣。
UPD: I know that I can define pi myself as 4*atan(1) or acos(1) or double pi = 3.14. Sure. But why in 2018 do I still have to do it? How do standard math functions work without pi?
UPD:我知道我可以将 pi 定义为 4*atan(1) 或 acos(1) 或 double pi = 3.14。当然。但是为什么在2018年我还必须这样做呢?标准数学函数如何在没有 pi 的情况下工作?
UPD2: According to thistrip report for C++ Committee meeting in July 2019 in Cologne, proposal P0631(math constants) was accepted into C++20. So it looks like at long last we will have number pi in the standard library!
UPD2:根据2019 年 7 月在科隆举行的 C++ 委员会会议的这次旅行报告,提案 P0631(数学常数)被 C++20 接受。所以看起来我们终于在标准库中拥有数字 pi!
采纳答案by Bathsheba
No, piis still not a constant introduced into the language, and it's a pain in the neck.
不,pi仍然不是引入到语言中的常量,它是一个令人头疼的问题。
I'm fortunate in that I use boostand they define piwith a sufficiently large number of decimal places for even a 128 bit long double
.
我很幸运,因为我使用了boost并且他们定义了pi,即使是 128 位也有足够多的小数位long double
。
If you don't use Boost then hardcode it yourself. Defining it with a trigonometric function is tempting but if you do that you can't then make it a constexpr
. The accuracy of the trigonometric functions is also not guaranteed by any standard I know of (cf. std::sqrt
), so really you are on dangerous ground indeed relying on such a function.
如果您不使用 Boost,请自行硬编码。用三角函数定义它很诱人,但如果你这样做,你就不能把它变成一个constexpr
. 三角函数的精度也没有(任何标准我知道,保证CF。std::sqrt
),所以真正你在危险的地面确实依赖于这样的功能。
There is a way of getting a constexpr
value for piusing metaprogramming: see http://timmurphy.org/2013/06/27/template-metaprogramming-in-c/
有一种使用元编程constexpr
获取pi值的方法:参见http://timmurphy.org/2013/06/27/template-metaprogramming-in-c/
回答by 0x476f72616e
As others said there is no std::pi
but if you want precise PI
value you can use:
正如其他人所说,没有,std::pi
但如果你想要精确的PI
价值,你可以使用:
constexpr double pi = std::acos(-1);
This assumes that your C++ implementation produces a correctly-rounded value of PI from acos(-1.0)
, which is common but not guaranteed.
这假设您的 C++ 实现从 生成了正确舍入的 PI 值acos(-1.0)
,这是常见但不能保证的。
It's not constexpr
, but in practice optimizing compilers like gcc and clang evaluate it at compile time. Declaring it const
is important for the optimizer to do a good job, though.
不是constexpr
,但实际上优化编译器(如 gcc 和 clang)会在编译时对其进行评估。不过,声明const
优化器做好工作很重要。
回答by Ron
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:
直到 C++20,不,没有任何标准引入表示数字 pi(π)的常数。您可以在代码中近似数字:
constexpr double pi = 3.14159265358979323846;
Other languages such as C# havethe constant declared in their libraries.
如C#等语言有不断的在自己的音乐库声明。
Update:Starting with the C++20, there indeed is a pi
constant declared inside the <numbers>
header. It is accessed via: std::numbers::pi
.
更新:从 C++20 开始,头文件中确实pi
声明了一个常量<numbers>
。它通过以下方式访问:std::numbers::pi
。
回答by Davis Herring
M_PI
is defined by "a standard", if not a languagestandard: POSIXwith the X/Open System Interfaces extension (which is very commonly supported and required for official UNIX branding).
M_PI
如果不是语言标准,则由“标准”定义:带有 X/Open System Interfaces 扩展的POSIX(官方 UNIX 品牌非常普遍支持和要求)。
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).
(仍然)不确定 C++20 中会是什么,但既然你问了:它可能会有这样的常量。该论文在最后一轮 C++20 特性中被合并(用于 2019 年 8 月的委员会草案)。
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<float>
. The full list of constants can be see in [numbers.syn].
具体而言,都将是std::numbers::pi
(类型double
)和可变模板,如果你想有一个不同的浮点类型,例如,你可以使用std::numbers::pi_v<float>
。完整的常量列表可以在[numbers.syn] 中看到。
回答by Hyman Aidley
It is not obviously a good idea because there is no obvious type with which define pi that is universally applicable across domains.
这显然不是一个好主意,因为没有明显的类型来定义普遍适用于跨域的 pi。
Pi is, of course, an irrational number so it cannot be correctly represented by anyC++ type. You might argue that the natural approach, therefore, is to define it in the largest floating point type available. However, the size of the largest standard floating point type long double
is not defined by the C++ standard so the value of the constant would vary between systems. Worse, for any program in which the working type was not this largest type, the definition of pi would be inappropriate since it would impose a performance cost on every use of pi.
当然,Pi 是一个无理数,因此它不能被任何C++ 类型正确表示。因此,您可能会争辩说,自然的方法是以可用的最大浮点类型定义它。但是,long double
C++ 标准没有定义最大标准浮点类型的大小,因此常量的值会因系统而异。更糟糕的是,对于工作类型不是最大类型的任何程序,pi 的定义将是不合适的,因为它会在每次使用 pi 时增加性能成本。
It is also trivial for any programmer to find the value of pi and define their own constant suitable for use, so it does not provide any great advantage to include it in the maths headers.
对于任何程序员来说,找到 pi 的值并定义自己适合使用的常量也是微不足道的,因此将它包含在数学头文件中并没有提供任何很大的优势。
回答by Tiger4Hire
Edited - To remove the term necessary, because it proved controversial. It is too much of an absolute term.
编辑 - 删除必要的术语,因为它被证明是有争议的。这是一个太多的绝对术语。
C++ is a large and complex language, for that reason the Standards Committee only include things which are strongly required. As much as possible is left to non-language standard libraries... like Boost.
boost::math::constants
C++ 是一种庞大而复杂的语言,因此标准委员会只包含强烈要求的东西。尽可能多地留给非语言标准库......比如Boost。
boost::math::constants