delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/20/09:38:39

From: "Alfredo J. Cole" <ajc AT acyc DOT com>
To: <djgpp AT delorie DOT com>
Subject: Round off errors
Date: Fri, 20 Feb 1998 08:30:41 -0600
Message-ID: <01bd3e0c$200881c0$LocalHost@alfredoc>
MIME-Version: 1.0

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 <stdio.h>

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;
   }

------------------------------

--
Alfredo J. Cole
ACyC (ajc AT acyc DOT com)
Tegucigalpa, Honduras
Phone: +(504) 265902  Fax: +(504) 267672
http://www.acyc.com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019