448,797 Members | 1,315 Online
Need help? Post your question and get tips & solutions from a community of 448,797 IT Pros & Developers. It's quick & easy.

# Comparing two floats or doubles to a precision

 P: n/a Hi, Is it possible to compare / output two numbers to a certain precision? for instance: #include #include [itex] #include #include #include #include #include #include main() { double one = 0.987432100 double two = 0.987123456 if ( one == two ) // [[ to a precision of 3 d.p.]] { std::cout << "YES! :D "; } else { cout << "No! >:( "; } Thanks, Alex :) -- Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual... Jul 19 '05 #1
4 Replies

 P: n/a {AGUT2} {H}-IWIK wrote: Hi, Is it possible to compare / output two numbers to a certain precision? for instance: #include #include [itex] #include #include #include #include #include #include main() int main() There is no 'implicit int' rule in C++. { double one = 0.987432100 double two = 0.987123456 if ( one == two ) // [[ to a precision of 3 d.p.]] You could write a function for it. But depending on exactly what you want to do it could be tricky. In this case you could probably multiply by 1000, convert to int, then compare. But this doesn't work well as a general solution because 1) int (or long) may not be wide enough for the result, and 2) the rounding method (truncation) may not be what you want. { std::cout << "YES! :D "; } else { cout << "No! >:( "; std::cout Also, you are missing: } And finally, you need to end your program's output with a newline: std::cout << std::endl; } Thanks, Alex :) -Kevin -- My email address is valid, but changes periodically. To contact me please use the address from a recent posting. Jul 19 '05 #2

 P: n/a >> int main() Also, you are missing: } And finally, you need to end your program's output with a newline: std::cout << std::endl; OK, I apologise PROFUSELY for my apalling syntax. :D I'm trying to jump in headfirst as that's how (I find) I learn the fastest. Your tips are appreciated. Also, thank you for your idea. However, after lots (and lots and lots) of Googling, I found the answer! I have got an if ( fabs(double1-double2) < 0.0000001 ) { .... } In my program and this seems to work wonderfully. Thank you again for your time. Alex -- Reply to:alex an.ti livingstone sp@am btinternet.com cutting the usual... If you fancy a chat, #agut on quakenet :) For all your UT2003 questions, visit the official UT2003 newsgroup FAQ at: http://www.unrealtower.org/faq.php Jul 19 '05 #3

 P: n/a {AGUT2} {H}-IWIK wrote: if ( fabs(double1-double2) < 0.0000001 ) Yeah, I should have thought of that. -Kevin -- My email address is valid, but changes periodically. To contact me please use the address from a recent posting. Jul 19 '05 #4

 P: n/a {AGUT2} {H}-IWIK wrote in message news:... I have got an if ( fabs(double1-double2) < 0.0000001 ) { ... } In my program and this seems to work wonderfully. Note however that this is not the general rule. Consider the problem where you have the typical magnitude of values inferior to 0.0000001, then all your values will seem equal according to that rule. A more general solution would be to consider the magnitude of the numbers involved in the comparison: #include bool equals(double d1, double d2, double precision); int main() { double a = 1, b = 1.0001; if (equals(a, b, 0.00001)) std::cout << "equals" << std::endl; else std::cout << "differs" << std::endl; return 0; } bool equals(double d1, double d2, double precision) { double eps1 = fabs(d1), eps2 = fabs(d2), eps; eps = (eps1 > eps2) ? eps1 : eps2; if (eps == 0.0) return true; //both numbers are 0.0 //eps hold the minimum distance between the values //that will be considered as the numbers are equal //considering the magnitude of the numbers eps *= precision; return (fabs(d1 - d2) < eps); } Jul 19 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.