// Toy implementation:
#if __cplusplus <= 201103L
# include <bits/c++14_warning.h>
#else
namespace std
{
namespace experimental
{
inline namespace maths_v1
{
inline namespace constants
{
inline namespace math_constants
{
/// Constant: @f$ \pi @f$ (see POSIX @c M_PI).
template<typename _RealType>
constexpr _RealType
m_pi = 3.1415'92653'58979'32384'62643'38327'95028'84195e+0L;
/// Constant: @f$ \pi / 2 @f$ (see POSIX @c M_PI_2).
template<typename _RealType>
constexpr _RealType
m_pi_2 = 1.5707'96326'79489'66192'31321'69163'97514'42098e+0L;
/// Constant: @f$ \pi / 3 @f$.
template<typename _RealType>
constexpr _RealType
m_pi_3 = 1.0471'97551'19659'77461'54214'46109'31676'28063e+0L;
/// Constant: @f$ \pi / 4 @f$ (see POSIX @c M_PI_4).
template<typename _RealType>
constexpr _RealType
m_pi_4 = 7.8539'81633'97448'30961'56608'45819'87572'10488e-1L;
/// Constant: @f$ 4 \pi / 3 @f$.
template<typename _RealType>
constexpr _RealType
m_4pi_3 = 4.1887'90204'78639'09846'16857'84437'26705'12253e+0L;
/// Constant: @f$ 2 \pi @f$.
template<typename _RealType>
constexpr _RealType
m_2pi = 6.2831'85307'17958'64769'25286'76655'90057'68391e+0L;
/// Constant: @f$ 4 \pi @f$.
template<typename _RealType>
constexpr _RealType
m_4pi = 1.2566'37061'43591'72953'85057'35331'18011'53678e+1L;
/// Constant: degrees per radian @f$ 180 / \pi @f$.
template<typename _RealType>
constexpr _RealType
m_deg_rad = 5.7295'77951'30823'20876'79815'48141'05170'33237e+1L;
/// Constant: radians per degree @f$ \pi / 180 @f$.
template<typename _RealType>
constexpr _RealType
m_rad_deg = 1.7453'29251'99432'95769'23690'76848'86127'13443e-2L;
/// Constant: @f$ \sqrt{\pi / 2} @f$.
template<typename _RealType>
constexpr _RealType
m_sqrt_pi_2 = 1.2533'14137'31550'02512'07882'64240'55226'26505e+0L;
/// Constant: @f$ 1 / \pi @f$ (see POSIX @c M_1_PI).
template<typename _RealType>
constexpr _RealType
m_1_pi = 3.1830'98861'83790'67153'77675'26745'02872'40691e-1L;
/// Constant: @f$ 2 / \pi @f$ (see POSIX @c M_2_PI).
template<typename _RealType>
constexpr _RealType
m_2_pi = 6.3661'97723'67581'34307'55350'53490'05744'81383e-1L;
/// Constant: @f$ 1 / \sqrt{\pi} @f$.
template<typename _RealType>
constexpr _RealType
m_1_sqrt_pi = 5.6418'95835'47756'28694'80794'51560'77258'58438e-1L;
/// Constant: @f$ \sqrt{\pi} @f$.
template<typename _RealType>
constexpr _RealType
m_sqrt_pi = 1.7724'53850'90551'60272'98167'48334'11451'82797e+0L;
/// Constant: @f$ 2 / \sqrt{\pi} @f$ (see POSIX @c M_2_SQRTPI).
template<typename _RealType>
constexpr _RealType
m_2_sqrt_pi = 1.1283'79167'09551'25738'96158'90312'15451'71688e+0L;
/// Constant: Euler's number @f$ e @f$ (see POSIX @c M_E).
template<typename _RealType>
constexpr _RealType
m_e = 2.7182'81828'45904'52353'60287'47135'26624'97759e+0L;
/// Constant: @f$ 1 / e @f$.
template<typename _RealType>
constexpr _RealType
m_1_e = 3.6787'94411'71442'32159'55237'70161'46086'74462e-1L;
/// Constant: @f$ \log_2(e) @f$ (see POSIX @c M_LOG2E).
template<typename _RealType>
constexpr _RealType
m_log2_e = 1.4426'95040'88896'34073'59924'68100'18921'37427e+0L;
/// Constant: @f$ \log_2(10) @f$.
template<typename _RealType>
constexpr _RealType
m_log2_10 = 3.3219'28094'88736'23478'70319'42948'93901'75867e+0L;
/// Constant: @f$ \log_10(2) @f$.
template<typename _RealType>
constexpr _RealType
m_log10_2 = 3.0102'99956'63981'19521'37388'94724'49302'67680e-1L;
/// Constant: @f$ \log_10(e) @f$ (see POSIX @c M_LOG10E).
template<typename _RealType>
constexpr _RealType
m_log10_e = 4.3429'44819'03251'82765'11289'18916'60508'22940e-1L;
/// Constant: @f$ \log_10(pi) @f$.
template<typename _RealType>
constexpr _RealType
m_log10_pi = 4.9714'98726'94133'85435'12682'88290'89887'36507e-1L;
/// Constant: @f$ \ln(2) @f$ (see POSIX @c M_LN2).
template<typename _RealType>
constexpr _RealType
m_ln_2 = 6.9314'71805'59945'30941'72321'21458'17656'80748e-1L;
/// Constant: @f$ \ln(3) @f$.
template<typename _RealType>
constexpr _RealType
m_ln_3 = 1.0986'12288'66810'96913'95245'23692'25257'04648e+0L;
/// Constant: @f$ \ln(10)@f$ (see POSIX @c M_LN10).
template<typename _RealType>
constexpr _RealType
m_ln_10 = 2.3025'85092'99404'56840'17991'45468'43642'07602e+0L;
/// Constant: Euler-Mascheroni @f$ \gamma_E @f$.
template<typename _RealType>
constexpr _RealType
m_gamma_e = 5.7721'56649'01532'86060'65120'90082'40243'10432e-1L;
/// Constant: Golden Ratio @f$ \phi = (1 + \sqrt{5})/2 @f$.
template<typename _RealType>
constexpr _RealType
m_phi = 1.6180'33988'74989'48482'04586'83436'56381'17720e+0L;
/// Constant: @f$ \sqrt{2}@f$ (see POSIX @c M_SQRT2).
template<typename _RealType>
constexpr _RealType
m_sqrt_2 = 1.4142'13562'37309'50488'01688'72420'96980'78569e+0L;
/// Constant: @f$ \sqrt{3} @f$.
template<typename _RealType>
constexpr _RealType
m_sqrt_3 = 1.7320'50807'56887'72935'27446'34150'58723'66945e+0L;
/// Constant: @f$ \sqrt{5} @f$.
template<typename _RealType>
constexpr _RealType
m_sqrt_5 = 2.2360'67977'49978'96964'09173'66873'12762'35440e+0L;
/// Constant: @f$ \sqrt{7} @f$.
template<typename _RealType>
constexpr _RealType
m_sqrt_7 = 2.6457'51311'06459'05905'01615'75363'92604'25706e+0L;
/// Constant: @f$ 1 / \sqrt{2}@f$ (see POSIX @c M_SQRT1_2).
template<typename _RealType>
constexpr _RealType
m_1_sqrt_2 = 7.0710'67811'86547'52440'08443'62104'84903'92845e-1L;
/// Constant: Catalan's @f$ G = 1 - 1/9 + 1/25 - 1/49 + 1/81 - ... @f$.
template<typename _RealType>
constexpr _RealType
m_catalan = 9.1596'55941'77219'01505'46035'14932'38411'07741e-1L;
/// Constant: @f$ \pi^2/6 @f$.
template<typename _RealType>
constexpr _RealType
m_pi2_6 = 1.6449'34066'84822'64364'72415'16664'60251'89218e+0L;
} // inline namespace math_constants
} // inline namespace constants
} // inline namespace maths_v1
} // namespace experimental
} // namespace std
#endif // C++14
On 2017-03-09 17:44, 3dw...@verizon.net wrote:
> I think that this could devolve into endless bikeshed discussions and what
> constants to include.
template <typename T>
class pi_helper
{
static constexpr value = 3.14159...;
constexpr operator T() { return value; }
template <int numerator, int denominator>
constexpr T times() { return numerator * value / denominator; }
}
template <typename T>
inline constexpr pi_helper pi<T>;
...and as QOI, implementations can provide explicit specializations of
any of `times<N,D>()` that make sense. This way the namespace isn't
polluted by bunches of `pi_1_3` and so forth, and it's clearer what
value you are asking for.
For roots, I think something like:
template <typename T, unsigned value>
extern T sqrt;
...makes sense. We should specify that at least a particular set of
these must be available, but this would provide a hook where compilers
can provide an intrinsic such that any square root is available as a
constexpr. (This would also permit compilers to choose to provide them
via an intrinsic that can always compute them, but e.g. using a
variable-precision algorithm that ensures the result is always accurate
within the limits of the requested type.) We can do something similar
for logarithm constants.
I don't think we should have constants for 180/pi and pi/180; having
constexpr functions for these operations will I think lead to better code.
Otherwise, we should definitely have Euler's constant, on principle if
nothing else, but I don't think I've ever used the golden ratio or
anything involving square roots and pi. (When are those useful? When is
pi²/6 useful?)
--
Matthew
constexpr _Tp __const_pi(Tp _proto = _Tp{})
Hello everyone,Many areas of software development where C++ is actively used involve mathematical calculations. The language inherited a sizable math function library from C, and now has its own numerics library that grows with every release. There is however a small but annoying omission: C++ standard headers do not include definitions of mathematical constants. To this math major it appears to be a very strange gap: wherever common mathematical functions are used, the constants such as π or e appear quite frequently. Yes, it is not terribly difficult to define a constant, but from the C++ user's perspective this is as if they were served a dish at a restaurant but asked to bring their own salt.
Hi Beman,
Thank you for your message. Yes, I am aware of the good work that Boost contributors have done on Boost constants. It doesn’t surprise me that math functions are difficult to implement in a true cross-platform and computationally stable way. I spoke with Paul Bristow via email and got some useful feedback from him.
The goal of my proposal is far more narrow. I focus specifically on constants, and explicitly state that several aspects of their implementation should be left up to the implementer’s discretion. It is not my intent at all to compile a comprehensive set of math constants. I merely want to “push downstream” the set included in several derivative standardization efforts.
--
You received this message because you are subscribed to a topic in the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.org/d/topic/std-proposals/FbSyFYzsoYI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAAygHNOvBFjUryaTbp7UT-d1mVyt8vXYptCatOpfD1DgGs-m8A%40mail.gmail.com.
Hi Beman,
Thank you for your message. Yes, I am aware of the good work that Boost contributors have done on Boost constants. It doesn’t surprise me that math functions are difficult to implement in a true cross-platform and computationally stable way. I spoke with Paul Bristow via email and got some useful feedback from him.
The goal of my proposal is far more narrow. I focus specifically on constants, and explicitly state that several aspects of their implementation should be left up to the implementer’s discretion. It is not my intent at all to compile a comprehensive set of math constants. I merely want to “push downstream” the set included in several derivative standardization efforts.