C++ comparing doubles

Discussion in 'OT Technology' started by Pudge, Nov 18, 2009.

  1. Pudge

    Pudge New Member

    Joined:
    Nov 24, 2008
    Messages:
    2,572
    Likes Received:
    0
    Location:
    Athens, GA
    I set the value from argv[2] into a 'double' variable and set up a while loop from the counter(another double) to increment 0.001 until it reaches argv[2]. The problem is that even if it reaches it it just keeps counting and I don't know why.

    It's in the main method (this is a program to test knowledge of system signals):

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <signal.h>
    #include <curses.h>
    #include <stdio.h>
    #include <unistd.h>
    
    using namespace std;
     double total;
     double numCount;
     bool debug=false;
     int alrm=0;
    void term(int sig){
           cout << "SIGUSR1" << endl;
          cout << "total: " << total << endl;
          signal(SIGUSR1, term);
    }
    void termTwo(int sig){
           cout << "SIGUSR2" << endl;
           if(debug==false){
            printf("Debug mode is now on\n");
            debug=true;
            signal(SIGUSR1, SIG_IGN);
                  alarm(alrm);
            
        }
        else{
            printf("Debug mode is now off\n");
            debug=false;
            signal(SIGUSR1, term);
               alarm(0);
    
    }  
        signal(SIGUSR2, termTwo);
    }
    void termThree(int sig){
           cout << "\ncount:  " << numCount;
        cout << "\ntotal: " << total << endl;
        cout << "PROGRAM TERMINATED" << endl;
        exit(1);
    }
    void termFour(int sig){
           cout << "SIGALRM" << endl;
        cout << "total: " << total << endl;
        signal(SIGALRM, termFour);
           alarm(alrm);
           
    }
    int main(int argc, char *argv[])
    {
        if(argc!=4) {
        printf("Usage: Buzzoff.out <int 1> <int 2> <int 3>\n");
        return 1;
      }
       double value=(double)strtoul(argv[2],0,0);
       double temp=(double)strtoul(argv[1],0,0);
       alrm=(int) argv[3];
       signal(SIGUSR1, term);
       signal(SIGUSR2, termTwo);
       signal(SIGINT, termThree);
       signal(SIGALRM, termFour);
        
        while(numCount!=value){
               if(numCount==value){
            exit(1);
    }
            cout << numCount << endl;
            cout << value << endl; 
               numCount+=0.001;
               total+=numCount*temp;              
                              }     
       
    }
    
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    post your output please
     
  3. StevesVR4

    StevesVR4 Get Arrested

    Joined:
    Jul 1, 2003
    Messages:
    7,328
    Likes Received:
    0
    The == operator is basically useless for floating point numbers. The numbers are not precise enough for direct comparison.
     
  4. CodeX

    CodeX Guest

    This.

    I didn't look at the code but if these values are calculated it's very unlikely that you will get floating point results that can be evaluated as equal even if you expect them to be equal.

    If you assign them, then sure, it should work no problem.

    Instead of == try checking to see if it is in some small range of values.

    if(double > low && double < high)

    Something like that.

    On a side note, the way floating point values work is very interesting. Unlike integer values they don't have a maximum value per-say... well, it is very very large anyway. Floating point numbers can dynamically sacrifice size for precision and vice-versa. Read this for more info:

    http://steve.hollasch.net/cgindex/coding/ieeefloat.html
     
  5. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    :werd:

    this is why many languages don't even allow direct equality comparison between floating point numbers.
     
  6. mc_88

    mc_88 New Member

    Joined:
    Aug 17, 2009
    Messages:
    18
    Likes Received:
    0
    use this: if (Abs(x – y) <= EPSILON)
    EPSILON is a small amount that is acceptable for comparing doubles and floats. I think C++ has it already. If not replace it with .001 or whatever precision you need
     

Share This Page