The addition calculation error of the double precision floating point number and the preliminary method thereof, I started to find calculation errors in the SUM function of MySQL, after analysis, it is considered to be related to the double precision floating point number #include
For (i = 0; i <8192; i ) {s = x;} Printf ("% 18.2LF", S);
For (i = 0; i <8192; i ) {S1 = x1;} Printf ("% 18.2LF", S1); S1 = x1 * 8192.00; Printf ("% 18.2LF / N", S1);
}
Were compiled to run on Windows and unix below, the result is the same 81919999991808.00 819199999918.02 819199999918.08 This shows that double-precision floating-point addition in the calculation behind the decimal point there is a big error, and multiply like some of my initial thought of the two kinds of solutions, not Mature, I have not tried other languages, please refer to
Workaround 1: Avoid using Double, no problem, such as this in VB, using currency type private suss1_click () Dim i as doubledim s currendim x askDIM SC AS CURRENCYDIM XC AS CURRENCYX = 9999999.99s = 0for i = 1 To 8192S = s xnext ixc = 99999999.99sc = 0for i = 1 to 8192sc = sc xcnext imsgbox S & "" & SCEND SUB Results: 819199999999917 81999999918.08
Workaround 2: Do not use a decimal point when adding, add the decimal point to the right to connect the decimal point to the right and remove the decimal point, then remove the decimal point to the decimal point.