Пример #1
0
int Decimal::compare(const Decimal & other) const
{
    int lo1, hi1, lo2, hi2;
    clip(lo1, hi1);
    other.clip(lo2, hi2);

    //First check for zero comparison..
    if (lo1 > hi1)
    {
        if (lo2 > hi2)
            return 0;
        return other.negative ? +1 : -1;
    }
    if (lo2 > hi2)
        return negative ? -1 : +1;

    if (negative ^ other.negative)
        return negative ? -1 : +1;

    if (hi1 != hi2)
        return (hi1 > hi2) ^ negative ? +1 : -1;

    int limit = lo1 < lo2 ? lo2 : lo1;
    for (;hi1 >= limit; hi1--)
    {
        int diff = digits[hi1] - other.digits[hi1];
        if (diff != 0)
            return (diff > 0) ^ negative ? +1 : -1;
    }

    if (lo1 == lo2)
        return 0;
    if (lo1 < lo2)
        return negative ? -1 : +1;
    return negative ? +1 : -1;
}