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)): #include <iostream> #include <cmath> using namespace std; int main() { double numbers = 0.25; // double number to work with int count = 0; cout << "Show numbers 1.75 and over:" << endl; cout << "___________________________" << endl; for (count; count < 10; count++) // count to ten { numbers = numbers + 0.25; // double + 0.25 for my values 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; } } return 0; } Thank you in advance
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)): (numbers < 1.75)
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
I've had another look and can see another problem, this one will cause all numbers to be output Code ( (Unknown Language)): 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)): if fabs(numbers-1.75) < 0.01 { cout << (numbers); cout fabs(numbers - 1.75) << endl; }
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 Thank you once again for your help.
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)