Reputation: 455
I need (I must) to write a lot of floats to a qdatastream and it is neccesary I use only 4 bytes. setFloatingPointPrecision or writes 4 or 8 both for floats and doubles. I thought that it was applicable to qreal, but I surprise it works on float a double type..
Is there any solution ? Thanks
Upvotes: 1
Views: 3730
Reputation: 12321
A possible workaround is to write a function and overload it:
void writeReal(QDataStream& out, float f)
{
out.setFloatingPointPrecision(QDataStream::SinglePrecision);
out << f;
}
void writeReal(QDataStream& out, double f)
{
out.setFloatingPointPrecision(QDataStream::DoublePrecision);
out << f;
}
Now simply use this function for serializing floats and doubles
QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
writeReal(out, (float) 3.14); // Casting to float so single precision will be used
writeReal(out, (double) 3.14); // Casting to double so double precision will be used
Upvotes: 6
Reputation: 96241
If you want to write only four bytes, have you considered static_cast
ing your double
s to float
? I'm not sure what a qdatastream
but that might work for you.
Upvotes: 0