Programming and Graphics

(Kiana) #1

12 Introduction to C++ Programming and Graphics


Using these rules, we confirm the previous calculation,


111
x 101
---
111 (111 times 1)
000 (111 times 0 shifted once)
+ 111 (111 times 1 shifted twice)
------
100011

The procedure is similar to that learned in elementary school for multiplying
two numbers in their decimal representation.


Different processors employ different methods of performing division (see,
for example,http://www.cap-lore.com/Hardware/Divide.html.) In numer-
ical analysis, we learn that division can be implemented in terms of multipli-
cation. Suppose that we want to compute the ratiox=a/b, whereaandb
are given real numbers. This can be done by writingx=a×r,wherewehave
defined the inverse ofb,r≡ 1 /b. A simple rearrangement yieldsr=r(2−br),
which suggests the following algorithm:



  • Guess the value ofr, call itr(0).

  • Improve the guess by computing:r(1)=r(0)(2−br(0)).

  • Further improve the guess by computing:r(2)=r(1)(2−br(1)).

  • Repeat until convergence.


This algorithm implements Newton’s method for solving a nonlinear equation.
It can be shown that a necessary and sufficient condition for the iterations to
converge is that the initial guess lie inside the interval (0, 2 /b).


In computer hardware, addition and subtraction are implemented by
straightforward combinations of electrical signals. Multiplication is a more com-
plex operation, involving multiplication by a single binary digit using the rules
described above, column shifting, and addition of the various subtotals. For this
reason, addition and subtraction are often not counted as floating point oper-
ations (flops). Modern CPUs are endowed with a floating point unit (FPU)
that performs addition, subtraction, multiplication, division, and sometimes
computation of the square root of real numbers at comparable cost.


Problems


1.3.1.Add the binaries of 7 and 10, and confirm that the result is the binary
of 17.

Free download pdf