taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.3.1-7

Exercise 1.3.1-7

not stated yet in Lean solution written

RatingM15
MarkersM (mathematical)
KindK8, compute by machine: find a number or an object by a large computation from the written solution
SectionSection 1.3.1 of Volume 1. Companion chapter: Description of MIX

The task

Define the result of DIV in the no-overflow case by the functions ⌊X/Y⌋\lfloor X/Y \rfloor and X mod YX \bmod Y.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

The registers hold signs and magnitudes, not two's complement numbers. Treat magnitudes first.

Hint 2

Then look at what the signs do: the sign of rX and the sign of rA after the instruction.

Solution

Show the solution

Let nn be the magnitude of the ten-byte number rAX and dd the magnitude of VV. Assume d>0d > 0 and ⌊n/d⌋<b5\lfloor n/d \rfloor < b^5 (otherwise overflow occurs and the machine sets the toggle). After DIV:

∣rA∣=⌊n/d⌋,∣rX∣=n mod d.|\text{rA}| = \lfloor n/d \rfloor, \qquad |\text{rX}| = n \bmod d.

The sign of rX is the old sign of rA. The sign of rA is ++ if the old signs of rA and of VV were equal, and −- otherwise.

In terms of signed numbers, let N=±nN = \pm n with the sign of rA. If V>0V > 0 (sign ++), then rA←sgn⁡(N)⌊n/d⌋\text{rA} \leftarrow \operatorname{sgn}(N)\lfloor n/d\rfloor. In particular, for N≥0N \ge 0 and V>0V > 0 this is the floor division of NN by VV, and for N<0N<0 it is the ceiling of N/VN/V. The remainder has the sign of the dividend, not of the divisor.

Check

python3 with signed values, ∣n∣=17|n| = 17, ∣d∣=5|d| = 5: the pairs (N,V)=(17,5),(17,−5),(−17,5),(−17,−5)(N, V) = (17, 5), (17, -5), (-17, 5), (-17, -5) give (rA,rX)=(3,2),(−3,2),(−3,−2),(3,−2)(\text{rA}, \text{rX}) = (3, 2), (-3, 2), (-3, -2), (3, -2). In each case ∣rA∣⋅5+∣rX∣=17|\text{rA}| \cdot 5 + |\text{rX}| = 17.

Remarks

The rule differs from the floor rule of Section 1.2.4, because the magnitude is divided and the sign is attached afterwards.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.