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. Weird substraction result
Forum Updated to NodeBB v4.3 + New Features

Weird substraction result

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 5 Posters 725 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.
  • D Offline
    D Offline
    DavidM29
    wrote on last edited by
    #1

    Hello,

    I'm asking my software to make a substraction and print the result in a Text and I found a situation where the result was weird. I thought it was because I had something wrong in my program so I tried the same calculation on a simple program and I get the same result.

    Here is the problem :
    I'm trying to make this calculation : 25-0.9-0.9 which should result as 23.2
    But what I'm having is : 23.200000000000003
    0_1539616583278_0cd59785-9378-4107-a174-32986f4e03be-image.png

    Here is the code I run :

    main.qml

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        Text{
            id: value
            text : qsTr((25-0.9-0.9).toString())
            anchors.centerIn: parent
        }
    }
    

    Does anybody has any idea why it behave that way ?
    I already found a way to correct this by using the toFixed(1) method but I wonder why I do have this behaviour. I've never seen a computer being wrong on a calculation so I believe I have done something wrong.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Welcome to the magic world Floating-point arithmetic.

      I've never seen a computer being wrong on a calculation

      You are in for a rough ride!

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      J.HilkJ 1 Reply Last reply
      5
      • VRoninV VRonin

        Welcome to the magic world Floating-point arithmetic.

        I've never seen a computer being wrong on a calculation

        You are in for a rough ride!

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        @VRonin best reply ever πŸ˜‚πŸ‘Œ

        @DavidM29 you should read this wikipedia article, probably the most (in)famous case of a computer being wrong in calculations
        https://en.m.wikipedia.org/wiki/Pentium_FDIV_bug

        But for the behaviour you ecounterd, read up on floating point numbers and precision
        https://en.m.wikipedia.org/wiki/Floating-point_arithmetic


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        4
        • C Offline
          C Offline
          Cyrille de Brebisson
          wrote on last edited by
          #4

          Hello,

          I will try to make it short and easy to understand...

          Let us assume that you are doing math by had, with 3 digits of precision and execute the following operation:
          1 - (1/3 * 3) = 1-(0.3333*3) = 1-0.999 = 0.001
          You know that mathematically you should get 0, however, when YOU do the calculations, you get 0.001!

          Well, computers have similar issues, however, since they use base 2 instead of base 10, the issues are to be found in different operations than the ones that you are used to. Hence the "stupefaction".

          In your case, some of your numbers (0.9) do not have an exact representation in base 2, so you get rounding (down) on these numbers and end up with the result that you see.

          Now, you need to be VERY carefull, because, in addition to this, the computer will often "hide" this to you by "rounding" the number (display 23.2 while in fact it has 23.20000...002 in memory). Then you THINK that you have the correct number, but if you perform another operation (like do an == test with 23.2), you do not get what you think!

          Best advise, never use floats unless you HAVE to.
          Never do == or != operations on floats (do abs(value1-value2)<small number)

          Cyrille

          1 Reply Last reply
          3
          • D Offline
            D Offline
            DavidM29
            wrote on last edited by
            #5

            Thank you all your answers are useful. I understand better now. Did not think about this.

            1 Reply Last reply
            0
            • kkoehneK Offline
              kkoehneK Offline
              kkoehne
              Moderators
              wrote on last edited by
              #6

              For those interested in more details, I can recommend reading "What Every Computer Scientist Should Know About Floating-Point Arithmetic", see e.g. https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html . It's a classic.

              Director R&D, The Qt Company

              1 Reply Last reply
              1

              • Login

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