Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Creator double spin box possible broken[inprecise values]
Forum Updated to NodeBB v4.3 + New Features

Qt Creator double spin box possible broken[inprecise values]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mariusmssj
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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

      Greetz, Jeroen

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mariusmssj
        wrote on last edited by
        #3

        aaa thank you.

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          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;
          @

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mariusmssj
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on last edited by
              #6

              [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

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dbzhang800
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mariusmssj
                  wrote on last edited by
                  #8

                  [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 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved