Home  >  Article  >  Web Front-end  >  Explore the reasons why JavaScript floating point value operations are rounding errors

Explore the reasons why JavaScript floating point value operations are rounding errors

巴扎黑
巴扎黑Original
2017-07-22 16:54:281685browse

Question

In javascript, both integers and floating-point numbers belong to the Number data type (one of the simple data types). We often find that when printing floating-point numbers like 1.0 The result is 1 instead of 1.0. This is because the memory space for storing floating point numbers is twice that of storing integer values, so ECMAScript will lose no time in converting floating point numbers to integers.
Although the above situation makes patients with obsessive-compulsive disorder a little uncomfortable, it is not a big mistake anyway. The next situation will be very scary. For example, when we calculate 0.1 plus 0.2, the output result is not 0.3, but 0.3000000000000004. what the fuck? ! For children who encounter this situation for the first time, do you feel that your world view has been challenged?

Cause

So I quickly opened the book to save my soul and body, and found that it was clearly written in the book: ECMAScrip is based on IEEE754 numerical floating point calculation. This numerical calculation method will change the numerical value. Save it as binary and then perform calculations. Since floating point numbers are infinite when expressed in binary, rounding errors will occur during arithmetic calculations. Due to the existence of rounding errors, floating point number calculations are far less accurate. Far inferior to integer calculations, Finally remember to never test the value of a specific floating point number.

Solution

The so-called prescribe the right medicine, if you know the cause of the problem, you can find the solution to the problem. Since it is an error caused by the binary system of floating point numbers being infinite, this error does not exist in integer operations. Are you smart enough to see the truth? That's right, that is to convert floating point numbers into integers in arithmetic engineering, and then convert the results into floating point numbers. Guest Officer, the following is the fresh code~

1 //加法  2 function FloatAdd(arg1,arg2){  
3        var r1,r2,m; 
4        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0; //参数1为整数};  //参数1小数点后的位数5        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0; //参数2为整数}; //参数2小数点后的位数6        m=Math.pow(10,Math.max(r1,r2));  //取其中较大的位数7        return (arg1*m+arg2*m)/m;        //先将arg1和arg2转换为整数进行计算,然后再转换回浮点数8   }


The original URL is attached to the above reprint


toFixed() The method should also be able to handle part of the rounding error problem in a fool-proof way.

Syntax: number.toFixed(x) x: Specifies the number of decimal places, which is a value between 0 and 20, including 0 and 20. Some implementations can support a larger numerical range. If this parameter is omitted, 0 will be used instead.


END


The above is the detailed content of Explore the reasons why JavaScript floating point value operations are rounding errors. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn