Logo Search packages:      
Sourcecode: kbruch version File versions  Download package

void ratio::reduce (  )

reduce the ratio

Definition at line 315 of file ratio.cpp.

References primenumber::get_first(), primenumber::get_next(), m_denominator, m_numerator, and primenumber::move_back().

Referenced by operator*(), operator+(), ratio(), setDenominator(), setNumerator(), setRatio(), TaskView::showResult(), ExerciseMixedNumbers::showResult(), ExerciseConvert::showResult(), and task::solve().

{
    /* we try prime numbers as divisors; I think it is the fastet way to do */
    primenumber number;
    short sign_numerator = 0, sign_denominator = 0;

    /* make the whole ratio positive; save the signs; it is easier to reduce
     * the ratio, if it is positive */
    if (m_numerator < 0) { // save numerator sign
        sign_numerator = 1;
        m_numerator *= -1;
    }
    if (m_denominator < 0) { // save denominator sign
        sign_denominator = 1;
        m_denominator *= -1;
    }

    for (int divisor = number.get_first();
            divisor <= m_numerator && divisor <= m_denominator; divisor = number.get_next()) {
        if (divisor == 0) {
#ifdef DEBUG
            kDebug() << "ratio::reduce() -> divisor == 0 !!!";
            kDebug() << "m_numerator: " << m_numerator;
            kDebug() << "m_denominator: " << m_denominator;
            // cin.get();
#endif
            /* so that the application does not crash with a floating
             * point exception; the error should not appear, but in some
             * cases it does and I do not know why */
            continue;
        }

        /* is the prime number a divisor of numerator and denominator? */
        if ( (m_numerator % divisor == 0) && (m_denominator % divisor == 0)) {
            /* reduce the ratio by the divisor */
            m_numerator /= divisor;
            m_denominator /= divisor;

            /* we have to go recursive, if the 2 is a divisor, because there
             * is no way to step one number before 2 -> there is no prime
             * number smaller than 2 */
            if (divisor == 2)
                reduce();
            else
                number.move_back(); // the prime number could be a divisor again
        } // if ((zaehler % divisor == 0) && (nenner % divisor == 0))
    } // for (unsigned int divisor = number.get_first(); ...

    /* restore the correct signs */
    if (sign_numerator)
        m_numerator *= -1;
    if (sign_denominator)
        m_denominator *= -1;
    if (m_numerator == 0)
        m_denominator = 1;

    return;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index