From: mlevedahl AT erols DOT com (mark levedahl) Newsgroups: comp.os.msdos.djgpp Subject: Re: Round off errors Date: Fri, 20 Feb 1998 20:38:49 GMT Organization: Erol's Internet Services Lines: 67 Message-ID: <34efe8fb.923747@news.erols.com> References: <01bd3e0c$200881c0$LocalHost AT alfredoc> NNTP-Posting-Host: 207-172-112-238.s238.tnt4.ann.erols.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Fri, 20 Feb 1998 08:30:41 -0600, "Alfredo J. Cole" wrote: Works fine using double rather than float. float will give 6 to 7 digits of accuracy, so your example fails for known reasons. double offers about 15 significant figures. Unless you're involved in tracking the Federal budget and/or National Debt, this should be more than adequate, even across a large number of calculations. Still, BCD would be safer.. >Compiling the following code with DJGPP gives the erroneous result >(5551.20). The same code compiled with Borland's bcd class gives the >correct result (5551.22). I am developing an accounting system with >DJGPP and it requires that the results be exact. Is there a similar >class or option under DJGPP that will eliminate these round off >errors? > >Thank you. > >------------------------------ >/* Example of round-off error */ >/* When this program is executed with IEEE binary floating > point arithmetic, the answer is incorrect due to accumulated > round-off error. When BCD is used, the correct answer > is obtained. > */ > >#include > >float data[100] = { > 4997.01, 921.05, -1921.06, 4621.03, -605.20, > -3105.12, 4665.01, -271.23, -212.23, -1548.18, > -2351.12, -2787.12, 1474.19, 1817.07, -2299.12, > 2648.13, -3787.12, 3948.13, -3401.12, 170.14, > 4030.14, -334.22, -704.20, -2233.12, 3369.13, > -4218.24, 2214.13, -4707.24, 2369.13, 4816.01, > -4009.12, 332.15, 4501.01, -1349.18, -438.22, > -4996.24, 4020.13, -2623.12, 2541.13, -608.20, > 3929.14, -3300.12, -4314.24, 839.16, 2173.14, > 1243.07, -2291.12, 693.16, 4061.14, -2625.12, > -4048.12, 2162.13, 686.05, 2133.13, -2149.12, > 3884.13, -199.24, -3611.12, 3282.13, -95.24, > -4614.24, 1875.07, 2203.13, -1371.18, -1616.06, > 1210.07, -3014.12, 4197.01, -4141.24, 3255.13, > -3203.12, 547.16, 2713.14, -1039.18, 982.05, > -1924.06, 3419.13, -2488.12, 4334.03, -4469.24, > 4088.14, -3840.12, 585.05, -1932.06, 4496.01, > -918.20, -445.22, -1856.18, 2007.07, -2631.12, > 351.03, 774.05, 84.01, -1089.06, 4278.01, > -4860.24, 1165.07, 4482.03, -8.23, -3432.12 }; > >int main () { > /* add 100 numbers and print the result */ > /* In this example, the array is initialized to contain > the data. In real programs the data might be read > from a file or the keyboard using scanf. > */ > float answer = 0.0; > int i; > for (i = 0; i < 100; ++i) answer += data[i]; > printf("The sum is %10.2f\n",answer); > printf("The correct answer is 5551.22\n"); > return 0; > } > >------------------------------