[PATCH] C++ math constants

Jakub Jelinek jakub@redhat.com
Thu Feb 21 19:29:00 GMT 2013


On Thu, Feb 21, 2013 at 08:06:02PM +0100, Oleg Endo wrote:
> On Thu, 2013-02-21 at 17:14 +0000, Alec Teal wrote:
> > On 21/02/13 16:32, Ulrich Drepper wrote:
> > > How about the attached file as a start for <ext/math>.  I used the
> > > constexpr approach (instead of function calls) and replicated the
> > > constants that are available in <math.h> in Unix.
> > >
> > > What other constants to add?
> > Pi/3
> 
> > 1/e
> > 
> 
> I'm wondering, why adding constants for these if the compiler can figure
> it out from e.g. "math::pi/3" (or whatever the namespace is)?

It can't, you generally want to avoid double rounding for these, first
rounding infinite precision pi to the corresponding floating point format and
then performing in that finite precision division by 3 (and rounding again
to that precision) might very well give different results from dividing
infinite precision pi by 3 and only then rounding the result to the
corresponding precision.  Haven't tried it, so you might be lucky and in
some of float, double or long double it might be equal.

> > ln(3)
> > ln(10) (for base conversions)
> > sqrt(3)
> > sqrt(5)
> > sqrt(7)
> 
> Aren't these evaluated by the compiler and converted to constants?

Only if optimizing.  Furthermore, I don't think std::sqrt etc. are
constexpr, therefore you can't use them to initialize constexpr vars or in
constexpr functions.

	Jakub



More information about the Gcc-patches mailing list