CIS3355:
Business Data Structures |
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:
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:
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
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)
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
|