19.2 Floating AdditionSubtraction

I l @ ve RuBoard

19.2 Floating Addition/Subtraction

To add two numbers , such as 2.0 and 0.3, the computer must perform the following steps:

  1. Start with the numbers.

     +2.000E+0 The number is 2.0. +3.000E-1 The number is 0.3. 
  2. Add guard digits to both numbers.

     +2.0000E+0 The number is 2.0. +3.0000E-1 The number is 0.3. 
  3. Shift the number with the smallest exponent to the right one digit and increment its exponent. Continue until the exponents of the two numbers match.

     +2.0000E+0 The number is 2.0. +0.3000E-0 The number is 0.3. 
  4. Add the two fractions. The result has the same exponent as the two numbers.

     +2.0000E+0 The number is 2.0. +0.3000E-0 The number is 0.3. _________________________________ +2.3000E+0 The result is 2.3. 
  5. Normalize the number by shifting it left or right until there is just one nonzero digit to the left of the decimal point. Adjust the exponent accordingly . A number like +0.1234E+0 would be normalized to +1.2340E-1. Because the number +2.3000E+0 is already normalized, we do nothing.

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

     +2.3000E+0 Round last digit. +2.300E+0 The result is 2.3. 

To subtract a number:

  1. Change the sign of the second operand.

  2. Add.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net