By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

static_cast vector<float> to vector<double>

P: n/a
Hi,
I have something like this:

vector<floatvec1;
and I want to cast it, so I use
vector vec2<double= static_cast< vector<double(vec1);

I always become a
error: syntax error before `>' token
in that line
I let a space between the ">"

Please help me:-)
Thanks
R.

Nov 14 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

ri************@gmx.de skrev:
Hi,
I have something like this:

vector<floatvec1;
and I want to cast it,
No you definitely don't want to do that.
so I use
vector vec2<double= static_cast< vector<double(vec1);
static_cast will not work there. Why are you changing between float and
double in the first place? You need a very good reason to use float:
double has better precision and is usually just as fast (or faster) as
well.
>
I always become a
error: syntax error before `>' token
in that line
I let a space between the ">"
[snip]

Nov 14 '06 #2

P: n/a
ri************@gmx.de wrote:
Hi,
I have something like this:

vector<floatvec1;
and I want to cast it, so I use
vector vec2<double= static_cast< vector<double(vec1);
a) That's a syntax error. You meant

vector< double vec2 = static_cast< vector<double(vec1);

b) Even corrected, it would not accomplish anything for you since there is
no valid conversion from vector<floatto vector<double>. Try:

vector<doublevec2 ( vec1.begin(), vec1.end() );
Best

Kai-Uwe Bux
Nov 14 '06 #3

P: n/a
ri************@gmx.de wrote:
I have something like this:

vector<floatvec1;
and I want to cast it, so I use
vector vec2<double= static_cast< vector<double(vec1);
vector<doublevec2 = ...
I always become a
error: syntax error before `>' token
in that line
I let a space between the ">"
But even with that correction, this won't work. A vector that holds
doubles is not a vector that holds floats, and there's no way to tell
the compiler to pretend that it is.

Having to convert between float and double sometimes indicates a design
problem. What is the problem that you're trying to solve with this cast?

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 14 '06 #4

P: n/a
Hi

peter koch wrote:
Why are you changing between float and
double in the first place? You need a very good reason to use float:
double has better precision and is usually just as fast (or faster) as
well.
Weeeeell... I read a discussion about that recently, I would like to believe
it. But as I was trying to optimize some math code today, I looked it up
again. I can only say things about Intel processors, but...

From the "IntelĀ® 64 and IA-32 Architectures Optimization Reference Manual":

User/Source Coding Rule 15. (M impact, ML generality) Do not use double
precision unless necessary. Set the precision control (PC) field in the x87
FPU control word to "Single Precision". This allows single precision
(32-bit) computation to complete faster on some operations (for example,
divides due to early out). However, be careful of introducing more than a
total of two values for the floating point control word, or there will be a
large performance penalty. See Section 3.8.3.

[...]

3.8.3.2 Precision
If single precision is adequate, use it instead of double precision. This is
true because:

Single precision operations allow the use of longer SIMD vectors, since more
single precision data elements can fit in a register.

If the precision control (PC) field in the x87 FPU control word is set to
single precision, the floating-point divider can complete a
single-precision computation much faster than either a double-precision
computation or an extended double-precision computation. If the PC field is
set to double precision, this will enable those x87 FPU operations on
double-precision data to complete faster than extended double-precision
computation. These characteristics affect computations including
floating-point divide and square root.

Markus
Nov 14 '06 #5

P: n/a
Thank you very much for you fast answers.
The problem is that I have to use a library and I“m obliged to call
the function with the right arguments. That“s why I have to cast...
To change all, is possible but too complicated since I don“t need the
precision of a double
thanks
R.


Markus Moll schrieb:
Hi

peter koch wrote:
Why are you changing between float and
double in the first place? You need a very good reason to use float:
double has better precision and is usually just as fast (or faster) as
well.

Weeeeell... I read a discussion about that recently, I would like to believe
it. But as I was trying to optimize some math code today, I looked it up
again. I can only say things about Intel processors, but...

From the "Intel® 64 and IA-32 Architectures Optimization Reference Manual":

User/Source Coding Rule 15. (M impact, ML generality) Do not use double
precision unless necessary. Set the precision control (PC) field in the x87
FPU control word to "Single Precision". This allows single precision
(32-bit) computation to complete faster on some operations (for example,
divides due to early out). However, be careful of introducing more than a
total of two values for the floating point control word, or there will bea
large performance penalty. See Section 3.8.3.

[...]

3.8.3.2 Precision
If single precision is adequate, use it instead of double precision. Thisis
true because:

Single precision operations allow the use of longer SIMD vectors, since more
single precision data elements can fit in a register.

If the precision control (PC) field in the x87 FPU control word is set to
single precision, the floating-point divider can complete a
single-precision computation much faster than either a double-precision
computation or an extended double-precision computation. If the PC field is
set to double precision, this will enable those x87 FPU operations on
double-precision data to complete faster than extended double-precision
computation. These characteristics affect computations including
floating-point divide and square root.

Markus
Nov 14 '06 #6

P: n/a

Pete Becker wrote:
A vector that holds
doubles is not a vector that holds floats, and there's no way to tell
the compiler to pretend that it is.
Sure there is:

vector<double& vec2 = *reinterpret_cast< vector<double>*
>(&float_vect);

To the OP: this won't do what you want, it will only introduce
undefined behavior and do 'interesting things'. Don't actually use it.

Nov 14 '06 #7

P: n/a
Noah Roberts wrote:
Pete Becker wrote:
>A vector that holds
doubles is not a vector that holds floats, and there's no way to tell
the compiler to pretend that it is.

Sure there is:

vector<double& vec2 = *reinterpret_cast< vector<double>*
>(&float_vect);


To the OP: this won't do what you want, it will only introduce
undefined behavior and do 'interesting things'. Don't actually use it.
Whoops, I should have said "there's no sane way to tell the compiler to
pretend that it is."

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 14 '06 #8

P: n/a
Noah Roberts:
Sure there is:

vector<double& vec2 = *reinterpret_cast< vector<double>*
>>(&float_vect);


To the OP: this won't do what you want, it will only introduce
undefined behavior and do 'interesting things'. Don't actually use it.

Another alternative would be:

vector<double&vec2 = reinterpret_cast<vector<double>&>(float_vect);

This way also works in cases where the addressof operator is overloaded.

--

Frederick Gotham
Nov 14 '06 #9

P: n/a
Frederick Gotham wrote:
Noah Roberts:
>Sure there is:

vector<double& vec2 = *reinterpret_cast< vector<double>*
>>(&float_vect);

To the OP: this won't do what you want, it will only introduce
undefined behavior and do 'interesting things'. Don't actually use it.


Another alternative would be:

vector<double&vec2 = reinterpret_cast<vector<double>&>(float_vect);

This way also works in cases where the addressof operator is overloaded.
'works' here is defined very loosely.

Nate
Nov 14 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.