Книга: Advanced PIC Microcontroller Projects in C

1.22.4 Addition and Subtraction of Floating Point Numbers

1.22.4 Addition and Subtraction of Floating Point Numbers

The exponents of floating point numbers must be the same before they can be added or subtracted. The steps to add or subtract floating point numbers are:

• Shift the smaller number to the right until the exponents of both numbers are the same. Increment the exponent of the smaller number after each shift.

• Add (or subtract) the mantissa of each number as an integer calculation, without considering the decimal points.

• Normalize the result.

An example follows.

Example 1.37

Show decimal numbers 0.510 and 0.7510 in floating point and then calculate the sum of these numbers.

Solution 1.37

As shown in Example 1.36, we can convert the numbers into floating point as:

0.510 = 0 01110110 (1)000 0000 0000 0000 0000 0000

Similarly,

0.7510 = 0 01110110 (1)100 0000 0000 0000 0000 0000

Since the exponents of both numbers are the same, there is no need to shift the smaller number. If we add the mantissa of the numbers without considering the decimal points, we get:

  (1)000 0000 0000 0000 0000 0000
+ (1)100 0000 0000 0000 0000 0000
 --------------------------------
 (10)100 0000 0000 0000 0000 0000

To normalize the number, shift it right by one digit and then increment its exponent. The resulting number is:

0 01111111 (1)010 0000 0000 0000 0000 0000

This floating point number is equal to decimal number 1.25, which is the sum of decimal numbers 0.5 and 0.75.

A program for converting floating point numbers into decimal, and decimal numbers into floating point, is available for free on the following web site:

http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html

Оглавление книги


Генерация: 1.585. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз