Qt Creator double spin box possible broken[inprecise values]



  • 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?



  • 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



  • aaa thank you.

    What function could I use to turn that 0.90000000000002 into 0.9? Well do round-up to 1 decimal place?


  • Moderators

    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 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 !



  • [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



  • [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 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.