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

1.22.2 Converting a Decimal Number into Floating Point

1.22.2 Converting a Decimal Number into Floating Point

To convert a given decimal number into floating point, carry out the following steps:

• Write the number in binary.

• Normalize the number.

• Find the mantissa and the exponent.

• Write the number as a floating point number.

Some examples follow:

Example 1.34

Convert decimal number 2.2510 into floating point.

Solution 1.34

Write the number in binary:

2.2510 = 10.012

Normalize the number:

10.012 = 1.0012 ? 21

Here, s = 0, e – 127 = 1 or e = 128, and f = 00100000000000000000000.

(Remember that a number 1 is assumed on the left side, even though it is not shown in the calculation). The required floating point number can be written as:

s     e                 f
0 10000000 (1)001 0000 0000 0000 0000 0000

or, the required 32-bit floating point number is:

01000000000100000000000000000000

Example 1.35

Convert the decimal number 134.062510 into floating point.

Solution 1.35

Write the number in binary:

134.062510 = 10000110.0001

Normalize the number:

10000110.0001 = 1.00001100001 ? 27

Here, s = 0, e – 127 = 7 or e = 134, and f = 00001100001000000000000.

The required floating point number can be written as:

s     e                f
0 10000110 (1)00001100001000000000000

or, the required 32-bit floating point number is:

01000011000001100001000000000000

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


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