Qt Creator double spin box possible broken[inprecise values]
-
wrote on 31 Mar 2012, 18:56 last edited by
It seems that the double spinbox has a weird offset and is giving me weird values
My double spin box setup:
[img]http://i.imgur.com/CaR9l.png[/img]When I run in debug mode and press down to change from 1.0 to 0.9 I get this:
[img]http://i.imgur.com/xIvP7.png[/img]
(Which I think is clearly wrong value)And if I try to get to 0.0 I get this
[img]http://i.imgur.com/K0f8p.png[/img]Am I doing something wrong or is double spin box just broken?
-
wrote on 31 Mar 2012, 19:05 last edited by
Nup, it is absolutly correct!! It has to do with the amount of bits in a double and the value that they can represent. You should use a rounded value instead. So when you have a value that is only able to have 1 decimal, round it to that value first before doing anything with the value.
All processors will have the same mistake and how less bits you use, the bigger the error in the last decimals. Only some compilers will handle this effect for you when you use standard functions or classes.
Remember, you are working digitally, not analog.
Greetz -
wrote on 31 Mar 2012, 19:08 last edited by
aaa thank you.
What function could I use to turn that 0.90000000000002 into 0.9? Well do round-up to 1 decimal place?
-
wrote on 31 Mar 2012, 19:41 last edited by
you can use "qRound":http://qt-project.org/doc/qt-4.8/qtglobal.html#qRound for rounding to the nearest int. You need scaling up and back down. In your case:
@
qreal num = 0.90000000000002;
qreal rounded = qRound ( num * 10 ) / 10.0;
@ -
wrote on 31 Mar 2012, 21:23 last edited by
[quote author="koahnig" date="1333222906"]you can use "qRound":http://qt-project.org/doc/qt-4.8/qtglobal.html#qRound for rounding to the nearest int. You need scaling up and back down. In your case:
@
qreal num = 0.90000000000002;
qreal rounded = qRound ( num * 10 ) / 10.0;
@[/quote]thank you !
-
wrote on 31 Mar 2012, 21:35 last edited by
[quote author="mariusmssj" date="1333220882"]aaa thank you.
What function could I use to turn that 0.90000000000002 into 0.9? Well do round-up to 1 decimal place?[/quote]
Hi mariusmss,
In fact, you can not!
There will never exist a float value 0.9 in your computer. as you can not
0.9 = (1/2) + (1/4) + (1/8) + (1/32) + ...
with limited length.
Perhaps all your need is a string "0.9" instead of float number 0.9 , otherwise, it make no sense to do so.
Regards,
Debao
-
wrote on 31 Mar 2012, 21:38 last edited by
[quote author="koahnig" date="1333222906"]you can use "qRound":http://qt-project.org/doc/qt-4.8/qtglobal.html#qRound for rounding to the nearest int. You need scaling up and back down. In your case:
@
qreal num = 0.90000000000002;
qreal rounded = qRound ( num * 10 ) / 10.0;
@[/quote]This is wrong here. Round used in following case.
0.9029293424 == > 0.9 (And of course, the later will actually be 0.90000000000002)
-
wrote on 1 Apr 2012, 06:48 last edited by
[quote author="1+1=2" date="1333229922"]
[quote author="koahnig" date="1333222906"]you can use "qRound":http://qt-project.org/doc/qt-4.8/qtglobal.html#qRound for rounding to the nearest int. You need scaling up and back down. In your case:
@
qreal num = 0.90000000000002;
qreal rounded = qRound ( num * 10 ) / 10.0;
@[/quote]This is wrong here. Round used in following case.
0.9029293424 == > 0.9 (And of course, the later will actually be 0.90000000000002)[/quote]
well yeah it still have me the weird 0.90000000000002 number but when my spinbox reached 0.0 it didn't mess it up so i guess it did work
1/8