Пример #1
0
    void Number::compare(const Number& num, bool comparisons[3]) const
    {
#ifndef PMP_DISABLE_VECTOR
        if (is_v() || num.is_v())
        {
            comparisons[0] = comparisons[1] = comparisons[2] = true;
            for (size_t i = 0; i < size(); ++i)
            {
                for (size_t j = 0; j < num.size(); ++j)
                {
                    bool comps[3];
                    (*this)[i].compare(num[i], comps);
                    if (!comps[0]) comparisons[0] = false;
                    if (!comps[1]) comparisons[1] = false;
                    if (!comps[2]) comparisons[2] = false;
                }
            }
        }
        else
#endif  // ndef PMP_DISABLE_VECTOR
        {
            comparisons[0] = comparisons[1] = comparisons[2] = false;
            int comp = compare(num);
            if (comp == -1)     comparisons[0] = true;
            else if (comp == 0) comparisons[1] = true;
            else if (comp == 1) comparisons[2] = true;
        }
    }
Пример #2
0
    int Number::compare(const Number& num) const
    {
#ifndef PMP_DISABLE_VECTOR
        if (is_v() || num.is_v())
        {
            bool comparisons[3];
            compare(num, comparisons);
            if (comparisons[0])      return -1;
            else if (comparisons[1]) return 0;
            else if (comparisons[2]) return 1;
            else return -2;
        }
#endif

        floating_type f;
        switch (type())
        {
        case Number::INTEGER:
            switch (num.type())
            {
            case Number::INTEGER:
                return get_i().compare(num.get_i());

            case Number::FLOATING:
                f = to_f();
                return f.compare(num.get_f());

            case Number::RATIONAL:
                f = to_f();
                return f.compare(num.to_f());

            default:
                assert(0);
                return 0;
            }

        case Number::FLOATING:
            switch (num.type())
            {
            case Number::INTEGER:
                f = num.to_f();
                return get_f().compare(f);

            case Number::FLOATING:
                return get_f().compare(num.get_f());

            case Number::RATIONAL:
                return get_f().compare(num.to_f());

            default:
                assert(0);
                return 0;
            }

        case Number::RATIONAL:
            switch (num.type())
            {
            case Number::INTEGER:
                f = num.to_f();
                return to_f().compare(f);

            case Number::FLOATING:
                return to_f().compare(num.get_f());

            case Number::RATIONAL:
                return to_f().compare(num.to_f());

            default:
                assert(0);
                return 0;
            }

        default:
            assert(0);
            return 0;
        }
    }