Home > Web Front-end > JS Tutorial > JavaScript floating point value operation bug

JavaScript floating point value operation bug

高洛峰
Release: 2016-10-20 17:53:58
Original
1442 people have browsed it

Problem

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 a floating point number such as 1.0, the result is 1 instead of 1.0. This is due to saving the floating point number. Points take up twice as much memory as storing integer values, so ECMAScript wastes no time 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 worldview 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. If you don’t know what IEEE754 is, just click me. This numerical calculation method will Save the value as binary and then perform the calculation. Since floating point numbers are infinite when expressed in binary, rounding errors will occur when performing arithmetic calculations. Due to the existence of rounding errors, floating point number calculations are far less accurate than integer calculations. , and finally remember to never test the value of a specific floating point number.
So Mr. Yang Jiang is right when he says that young people think more and study less. If they are uneducated, they will always doubt life, so they still need to study more! ! !

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, here is the fresh code~

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template