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. 1.1 - 1.1 != 0 ???
Forum Updated to NodeBB v4.3 + New Features

1.1 - 1.1 != 0 ???

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 5.8k 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.
  • N Offline
    N Offline
    nouch64
    wrote on last edited by
    #1

    Hi,

    I got a feature I can't explain with QT 4.7.4 (32bit) on Windows :
    @
    float test;
    test = 1.1;
    test -= 1.1;

    qDebug() << "test:" << test;
    
    if (test != 0)
    { qDebug() << "not null"; }
    

    @

    result :
    @
    test: 2.38419e-08
    not null
    @

    Have you got an explanation for that ?

    Thanks for all.
    Best regards.

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      Read this "faq":http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nouch64
        wrote on last edited by
        #3

        I confirmed the same behavior with gcc 4.5.3, but still I can't find the explanation.

        Thanks for all.
        Best regards.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nouch64
          wrote on last edited by
          #4

          Whaw !
          More than 10 years of coding and I never faced this problem before, it's amazing !

          Thanks for the link.
          Best regards.

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

            [quote author="nouch64" date="1340448236"]I confirmed the same behavior with gcc 4.5.3, but still I can't find the explanation.

            Thanks for all.
            Best regards.[/quote]

            Well, the link provided by Zlatomir has the explanation, I think.

            You really should be doing something like this, when comparing floating point numbers:
            @#define EPS 0.0000001
            if(abs(test) > EPS)
            {
            qDebug() << "not null";
            }@

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • I Offline
              I Offline
              ivan
              wrote on last edited by
              #6

              Qt even has a float/double comparing function - qFuzzyCompare:
              http://doc.qt.nokia.com/4.7-snapshot/qtglobal.html#qFuzzyCompare-2

              Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
              Qt Ambassador (from the old Nokia days)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #7

                When using qFuzzyCompare() take care of the following hint that can be found in the docs:
                Note that comparing values where either p1 or p2 is 0.0 will not work.

                There is an undocumented(?) qFuzzyIsNull() function though, which should perfectly fit here.

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Scylla
                  wrote on last edited by
                  #8

                  How about
                  @
                  double epsilon = std::numeric_limits<double>::epsilon();
                  double a1 = 1.1;
                  double b1 = 1.1;
                  if (fabs(a1-b1)<epsilon){
                  //result is 0
                  }else{
                  //result is not 0
                  }
                  @

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MuldeR
                    wrote on last edited by
                    #9

                    Actually it's not a good idea to use a fixed Epsilon, if you want to test two (arbitrary) floating point numbers for equality. The numeric_limits::epsilon() is the "difference between 1 and the least value greater than 1 that is representable". If you are comparing very small floating point numbers, this epsilon might be much bigger than the values you are comparing! And if you are comparing very large floating point numbers, this epsilon might be unreasonably small! Instead of the fixed epsilon, the epsilon should be "scaled" according to the floating point numbers you are comparing, just like qFuzzyCompare() does. Testing a given floating point number for equality with zero is a special case though. Because we know that one of the two numbers we are comparing will always be zero, a reasonable fixed epsilon can be chosen - just like qFuzzyIsNull() does.

                    See also:
                    http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

                    My OpenSource software at: http://muldersoft.com/

                    Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                    Go visit the coop: http://youtu.be/Jay...

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BruceDawson
                      wrote on last edited by
                      #10

                      As the author of that article I want to state emphatically that you should not need any epsilon in this case. The non-zero result here is the result of a programmer mistake. If you correct that mistake then the error will disappear.

                      A float is roughly a 7-digit numbers. A double is roughly a 15-digit number. What you are doing is equivalent to:

                      float test;
                      test = 0.333333333333333;
                      test -= 0.333333333333333;

                      The trouble is that when you assign to 'test' the value is truncated to 0.3333333. That's where the error comes in.

                      It's not obvious because 1.1 seems like such a nice tidy number, but in binary floating-point 1.1 is a repeating fraction that cannot be perfectly represented.

                      Don't use an epsilon to hide a coding error.

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        BruceDawson
                        wrote on last edited by
                        #11

                        http://randomascii.wordpress.com/2012/06/26/doubles-are-not-floats-so-dont-compare-them/

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lgeyer
                          wrote on last edited by
                          #12

                          Thank you for those interesting reads and sharing them here!

                          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