Thread: Opinion on Overloading < and > For My Number Class

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

    Opinion on Overloading < and > For My Number Class

    I have two ideas in mind on how to overload these operators. One would involve using the subtraction operator and checking the sign of the result. The other would involve comparing the values and signs (as the sign is stored separately from the value). Which would you go with?

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    If speed is an issue, I would code it both ways and profile to see which is faster... the one that uses the fewest operations (assuming they're all bitwise operations and they all take the same amount of time) would obviously be the best...
    Away.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I'd need to know a little more about your number class (big integer/float type thing, I'm guessing, using an array to store the values). If so, then I think individual comparison would work faster on average.

    For example, check the sign bits, and if one is positive, and the other negative, return immediately. Then, compare the most significant byte/block, and continue until you can determine one is larger/smaller than the other. That way, you do not need to perform operations on all blocks of the number (except worst case scenario), where you do if you do the subtraction. Also, if the numbers vary greatly, then the temporary created by the subtraction may be quite large.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Store them as strings and use strlen or strcmp on them.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by quzah
    Store them as strings and use strlen or strcmp on them.

    Quzah.
    Ditto. I think they also overloaded the > and < operators for that class (probably for sorting in vectors).

    Or, if you're not using strings, do what Zach L. said:
    [code]
    - compare the number of blocks/bytes/etc. The biggest one is bigger.
    - if they're both the same, compare the blocks individually.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Yeah, I'm storing them as string objects with a bool representing the sign. From what I'm thinking, the subtraction is probably gonna be slower. So, I'm probably gonna use the .length function along with the sign.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 02:06 PM
  2. C++ Overloading < > == for use with char * or string
    By neolyn in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2004, 03:37 PM
  3. Overloading -> operator
    By Hunter2 in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2004, 03:08 PM
  4. Overloading < or > operators
    By mouse163 in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2003, 08:32 PM