Practical C Programming, 3rd Edition

I l @ ve RuBoard

When we want to multiply two numbers , such as 0.12 x 11.0, the following rules apply:

  1. Start with the numbers:

    +1.200E-1 The number is 0.12. +1.100E+1 The number is 11.0.

  2. Add the guard digit.

    +1.2000E-1 The number is 0.12. +1.1000E+1 The number is 11.0.

  3. Multiply the two fractions and add the exponents (1.2 x 1.1 = 1.32, -1 + 1 = 0).

    +1.2000E-1 The number is 0.12. +1.1000E+1 The number is 11.0. __________________________________ +1.320E+0 The result is 1.32.

  4. Normalize the result.

    +1.32000E+0 The number is 1.32.

  5. If the guard digit is greater than or equal to 5, round the next digit up. Otherwise, truncate the number.

    +1.3200E+0 The number is 1.32

Notice that in multiply, you didn't have to go through all that shifting. The rules for multiplication are a lot shorter than those for add as far as the computer hardware designers are concerned . Integer multiplication is a lot slower than integer addition. In floating point, multiplication speed is a lot closer to that of addition.

To divide numbers like 100.0 by 30.0, we must perform the following steps:

  1. Start with the numbers.

    +1.000E+2 The number is 100.0. +3.000E+1 The number is 30.0.

  2. Add the guard digit.

    +1.0000E+2 The number is 100.0. +3.0000E+1 The number is 30.0.

  3. Divide the fractions, and subtract the exponents.

    +1.0000E+2 The number is 100.0. +3.0000E+1 The number is 30.0. ___________________________________ +0.3333E+1 The result is 3.333.

  4. Normalize the result.

    +3.3330E+0 The result is 3.333.

  5. If the guard digit is less than or equal to 5, round the next digit up. Otherwise, truncate the number.

    +3.333E+0 The result is 3.333.

I l @ ve RuBoard

Категории