Use integer template argument to create compiletime double
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++
New contributor
|
show 3 more comments
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++
New contributor
3
maybestd::pow(10, exp)
?
– iBug
2 days ago
1
@iBug he needs something with compile time, sostd::pow
will not work since it is notconstexpr
.
– 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" — 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
|
show 3 more comments
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++
New contributor
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++
c++
New contributor
New contributor
edited 2 days ago
Rubix Cube
New contributor
asked 2 days ago
Rubix CubeRubix Cube
334
334
New contributor
New contributor
3
maybestd::pow(10, exp)
?
– iBug
2 days ago
1
@iBug he needs something with compile time, sostd::pow
will not work since it is notconstexpr
.
– 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" — 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
|
show 3 more comments
3
maybestd::pow(10, exp)
?
– iBug
2 days ago
1
@iBug he needs something with compile time, sostd::pow
will not work since it is notconstexpr
.
– 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" — 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" — 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" — 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
|
show 3 more comments
3 Answers
3
active
oldest
votes
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.
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
add a comment |
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.
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
add a comment |
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
What about the case ofstd::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 throwif
, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
What about the case ofstd::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 throwif
, 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
add a comment |
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
What about the case ofstd::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 throwif
, 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
add a comment |
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
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
edited 2 days ago
MrMaavin
330312
330312
answered 2 days ago
Stack DannyStack Danny
1,143320
1,143320
What about the case ofstd::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 throwif
, 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
add a comment |
What about the case ofstd::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 throwif
, 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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 notconstexpr
.– 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" — 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