Page 254 - ARM 64 Bit Assembly Language
P. 254

Non-integral mathematics 243

                     8.2 Fractions and bases

                     One interesting phenomenon that is often encountered when using radix point notation is that
                     fractions which terminate in one base may become non-terminating repeating fractions in an-
                     other base. For example, the radix point binary representation of the decimal fraction  1  is a
                                                                                                10
                     non-terminating repeating fraction, as shown in Example 18.
                     Example 18. A non-terminating, repeating binimal.

                                                        .1 × 2 = 0.2
                                                        .2 × 2 = 0.4
                                                        .4 × 2 = 0.8

                                                        .8 × 2 = 1.6
                                                        .6 × 2 = 1.2
                                                        .2 × 2 = 0.4


                     The resulting fractional part from the last step performed is exactly the same as in the second
                     step. Therefore, the sequence will repeat. If we continue, we will repeat the sequence of steps
                     2-5 forever. Hence, the final binary representation will be:


                                              0.1 10 = .00011001100110011... 2
                                                    = .00011 2
                     Because of this phenomenon, it is impossible to exactly represent 1.10 10 (and many other
                     fractional quantities) as a binary fraction in a finite number of bits using radix point notation.


                     8.2.1 Rounding errors


                     The fact that some base 10 fractions cannot be exactly represented in binary has lead to many
                     subtle software bugs and incorrect results when programmers attempt to work with currency
                     (and other quantities) as real-valued numbers. In this section, we explore the idea that the rep-
                     resentation problem can be avoided by working in some base other than base 2. If that is the
                     case, then we can simply build hardware (or software) to work in that base, and will then be
                     able to represent any fractional value precisely using a finite number of digits.

                     For brevity, we will refer to a binary fractional quantity as a binimal and a decimal fractional
                     quantity as a decimal. We would like to know whether there are more non-terminating deci-
                     mals than binimals, more non-terminating binimals than decimals, or neither. Since there are
                     an infinite number of non-terminating decimals and an infinite number of non-terminating
   249   250   251   252   253   254   255   256   257   258   259