JavaScript floating-point value arithmetic rounding error

Source: Internet
Author: User

Problem

In JavaScript, where integers and floating-point numbers belong to the Number data type (one of the simple data types), we often find that the result of printing 1.0 such a float is not 1 1.0 , because the memory space that holds the floating-point numbers is twice times the value of the saved integer. So ECMAScript will lose no chance to convert the floating-point number to an integer.
This situation is a bit uncomfortable for obsessive-compulsive patients, but it's not a big mistake at all, and the next thing is scary. For example, when we calculate the 0.1 addition 0.2 , it's output is not 0.3 , but 0.3000000000000004 . What the fuck?! Have you ever felt the challenge of the world view for the first time in a child's shoes?

Cause

So quickly to save his soul and body, found the book is impressively written: Ecmascrip is based on IEEE754 numerical floating point calculation, this numerical method will save the value as a binary and then calculate, because the floating-point number in binary expression is infinite, so in the arithmetic calculation will produce rounding error , because of the rounding error, the accuracy of floating-point calculation is far less than that of integers, and finally the value of a particular floating-point number is never tested .

Solution Solutions

The so-called remedy, know the cause of the problem so that you can find a solution to the problem. Since the binary of floating-point numbers is an infinite number of errors, this error does not exist in integer arithmetic, is it possible for you to get a glimpse of the truth? Yes, that is to convert the floating-point number to an integer in an arithmetic project, and then convert the resulting result to a floating-point number. Sir, here is the new code ~

1 //addition2 functionFloatadd (arg1,arg2) {3        varr1,r2,m;4        Try{r1=arg1.tostring (). Split (".") [1].length}Catch(e) {r1=0;//parameter 1 is an integer}; Parameter 1 number of digits after the decimal point5        Try{r2=arg2.tostring (). Split (".") [1].length}Catch(e) {r2=0;//parameter 2 is an integer}; Parameter 2 number of digits after the decimal point6M=math.pow (10,math.max (R1,R2));//take the larger number of digits7        return(arg1*m+arg2*m)/m; Convert arg1 and arg2 to integers before converting back to floating point numbers8}


The above reprint attached the original website 1190000007207259

The toFixed () method should also handle part of the rounding error problem in a dummy manner.

Syntax: number.tofixed (x) x: Specifies the number of decimal places, which are values between 0 and 20, including 0 and 20, and some implementations can support a larger range of values. If this argument is omitted, 0 will be used instead.

END

JavaScript floating-point value arithmetic rounding error

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.