c++ compare two doubles

Discussion in 'Homework Help' started by Elvz, Dec 4, 2011.

  1. Elvz

    Thread Starter New Member

    Nov 27, 2011
    3
    0
    Hi, this is my first post (so please go easy^^), I've been given an assignment to produce a sine wave simulator in terminal. I'm using the GNU g++ compiler.

    My problem is comparing two doubles and outputting the higher value, I've included a small program to demonstrate my problem. In the program I would like to output the numbers above 1.75. Also if there is a better way of comparing doubles I would appreciate any ideas:

    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. #include <cmath>
    4.  
    5. using namespace std;
    6.  
    7. int main()
    8. {
    9.     double numbers = 0.25; // double number to work with
    10.     int count = 0;
    11.  
    12. cout << "Show numbers 1.75 and over:" << endl;
    13. cout << "___________________________" << endl;
    14.  
    15. for  (count; count < 10; count++) // count to ten
    16. {
    17.     numbers = numbers + 0.25; // double + 0.25 for my values
    18.  
    19.     if (fabs(numbers < 1.75) < 0.01) ; // if float absolute number over 1.75 then
    20.     {
    21.         cout << (numbers);        // output the numbers.
    22.         cout << ("    = ") << (fabs(numbers < 1.75)<0.01) << endl;
    23.     }
    24.  
    25. }
    26. return 0;
    27. }
    28.  
    Thank you in advance :)
     
  2. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    You say you've got a problem but you don't actually say what the problem is :)

    I may have got this wrong but the fabs function expects a floating point number but you are passing it what amounts to a boolean
    Code ( (Unknown Language)):
    1. (numbers < 1.75)
     
  3. Elvz

    Thread Starter New Member

    Nov 27, 2011
    3
    0
    Thank you for your quick reply :), I'm new to using the fabs function and have to admit I really should have researched more into it before posting (The chances are I'm using completely the wrong function for what I need).

    In my actual assignment I need to accurately compare two points on a sine wave and plot a character on screen depicting if it's equal, larger or smaller than the previous. In the code I posted, for simplicity I gave a fixed value. My problem seems to be only outputting the values above 1.75.

    You mentioned the result basically amounting to a boolean and I have tried to use this to my advantage, but no matter what I try, all values are still being displayed.

    Thank you once again, I'm starting to think about moving away from fabs (as I'm not using it right), could you please advise me to alternative ways of comparing floats/doubles
     
  4. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    I've had another look and can see another problem, this one will cause all numbers to be output
    Code ( (Unknown Language)):
    1.  
    2.     if (fabs(numbers < 1.75) < 0.01) ; // if float absolute number over 1.75 then     {         cout << (numbers);        // output the numbers.         cout << ("    = ") << (fabs(numbers < 1.75)<0.01) << endl;     }
    After your if condition you have a ";" - this will mark then end of the block of code after if the if. Therefore the code between the "{" and "}" that follow will always get executed.

    Trying something like:

    Code ( (Unknown Language)):
    1.  
    2. if fabs(numbers-1.75) < 0.01 {
    3.       cout << (numbers);
    4.       cout fabs(numbers - 1.75) << endl;
    5. }
    6.  
     
  5. Elvz

    Thread Starter New Member

    Nov 27, 2011
    3
    0
    Haha thank you DumboFixer, I really should pay more attention :)
    I finally did manage to get my program to compare the two doubles by simply directly comparing them both in an "if" statement. Strange, I was told not to do this because of differences between compilers and platforms (but it worked like a charm).

    I guess, I really should just experiment more before posting, it seems sometimes the simplest solution is the best one :D
    Thank you once again for your help.
     
  6. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    Glad you got it sorted.

    You do have to exercise caution when dealing with floating point numbers but generally they don't cause a problem (the main problem is checking for equality)
     
Loading...