11.001001000011111101101010100010001000 Arithmazium
Home

compute_A_and_inverse()

This function returns the values of A and ONE_OVER_A. Some tests want to to be run on all powers of \( 2 \), even in octal arithmetic with 3-bit digits and hex with 4-bit digits.

def compute_A_and_inverse(radix):
    """Return value for constants A and ONE_OVER_A, based on radix.

    a is 2 if radix is 1 (logarithmic) or a  power of 2.
    a is 10 if radix is a power of 10. Otherwise a is the radix.
    There are two tests that "a" behaves well in division.

    Args:
        radix: passed from B

    Returns:
        a, 1 / a
    """

The while loop terminates with x == ONE precisely when radix is a power of \( 2 \) or \( 10 \). Note the sum used to capture the value \( 10 \). This entire program is built on the handful of integer constants defined at the outset. See, for example, Small integers,

    a = max(TWO, radix)  # Default, picking up log radix=1
    for z in [TWO, NINE + ONE]:
        y = radix
        while True:
            x = y
            y = y / z
            if floor(y) != y: break
        if x == ONE:
            a = z  # Have found a power of 2 or 10
            break

Finally, check that for a and B, that \( x \times 1 / x = 1 \).

    one_over_a = ONE / a
    x = a
    for x in [a, B]:
        y = ONE / x
        z = x * y - ONE_HALF
        test_cond(err_failure, z == ONE_HALF, "x * (1/x) differs from 1")
    return a, one_over_a


Your turn

Exercise: What value a is computed if the input radix is \( 60 \), as preferred by the Baylonians? Same question for \( 100 \)?

x = B - ONE    100 ... 0 B = 2 y = x - ULP_OF_ONE_PLUS    011 ... ??? what are the low-order bits? B_MINUS_ULP = y + ONE    ??? ... ??? how many 1 bits are there?
Home