3rd Lesson floating-point secret

Source: Internet
Author: User

1. Floating-point number in memory--storage mode: sign bit, exponent, mantissa symbol

Type

Sign bit

Index

Tail

Float

1-bit (31st bit)

8-bit (第23-30位)

23-bit (第0-22位)

Double

1-bit (63rd bit)

11-bit (第52-62位)

52-bit (第0-51位)

float The representation of a double type of data within a computer is the same, but because of the different storage space, the range and precision of the data values can be expressed separately.

2. example of storage of floating-point numbers

2.1 steps to convert floating-point numbers

(1) converting floating-point numbers to binary

(2) using scientific notation to represent binary floating-point numbers

(3) Calculate the value after the exponential offset: (with offset , float type: +127,double type: +1023)

Example: for exponential 6, the following values are offset:

float: 127 + 6→133

Double: 1023 +6 →1029

2.2 The conversion process is demonstrated using the real number 8.25 in the float type as an example in memory notation

(1) 8.25 binary notation: 1000.01, then into exponential form: 1.00001* (2^3)

① sign bit: 0

The ② index is 3: 127+3 =130→10000010

③ decimals: 00001. To turn to the mantissa, you need to fill 0

(2) 8.25 float in memory:0 0010 000000 0000 0000 0000 =0x41040000

(The Red part is the sign bit, the exponent is green , the mantissa is the Purple part plus 0 after the 23 bits)

Memory representation of the "Programming implementation 1" decimal floating-point number

#include <stdio.h>intMain () {floatf =8.25; //in order to display the binary value of the floating-point memory representation in 16 binary, the//segment memory displays that string of binary numbers as integers for human readingUnsignedint* p = (unsignedint*) &f;//integer pointer to f memoryprintf"0x%08x\n", *p);//output 0x41040000 with integral type    return 0;}

3. Secret of floating-point types

(1) Think: int and float both account for 4 bytes of memory, and the range of type int: [ -231,231-1],float type range: [ -3.4*1038,3.4*1038], float is much larger than Int's range?

(2) Secret

Float can represent exactly the same number of numbers as int

②float can be expressed between the number is discontinuous , there is a jump

③float is just an approximate representation and cannot be used as an exact number.

④ because memory notation is relatively complex, float is much slower to operate than int

(3) Note: Double and float have the same memory notation, because double is not accurate. Because the double occupies more memory, it can represent a higher precision than float.

Imprecise example of "programmatic implementation" float type

#include <stdio.h>intMain () {floatF1 =3.1415f; floatF2 =123456789; //accurate to 10 digits after the decimal pointprintf"%0.10f\n", F1);//3.1414999962--not accurateprintf"%0.10f\n", F2);//123456792.0000000000 ---discontinuous//in the same vein, it is possible to experiment with a float type of 123456780-123456788 in memory//is represented by the number 123456784.0000000000.     return 0;}

4. Summary

(1) floating-point type differs from integer type in memory notation

(2) More complex memory representations of floating-point types

(3) A floating-point type can represent a larger range

(4) Floating-point type is an inexact type

(5) Slow operation of floating-point types

3rd Lesson floating-point secret

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.