Paligulus
Paligulus

Reputation: 493

C++ Comparing Doubles

Is there any known problem using < or > with doubles?

d1 > d2

d2 > d1

I cannot find anything that would indicate that this would be a problem.

However, I assume this will be a problem due to the issues with equality and I will have to use "almost equal" logic using Epsilon:

d1 >= d2

d2 >= d1

Upvotes: 1

Views: 394

Answers (2)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272762

> and < work exactly as expected; if the value of d1 is greater than the value of d2, then d1 > d2 will be true (and vice versa). The problem (if there is one) is that d1 and d2 might not be exactly the values you expected.

However, replacing > with >= will change the behaviour for exactly one value of d1 (for a fixed value of d2).

Upvotes: 1

Mike Kwan
Mike Kwan

Reputation: 24477

The only problem is that you may get a 'false positive' where there are two variables d1 and d2 which you deem to be of equal value. In that case if you do d1 > d2, there is a chance the expression will evaluate to true.

Upvotes: 0

Related Questions