Originally Posted by

**Thantos**
I'm pretty sure this is right as I can't find any case where its not and just thinking about it, its logical.

Give two objects, a & b, that can be subtracted from each other and stored in c:

a - b = c

If c is negative that would indicate that a < b.

Anyone know of a case that would cause this to be false? For the object I'm dealing with I only have to worry about rational reals.

That's the way computer hardware does comparisons: subtract b from a and see if the result was negative.

Of course with assembly language, the presence of the Carry flag would sometimes occur with "negative zero". Fortunately high level languages don't pass along the problem to us --- or do they?

If the reals that you refer to are rational in the number base in which they are stored, your algorithm is OK. Otherwise, things that "should be" equal are not.

I'm not sure if this is what you had in mind.

Try this

Code:

#include <stdio.h>
int main()
{
double a, b, c, d, e;
a = 1.0/10.0;
b = 1.0/10.0;
c = 1.0 - 8.0/10.0;
printf("a = %f, b = %f, c = %f\n", a, b, c);
printf("(a+b)-c = %f\n\n", (a+b)-c);
printf("c-(a+b) = %f\n\n", c-(a+b));
if (c < (a+b)) {
printf("c < a+b\n");
}
if (c > (a+b)) {
printf("c > a+b\n");
}
if (c == (a+b)) {
printf("c == a+b\n");
}
printf("\nWait a minute: how can this be?\n");
printf("Let's try something a little different:\n\n");
if (c - (a+b) < 0) {
printf("c - (a+b) < 0\n");
}
else if (c - (a+b) > 0) {
printf("c - (a+b) > 0\n");
}
else {
printf("c = (a+b)\n");
}
printf("The only other thing I can think of is to print out more digits:\n");
printf("a = %.18e\nb = %.18e\nc = %.18e\n\n", a, b, c);
printf("c-(a+b) = %.20e\n\n", c-(a+b));
printf("Oh, yeah --- Now I See\n');
return 0;
}

I got, more or less, the same results from Borland bcc32, Microsoft Visual c++ and GNU gcc on my Windows XP box:

a = 0.100000, b = 0.100000, c = 0.200000

(a+b)-c = 0.000000

c-(a+b) = -0.000000

c < a+b

Wait a minute: how can this be?

Let's try something a little different:

c - (a+b) < 0

The only other thing I can think of is to print our more digits:

a = 1.000000000000000056e-01

b = 1.000000000000000056e-01

c = 1.999999999999999556e-01

c-(a+b) = -5.55111512312578270212e-17

Regards,

Dave