#### Archived

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.

## 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 on other sites
You might find this thread educational; S1CA''s post especially. In brief, however: Floating point math is never entirely precise, so don''t expect it to be.

##### Share on other sites
Actually I just copy and pasted you code, and it in fact succeeded.

##### Share on other sites
The conditional succeeds for me using VC++ 6, VC++ .net and gcc 3.2

##### 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
}

Thanks!

-Pete

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 on other sites
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)

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 20
• 14
• ### Forum Statistics

• Total Topics
632950
• Total Posts
3009383
• ### Who's Online (See full list)

There are no registered users currently online

×