Use integer template argument to create compiletime double

  • A+

Is it possible to create a double which holds the value of 1*10^x where x is based on a integer template parameter. So something like:

template < int exp > struct DoubleValue {     static constexpr double value = ????; }  double d = DoubleValue<20>::value; // = 1e20 double d = DoubleValue<-20>::value; // = 1e-20 

As it can be created with litterals, it seems that something like this should be possible.

I would like the value to be evaluated at compile time (so std::pow will not work as far as I know). Also, if possible, I would like to be able to avoid actual iterative computations ((maybe unfounded) fear for precision problems). I would also like to be able to use larger values as exponent, like for example 200, which makes it impossible to store the value in a standerd integer type.


Assuming that your compiler supports C++14 or higher (which should be a valid assumption in the year 2019) this is very simple using a constexpr function:

constexpr double myPow(double x, int exp) {     double pow = 1.0;     for (int i = 0; i < exp; ++i)         pow *= x;     for (int i = 0; i > exp; --i)         pow /= x;     return pow; }  template < int exp > struct DoubleValue {     static constexpr double value = myPow(10.0, exp); }; 

See here to verify that it works and that even without optimization the value is generated at compile time.

Depending on your use case you might not even need the DoubleValue struct but can directly use myPow().


As pointed out by @Bob__ in the comments, there may be better algorithms regarding numerical precision than the one presented here. But since C++14 many basic language features can be used in the body of a constexpr function. So, as long as you don't need any external library for it, you are free to implement whatever algorithm fits your needs.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: