PDA

View Full Version : QDataStream::setFloatingPointPrecision /double / sinlge floats and doubles ?



tonnot
17th November 2011, 09:17
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 ?

Phlucious
20th December 2011, 21:06
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::operator>>(float) and QDataStream::operator>>(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.

galinette
15th June 2012, 10:55
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 :(

ChrisW67
15th June 2012, 12:47
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.

drumphil
4th November 2014, 23:53
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.