What is the sum of 100 0.1 equals?

Source: Internet
Author: User

First, preface

In the cognitive process, you may think that the computer will not be a computational error, but in fact, there is still a program after the operation can not get the correct value of the situation. Among them, the most classic is the decimal operation.

Second, the introduction

In our world, 100 of 0.1 add up is 10, this is no doubt. But let's take a look at the computational results of the computer world:

The first is a calculation of the code:

#include <stdio.h>int main (void) {       float  sum;         int i;         0 ;          for (i=0 ;i<; i++) {            0.1;        }        printf ("%f\n", sum);}

The results of the operation are as follows:

10.000002

The result of the computer being compiled, linked, and run is 10.000002. The program is not wrong. Now let's take a look at the specific reasons.

Third, the computer calculation result is incorrect reason

  simply put, it is impossible to represent the correct value, which results in an approximation of the calculated result . Further analysis below.

First, let's take a look at how binary numbers represent decimals in the computer World:

For example, the binary number of the decimal point of 1011.0011 is converted to a decimal number. (Simply multiply the number of digits and the bit weights and then add the result multiplied)

That is: 1*2^3+0*2^2+1*2^1+1*2^0+0*2^ (-1) +0*2^ (-2) +1*2^ (-3) +1*2^ (-4) = 11.1875.

Understanding the binary representation of the decimal-to-decimal method, the reason for the calculation error is easy to understand. The range of values in binary notation with 4 digits after the decimal point is: 0.0000~0.1111. Therefore, the corresponding decimal result is as follows:

As you can see from the table above, the next one in 0 is 0.625. Therefore, numeric computers between 0~0.0625 cannot be represented by a binary number of 4 digits after the decimal point. So you can see that 0.1 cannot be represented by a 4-bit binary number. Even if you increase the number of bits in the binary, you cannot get the result of 2^ (-X) =0.1.

In fact, when the decimal 0.1 turns into binary, it becomes 0.0001100110011 ... (1100 loops) such a loop decimal. Just like 1/3 is a truth. Therefore 100 each 0.1 is added not equal to 10, but equal to the approximate value.

---------------------------------------------above can answer the title of the reason---------------------------------------------

Iv. what are floating-point numbers?

Actually, just like that. 1011.0011 this form of representation is purely a binary representation of the paper, which is not available inside the computer (only 0101001 inside the computer ...). No "." This concept). In fact, the programming language provides a double-precision floating-point number (double) and a single-precision floating-point number (float). Double-precision floating-point number types use 64-bit, single-precision floating-point numbers to represent all decimals with 32-bit.

Floating point number: is a decimal number expressed in symbols, mantissa, cardinality, and exponent.

  

Where: ± represents the symbol, m for the mantissa, N for the cardinality, and E for the exponent. Cardinality is not considered in the actual data. So:

which

1, symbol part: 1 means negative, 0 means positive or 0.

2, the end of the part is: The decimal point before the value of the fixed bit 1 of the regular expression.

3, the index part: use is excess system performance.

  

Let's look at the number of tails first. For the decimal 0.75. We have the following representation methods:

①, 0.75 = 0.75*10^0

②, 0.75 = 75*10^ (-2)

③, 0.75 = 0.075*10^1

The decimal representation is: 0 before the decimal point, and the first digit after the decimal point is not a 0 rule representation. The same is true for binary, which uses the following: to pin the value before the decimal point to 1. That is, the decimal number of the binary number is shifted to the left or right (logical shift) several times, the first bit of the integer part becomes 1, and the second bit becomes 0. and the 1th bit of 1 is not saved in the actual data.

For example 1011.0011:

Shift to 0001.0110011, make sure 23 digits after decimal point: 0001.01100110000000000000000, leave only after the decimal point to complete the regular: 01100110000000000000000.

Look at the index section again. Excess System performance: Set the middle value of the exponential part of the range to 0 so that negative numbers do not need to be represented by symbols . For example, when the exponential portion is 8 for single-precision floating-point, the maximum value of 11111111=225 1/2 is 01111111=127 represents 0. Double precision is similar.

Therefore, for the performance of single-precision floating-point numbers, the expression range is: 00000000~11111111 is -127~128. Look at the following example:

#include <stdio.h>#include<string.h>intMainintargcChar*argv[]) {    floatdata; unsignedLongBuff; inti; Charn[ the]; //Store 0.75 As a single-precision floating-point number in datadata = (float)0.75; memcpy (&buff,&data,4);  for(i= -; i>=0; i--) {        if(i==1|| i==Ten) {N[i]='-'; }Else {            if(buff%2==1) {N[i]='1'; }Else{N[i]='0'; } Buff/=2; }} n[ -] =' /'; printf ("%s\n", n);}

Operation Result:

0-01111110-10000000000000000000000

Where 01111110 is 126,excess expressed as-1.

The first digit before the decimal point is 1. So the mantissa is: 1.10000000000000000000000 is 1.5.

i.e. +1.5*2^ (-1) = 0.75.

V. How to avoid problems caused by error in decimal calculation

You can replace decimals with integers to calculate. Then zoom in on the corresponding multiples.

What is the sum of 100 0.1 equals?

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.