QML and C++, Float Variable not matching
-
Hello :)
I'm using a QObject to send a signal with a float variable to a QML file. But when I print the the value using
console.log
, the value doesn't match the precise value. For example, the values 116,804 in C++ becomes 116,8041611 and 116,814 becomes 116,8141632. I thought the reason for this might be the conversion from float to real, but as far as I know both, float and real, are stored in IEEE 754 single precision floating point number.The differences are very small and might not be relevant, but the project I work for requires me to explain why exactly the differences occur and proof that they will stay very small for every value.
Kind regards
-
Hi
Seems just to be a case of how many digits are printed
If you do
qDebug() << qSetRealNumberPrecision( 7 ) << yourfloat;
on c++ side, dont it match what you see with console.log ? -
@mrjj Yes, thank you, if I use
qDebug() << qSetRealNumberPrecision( 16 ) << yourfloat;
I get a simliar value, but the number of digits varies.
For examplestd:cout: 0.2
qDebug: 0.2000000029802322
qml: 0.20000000298023224std:cout: 48.1998
QDebug: 48.19983673095703
qml: 48.19983673095703Do you maybe know why the number of digits varies?
-
@Olivia-Dodonic
Hi
I think its different default "show values" for how many digits are "printed"
std::cout has
http://www.cplusplus.com/reference/iomanip/setprecision/
to alter it.