Results 1 to 5 of 5

Thread: QDataStream::setFloatingPointPrecision /double / sinlge floats and doubles ?

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QDataStream::setFloatingPointPrecision /double / sinlge floats and doubles ?

    On qt reference you write :
    if the floating point precision is DoublePrecision and the version of the data stream is Qt_4_6 or higher, all floating point numbers will be written and read with 64-bit precision. If the floating point precision is SinglePrecision and the version is Qt_4_6 or higher, all floating point numbers will be written and read with 32-bit precision.
    I want always 32 for my floats and 64 for my doubles.

    I dont understand what is for setFloatingPointPrecision...

    Auto answer... Only affects to Qreal ?
    Last edited by tonnot; 17th November 2011 at 08:33.

  2. #2
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataStream::setFloatingPointPrecision /double / sinlge floats and doubles ?

    I'm curious about this as well. It's extremely annoying to have to switch the FloatingPointPrecision just so that I can read a float as a float instead of as a double. This seems completely arbitrary.

    They have methods for both QDataStream:perator>>(float) and QDataStream:perator>>(double), so why would I ever want them to do exactly the same thing? Nothing else in QDataStream's IO does this... that's exactly why I use it in the first place—to read exactly the number of bytes and signage that I intend to read.

  3. #3
    Join Date
    Jun 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream::setFloatingPointPrecision /double / sinlge floats and doubles ?

    I just discovered this... This messed up a template function where I used QDataStream::operator>> with a templated T type parameter. Now I have to make specialized templates :mad:

    So since 4.6, if you call QDataStream::operator>>(float &) with floatingPointPrecision set to DoublePrecision, you just end up with corrupted float value. Very useful property :(

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDataStream::setFloatingPointPrecision /double / sinlge floats and doubles ?

    So since 4.6, if you call QDataStream::operator>>(float &) with floatingPointPrecision set to DoublePrecision, you just end up with corrupted float value. Very useful property :(
    The value written to the stream is in no way corrupt: it is just not what you were expecting.

    QDataStream is designed to write and read a platform-independent byte stream for serialising objects. That platform-independence has to allow for interchange with compilers/platforms where sizeof(double) == sizeof(float), natural byte orders are different etc. QDataStream adopts a set of conventions to achieve this. If you call QDataStream::operator<<(float &) you get a default stream of bytes in the output that will be correctly read by QDataStream::operator>>(float &) by a QDataStream of the same version. The same is true for doubles. QDataStream has versioning ability in order that backward compatibility can be maintained. If you want the old behaviour then call setVersion(QDataStream::Qt_4_5).

    If you are using QDataStream to read or write an arbitrary byte stream where you want to control byte order exactly then you probably should be using QIODevice directly.

  5. #5
    Join Date
    Nov 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDataStream::setFloatingPointPrecision /double / sinlge floats and doubles ?

    I agree with galinette and Phlucious. This is very annoying. I want to be able to read/write both 32 and 64 bit floats depending on the type.
    May I suggest a 3rd flag for floatingPointPrecesion, QDataStream::MixedPrecision 2, which when set, reads/writes floats as 32 bit, and doubles as 64 bit.
    Thanks.

Similar Threads

  1. QTableView and display of doubles
    By b1 in forum Qt Programming
    Replies: 9
    Last Post: 18th November 2011, 22:52
  2. Replies: 2
    Last Post: 17th May 2011, 10:47
  3. Program not finding an existing double in a QList<double>
    By aarelovich in forum Qt Programming
    Replies: 2
    Last Post: 9th May 2011, 20:59
  4. QPainter::drawPixmap with floats ?
    By christophe.daudin in forum Qt Programming
    Replies: 8
    Last Post: 20th October 2009, 10:19
  5. vector of doubles to QByteArray
    By gmseed in forum Qt Programming
    Replies: 5
    Last Post: 16th October 2009, 15:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.