The addition calculation error of double precision floating point number and preliminary method thereof

xiaoxiao2021-03-06  59

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 main () { Char Str [256]; Double X = 9999999999.00; Double S = 0.0; Double X1 = 99999999.99; Double S1 = 0.0; INT I;

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.

转载请注明原文地址:https://www.9cbs.com/read-109124.html

New Post(0)