IEEE754 standard floating point storage format, ieee754 standard floating point

Source: Internet
Author: User

IEEE754 standard floating point storage format, ieee754 standard floating point

Basic storage format (from high to low): Sign + Exponent + Fraction

Sign: Symbol bit

Exponent: Level Code

Fraction: valid number

32-bit floating point Storage Format

Sign: 1 bit (31st bits)

Exponent: 8 bits (8 bits from 30th to 23)

Fraction: 23 bits (23 bits from 22nd to 0)

The true value of a 32-bit non-0 floating point is (python syntax ):

(-1) **Sign * 2 **(Exponent-127) * (1 + Fraction)

Example:

A = 12.5

1. Solve the symbol bit

If a is greater than 0, the Sign is 0, which is expressed as: 0 in binary format.

2. Solving Order Codes

A Indicates binary: 1100.0

If the decimal point needs to be moved three places to the left, the Exponent is 130 (127 + 3), expressed in binary as: 10000010

3. Solving valid numbers

If a valid number needs to be removed from the maximum implicit 1, the integer part of the valid number is: 100.

To convert decimal places to decimal places, use the following method to convert decimal places to decimal places: * 2. If an integer is obtained, the decimal places are: 1.

If the value is 0, the binary value of a can be expressed as: 01000001010010000000000000000000.

That is, 0100 0001 0100 1000 0000 0000 0000 0000

In hexadecimal notation: 0x41480000

4. Restore the true value

Sign = bin(0) = 0Exponent = bin(10000010) = 130Fraction = bin(0.1001) = 2 ** (-1) + 2 ** (-4) = 0.5625

True Value:

(-1) **0 * 2 **(130-127) * (1 + 0.5625) = 12.5

32-bit floating point binary storage Parsing Code (c ++ ):

Https://github.com/mike-zhang/cppExamples/blob/master/dataTypeOpt/IEEE754Relate/floatTest1.cpp

Running effect:

[root@localhost floatTest1]# ./floatToBin1sizeof(float) : 4sizeof(int) : 4a = 12.500000showFloat : 0x 41 48 00 00UFP : 0,82,480000b : 0x41480000showIEEE754 a = 12.500000showIEEE754 varTmp = 0x00c00000showIEEE754 c = 0x00400000showIEEE754 i = 19 , a1 = 1.000000 , showIEEE754 c = 00480000 , showIEEE754 b = 0x41000000showIEEE754 i = 18 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 17 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 16 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 15 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 14 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 13 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 12 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 11 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 10 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 9 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 8 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 7 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 6 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 5 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 4 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 3 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 2 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 i = 1 , a1 = 0.000000 , showIEEE754 b = 0x41000000showIEEE754 : 0x41480000[root@localhost floatTest1]#
Analysis of 64-bit floating point Storage Format

Sign: 1 bit (31st bits)

Exponent: 11 bits (11 bits from 62nd to 52)

Fraction: 52 bits (52 bits from 51st to 0)

The true value of a 64-bit non-0 floating point is (python syntax ):

(-1) **Sign * 2 **(Exponent-1023) * (1 + Fraction)

Example:

A = 12.5

1. Solve the symbol bit

If a is greater than 0, the Sign is 0, which is expressed as: 0 in binary format.

2. Solving Order Codes

A Indicates binary: 1100.0

If the decimal point needs to be moved three places to the left, the Exponent is 1026 (1023 + 3), expressed in binary as: 10000000010

3. Solving valid numbers

If a valid number needs to be removed from the maximum implicit 1, the integer part of the valid number is: 100.

To convert decimal places to decimal places, use the following method to convert decimal places to decimal places: * 2. If an integer is obtained, the decimal places are: 1.

If the value is 0, the binary value of a can be expressed:

0100000000101001000000000000000000000000000000000000000000000000

That is: 0100 0000 0010 1001 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

In hexadecimal notation: 0x4029000000000000

4. Restore the true value

Sign = bin(0) = 0Exponent = bin(10000000010) = 1026Fraction = bin(0.1001) = 2 ** (-1) + 2 ** (-4) = 0.5625

True Value:

(-1) **0 * 2 **(1026-1023) * (1 + 0.5625) = 12.5

64-bit floating point binary storage Parsing Code (c ++ ):

Https://github.com/mike-zhang/cppExamples/blob/master/dataTypeOpt/IEEE754Relate/doubleTest1.cpp

Running effect:

[root@localhost t1]# ./doubleToBin1sizeof(double) : 8sizeof(long) : 8a = 12.500000showDouble : 0x 40 29 00 00 00 00 00 00UFP : 0,402,0b : 0x0showIEEE754 a = 12.500000showIEEE754 logLen = 3showIEEE754 c = 4620693217682128896(0x4020000000000000)showIEEE754 b = 0x4020000000000000showIEEE754 varTmp = 0x8000000000000showIEEE754 c = 0x8000000000000showIEEE754 i = 48 , a1 = 1.000000 , showIEEE754 c = 9000000000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 47 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 46 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 45 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 44 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 43 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 42 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 41 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 40 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 39 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 38 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 37 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 36 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 35 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 34 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 33 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 32 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 31 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 30 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 29 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 28 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 27 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 26 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 25 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 24 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 23 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 22 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 21 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 20 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 19 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 18 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 17 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 16 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 15 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 14 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 13 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 12 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 11 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 10 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 9 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 8 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 7 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 6 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 5 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 4 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 3 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 2 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 i = 1 , a1 = 0.000000 , showIEEE754 b = 0x4020000000000000showIEEE754 : 0x4029000000000000[root@localhost t1]#

 

Okay, that's all. I hope it will help you.

Github address:

Bytes

Please add

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.