Why cannot js correctly handle decimal operations ?, Js correctly handles decimal operations

Source: Internet
Author: User

Why cannot js correctly handle decimal operations ?, Js correctly handles decimal operations

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

Will the above program output 1?

In25 JavaScript interview questions you need to knowIn the article, the 8th questions briefly explain why JavaScript cannot correctly handle decimal operations. Today, I picked up my old question and analyzed it at a deeper level.

However, it should be noted that the failure to correctly handle decimal operations is not a design error of the JavaScript language itself. Other advanced programming languages, such as C and Java, the decimal operation cannot be correctly processed:

#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}

Representation of numbers in a computer

As we all know, programs written in advanced programming languages must be interpreted, compiled, and converted into machine languages that can be recognized by the CPU (Central Processing Unit, it does not recognize decimal, octal, and hexadecimal numbers of numbers. The hexadecimal numbers we declare in the program will be converted into binary numbers for calculation.

Why not convert it to a three-digit number?

A computer is composed of many electronic components, such as Integrated circuits, which look like this:

The IC has many shapes, and many pins are arranged side by side or inside it (the figure shows only one side ). All the pins of the IC are in either 0 V or 5 V status, that is, one IC pin can only represent two states. This feature of IC determines that data in the computer can only be processed in binary.

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

Therefore, in a Number operation, all operands are converted to binary numbers for calculation. For example, 39 is converted to binary 00100111.

Binary representation of decimal places

As mentioned above, the data in the program will be converted into binary numbers. When decimal places are involved in the operation, they will also be converted into binary values, for example, decimal 11.1875 will be converted to 1101.0010.

The value range of the four digits after the decimal point in binary format is 0.0000 ~ 0.1111. Therefore, it can only represent the decimal digits of 0.5, 0.25, 0.125, 0.0625, and, and the digits after the decimal point combination (SUM:

From the table above, we can see that the next digit of the decimal number 0 is 0.0625, so, 0 ~ The decimal point between 0.0625 cannot be expressed by the binary number of the four digits after the decimal point. If the number of digits after the decimal point is increased, the corresponding decimal number will also increase, however, no matter how many digits are added, you cannot get the 0.1 result. In fact, the conversion from 0.1 to binary is 0.00110011001100110011 ...... Note that 0011 is infinite repetition:

Console. log (0.2 + 0.1); // The binary representation of the operand is 0.1 => 0.0001 1001 1001 1001... (Infinite loop) 0.2 => 0.0011 0011 0011 0011... (Infinite loop)

The Number type of js is not classified into integer type, single precision, double precision, etc., but unified as double precision floating point type. According to IEEE, a single-precision floating point number uses 32 digits to represent all decimals, while a double-precision floating point uses 64 digits to represent all decimals. A floating point number consists of symbols, tails, indexes, and base numbers, therefore, not all digits are used to represent decimal places. Symbols and indexes also occupy digits. The base does not occupy digits:

The decimal part of a double-precision floating-point number supports a maximum of 52 digits. Therefore, after the two are added, We can get such a string 0.0100110011001100110011001100110011001100... The number of binary digits truncated due to the decimal scale limit of the floating point number. At this time, convert it to decimal, and then it becomes 0.30000000000000004.

Summary

Javascript cannot correctly handle decimal operations, like other advanced programming languages. This is not a language design error, but a computer itself cannot correctly handle decimal operations, the decimal operation often produces unexpected results, because not all decimal places can be expressed in binary.

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • JavaScript Regular Expressions are the easiest to verify integer, decimal, real, and valid decimal places
  • Javascript decimal integer function
  • JS retains the decimal point (rounded to five, four to six) Implementation ideas and examples
  • Javascript format the numeric amount separated by commas to keep two decimal places
  • How to get the float type two digits after the decimal point in js
  • How to convert small numbers to Integers in js
  • Convert js numbers to float and take N decimal places
  • How to solve multiple decimal places in the product operation of Javascript floating point number
  • How to solve multiple decimal places in Javascript decimal operations

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.