How floating-point numbers are stored in a computer _c language

Source: Internet
Author: User
Tags decimal to binary

In C and C # languages, data for floating-point types is stored in a single-type (float) and double type (double), and float data occupies 64bit of 32bit,double data, and we declare a variable float f= 2.25f, How does the memory be allocated? Wouldn't the world have been a mess if it had been distributed indiscriminately, in fact, whether float or double in the storage mode are compliant with the IEEE specification, float follows the IEEE R32.24, and double follows the R64.53.

  Both single precision and double precision are divided into three parts in storage:

1. Sign bit (Sign): 0 represents positive, 1 represents negative
2. Digit (Exponent): Used to store exponential data in scientific notation and to use shift storage
3. Tail number part (Mantissa): Tail number part

Where float is stored as shown in the following illustration:

The dual-precision storage mode is:

R32.24 and R64.53 storage methods are the use of scientific notation to store data, such as 8.25 with the scientific notation decimal notation is: 8.25*, and 120.5 can be expressed as: 1.205*, the knowledge of these elementary schools do not have to say more. And our stupid computer doesn't even know the decimal data, he only knows 0,1, so in the computer store, the first to change the number above to binary scientific notation, 8.25 with binary representation can be expressed as 1000.01, I am not even this will not convert it? Then I guess I'm going to be out of it. 120.5 with binary representation as: 1110110.1 with the binary scientific notation of notation 1000.01 can be expressed as 1.0001*,1110110.1 can be expressed as 1.1101101*, any number of the scientific notation of the notation is 1.xxx*, The tail number of parts can be expressed as XXXX, the first is 1, why do you want to show it? You can omit the 1 in front of the decimal point, so the number of the tail of 23bit, can be expressed as the precision of 24bit, the truth is here, that 24bit can be accurate to a few decimal points, we know that 9 of the binary is expressed as 1001, so 4bit can be accurate decimal 1 decimal places, 24bit can make the float can be accurate to 6 digits after the decimal point, and for the exponential part, because the exponent can be negative, the 8-bit exponential potential of the index range should be:-127-128, so the index part of the storage using shift storage, stored data is metadata + 127, let's look at how 8.25 and 120.5 really store in memory.

First look at 8.25, using the binary scientific notation as: 1.0001*

According to the storage mode above, the sign bit is: 0, the symbol is positive, the digit is: 3+127=130, the bit is partly, so 8.25 of the storage mode is as shown in the following figure:

The single precision floating-point number 120.5 is stored as shown in the following illustration:

So if you give a piece of data in memory and tell you that it is a single precision store, how do you know the decimal value of that data? In fact, the above is the reverse process, such as the following memory data: 0100001011101101000000000000, first we will be the data segment, 0 10000 0101 110 1101 0000 0000 0000 0000, the storage in memory is shown in the following illustration:

According to our calculation method, we can calculate that such a set of data is expressed as: 1.1101101*=120.5

The storage of the double-precision floating-point number is the same as that of the single precision storage, the difference is the digit of the exponent part and the tail digit part. So here is no longer a detailed description of the dual-precision storage mode, only 120.5 of the last storage mode given, we can carefully think about why this is the case

Below I on this basic knowledge point to solve one of our doubts, please look at the following procedure, pay attention to observe the output results

Copy Code code as follows:
float F = 2.2f;
Double d = (double) F;
Console.WriteLine (d.tostring ("0.0000000000000"));
f = 2.25f;
D = (double) F;
Console.WriteLine (d.tostring ("0.0000000000000"));

Perhaps the output of the results of the people puzzled, A single precision of 2.2 converted to double precision, after 13 digits after the decimal point to become 2.2000000476837, and the single precision of 2.25 converted to double precision, changed to 2.2500000000000, why 2.2 after the conversion of the value changed and 2.25 did not change it? Strange, isn't it? In fact, we can probably find the answer by introducing the two kinds of storage results. First, let's take a look at 2.25 of the single precision storage mode, very simple 0 1000 0001 001 0000 0000 0000 0000 0000, and 2.25 of the double precision is: 0 100 0000 0001 0010 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000, so 2.25 in the conversion, the value is not changed, and we look at 2.2, 2.2 with scientific The method of number notation should be: convert decimal decimal to binary decimal to decimal *2, take integer part, so 0.282=0.4, so binary decimal is the first digit of 0.4 integer part 0,0.4x2=0.8, the second is 0,0.8*2=1.6, and the third is 1,0.6x2 = 1.2, the fourth digit is 1,0.2*2=0.4, the fifth digit is 0, so it is never possible to multiply to = 1.0, the resulting binary is an infinite loop arrangement 00110011001100110011 ... , for single-precision data, the tail number can only represent 24bit of precision, so 2.2 of float is stored as:

But this way of storing, converting to decimal values, is not 2.2, it should be decimal in the conversion to binary may be inaccurate, such as 2.2, and double type of data also have the same problem, so in the floating-point representation will produce a bit of error, in the single precision conversion to double precision, there will also be error, for the ability to use binary representation of decimal data, such as 2.25 , this error will not exist, so there will be a relatively strange output above.

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

Related Article

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.