• 07-01-2003
golfinguy4
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?
• 07-01-2003
confuted
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...
• 07-01-2003
Zach L.
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.
• 07-01-2003
quzah
Store them as strings and use strlen or strcmp on them. ;)

Quzah.
• 07-01-2003
ygfperson
Quote:

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.
• 07-01-2003
golfinguy4
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.