The flowchart at the right describes the Number Class division algorithm, and the diagram below illustrates the detailed steps for a specific
example. DoDivide supports either division or a quotient and/or remainder function,
depending on whether non-null arguments are supplied. For the quotient and remainder
cases, the logic will generally terminate sooner than for a full division; this diagram
only shows the details of the division case.
First the signs of the result, quotient, and remainder are determined. The
result's (and quotient's) sign is positive if both the operands have same sign, otherwise
it is negative, and the sign of the remainder is always that of the dividend, i.e. the
number being operated upon. The result's exponent is calculated as the difference of
the exponents of the dividend and the divisor argument. A maximum number of result
mantissa digits is calculated by adding the result's exponent, plus an allowance for the
Number Class variable's declared scale factor (i.e. digits to be retained to the right of
the decimal point), plus an extra digit to support proper rounding to the desired scale. A result accumulator of sufficient length is allocated and the dividend is copied into
this temporary Number Class variable, which will be used as the remaining dividend in the
following logic. (For the special case of a quotient/remainder operation, however,
the calculation immediately finishes at this point if the divisor is larger than the
dividend.) The exponent of the temporary dividend is then adjusted to be the same as
that of the divisor. If this leaves the dividend smaller than the divisor, the
dividend's exponent is incremented and the result's exponent is decremented, as the final
initialization step. The main loop generates one mantissa digit on each cycle, filling
the result accumulator from left to right. Each result digit is computed by an inner
loop that subtracts the divisor from the dividend, until the remaining dividend is less
than (in absolute value) the divisor. Note that these comparison and subtraction
operations are performed via Number Class functions, CompAbsVal and DoSubtract, since the divisor and dividend are both
Number Class objects. The number of subtractions performed by the inner loop yields
the next digit of the result. If the remainder is zero, the outer loop terminates;
otherwise the remaining dividend is multiplied by 10 and processing for the next result
digit continues, up to the required scale and precision. (If obtaining only
quotient/remainder, exit when we have calculated all the result digits to the left of the
decimal point, i.e. the quotient.)
Trailing zeroes are always removed, except at most a last zero digit if needed to fill
out the last full byte. A final rounding step is done only if the result's scale
exceeds the scale of the number being operated upon.
In the illustration below, unbiased byte values are shown (in black) for clarity,
however the actual implementation operates directly upon biased Number Class values in
their standard internal representation. The accumulator (in green) is generated with biased mantissa bytes as each result
digit is obtained. Note that this example illustrates an instance where the initial
temporary dividend (1.024), after forcing its exponent to that of the divisor (0), is
smaller than the divisor (4.0), so the dividend's exponent is incremented and the result's
exponent is decremented. Thus the outer loop logic begins its processing against a
dividend of 10.24.

|