Keith Refson writes:
> I'll copy this to the list as it's of general interest. Herein begins
> floating-point arithmetic 101. :)
>
> Dave Peticolas writes:
> > I wrote
> > > it is inevitable that rounding errors will accumulate when
> > > adding a large column of figures because none of the quantities being
> > > added has an exact binary fractional representation. Rounding after
> > > addition will NOT solve this problem.
> >
> > Why is that? I would have thought that rounding would eliminate the
> > introduced error.
>
> less than the expected one. Try the following program on your computer
>
> #include <stdio.h>
> main()
> {
> int i;
> float r=0;
> for(i=0; i< 1000000; i++)
> r += 1.01;
> printf("Result = %16.4f\n",r);
> }
>
I guess I should have clarified my statement. What I meant was
that you should round after each addition. Also, we are using
doubles, not floats which have much less precision.
When I run the following program:
#include <stdio.h>
#include <math.h>
main()
{
double r = 0.0;
int i;
for(i = 0; i < 1000000; i++)
{
r += 1.01;
r *= 100.0;
r = floor(r + 0.5);
r /= 100.0;
}
printf("Result = %16.10f\n", r);
}
I get: 1010000.0000000000
dave
--
Gnucash Developer's List
To unsubscribe send empty email to: [EMAIL PROTECTED]