Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Frustration with a simple comparison

This topic is 5454 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I''m doing the following comparison: double u = 4; double U[11]; U[9] = 4.0; U[10] = 4.0; if (u >= U[9] && u <= U[10]) { //do something } And it fails! Looking through the debugger, I know the following: u = 4.0000000000000 U[9] = 4.0000000000000 U[10] = 4.0000000000000 u >= U[9] returns "1" u <= U[10] returns "0" Therefore, (u >= U[9] && u <= U[10]) returns 0. So, u <= U[10] appears to be my problem.... I''m guessing it has something to do with double comparison/mathematics. How do I fix my problem? 4 is obviously less than or equal to 4! Thanks, Pete PS: I''m using MS Visual C++ 6.0 on a pentium-4 running WinXP.

Share this post

Link to post
Share on other sites
Well, I found a fix from the article linked above; thanks miserable--it was an educational experience I shall hope not to forget. (Floating point error drives me nuts! Especially since the MSVC++ watch window rounds my doubles!)

double EPSILON = 0.0000001;
if (u > U[9]-EPSILON && u < U[10]+EPSILON )
//do stuff



PS: that's weird that the comparison worked for both of you... damn the computing gods from afar!

[edited by - pschmitt on November 13, 2003 1:21:41 AM]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Floating points can represent some numbers exactly, such as 4.0. So the comparison here *should* work, like it did for others who tested it. But if you have some calculation whose result will be 4.0, it''s likely that you''ll get some inaccuracy so that the result will not be exactly 4.0.

(unless you do something like {a=2.0; a*=2.0;} or {a=5.5; a-=1.5}, i.e. manipulate only numbers that have full accuracy)

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!