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. Weird problem with double and comparison operation
Forum Updated to NodeBB v4.3 + New Features

Weird problem with double and comparison operation

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 974 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.
  • pedrogkbP Offline
    pedrogkbP Offline
    pedrogkb
    wrote on last edited by
    #1

    I have a function to check the sum of a table, it has worked so far as it is a very simple function, however I have a very weird issue where the variable soma end up with the value 100, but when comparing it to 100 it returns false.
    Here's my code:

        QDoubleSpinBox*  tempSpin;
        double soma = 0;
        for(int i = 0; i< ui->tableSecao->rowCount(); i++){
            tempSpin = qobject_cast<QDoubleSpinBox*>(ui->tableSecao->cellWidget(i, 2));
            soma+=tempSpin->value();
        }
        qDebug() << soma;
        qDebug() << (soma==100);
        if(soma<100){
            return 1;
        }else{
            return 0;
        }
    

    When I put 100 in one row and zeroes on the others, the return of the compatison is true, when I put the actual values, and the sum is exactly 100.00, the return is false.

    J.HilkJ 1 Reply Last reply
    0
    • pedrogkbP pedrogkb

      I have a function to check the sum of a table, it has worked so far as it is a very simple function, however I have a very weird issue where the variable soma end up with the value 100, but when comparing it to 100 it returns false.
      Here's my code:

          QDoubleSpinBox*  tempSpin;
          double soma = 0;
          for(int i = 0; i< ui->tableSecao->rowCount(); i++){
              tempSpin = qobject_cast<QDoubleSpinBox*>(ui->tableSecao->cellWidget(i, 2));
              soma+=tempSpin->value();
          }
          qDebug() << soma;
          qDebug() << (soma==100);
          if(soma<100){
              return 1;
          }else{
              return 0;
          }
      

      When I put 100 in one row and zeroes on the others, the return of the compatison is true, when I put the actual values, and the sum is exactly 100.00, the return is false.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @pedrogkb when you compare 2 doubles, or floats for that matter, chances are they are actually not equal

      Qt offers QFuzzyCompare for this exact reason
      https://doc.qt.io/qt-5/qtglobal.html#qFuzzyCompare

      You should read up about double /float in computer science.


      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
      7
      • pedrogkbP Offline
        pedrogkbP Offline
        pedrogkb
        wrote on last edited by
        #3

        Wow, thanks, I'm a Software Engineering student, i've studied floating point operations but never seem anything about this issue when comparing two values.

        mrjjM 1 Reply Last reply
        0
        • pedrogkbP pedrogkb

          Wow, thanks, I'm a Software Engineering student, i've studied floating point operations but never seem anything about this issue when comparing two values.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @pedrogkb
          Hi
          I like this as friendly info about the issues with floating-point numbers in code.
          https://floating-point-gui.de/

          1 Reply Last reply
          4

          • Login

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