Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static_cast vector<float> to vector<double>

Reply
Thread Tools

static_cast vector<float> to vector<double>

 
 
richard_lavoie@gmx.de
Guest
Posts: n/a
 
      11-14-2006
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.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-14-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      11-14-2006

(E-Mail 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]

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-14-2006
(E-Mail Removed) wrote:
> 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<double> vec2 = ...

> 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)
 
Reply With Quote
 
Markus Moll
Guest
Posts: n/a
 
      11-14-2006
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
 
Reply With Quote
 
richard_lavoie@gmx.de
Guest
Posts: n/a
 
      11-14-2006
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 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


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      11-14-2006

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.

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-14-2006
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)
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      11-14-2006
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
 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      11-14-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is static_cast<int>(static_cast<double>(a)) == a? Bo Peng C++ 11 10-20-2006 12:59 PM
Re: static_cast question Alan Sung C++ 0 08-02-2003 02:47 AM
static_cast<> alg C++ 2 07-14-2003 06:01 PM
how to static_cast<MyClass* &>(p) ??? Wenjie C++ 4 07-12-2003 03:04 AM
static_cast confusion Chandra Shekhar Kumar C++ 7 06-27-2003 02:54 PM



Advertisments