Exemplo n.º 1
0
    Number fabs(const Number& num1)
    {
        switch (num1.type())
        {
        case Number::INTEGER:
            {
                integer_type i = b_mp::abs(num1.get_i());
                return Number(i);
            }

        case Number::FLOATING:
            {
                floating_type f = b_mp::fabs(num1.to_f());
                return Number(f);
            }

        case Number::RATIONAL:
            {
                rational_type r = b_mp::abs(num1.get_r());
                return Number(r);
            }

#ifndef PMP_DISABLE_VECTOR
        case Number::VECTOR:
            {
                vector_type v = num1.get_v();
                for (size_t i = 0; i < v.size(); ++i)
                {
                    v[i] = abs(v[i]);
                }
                return Number(v);
            }
#endif

        default:
            assert(0);
            return 0;
        }
    }
Exemplo n.º 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;
        }
    }