Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Calculations with real numbers - wrong results
Forum Updated to NodeBB v4.3 + New Features

Calculations with real numbers - wrong results

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 5 Posters 464 Views 2 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.
  • D Offline
    D Offline
    D_Tec
    wrote on 25 Aug 2020, 13:23 last edited by
    #1

    Hi,

    I just ran into another issue I am not able to fix. In my app I do some simple calculation with real numbers and sometime the results are incorrect. I cannot trigger it, it happens randomly like:
    adding 0.2 to 20.775 = 20.974999999999998

    The 0.2 is a number I extract from a json import.
    The 20.775 is a property from a QML component
    The 20.974999999999998 is the same poperty after I added 0.2

    The calculation is done

    warehouse.total += jsonData.cost
    

    In most cases this is not an issue but sometimes I see this odd results.

    J 1 Reply Last reply 25 Aug 2020, 13:29
    0
    • D D_Tec
      25 Aug 2020, 13:23

      Hi,

      I just ran into another issue I am not able to fix. In my app I do some simple calculation with real numbers and sometime the results are incorrect. I cannot trigger it, it happens randomly like:
      adding 0.2 to 20.775 = 20.974999999999998

      The 0.2 is a number I extract from a json import.
      The 20.775 is a property from a QML component
      The 20.974999999999998 is the same poperty after I added 0.2

      The calculation is done

      warehouse.total += jsonData.cost
      

      In most cases this is not an issue but sometimes I see this odd results.

      J Offline
      J Offline
      JonB
      wrote on 25 Aug 2020, 13:29 last edited by
      #2

      @D_Tec
      This is not a QML issue, it is an issue with the representation of floating point numbers. Use qFuzzyCompare to compare floating point numbers for equality.

      1 Reply Last reply
      3
      • J Offline
        J Offline
        JKSH
        Moderators
        wrote on 25 Aug 2020, 13:30 last edited by
        #3

        It is impossible to store 0.2 exactly as a floating point value.

        • This website has a good explanation: https://floating-point-gui.de/
        • You can experiment with this: https://www.h-schmidt.net/FloatConverter/IEEE754.html (it only processes 32-bit numbers, but the same concepts apply to 64-bit numbers)

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        4
        • D Offline
          D Offline
          D_Tec
          wrote on 25 Aug 2020, 13:42 last edited by
          #4

          Thanks for the hints. I thought it was a rounding issue but I was not aware of the situation. I will try to round prior displaying any float.

          1 Reply Last reply
          1
          • G Online
            G Online
            GrecKo
            Qt Champions 2018
            wrote on 25 Aug 2020, 13:45 last edited by
            #5

            Rounding before won't help you, use toFixed instead

            1 Reply Last reply
            4
            • S Offline
              S Offline
              SimonSchroeder
              wrote on 26 Aug 2020, 06:53 last edited by
              #6

              @D_Tec said in Calculations with real numbers - wrong results:

              warehouse.total += jsonData.cost

              Based on your variable names it looks like you want to do computations with a monetary unit. It is advisable that you do not use floating point for this. If you add small amounts to your warehouse.total you will eventually have true rounding errors. Instead, you should do all computations with integers. If you decide to have at most 3 digits after the point, you would represent your numbers like this: 200 (=0.2) and 20775 (=20.775). Now, you don't have any loss during computations. When you want to show the number you just need to add the point in the right place (making sure you have enough leading zeros).

              1 Reply Last reply
              6

              5/6

              25 Aug 2020, 13:45

              • Login

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