I have long wondered how floating point storage works.
Single (single-precision floating-32-bit ):
For example, floating point number: 13.625 (1*101 + 3*100 + 6*10-1 + 2*10-2 + 5*10-3)
Its binary representation is: 1101.101 (1*23 + 1*22 + 0*21 + 1*20 + 1*2-1 + 0*2-2 + 1*2-3)
Coefficient (or tail number) Normalization: 1101.101 = 1.101101*23
After the coefficients are normalized, they all look like 1. XXXXX..., so in order to save space, 1 in front of "point" does not need to be stored.
In this way, we can know that the index (e) of this number is 00000011 (3 in decimal format), and the ending number (f) is 101101 (saving the previous 1. It must be added during the return calculation)
The sign (s) occupies only one binary bit. It is very simple: 0 is positive, and 1 is negative.
The index (e) has another rule here: actual storage = e + 127; this is to coordinate the positive and negative of the index.
The final result is re-implemented:
S: It should be 0. Here it is a positive number;
E: it should be: 10000010, which corresponds to 130 (3 + 127) of the 10-digit system );
F: 10110100000000000000000, with 23 tails.
The result should be: 01000001010110100000000000000000
Test:
{View binary functions} function Tobin (P: pbytearray; B: integer): string; var I, j: integer; begin result: = stringofchar ('0 ', B * 8); for I: = 0 to B-1 do for J: = 0 to 7 do if odd (P ^ [b-1-i] SHR J) then result [I * 8 + 8-J]: = '1'; end; {Test 1} procedure tform1.button1click (Sender: tobject); var F1, F2: single; S1, s2: string; begin F1: = 13.625; F2: =-13.625; S1: = Tobin (@ F1, sizeof (F1); S2: = Tobin (@ F2, sizeof (F2); memo1.lines. add (S1); // 01000001010110100000000000000000 memo1.lines. Add (S2); // 1100000101011020.00000000000000000end; {Test 2} procedure tform1.button2click (Sender: tobject); var F1, F2: single; begin ASM mov F1, 1_mov F2, 1_end; showmessagefmt ('% G, % G', [F1, F2]); // 13.625,-13.625end;
Storage rules for other floating point numbers:
Double or real (double-precision floating-64-bit ):
Extended (extended precision Floating Point-80 bits ):
Real48 (the 48-bit floating point number that has been eliminated ):