static_cast vector<float> to vector<double>

Discussion in 'C++' started by richard_lavoie@gmx.de, Nov 14, 2006.

  1. Guest

    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.
    , Nov 14, 2006
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Nov 14, 2006
    #2
    1. Advertising

  3. peter koch Guest

    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]
    peter koch, Nov 14, 2006
    #3
  4. Pete Becker Guest

    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)
    Pete Becker, Nov 14, 2006
    #4
  5. Markus Moll Guest

    float vs double (was: static_cast vector<float> to vector<double>)

    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
    Markus Moll, Nov 14, 2006
    #5
  6. Guest

    Re: float vs double (was: static_cast vector<float> to vector<double>)

    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
    , Nov 14, 2006
    #6
  7. Noah Roberts Guest

    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.
    Noah Roberts, Nov 14, 2006
    #7
  8. Pete Becker Guest

    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)
    Pete Becker, Nov 14, 2006
    #8
  9. 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
    Frederick Gotham, Nov 14, 2006
    #9
  10. Nate Barney Guest

    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
    Nate Barney, Nov 14, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim West
    Replies:
    4
    Views:
    1,405
    Jeff Schwab
    Jan 16, 2004
  2. bd
    Replies:
    0
    Views:
    605
  3. Bo Peng
    Replies:
    11
    Views:
    1,068
    Victor Bazarov
    Oct 20, 2006
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,508
    James Kanze
    Oct 8, 2009
  5. junyangzou
    Replies:
    13
    Views:
    247
Loading...

Share This Page