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
    2 days ago






  • 1





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

    – Marek R
    2 days ago






  • 3





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

    – Ken Y-N
    2 days 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
    2 days ago






  • 1





    @RubixCube are user-defined literals what you are looking for? (C++11)

    – user268396
    2 days 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
    2 days ago






  • 1





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

    – Marek R
    2 days ago






  • 3





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

    – Ken Y-N
    2 days 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
    2 days ago






  • 1





    @RubixCube are user-defined literals what you are looking for? (C++11)

    – user268396
    2 days 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 2 days 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 2 days 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
    2 days ago






  • 1





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

    – Marek R
    2 days ago






  • 3





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

    – Ken Y-N
    2 days 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
    2 days ago






  • 1





    @RubixCube are user-defined literals what you are looking for? (C++11)

    – user268396
    2 days ago














  • 3





    maybe std::pow(10, exp)?

    – iBug
    2 days ago






  • 1





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

    – Marek R
    2 days ago






  • 3





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

    – Ken Y-N
    2 days 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
    2 days ago






  • 1





    @RubixCube are user-defined literals what you are looking for? (C++11)

    – user268396
    2 days ago








3




3





maybe std::pow(10, exp)?

– iBug
2 days ago





maybe std::pow(10, exp)?

– iBug
2 days ago




1




1





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

– Marek R
2 days ago





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

– Marek R
2 days ago




3




3





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

– Ken Y-N
2 days ago





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

– Ken Y-N
2 days 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
2 days 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
2 days ago




1




1





@RubixCube are user-defined literals what you are looking for? (C++11)

– user268396
2 days ago





@RubixCube are user-defined literals what you are looking for? (C++11)

– user268396
2 days ago












3 Answers
3






active

oldest

votes


















6














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





Update



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.






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
    2 days 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
    2 days ago






  • 1





    Please note that the algorithm used here is more prone to numeric errors than other alternatives: wandbox.org/permlink/tiupixvpbtwo9Zwd

    – Bob__
    2 days 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
    2 days ago











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

    – Algirdas Preidžius
    2 days 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
    2 days ago











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

    – lisyarus
    2 days 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
    2 days 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
    2 days 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

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














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





Update



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.






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
    2 days 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
    2 days ago






  • 1





    Please note that the algorithm used here is more prone to numeric errors than other alternatives: wandbox.org/permlink/tiupixvpbtwo9Zwd

    – Bob__
    2 days ago
















6














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





Update



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.






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
    2 days 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
    2 days ago






  • 1





    Please note that the algorithm used here is more prone to numeric errors than other alternatives: wandbox.org/permlink/tiupixvpbtwo9Zwd

    – Bob__
    2 days ago














6












6








6







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





Update



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.






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





Update



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.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









sebrockmsebrockm

1,045218




1,045218













  • 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
    2 days 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
    2 days ago






  • 1





    Please note that the algorithm used here is more prone to numeric errors than other alternatives: wandbox.org/permlink/tiupixvpbtwo9Zwd

    – Bob__
    2 days 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
    2 days 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
    2 days ago






  • 1





    Please note that the algorithm used here is more prone to numeric errors than other alternatives: wandbox.org/permlink/tiupixvpbtwo9Zwd

    – Bob__
    2 days 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
2 days 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
2 days 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
2 days 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
2 days ago




1




1





Please note that the algorithm used here is more prone to numeric errors than other alternatives: wandbox.org/permlink/tiupixvpbtwo9Zwd

– Bob__
2 days ago





Please note that the algorithm used here is more prone to numeric errors than other alternatives: wandbox.org/permlink/tiupixvpbtwo9Zwd

– Bob__
2 days 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
    2 days ago











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

    – Algirdas Preidžius
    2 days 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
    2 days ago











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

    – Algirdas Preidžius
    2 days 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 2 days ago

























answered 2 days ago









Algirdas PreidžiusAlgirdas Preidžius

1,59231015




1,59231015













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

    – lisyarus
    2 days ago











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

    – Algirdas Preidžius
    2 days ago



















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

    – lisyarus
    2 days ago











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

    – Algirdas Preidžius
    2 days ago

















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

– lisyarus
2 days ago





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

– lisyarus
2 days ago













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

– Algirdas Preidžius
2 days ago





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

– Algirdas Preidžius
2 days 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
    2 days ago











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

    – lisyarus
    2 days 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
    2 days 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
    2 days 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
    2 days ago











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

    – lisyarus
    2 days 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
    2 days 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
    2 days 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 2 days ago









MrMaavin

330312




330312










answered 2 days ago









Stack DannyStack Danny

1,143320




1,143320













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

    – Algirdas Preidžius
    2 days ago











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

    – lisyarus
    2 days 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
    2 days 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
    2 days ago





















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

    – Algirdas Preidžius
    2 days ago











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

    – lisyarus
    2 days 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
    2 days 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
    2 days ago



















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

– Algirdas Preidžius
2 days ago





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

– Algirdas Preidžius
2 days ago













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

– lisyarus
2 days ago





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

– lisyarus
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?