static_cast vector<float> to vector<double>

R

richard_lavoie

Hi,
I have something like this:

vector<float> vec1;
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.
 
K

Kai-Uwe Bux

Hi,
I have something like this:

vector<float> vec1;
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<float> to vector<double>. Try:

vector<double> vec2 ( vec1.begin(), vec1.end() );


Best

Kai-Uwe Bux
 
P

peter koch

(e-mail address removed) skrev:
Hi,
I have something like this:

vector<float> vec1;
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]
 
P

Pete Becker

I have something like this:

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

vector said:
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)
 
M

Markus Moll

Hi

peter said:
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
 
R

richard_lavoie

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 said:
Hi

peter said:
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
 
N

Noah Roberts

Pete said:
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 said:
(&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.
 
P

Pete Becker

Noah said:
Sure there is:




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)
 
F

Frederick Gotham

Noah Roberts:
Sure there is:




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.
 
N

Nate Barney

Frederick said:
Noah Roberts:



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top