[QSpin boxes and strange values]
-
Hello guys, I hope you're all doing well.
So I've created an application that rotates an object using quaternion. [It's an animation of the rotation]
Quaternion values are extracted from a certain csv file.
So each time the object rotates, the value of each element of the quaternion ( w, x, y and z) is displayed in the Spin boxes of the UI.
there are four spin boxes; I fixed the max number of decimals to 9, because some elements of the quaternion have 9 numbers after the comma.Other quaternion values have less than 9 numbers after the comma for example
w=0.7236
in the QSpin box, I get w=0.723698556.
98556 is added automatically and I don't even understand why that happens.Any idea on what's really happening or on how I can fix that ?
Any help would be really appreciated.
Thank you all.
-
@appdev
It's all to do with the accuracy ofdouble
values. Unlike integers, computers cannot represent floating point number with 100% accuracy, and far from all real numbers can be handled, only a subset.Let's start with: what
QSpin
box are you using. I doubt it's aQSpinBox
at all, is it aQDoubleSpinBox
?w=0.7236
in the QSpin box, I get w=0.723698556.Are you sure these are the actual numbers? Can you look closely again. I would have expected either one (bit not both) of
w=0.7237
w=0.723598556
-
@JonB First of all, thank you for the reply.
Yes it's a QDoubleSpinBox.
okay for example:
the first quaternion values extracted from the csv file are as follows:
w=-0.169757
x=-0.00949828
y=-0.550083
z=0.817619These are not the exact same values displayed in the Spin boxes.
w=-0.1697576934
x=-0.00949828277
y=-0.550082743
z=0.817619324The values in dark grey are added automatically, I don't even know how and why.
Why aren't they equal to 0 ?In design mode I fixed the number of decimals of each spin box to 9, meaning there are 9 numbers after the comma and before representing any orientation I have:
w=1.000000000
x=0.000000000
y=0.000000000
z=0.000000000 -
@appdev said in [QSpin boxes and strange values]:
Why aren't they equal to 0 ?
Because as I said not all values are exactly representable in a computer
double
. In fact, to be exact the number which are exactly representable is an infinitely small subset of real numbers!Forget about spin boxes for now.
-
Show your code which reads the numbers from CSV file.
-
Use something like QString QString::number(double n, char format = 'g', int precision = 6) to convert your numbers to strings for displaying/debugging purposes. See what happens if you play with the
precision
parameter to increase/decrease how many digits are output.
-