Page 257 - ARM 64 Bit Assembly Language
P. 257

246 Chapter 8

                  factor seven in its denominator, and therefore will only terminate in bases were seven is a
                  prime factor of the base. The fraction  1  will terminate in base 7, base 14, base 21, base 42,
                                                   7
                  and an infinite number of other bases, but not in base 30. Since there are an infinite number of
                  primes, no number system is immune from this problem. No matter what base the computer
                  works in, there are fractions that cannot be expressed exactly with a finite number of digits.
                  Therefore, it is incumbent upon programmers and hardware designers to be aware of round-
                  off errors and take appropriate steps to minimize their effects.
                  For example, there is no reason why the hardware clocks in a computer should work in base
                  ten. They can be manufactured to measure time in base two. Instead of counting seconds in
                  tenths, hundredths or thousandths, they could be calibrated to measure in fourths, eighths,
                  sixteenths, 1024ths, etc. This would eliminate the round-off error problem in keeping track of
                  time. For this reason, the most common crystal oscillators used for driving watches, clocks,
                  and other time-keeping devices are carefully calibrated to run at a frequency of 32768 Hz.


                  8.3 Fixed point numbers


                  As shown in the previous section, given a finite number of bits, a computer can only approx-
                  imately represent non-integral numbers. It is often necessary to accept that limitation and
                  perform computations involving approximate values. With due care and diligence, the results
                  will be accurate within some acceptable error tolerance. One way to deal with real-valued
                  numbers is to simply treat the data as fixed point numbers. Fixed point numbers are treated
                  as integers, but the programmer must keep track of the radix point during each operation. We
                  will present a systematic approach to designing fixed point calculations.

                  When using fixed point arithmetic, the programmer needs a convenient way to describe the
                  numbers that are being used. Most languages have standard data types for integers and float-
                  ing point numbers, but very few have support for fixed point numbers. Notable exceptions
                  include PL/1 and Ada, which provide support for fixed point binary and fixed point decimal
                  numbers. There is also an ISO standard extension to C which gives some support for fixed
                  point numbers. A few other languages also support fixed point. We will focus on fixed point
                  binary, but the techniques presented can also be applied to fixed point numbers in any base.
                  Using this approach, any language which supports integers can be used to perform fixed point
                  computation.


                  8.3.1 Interpreting fixed point numbers

                  Fixed point numbers are stored as integers, and integer operations are performed on them.
                  However, the programmer assigns a radix point location to each number, and tracks the radix
   252   253   254   255   256   257   258   259   260   261   262