Understanding floating point storage rules

Source: Internet
Author: User
I have long wondered how floating point storage works.

Single (single-precision floating-32-bit ):

S E F
1 8 23

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 ):

S E F
1 11 52

Extended (extended precision Floating Point-80 bits ):

S E I F
1 15 1 63

Real48 (the 48-bit floating point number that has been eliminated ):

S F E
1 39 8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.