Use integer template argument to create compiletime double












6















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.










share|improve this question









New contributor




Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3





    maybe std::pow(10, exp)?

    – iBug
    5 hours ago






  • 1





    @iBug he needs something with compile time, so std::pow will not work since it is not constexpr.

    – Marek R
    5 hours ago






  • 3





    Possible duplicate of c++ power of integer, template meta programming

    – Ken Y-N
    5 hours ago











  • "I would like the value to be evaluated at compile time" &mdash; What would be the use for this? To speed-up your runtime?

    – Holt
    5 hours ago











  • I added static constexpr, I was indeed sloppy :)

    – Rubix Cube
    5 hours ago
















6















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.










share|improve this question









New contributor




Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 3





    maybe std::pow(10, exp)?

    – iBug
    5 hours ago






  • 1





    @iBug he needs something with compile time, so std::pow will not work since it is not constexpr.

    – Marek R
    5 hours ago






  • 3





    Possible duplicate of c++ power of integer, template meta programming

    – Ken Y-N
    5 hours ago











  • "I would like the value to be evaluated at compile time" &mdash; What would be the use for this? To speed-up your runtime?

    – Holt
    5 hours ago











  • I added static constexpr, I was indeed sloppy :)

    – Rubix Cube
    5 hours ago














6












6








6








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.










share|improve this question









New contributor




Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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.







c++






share|improve this question









New contributor




Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 5 hours ago







Rubix Cube













New contributor




Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 5 hours ago









Rubix CubeRubix Cube

334




334




New contributor




Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Rubix Cube is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 3





    maybe std::pow(10, exp)?

    – iBug
    5 hours ago






  • 1





    @iBug he needs something with compile time, so std::pow will not work since it is not constexpr.

    – Marek R
    5 hours ago






  • 3





    Possible duplicate of c++ power of integer, template meta programming

    – Ken Y-N
    5 hours ago











  • "I would like the value to be evaluated at compile time" &mdash; What would be the use for this? To speed-up your runtime?

    – Holt
    5 hours ago











  • I added static constexpr, I was indeed sloppy :)

    – Rubix Cube
    5 hours ago














  • 3





    maybe std::pow(10, exp)?

    – iBug
    5 hours ago






  • 1





    @iBug he needs something with compile time, so std::pow will not work since it is not constexpr.

    – Marek R
    5 hours ago






  • 3





    Possible duplicate of c++ power of integer, template meta programming

    – Ken Y-N
    5 hours ago











  • "I would like the value to be evaluated at compile time" &mdash; What would be the use for this? To speed-up your runtime?

    – Holt
    5 hours ago











  • I added static constexpr, I was indeed sloppy :)

    – Rubix Cube
    5 hours ago








3




3





maybe std::pow(10, exp)?

– iBug
5 hours ago





maybe std::pow(10, exp)?

– iBug
5 hours ago




1




1





@iBug he needs something with compile time, so std::pow will not work since it is not constexpr.

– Marek R
5 hours ago





@iBug he needs something with compile time, so std::pow will not work since it is not constexpr.

– Marek R
5 hours ago




3




3





Possible duplicate of c++ power of integer, template meta programming

– Ken Y-N
5 hours ago





Possible duplicate of c++ power of integer, template meta programming

– Ken Y-N
5 hours ago













"I would like the value to be evaluated at compile time" &mdash; What would be the use for this? To speed-up your runtime?

– Holt
5 hours ago





"I would like the value to be evaluated at compile time" &mdash; What would be the use for this? To speed-up your runtime?

– Holt
5 hours ago













I added static constexpr, I was indeed sloppy :)

– Rubix Cube
5 hours ago





I added static constexpr, I was indeed sloppy :)

– Rubix Cube
5 hours ago












4 Answers
4






active

oldest

votes


















5














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().






share|improve this answer


























  • Off topic: "Assuming that your compiler supports C++14 or higher (which should be a valid assumption in the year 2019)"; When writing new projects, sure. But, in the project, that I am working on, we still have pieces of code being compiled with VS2003.. Legacy projects FTW!

    – Algirdas Preidžius
    4 hours ago






  • 1





    Luckily, the project I wanted to use this in was just updated :). I chose this anwer as it is clearer in its intent compared to recursive template solutions.

    – Rubix Cube
    3 hours ago



















1














This one works for me:



// File: 54195854.cpp
// Author: iBug

#include <iostream>
#include <cmath>

template <int e>
struct DoubleValue {
static double value;
};

template <int e>
double DoubleValue<e>::value = std::pow(10.0, e);

int main(void) {
std::cout << DoubleValue<10>::value << std::endl;
std::cout << DoubleValue<-10>::value << std::endl;
}


I don't know if it's correct, but at least the above code compiles with G++ 7.3.0:



g++ -std=c++14 -pedantic 54195854.cpp


And the compiled program a.out generates this output:



1e+10
1e-10





share|improve this answer
























  • Sorry, this will not worlk for me, I would really like to have the values at compile time. I altered the question to be more precise

    – Rubix Cube
    5 hours ago











  • @RubixCube I think this well meets your needs. It does evaluate at compile-time, rather than runtime.

    – iBug
    5 hours ago











  • As far as I understand pow is not constexpr, so it evaluates when the program loads.

    – Rubix Cube
    5 hours ago











  • Maybe, this solution is the best we can get. However, it seems like a pitty te me as I’m able to write 1e24 and have that compile time in the code but when “24” is known at compile time as tempalte argument, I can’t find a way :)

    – Rubix Cube
    5 hours ago






  • 1





    @RubixCube - Discussing optimizations about code that wasn't compiled with optimizations is pointless. See this instead godbolt.org/z/vZAjvI - It's only -O1.

    – StoryTeller
    4 hours ago





















1














Since you need the value to available in compile time, pretty much the only way to solve it, that came to my mind is recursive templates. However, the fact, that you need for said template to do different things, based on the signedness of the passed value, complicates things. First thing that would come to mind, would be to write such a recursive template:



template <int exp>
struct DoubleValue
{
static constexpr double value = (exp < 0
? DoubleValue<exp+1>::value / 10
: 10 * DoubleValue<exp-1>::value);
};

// Default case
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


However, such solution wouldn't work, due to the fact, that both branches of the ternary expression, would need to be resolved, and that would, always, lead to the infinite recursion, since one of the branches wouldn't tend to 0. Then, SFINAE came to mind:



// Base case.
template <int exp, class Enable = void>
struct DoubleValue
{
};

// Case when exp is positive
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp > 0)>::type>
{
static constexpr double value = 10 * DoubleValue<exp-1>::value;
};

// Case when exp is negative
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp < 0)>::type>
{
static constexpr double value = DoubleValue<exp+1>::value / 10;
};

// Default case.
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


Live Demo.






share|improve this answer


























  • This is definitely not the only was - see answer by @sebrockm.

    – lisyarus
    4 hours ago











  • @lisyarus Sure, what I meant was "the only way that came to my mind". Edited to clarify that.

    – Algirdas Preidžius
    4 hours ago



















1














If you want it at compile time without std::pow, this should do it:



#include <iostream>

template <int e>
struct DoubleValue {
static constexpr double value = 10.0 * DoubleValue<e - 1>::value;
};
template <>
struct DoubleValue<0> {
static constexpr double value = 1.0;
};
int main() {
std::cout << DoubleValue<20>::value << 'n'; //1e+20
}


C++ Fiddle






share|improve this answer


























  • What about the case of std::cout << DoubleValue<-10>::value << std::endl;?

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius It is not hard to upgrade the code to work with negative powers, the idea stays the same.

    – lisyarus
    4 hours ago






  • 1





    @lisyarus Idea, yes, but one can't just throw if, or ternary expression, in there, since the both branches would need to be resolved, which would lead to infinite recursion. One would need to do more, than what is shown here, to force the same template to work with both positive, and negative numbers.

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius You are absolutely right, this needs some template machinery to work, albeit pretty standard one, but potentially not familiar to the OP.

    – lisyarus
    4 hours ago













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Rubix Cube is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54195854%2fuse-integer-template-argument-to-create-compiletime-double%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














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().






share|improve this answer


























  • Off topic: "Assuming that your compiler supports C++14 or higher (which should be a valid assumption in the year 2019)"; When writing new projects, sure. But, in the project, that I am working on, we still have pieces of code being compiled with VS2003.. Legacy projects FTW!

    – Algirdas Preidžius
    4 hours ago






  • 1





    Luckily, the project I wanted to use this in was just updated :). I chose this anwer as it is clearer in its intent compared to recursive template solutions.

    – Rubix Cube
    3 hours ago
















5














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().






share|improve this answer


























  • Off topic: "Assuming that your compiler supports C++14 or higher (which should be a valid assumption in the year 2019)"; When writing new projects, sure. But, in the project, that I am working on, we still have pieces of code being compiled with VS2003.. Legacy projects FTW!

    – Algirdas Preidžius
    4 hours ago






  • 1





    Luckily, the project I wanted to use this in was just updated :). I chose this anwer as it is clearer in its intent compared to recursive template solutions.

    – Rubix Cube
    3 hours ago














5












5








5







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().






share|improve this answer















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().







share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago

























answered 4 hours ago









sebrockmsebrockm

1,003217




1,003217













  • Off topic: "Assuming that your compiler supports C++14 or higher (which should be a valid assumption in the year 2019)"; When writing new projects, sure. But, in the project, that I am working on, we still have pieces of code being compiled with VS2003.. Legacy projects FTW!

    – Algirdas Preidžius
    4 hours ago






  • 1





    Luckily, the project I wanted to use this in was just updated :). I chose this anwer as it is clearer in its intent compared to recursive template solutions.

    – Rubix Cube
    3 hours ago



















  • Off topic: "Assuming that your compiler supports C++14 or higher (which should be a valid assumption in the year 2019)"; When writing new projects, sure. But, in the project, that I am working on, we still have pieces of code being compiled with VS2003.. Legacy projects FTW!

    – Algirdas Preidžius
    4 hours ago






  • 1





    Luckily, the project I wanted to use this in was just updated :). I chose this anwer as it is clearer in its intent compared to recursive template solutions.

    – Rubix Cube
    3 hours ago

















Off topic: "Assuming that your compiler supports C++14 or higher (which should be a valid assumption in the year 2019)"; When writing new projects, sure. But, in the project, that I am working on, we still have pieces of code being compiled with VS2003.. Legacy projects FTW!

– Algirdas Preidžius
4 hours ago





Off topic: "Assuming that your compiler supports C++14 or higher (which should be a valid assumption in the year 2019)"; When writing new projects, sure. But, in the project, that I am working on, we still have pieces of code being compiled with VS2003.. Legacy projects FTW!

– Algirdas Preidžius
4 hours ago




1




1





Luckily, the project I wanted to use this in was just updated :). I chose this anwer as it is clearer in its intent compared to recursive template solutions.

– Rubix Cube
3 hours ago





Luckily, the project I wanted to use this in was just updated :). I chose this anwer as it is clearer in its intent compared to recursive template solutions.

– Rubix Cube
3 hours ago













1














This one works for me:



// File: 54195854.cpp
// Author: iBug

#include <iostream>
#include <cmath>

template <int e>
struct DoubleValue {
static double value;
};

template <int e>
double DoubleValue<e>::value = std::pow(10.0, e);

int main(void) {
std::cout << DoubleValue<10>::value << std::endl;
std::cout << DoubleValue<-10>::value << std::endl;
}


I don't know if it's correct, but at least the above code compiles with G++ 7.3.0:



g++ -std=c++14 -pedantic 54195854.cpp


And the compiled program a.out generates this output:



1e+10
1e-10





share|improve this answer
























  • Sorry, this will not worlk for me, I would really like to have the values at compile time. I altered the question to be more precise

    – Rubix Cube
    5 hours ago











  • @RubixCube I think this well meets your needs. It does evaluate at compile-time, rather than runtime.

    – iBug
    5 hours ago











  • As far as I understand pow is not constexpr, so it evaluates when the program loads.

    – Rubix Cube
    5 hours ago











  • Maybe, this solution is the best we can get. However, it seems like a pitty te me as I’m able to write 1e24 and have that compile time in the code but when “24” is known at compile time as tempalte argument, I can’t find a way :)

    – Rubix Cube
    5 hours ago






  • 1





    @RubixCube - Discussing optimizations about code that wasn't compiled with optimizations is pointless. See this instead godbolt.org/z/vZAjvI - It's only -O1.

    – StoryTeller
    4 hours ago


















1














This one works for me:



// File: 54195854.cpp
// Author: iBug

#include <iostream>
#include <cmath>

template <int e>
struct DoubleValue {
static double value;
};

template <int e>
double DoubleValue<e>::value = std::pow(10.0, e);

int main(void) {
std::cout << DoubleValue<10>::value << std::endl;
std::cout << DoubleValue<-10>::value << std::endl;
}


I don't know if it's correct, but at least the above code compiles with G++ 7.3.0:



g++ -std=c++14 -pedantic 54195854.cpp


And the compiled program a.out generates this output:



1e+10
1e-10





share|improve this answer
























  • Sorry, this will not worlk for me, I would really like to have the values at compile time. I altered the question to be more precise

    – Rubix Cube
    5 hours ago











  • @RubixCube I think this well meets your needs. It does evaluate at compile-time, rather than runtime.

    – iBug
    5 hours ago











  • As far as I understand pow is not constexpr, so it evaluates when the program loads.

    – Rubix Cube
    5 hours ago











  • Maybe, this solution is the best we can get. However, it seems like a pitty te me as I’m able to write 1e24 and have that compile time in the code but when “24” is known at compile time as tempalte argument, I can’t find a way :)

    – Rubix Cube
    5 hours ago






  • 1





    @RubixCube - Discussing optimizations about code that wasn't compiled with optimizations is pointless. See this instead godbolt.org/z/vZAjvI - It's only -O1.

    – StoryTeller
    4 hours ago
















1












1








1







This one works for me:



// File: 54195854.cpp
// Author: iBug

#include <iostream>
#include <cmath>

template <int e>
struct DoubleValue {
static double value;
};

template <int e>
double DoubleValue<e>::value = std::pow(10.0, e);

int main(void) {
std::cout << DoubleValue<10>::value << std::endl;
std::cout << DoubleValue<-10>::value << std::endl;
}


I don't know if it's correct, but at least the above code compiles with G++ 7.3.0:



g++ -std=c++14 -pedantic 54195854.cpp


And the compiled program a.out generates this output:



1e+10
1e-10





share|improve this answer













This one works for me:



// File: 54195854.cpp
// Author: iBug

#include <iostream>
#include <cmath>

template <int e>
struct DoubleValue {
static double value;
};

template <int e>
double DoubleValue<e>::value = std::pow(10.0, e);

int main(void) {
std::cout << DoubleValue<10>::value << std::endl;
std::cout << DoubleValue<-10>::value << std::endl;
}


I don't know if it's correct, but at least the above code compiles with G++ 7.3.0:



g++ -std=c++14 -pedantic 54195854.cpp


And the compiled program a.out generates this output:



1e+10
1e-10






share|improve this answer












share|improve this answer



share|improve this answer










answered 5 hours ago









iBugiBug

19.4k53363




19.4k53363













  • Sorry, this will not worlk for me, I would really like to have the values at compile time. I altered the question to be more precise

    – Rubix Cube
    5 hours ago











  • @RubixCube I think this well meets your needs. It does evaluate at compile-time, rather than runtime.

    – iBug
    5 hours ago











  • As far as I understand pow is not constexpr, so it evaluates when the program loads.

    – Rubix Cube
    5 hours ago











  • Maybe, this solution is the best we can get. However, it seems like a pitty te me as I’m able to write 1e24 and have that compile time in the code but when “24” is known at compile time as tempalte argument, I can’t find a way :)

    – Rubix Cube
    5 hours ago






  • 1





    @RubixCube - Discussing optimizations about code that wasn't compiled with optimizations is pointless. See this instead godbolt.org/z/vZAjvI - It's only -O1.

    – StoryTeller
    4 hours ago





















  • Sorry, this will not worlk for me, I would really like to have the values at compile time. I altered the question to be more precise

    – Rubix Cube
    5 hours ago











  • @RubixCube I think this well meets your needs. It does evaluate at compile-time, rather than runtime.

    – iBug
    5 hours ago











  • As far as I understand pow is not constexpr, so it evaluates when the program loads.

    – Rubix Cube
    5 hours ago











  • Maybe, this solution is the best we can get. However, it seems like a pitty te me as I’m able to write 1e24 and have that compile time in the code but when “24” is known at compile time as tempalte argument, I can’t find a way :)

    – Rubix Cube
    5 hours ago






  • 1





    @RubixCube - Discussing optimizations about code that wasn't compiled with optimizations is pointless. See this instead godbolt.org/z/vZAjvI - It's only -O1.

    – StoryTeller
    4 hours ago



















Sorry, this will not worlk for me, I would really like to have the values at compile time. I altered the question to be more precise

– Rubix Cube
5 hours ago





Sorry, this will not worlk for me, I would really like to have the values at compile time. I altered the question to be more precise

– Rubix Cube
5 hours ago













@RubixCube I think this well meets your needs. It does evaluate at compile-time, rather than runtime.

– iBug
5 hours ago





@RubixCube I think this well meets your needs. It does evaluate at compile-time, rather than runtime.

– iBug
5 hours ago













As far as I understand pow is not constexpr, so it evaluates when the program loads.

– Rubix Cube
5 hours ago





As far as I understand pow is not constexpr, so it evaluates when the program loads.

– Rubix Cube
5 hours ago













Maybe, this solution is the best we can get. However, it seems like a pitty te me as I’m able to write 1e24 and have that compile time in the code but when “24” is known at compile time as tempalte argument, I can’t find a way :)

– Rubix Cube
5 hours ago





Maybe, this solution is the best we can get. However, it seems like a pitty te me as I’m able to write 1e24 and have that compile time in the code but when “24” is known at compile time as tempalte argument, I can’t find a way :)

– Rubix Cube
5 hours ago




1




1





@RubixCube - Discussing optimizations about code that wasn't compiled with optimizations is pointless. See this instead godbolt.org/z/vZAjvI - It's only -O1.

– StoryTeller
4 hours ago







@RubixCube - Discussing optimizations about code that wasn't compiled with optimizations is pointless. See this instead godbolt.org/z/vZAjvI - It's only -O1.

– StoryTeller
4 hours ago













1














Since you need the value to available in compile time, pretty much the only way to solve it, that came to my mind is recursive templates. However, the fact, that you need for said template to do different things, based on the signedness of the passed value, complicates things. First thing that would come to mind, would be to write such a recursive template:



template <int exp>
struct DoubleValue
{
static constexpr double value = (exp < 0
? DoubleValue<exp+1>::value / 10
: 10 * DoubleValue<exp-1>::value);
};

// Default case
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


However, such solution wouldn't work, due to the fact, that both branches of the ternary expression, would need to be resolved, and that would, always, lead to the infinite recursion, since one of the branches wouldn't tend to 0. Then, SFINAE came to mind:



// Base case.
template <int exp, class Enable = void>
struct DoubleValue
{
};

// Case when exp is positive
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp > 0)>::type>
{
static constexpr double value = 10 * DoubleValue<exp-1>::value;
};

// Case when exp is negative
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp < 0)>::type>
{
static constexpr double value = DoubleValue<exp+1>::value / 10;
};

// Default case.
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


Live Demo.






share|improve this answer


























  • This is definitely not the only was - see answer by @sebrockm.

    – lisyarus
    4 hours ago











  • @lisyarus Sure, what I meant was "the only way that came to my mind". Edited to clarify that.

    – Algirdas Preidžius
    4 hours ago
















1














Since you need the value to available in compile time, pretty much the only way to solve it, that came to my mind is recursive templates. However, the fact, that you need for said template to do different things, based on the signedness of the passed value, complicates things. First thing that would come to mind, would be to write such a recursive template:



template <int exp>
struct DoubleValue
{
static constexpr double value = (exp < 0
? DoubleValue<exp+1>::value / 10
: 10 * DoubleValue<exp-1>::value);
};

// Default case
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


However, such solution wouldn't work, due to the fact, that both branches of the ternary expression, would need to be resolved, and that would, always, lead to the infinite recursion, since one of the branches wouldn't tend to 0. Then, SFINAE came to mind:



// Base case.
template <int exp, class Enable = void>
struct DoubleValue
{
};

// Case when exp is positive
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp > 0)>::type>
{
static constexpr double value = 10 * DoubleValue<exp-1>::value;
};

// Case when exp is negative
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp < 0)>::type>
{
static constexpr double value = DoubleValue<exp+1>::value / 10;
};

// Default case.
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


Live Demo.






share|improve this answer


























  • This is definitely not the only was - see answer by @sebrockm.

    – lisyarus
    4 hours ago











  • @lisyarus Sure, what I meant was "the only way that came to my mind". Edited to clarify that.

    – Algirdas Preidžius
    4 hours ago














1












1








1







Since you need the value to available in compile time, pretty much the only way to solve it, that came to my mind is recursive templates. However, the fact, that you need for said template to do different things, based on the signedness of the passed value, complicates things. First thing that would come to mind, would be to write such a recursive template:



template <int exp>
struct DoubleValue
{
static constexpr double value = (exp < 0
? DoubleValue<exp+1>::value / 10
: 10 * DoubleValue<exp-1>::value);
};

// Default case
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


However, such solution wouldn't work, due to the fact, that both branches of the ternary expression, would need to be resolved, and that would, always, lead to the infinite recursion, since one of the branches wouldn't tend to 0. Then, SFINAE came to mind:



// Base case.
template <int exp, class Enable = void>
struct DoubleValue
{
};

// Case when exp is positive
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp > 0)>::type>
{
static constexpr double value = 10 * DoubleValue<exp-1>::value;
};

// Case when exp is negative
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp < 0)>::type>
{
static constexpr double value = DoubleValue<exp+1>::value / 10;
};

// Default case.
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


Live Demo.






share|improve this answer















Since you need the value to available in compile time, pretty much the only way to solve it, that came to my mind is recursive templates. However, the fact, that you need for said template to do different things, based on the signedness of the passed value, complicates things. First thing that would come to mind, would be to write such a recursive template:



template <int exp>
struct DoubleValue
{
static constexpr double value = (exp < 0
? DoubleValue<exp+1>::value / 10
: 10 * DoubleValue<exp-1>::value);
};

// Default case
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


However, such solution wouldn't work, due to the fact, that both branches of the ternary expression, would need to be resolved, and that would, always, lead to the infinite recursion, since one of the branches wouldn't tend to 0. Then, SFINAE came to mind:



// Base case.
template <int exp, class Enable = void>
struct DoubleValue
{
};

// Case when exp is positive
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp > 0)>::type>
{
static constexpr double value = 10 * DoubleValue<exp-1>::value;
};

// Case when exp is negative
template <int exp>
struct DoubleValue<exp, typename std::enable_if<(exp < 0)>::type>
{
static constexpr double value = DoubleValue<exp+1>::value / 10;
};

// Default case.
template <>
struct DoubleValue<0>
{
static constexpr double value = 1;
};


Live Demo.







share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago

























answered 4 hours ago









Algirdas PreidžiusAlgirdas Preidžius

1,52231015




1,52231015













  • This is definitely not the only was - see answer by @sebrockm.

    – lisyarus
    4 hours ago











  • @lisyarus Sure, what I meant was "the only way that came to my mind". Edited to clarify that.

    – Algirdas Preidžius
    4 hours ago



















  • This is definitely not the only was - see answer by @sebrockm.

    – lisyarus
    4 hours ago











  • @lisyarus Sure, what I meant was "the only way that came to my mind". Edited to clarify that.

    – Algirdas Preidžius
    4 hours ago

















This is definitely not the only was - see answer by @sebrockm.

– lisyarus
4 hours ago





This is definitely not the only was - see answer by @sebrockm.

– lisyarus
4 hours ago













@lisyarus Sure, what I meant was "the only way that came to my mind". Edited to clarify that.

– Algirdas Preidžius
4 hours ago





@lisyarus Sure, what I meant was "the only way that came to my mind". Edited to clarify that.

– Algirdas Preidžius
4 hours ago











1














If you want it at compile time without std::pow, this should do it:



#include <iostream>

template <int e>
struct DoubleValue {
static constexpr double value = 10.0 * DoubleValue<e - 1>::value;
};
template <>
struct DoubleValue<0> {
static constexpr double value = 1.0;
};
int main() {
std::cout << DoubleValue<20>::value << 'n'; //1e+20
}


C++ Fiddle






share|improve this answer


























  • What about the case of std::cout << DoubleValue<-10>::value << std::endl;?

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius It is not hard to upgrade the code to work with negative powers, the idea stays the same.

    – lisyarus
    4 hours ago






  • 1





    @lisyarus Idea, yes, but one can't just throw if, or ternary expression, in there, since the both branches would need to be resolved, which would lead to infinite recursion. One would need to do more, than what is shown here, to force the same template to work with both positive, and negative numbers.

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius You are absolutely right, this needs some template machinery to work, albeit pretty standard one, but potentially not familiar to the OP.

    – lisyarus
    4 hours ago


















1














If you want it at compile time without std::pow, this should do it:



#include <iostream>

template <int e>
struct DoubleValue {
static constexpr double value = 10.0 * DoubleValue<e - 1>::value;
};
template <>
struct DoubleValue<0> {
static constexpr double value = 1.0;
};
int main() {
std::cout << DoubleValue<20>::value << 'n'; //1e+20
}


C++ Fiddle






share|improve this answer


























  • What about the case of std::cout << DoubleValue<-10>::value << std::endl;?

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius It is not hard to upgrade the code to work with negative powers, the idea stays the same.

    – lisyarus
    4 hours ago






  • 1





    @lisyarus Idea, yes, but one can't just throw if, or ternary expression, in there, since the both branches would need to be resolved, which would lead to infinite recursion. One would need to do more, than what is shown here, to force the same template to work with both positive, and negative numbers.

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius You are absolutely right, this needs some template machinery to work, albeit pretty standard one, but potentially not familiar to the OP.

    – lisyarus
    4 hours ago
















1












1








1







If you want it at compile time without std::pow, this should do it:



#include <iostream>

template <int e>
struct DoubleValue {
static constexpr double value = 10.0 * DoubleValue<e - 1>::value;
};
template <>
struct DoubleValue<0> {
static constexpr double value = 1.0;
};
int main() {
std::cout << DoubleValue<20>::value << 'n'; //1e+20
}


C++ Fiddle






share|improve this answer















If you want it at compile time without std::pow, this should do it:



#include <iostream>

template <int e>
struct DoubleValue {
static constexpr double value = 10.0 * DoubleValue<e - 1>::value;
};
template <>
struct DoubleValue<0> {
static constexpr double value = 1.0;
};
int main() {
std::cout << DoubleValue<20>::value << 'n'; //1e+20
}


C++ Fiddle







share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago









MrMaavin

283211




283211










answered 5 hours ago









Stack DannyStack Danny

1,115319




1,115319













  • What about the case of std::cout << DoubleValue<-10>::value << std::endl;?

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius It is not hard to upgrade the code to work with negative powers, the idea stays the same.

    – lisyarus
    4 hours ago






  • 1





    @lisyarus Idea, yes, but one can't just throw if, or ternary expression, in there, since the both branches would need to be resolved, which would lead to infinite recursion. One would need to do more, than what is shown here, to force the same template to work with both positive, and negative numbers.

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius You are absolutely right, this needs some template machinery to work, albeit pretty standard one, but potentially not familiar to the OP.

    – lisyarus
    4 hours ago





















  • What about the case of std::cout << DoubleValue<-10>::value << std::endl;?

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius It is not hard to upgrade the code to work with negative powers, the idea stays the same.

    – lisyarus
    4 hours ago






  • 1





    @lisyarus Idea, yes, but one can't just throw if, or ternary expression, in there, since the both branches would need to be resolved, which would lead to infinite recursion. One would need to do more, than what is shown here, to force the same template to work with both positive, and negative numbers.

    – Algirdas Preidžius
    4 hours ago











  • @AlgirdasPreidžius You are absolutely right, this needs some template machinery to work, albeit pretty standard one, but potentially not familiar to the OP.

    – lisyarus
    4 hours ago



















What about the case of std::cout << DoubleValue<-10>::value << std::endl;?

– Algirdas Preidžius
4 hours ago





What about the case of std::cout << DoubleValue<-10>::value << std::endl;?

– Algirdas Preidžius
4 hours ago













@AlgirdasPreidžius It is not hard to upgrade the code to work with negative powers, the idea stays the same.

– lisyarus
4 hours ago





@AlgirdasPreidžius It is not hard to upgrade the code to work with negative powers, the idea stays the same.

– lisyarus
4 hours ago




1




1





@lisyarus Idea, yes, but one can't just throw if, or ternary expression, in there, since the both branches would need to be resolved, which would lead to infinite recursion. One would need to do more, than what is shown here, to force the same template to work with both positive, and negative numbers.

– Algirdas Preidžius
4 hours ago





@lisyarus Idea, yes, but one can't just throw if, or ternary expression, in there, since the both branches would need to be resolved, which would lead to infinite recursion. One would need to do more, than what is shown here, to force the same template to work with both positive, and negative numbers.

– Algirdas Preidžius
4 hours ago













@AlgirdasPreidžius You are absolutely right, this needs some template machinery to work, albeit pretty standard one, but potentially not familiar to the OP.

– lisyarus
4 hours ago







@AlgirdasPreidžius You are absolutely right, this needs some template machinery to work, albeit pretty standard one, but potentially not familiar to the OP.

– lisyarus
4 hours ago












Rubix Cube is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Rubix Cube is a new contributor. Be nice, and check out our Code of Conduct.













Rubix Cube is a new contributor. Be nice, and check out our Code of Conduct.












Rubix Cube is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54195854%2fuse-integer-template-argument-to-create-compiletime-double%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What other Star Trek series did the main TNG cast show up in?

Berlina muro

Berlina aerponto