Why can't js handle decimal arithmetic correctly?

Source: Internet
Author: User


Why can't js handle decimal arithmetic correctly?


Posted on 2016/1/17 13:27:34 638 people read



Category: JavaScript



First look at the following program:

var sum = 0;
for (var i = 0; i <10; i ++) {
  sum + = 0.1;
}

console.log (sum);

Will the above program output 1?

In the 25 JavaScript interview questions that you need to know, the eighth question succinctly talked about why js can not correctly handle the problem of decimal arithmetic. Today, I will pick up the old question again and analyze this question in a deeper level.

But first, the operation that cannot correctly handle decimals is not a design error of the JavaScript language itself. Other high-level programming languages, such as C, Java, etc., also cannot correctly handle decimal operations:

#include <stdio.h>

void main () {
    float sum;
    int i;

    sum = 0;

    for (i = 0; i <100; i ++) {
        sum + = 0.1;
    }

    printf (‘% f \ n’, sum); //10.000002
}

The representation of the number inside the computer
We all know that a program written in a high-level programming language needs to be interpreted, compiled, and other operations to be converted into a machine language that can be recognized by the CPU (Central Processing Unit). For the CPU, it does not recognize the decimal, octal, and decimal numbers. Hexadecimal, etc., these hexadecimal numbers we declare in the program will be converted into binary numbers for operation.

Why not convert to a ternary number for calculation?

The inside of the computer is made up of many electronic components such as IC (Integrated Circuit: Integrated Circuit), and it looks like this:

There are many shapes of IC, and many pins are arranged side by side on the inside or inside (only one side is shown). All pins of the IC have only two states of DC voltage 0V or 5V, that is, one IC pin can only represent two states. This characteristic of IC determines that the data inside the computer can only be processed with binary numbers.

Since 1 bit (one pin) can only represent two states, the binary calculation method becomes 0, 1, 10, 11, 100 .... This form:

Therefore, in the operation of numbers, all operands will be converted into binary numbers to participate in the operation, such as 39, will be converted into binary 00100111

Binary representation of decimals
As mentioned earlier, the data in the program will be converted into binary numbers, and decimal numbers will also be converted into binary when participating in the operation, such as the decimal 11.11875 will be converted into 1101.0010.

The 4 digits after the decimal point are represented by binary numbers in the range of 0.0000 to 0.1111. Therefore, this can only represent the decimal (the sum) of the four decimal numbers 0.5, 0.25, 0.125, and 0.0625 and the weights (addition) after the decimal point:

Binary number Corresponding decimal number
0.0000 0
0.0001 0.0625
0.0010 0.125
0.0011 0.1875
0.0100 0.25
0.1000 0.5
0.1001 0.5625
0.1010 0.625
0.1011 0.6875
0.1111 0.9375
As can be seen from the table above, the next digit of the decimal number 0 is 0.0625, so the decimal number between 0 and 0.0625 cannot be represented by a binary number with 4 digits after the decimal point; if you increase the number of digits after the decimal point of the binary number, The number of decimal numbers corresponding to it will also increase, but no matter how many digits are added, the result of 0.1 cannot be obtained. In fact, the conversion of 0.1 into binary is 0.00110011001100110011 ... Note that 0011 is repeated indefinitely:

console.log (0.2 + 0.1);

// The binary representation of the operand
0.1 => 0.0001 1001 1001 1001… (infinite loop)
0.2 => 0.0011 0011 0011 0011… (infinite loop)

The Number type of js does not have equal integral type, single precision, double precision, etc. like C / Java, but is uniformly expressed as double precision floating point type. According to IEEE regulations, single-precision floating-point numbers use 32 bits to represent all decimals, while double-precision floating-point numbers use 64 bits to represent all decimals, and floating-point numbers consist of signs, mantissas, exponents, and radixes, so not all digits are used To represent decimals, symbols, exponents, etc. also occupy digits, the base does not occupy digits:

The fractional part of a double-precision floating-point number supports up to 52 digits, so after adding the two, you get such a string of 0.01001100110011001100110011001100110011001100 ... a binary number truncated due to the limitation of the decimal places of floating-point numbers. 0.30000000000000004.

to sum up
js can't handle decimal arithmetic correctly, including other high-level programming languages, this is not a design error of the language itself, but the computer itself can't correctly handle the arithmetic of decimals, and the arithmetic of decimals will often get unexpected results because All decimal fractions can be represented in binary.

Why can't js handle decimal arithmetic correctly?

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.