Thread: Comparison by subtraction

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    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.

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    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

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What if you're comparing negatives? Are you wanting to take into account just magnitude, or total value?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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:
    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

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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
    Last edited by Dave Evans; 10-05-2004 at 03:55 PM.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks everyone. I was pretty sure I was correct but I just wanted to make sure

    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

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    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.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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. #10
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    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.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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
    Last edited by Dave Evans; 10-05-2004 at 04:51 PM.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    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.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well the complex part is moot for my problem since I stated
    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

    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. Function pointer comparison
    By matsp in forum C Programming
    Replies: 14
    Last Post: 08-28-2008, 10:05 AM
  4. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM