wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

Storing Floating-Point (Real) Numbers in RAM

 

First we need to look closer at how the float is converted from decimal to binary. 

We already know the basic layout for the data type float is:

1-bit Sign 7-bit Exponent 24-bit Mantissa

 We also know that (generally), the left most bit is the sign, and take either '0' or '1' values.

However, the characteristic of the exponent is not signed with a sign bit as with integers.  It is stored as a biased exponent. 

That means rather than storing the sign and the value separately, we add (bias) a constant term to the true value. 

Since we have 7-bits, we have a total of 27 =128 combinations, but we know that the exponent can be either negative or non-negative,

we really only have half that number. 

In our case we would add the value 64 ( which is half of 128 ) to the true value. 

Example 1:The value 7.89 or .789E+1 (i.e.,0.789*101).  The exponent value 1 would actually be stored as the value 1+64=6510  (=10000012)

Example 2:The exponent value -1710 ( =100012, or 00100012 on 7 bits) would actually be stored as the value -17+64=4710 (=1011112, or 01011112 on 7-bits). 

Example 3:The exponent value 2310 ( =101112, or 00101112 on 7 bits) would actually be stored as the value 23+64=8710 (=10101112). 

The range of exponent values is actually -(26-1) through +(26-1), or -63 through +63.  The binary representation 00000002 (the decimal value 0) is reserved for other uses. 

Converting the mantissa to binary representation requires a somewhat different algorithm than we used to convert integers into binary, but it still has to do with exponent position. 

For example, The integer 456 would have the exponent positions shown at the left (456=4*102 + 5*101 + 6*100).  If we were to consider the real number 456.789, however, the exponent positions would appear as they do on the right (4*102 + 5*101 + 6*100 + 7*10-1 + 8*10-2 + 9*10-3 ).  Notice that the exponents for the mantissa are the inverse of the integer portion of the number.  

So the procedure we need to use converting the decimal to binary is also the inverse of the procedure we used when converting a decimal integer to binary.  Previously, we divided the integer portion by two and kept track of the remainders and collected the reverse order received.  Now we need to multiply the mantissa by two and keep track of the quotients and collect in order received.  In both cases however, we can stop when the value to be multiplied or divided is 0.  

Consider how do we store the number 456.789?

We must first normalize the number: 

456.789       =    45.6789 * 101

                                    4.56789 * 102

                                    .456789 * 103

 Where the last notation is what we will use to store the number:

 Sign             Exponent               Mantissa

 1-bit                7-bits                    24-bits

0 (positive)      +3                         .456789

 

Storing the first 8-bits (the sign and the characteristic) is relatively easy:

 Sign:                                                  0 (positive)

Characteristic:                                   310  +   6410 =  6710 = 10000112 (on 7 bits)

 Sign and characteristic lay-out:       01000011 (using the first 8-bits)

 

We convert the mantissa .456789 as follows:

Mantissa

*

2

=Result

→ Quotient

 

Mantissa

*

2

=Result

→ Quotient

0.456789

*

2

0.9136

0

 

0.007744

*

2

0.0155

0

0.913578

*

2

1.8272

1

 

0.015488

*

2

0.0310

0

0.827156

*

2

1.6543

1

 

0.030976

*

2

0.0620

0

0.654312

*

2

1.3086

1

 

0.061952

*

2

0.1239

0

0.308624

*

2

0.6172

0

 

0.123904

*

2

0.2478

0

0.617248

*

2

1.2345

1

 

0.247808

*

2

0.4956

0

0.234496

*

2

0.4690

0

 

0.495616

*

2

0.9912

0

0.468992

*

2

0.9380

0

 

0.991232

*

2

1.9825

1

0.937984

*

2

1.8760

1

 

0.982464

*

2

1.9649

1

0.875968

*

2

1.7519

1

 

0.964928

*

2

1.9299

1

0.751936

*

2

1.5039

1

 

0.929856

*

2

1.8597

1

0.503872

*

2

1.0077

1

 

0.859712

 

2

1.7194

1

 

And COLLECTING FROM THE TOP, the mantissa (on 24-bits) is : 011101001111000000011111

Therefore, the real number +456.789 would be store as: 01000011011101001111000000011111 (on 32-bits)

What if we wanted to store the negative value of a real number (-456.789) do we need to compliment?

The answer is yes.  How?  That is beyond the realm of this discussion.  However there is enough information provided for the truley die hard student to figure it out. 

Now how is this binary representation stored in RAM?

We know that floating point numbers in the PC require 32-bits of contiguous RAM storage, and are generally (not universally) arranged according to the following layout:

Fig. 1

1-bit Sign

7-bit Exponent

24-bit Mantissa

As discussed briefly above, conversion of floating-point numbers into binary requires a lot of effort, and the negative values were not covered so true representation in RAM is difficult. 

However, if we were to make the statement shown in C Code:   float myfloat; and we found that the base address of variable myfloat was actually 1250, and if were to look at RAM, we would interpret the sequence of bits we found according to the layout given in Fig. 1 (1-bit sign, 7-bit Characteristic of the Exponent, and 24-bit mantissa)

Address 1249 1250 1251 1252 1253 1254 1255
Contents 11010101 00010111 00001011 01110001 00011101 10010111 01100111

                                                                                                                                                              

 

 

              0                                 0010111                                                        00010110111000100011101

                                                                                                   

Sign (positive)                Characteristic of the Exponent                                                   Mantissa  

 

 

Additional references regarding Ram allocation can be found at:

http://members.lycos.co.uk/leeedavison/6502/ehbasic/internals.html

http://www.nixu.fi/~lauri/hc11loki/fp11/hc11fp.doc.html

http://www.ccsinfo.com/faq/?21