Eigen c++ cast double to long int?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Quick question:
consider this (wrong) casting from a double to a long int:
Eigen::VectorXd Price = Map<VectorXd>(price, n);
double TickFactor = 1.0 / TickSize;
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <long int> ();
which gives the following error (Eigen 3.3.5, g++ 7.3.0):
eigen/Eigen/src/Core/util/StaticAssert.h:33:40: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
Now, this compiles:
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
here is my question. Does the line above allows for values of (Price * TickFactor)
that are larger than the upper limit on a short int
? --whatever that is on the current system, say 33K.
c++ eigen
|
show 3 more comments
Quick question:
consider this (wrong) casting from a double to a long int:
Eigen::VectorXd Price = Map<VectorXd>(price, n);
double TickFactor = 1.0 / TickSize;
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <long int> ();
which gives the following error (Eigen 3.3.5, g++ 7.3.0):
eigen/Eigen/src/Core/util/StaticAssert.h:33:40: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
Now, this compiles:
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
here is my question. Does the line above allows for values of (Price * TickFactor)
that are larger than the upper limit on a short int
? --whatever that is on the current system, say 33K.
c++ eigen
Have you tried it? Yes, it should, but why don't you make33000.
and then no need for a cast?
– Matthieu Brucher
Nov 23 '18 at 17:40
@MatthieuBrucher: 'but why don't you make 33000. and then no need for a cast?' I am not sure I understand. Can you elaborate a bit. Thanks!
– user189035
Nov 23 '18 at 17:42
If you just say(Price * TickFactor) > 33000.
, (to have a double value), then there should not be a need for a cast. The code would compile without it (and be more precise)
– Matthieu Brucher
Nov 23 '18 at 17:45
1
You need to store them asint64_t
by replacingVectorXi
by a typedef toMatrix<int64_t,Dynamic,1>
.
– ggael
Nov 23 '18 at 21:27
1
The cast in the first snippet is perfectly fine. You just can't assign the result to aVectorXi
– chtz
Nov 27 '18 at 10:30
|
show 3 more comments
Quick question:
consider this (wrong) casting from a double to a long int:
Eigen::VectorXd Price = Map<VectorXd>(price, n);
double TickFactor = 1.0 / TickSize;
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <long int> ();
which gives the following error (Eigen 3.3.5, g++ 7.3.0):
eigen/Eigen/src/Core/util/StaticAssert.h:33:40: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
Now, this compiles:
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
here is my question. Does the line above allows for values of (Price * TickFactor)
that are larger than the upper limit on a short int
? --whatever that is on the current system, say 33K.
c++ eigen
Quick question:
consider this (wrong) casting from a double to a long int:
Eigen::VectorXd Price = Map<VectorXd>(price, n);
double TickFactor = 1.0 / TickSize;
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <long int> ();
which gives the following error (Eigen 3.3.5, g++ 7.3.0):
eigen/Eigen/src/Core/util/StaticAssert.h:33:40: error: static assertion failed: YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY
#define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
Now, this compiles:
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
here is my question. Does the line above allows for values of (Price * TickFactor)
that are larger than the upper limit on a short int
? --whatever that is on the current system, say 33K.
c++ eigen
c++ eigen
edited Nov 23 '18 at 18:31
user189035
asked Nov 23 '18 at 17:38
user189035user189035
2,03833274
2,03833274
Have you tried it? Yes, it should, but why don't you make33000.
and then no need for a cast?
– Matthieu Brucher
Nov 23 '18 at 17:40
@MatthieuBrucher: 'but why don't you make 33000. and then no need for a cast?' I am not sure I understand. Can you elaborate a bit. Thanks!
– user189035
Nov 23 '18 at 17:42
If you just say(Price * TickFactor) > 33000.
, (to have a double value), then there should not be a need for a cast. The code would compile without it (and be more precise)
– Matthieu Brucher
Nov 23 '18 at 17:45
1
You need to store them asint64_t
by replacingVectorXi
by a typedef toMatrix<int64_t,Dynamic,1>
.
– ggael
Nov 23 '18 at 21:27
1
The cast in the first snippet is perfectly fine. You just can't assign the result to aVectorXi
– chtz
Nov 27 '18 at 10:30
|
show 3 more comments
Have you tried it? Yes, it should, but why don't you make33000.
and then no need for a cast?
– Matthieu Brucher
Nov 23 '18 at 17:40
@MatthieuBrucher: 'but why don't you make 33000. and then no need for a cast?' I am not sure I understand. Can you elaborate a bit. Thanks!
– user189035
Nov 23 '18 at 17:42
If you just say(Price * TickFactor) > 33000.
, (to have a double value), then there should not be a need for a cast. The code would compile without it (and be more precise)
– Matthieu Brucher
Nov 23 '18 at 17:45
1
You need to store them asint64_t
by replacingVectorXi
by a typedef toMatrix<int64_t,Dynamic,1>
.
– ggael
Nov 23 '18 at 21:27
1
The cast in the first snippet is perfectly fine. You just can't assign the result to aVectorXi
– chtz
Nov 27 '18 at 10:30
Have you tried it? Yes, it should, but why don't you make
33000.
and then no need for a cast?– Matthieu Brucher
Nov 23 '18 at 17:40
Have you tried it? Yes, it should, but why don't you make
33000.
and then no need for a cast?– Matthieu Brucher
Nov 23 '18 at 17:40
@MatthieuBrucher: 'but why don't you make 33000. and then no need for a cast?' I am not sure I understand. Can you elaborate a bit. Thanks!
– user189035
Nov 23 '18 at 17:42
@MatthieuBrucher: 'but why don't you make 33000. and then no need for a cast?' I am not sure I understand. Can you elaborate a bit. Thanks!
– user189035
Nov 23 '18 at 17:42
If you just say
(Price * TickFactor) > 33000.
, (to have a double value), then there should not be a need for a cast. The code would compile without it (and be more precise)– Matthieu Brucher
Nov 23 '18 at 17:45
If you just say
(Price * TickFactor) > 33000.
, (to have a double value), then there should not be a need for a cast. The code would compile without it (and be more precise)– Matthieu Brucher
Nov 23 '18 at 17:45
1
1
You need to store them as
int64_t
by replacing VectorXi
by a typedef to Matrix<int64_t,Dynamic,1>
.– ggael
Nov 23 '18 at 21:27
You need to store them as
int64_t
by replacing VectorXi
by a typedef to Matrix<int64_t,Dynamic,1>
.– ggael
Nov 23 '18 at 21:27
1
1
The cast in the first snippet is perfectly fine. You just can't assign the result to a
VectorXi
– chtz
Nov 27 '18 at 10:30
The cast in the first snippet is perfectly fine. You just can't assign the result to a
VectorXi
– chtz
Nov 27 '18 at 10:30
|
show 3 more comments
1 Answer
1
active
oldest
votes
This line
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
is essentially equivalent to
Eigen::VectorXi IntPrice(Price.size());
for(Eigen::Index i=0; i<Price.size(); ++i)
IntPrice[i] = static_cast<int>(Price[i] * TickFactor;
Unless on your system short int
and int
are the same, you are limited to the size of int
(not short int
), and the behavior for overflows is (I think) undefined.
If you want 64bit integers, do as ggael suggested:
typedef Eigen::Matrix<int64_t,Dynamic,1> VectorXi64;
VectorXi64 IntPrice = (Price * TickFactor).cast<int64_t>();
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
});
}
});
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%2f53450965%2feigen-c-cast-double-to-long-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This line
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
is essentially equivalent to
Eigen::VectorXi IntPrice(Price.size());
for(Eigen::Index i=0; i<Price.size(); ++i)
IntPrice[i] = static_cast<int>(Price[i] * TickFactor;
Unless on your system short int
and int
are the same, you are limited to the size of int
(not short int
), and the behavior for overflows is (I think) undefined.
If you want 64bit integers, do as ggael suggested:
typedef Eigen::Matrix<int64_t,Dynamic,1> VectorXi64;
VectorXi64 IntPrice = (Price * TickFactor).cast<int64_t>();
add a comment |
This line
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
is essentially equivalent to
Eigen::VectorXi IntPrice(Price.size());
for(Eigen::Index i=0; i<Price.size(); ++i)
IntPrice[i] = static_cast<int>(Price[i] * TickFactor;
Unless on your system short int
and int
are the same, you are limited to the size of int
(not short int
), and the behavior for overflows is (I think) undefined.
If you want 64bit integers, do as ggael suggested:
typedef Eigen::Matrix<int64_t,Dynamic,1> VectorXi64;
VectorXi64 IntPrice = (Price * TickFactor).cast<int64_t>();
add a comment |
This line
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
is essentially equivalent to
Eigen::VectorXi IntPrice(Price.size());
for(Eigen::Index i=0; i<Price.size(); ++i)
IntPrice[i] = static_cast<int>(Price[i] * TickFactor;
Unless on your system short int
and int
are the same, you are limited to the size of int
(not short int
), and the behavior for overflows is (I think) undefined.
If you want 64bit integers, do as ggael suggested:
typedef Eigen::Matrix<int64_t,Dynamic,1> VectorXi64;
VectorXi64 IntPrice = (Price * TickFactor).cast<int64_t>();
This line
Eigen::VectorXi IntPrice = (Price * TickFactor).cast <int> ();
is essentially equivalent to
Eigen::VectorXi IntPrice(Price.size());
for(Eigen::Index i=0; i<Price.size(); ++i)
IntPrice[i] = static_cast<int>(Price[i] * TickFactor;
Unless on your system short int
and int
are the same, you are limited to the size of int
(not short int
), and the behavior for overflows is (I think) undefined.
If you want 64bit integers, do as ggael suggested:
typedef Eigen::Matrix<int64_t,Dynamic,1> VectorXi64;
VectorXi64 IntPrice = (Price * TickFactor).cast<int64_t>();
answered Nov 27 '18 at 10:29
chtzchtz
7,99421335
7,99421335
add a comment |
add a comment |
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%2f53450965%2feigen-c-cast-double-to-long-int%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
Have you tried it? Yes, it should, but why don't you make
33000.
and then no need for a cast?– Matthieu Brucher
Nov 23 '18 at 17:40
@MatthieuBrucher: 'but why don't you make 33000. and then no need for a cast?' I am not sure I understand. Can you elaborate a bit. Thanks!
– user189035
Nov 23 '18 at 17:42
If you just say
(Price * TickFactor) > 33000.
, (to have a double value), then there should not be a need for a cast. The code would compile without it (and be more precise)– Matthieu Brucher
Nov 23 '18 at 17:45
1
You need to store them as
int64_t
by replacingVectorXi
by a typedef toMatrix<int64_t,Dynamic,1>
.– ggael
Nov 23 '18 at 21:27
1
The cast in the first snippet is perfectly fine. You just can't assign the result to a
VectorXi
– chtz
Nov 27 '18 at 10:30