# Comparison by subtraction

• 10-05-2004
Thantos
Comparison by subtraction
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.
• 10-05-2004
pablo615
You assume correctly, a is less than b. Now, if only a math study could post the mathematical proof (I'm a few too many years out of school to remember how), that would be interesting to see.

PK
• 10-05-2004
sean
What if you're comparing negatives? Are you wanting to take into account just magnitude, or total value?
• 10-05-2004
Dave Evans
Quote:

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:
Quote:

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
• 10-05-2004
Dave Evans
Quote:

Originally Posted by sean_mackrory
What if you're comparing negatives? Are you wanting to take into account just magnitude, or total value?

In mathematics the expression
a < b

means that there is a positive number, say p, such that

a + p = b

This is for all real numbers a, b, and c (positive, negative, etc. --- it's all algebra).

Therefore a < b if and only a - b = -p (where p is a positive number)

Therefore a < b if and only if (a - b) < 0

Regards,
Dave
• 10-05-2004
Thantos
Thanks everyone. I was pretty sure I was correct but I just wanted to make sure

Quote:

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?
Well my number is really a class and it is written in such a way that negative zero can not exist (at the end of every math function it calls a validate function that ensures this and a couple other things)

Thanks again
• 10-05-2004
Lithorien
Assuming that you are working only with positive numbers, then you are correct in assuming that any and all (a - b) < 0 will indicate that a is less than b.

Things get wonky when you start throwing in negative or complex (a+bi) numbers.
• 10-05-2004
Dave Evans
Quote:

Originally Posted by Lithorien
Assuming that you are working only with positive numbers, then you are correct in assuming that any and all (a - b) < 0 will indicate that a is less than b.

Things get wonky when you start throwing in negative or complex (a+bi) numbers.

In mathematics things work perfectly with real numbers, positive, negative or otherwise.

With complex numbers there is no such thing as "<". (They are not ordered.)

Regards,

Dave
• 10-05-2004
Dave Evans
Quote:

Originally Posted by Thantos
Thanks everyone. I was pretty sure I was correct but I just wanted to make sure

Well my number is really a class and it is written in such a way that negative zero can not exist (at the end of every math function it calls a validate function that ensures this and a couple other things)

Thanks again

Of course, the two's complement number system used in most computers does not have a representation for negative zero. My point is that sometimes you can print a floating point number which appears to be negative zero (but that's only because you didn't print enough significant digits).

In any comparison with doubles or floats you must be aware of the possibility of roundoff error, even though the numbers are "rational" in base 10.

My example shows on my computers that 0.1 + 0.1 may not give the same answer as 1.0 - 0.8

Regards,

Dave
• 10-05-2004
Lithorien
Quote:

Originally Posted by Dave Evans
In mathematics things work perfectly with real numbers, positive, negative or otherwise.

With complex numbers there is no such thing as "<". (They are not ordered.)

Regards,

Dave

sqrt(-1). X / 0.

You were saying something about working perfectly?

You can and do compare complex numbers in terms of greater and less than as well. If you couldn't, you wouldn't have your (X, i) coordinate plane.
• 10-05-2004
Dave Evans
Quote:

Originally Posted by Lithorien
sqrt(-1). X / 0.

You were saying something about working perfectly?

You can and do compare complex numbers in terms of greater and less than as well. If you couldn't, you wouldn't have your (X, i) coordinate plane.

Of course I spoke recklessly. Things only work with defined quantities.

If a is a real number and b is a real number then

a < b if and only if a - b < 0.

This works with all real numbers (positive, negative, or otherwise).

That's what I should have said ( and I should have left perfection to a Higher Power).

No, there is no order relation defined for complex numbers.

If z1 is a complex number and z2 is a complex number there is no operator "less than" defined to allow us to ask, "Is z1 less than z2?"

Now the coordinates of a complex number are real numbers (x-coordinate, y-coordinate). Complex numbers can be expressed as a magnitude and angle (both real numbers). As a matter of fact one abstract definition of a complex number is that it is an ordered pair of real numbers.

There is no ordering relation defined for complex numbers. Period. Flat statement.

Not defined.

Regards,

Dave
• 10-05-2004
Lithorien
Quote:

Originally Posted by Dave Evans
If z1 is a complex number and z2 is a complex number there is no operator "less than" defined to allow us to ask, "Is z1 less than z2?"

Now the coordinates of a complex number are real numbers (x-coordinate, y-coordinate). Complex numbers can be expressed as a magnitude and angle (both real numbers). As a matter of fact one abstract definition of a complex number is that it is an ordered pair of real numbers.

There is no ordering relation defined for complex numbers. Period. Flat statement.

Ok, fair enough. That was my mistake.

I've been taking derivatives and integrating by parts for the good majority of the day, please do forgive my arrogance.

My apoligies.
• 10-05-2004
Dave Evans
Quote:

Originally Posted by Lithorien
Ok, fair enough. That was my mistake.

I've been taking derivatives and integrating by parts for the good majority of the day, please do forgive my arrogance.

My apoligies.

No apology required. I misspoke; one demerit (but I don't get the brig, I hope). I haven't thought about some of these things for quite a while, and I appreciate being challanged when I make flat statements.

(My dad used to say, "You should never, never, ever make flat statements.")

(He also used to say, "If I've told you once, I've told you a million times: Stop exaggerating!")

Regards,

Dave
• 10-05-2004
Thantos
Well the complex part is moot for my problem since I stated
Quote:

For the object I'm dealing with I only have to worry about rational reals
So last time I check complex numbers didn't fall under the reals :)

Quote:

In any comparison with doubles or floats you must be aware of the possibility of roundoff error, even though the numbers are "rational" in base 10.
Of course, luckily for this class it is only casted to a double when round off isn't a problem, as the round off would happen at a point where we can say "we don't care" :)