How javascript can avoid errors in Digital Computation Accuracy _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to avoid the accuracy error of Digital Computation in javascript. If you need a friend, please refer to it and hope to help you. If I ask you how much the value is 0.1 + 0.2? You may give me a white eye, 0.1 + 0.2 = 0.3 ah, then ask? Even kindergarten children will answer such pediatric questions. But you know, putting the same problem in programming languages may not be as simple as you think.
Believe it? Let's take a look at a piece of JS.

Varnuma = 0.1;
Var numB = 0.2;
Alert (numA + numB) === 0.3 );

The execution result is false. Yes. When I saw this code for the first time, I naturally thought it was true, but the execution result surprised me. Is it my method of opening it wrong? Neither. Run the following code to find out why the result is false.

Varnuma = 0.1;
Var numB = 0.2;
Alert (numA + numB );

Originally, 0.1 + 0.2 = 0.30000000000000004. Is it amazing? In fact, almost all programming languages have similar precision errors for the four arithmetic operations of floating point numbers, however, methods have been encapsulated in C ++/C #/Java to avoid precision issues. JavaScript is a weak language, in terms of design, there is no strict data type for floating point numbers, so the problem of precision error is particularly prominent. Next we will analyze why this error occurs and how to fix it.

First, we need to consider the seemingly pediatric problem of 0.1 + 0.2 from the computer perspective. We know that what computers can read is binary, not decimal, so we should first convert 0.1 and 0.2 to binary:

0.1 => 0.0001 1001 1001 1001... (Infinite loop)
0.2 => 0.0011 0011 0011 0011... (Infinite loop)

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 a string of 0.0100110011001100110011001100110011001100110011001100 decimal digits truncated by the decimal point number. At this time, we can convert it to decimal, 0.30000000000000004.

So, how can we solve this problem? The result I want is 0.1 + 0.2 = 0.3 !!!

The simplest solution is to give a clear precision requirement. During the return value process, the computer will automatically turn around, for example:

Varnuma = 0.1;
Var numB = 0.2;
Alert (parseFloat (numA + numB). toFixed (2) === 0.3 );

But obviously this is not a permanent method. If there is a method that can help us solve the precision problem of these floating point numbers, that would be nice. Let's try the following method:

Math. formatFloat = function (f, digit ){
Var m = Math. pow (10, digit );
Return parseInt (f * m, 10)/m;
}

Varnuma = 0.1;
Var numB = 0.2;

Alert (Math. formatFloat (numA + numB, 1) === 0.3 );

What does this method mean? To avoid precision differences, we need to multiply the number to be calculated by the n power of 10, convert it into an integer that can be precisely recognized by the computer, and then divide it by the n power of 10, most programming languages handle the difference in precision in this way, so we can use it to handle the floating point Precision error in JS.

If someone asks you what the value of 0.1 + 0.2 is, please be careful !!

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.