www.digitalmars.com         C & C++   DMDScript  

D - Possibly problem with 'real'

reply "Steve Adams" <adamss ascinet.com> writes:
The program below prints

Finished    -0.00000000000000
Finished    -0.00000000000000

for the results.  But, it you change all the 'real' to 'double',
you correctly get:

Finished 21081851083598.38281400000000
Finished 21081851083600.37499900000000



import c.stdlib;
import math;

int main()
{
  long i;
  real sum=0.0,newsum,f;
  real sumerr=0.0;
  real err;

  for(i=1;i<=1000000000;i++)
    {
    f = sqrt( (real) i);
    newsum = sum + f;
    err = (newsum - sum ) - f;
    sumerr += err;
    sum = newsum;
    }
  printf("Finished %20.14f\n",sum);
  printf("Finished %20.14f\n",sum-sumerr);
  return( 0 );
}
Jul 01 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
Steve Adams wrote:
   printf("Finished %20.14f\n",sum);
   printf("Finished %20.14f\n",sum-sumerr);

Use "%20.14Lf" as the formatting code to print real.
Jul 01 2003
parent "Steve Adams" <adamss ascinet.com> writes:
That did it, thanks.


"Burton Radons" <loth users.sourceforge.net> wrote in message
news:bds4bs$2vtm$1 digitaldaemon.com...
 Steve Adams wrote:
   printf("Finished %20.14f\n",sum);
   printf("Finished %20.14f\n",sum-sumerr);

Use "%20.14Lf" as the formatting code to print real.

Jul 01 2003