its not printing proper no.of grid points on the screen?

53 views Asked by At
  int n,m;           
   float S,K,R,L,dr,dz;
   S=4.0f;
   K=2.0f;
   R=1.0f;
   L=2.0f;
   dr=0.01f;
   dz=0.1f;
   n=int((R/dr)+1);
   m=int((L/dz)+1);
   printf("%d\t%d\t",n,m);

for this iam getting 10,20 grid points but actually the value should be 11,21 for n,m rply and for dr=0.01 its printing n=101 except this all other values of 'dr' it printing one value less like for dr=0.1 n=10 ,dr=0.001 n=1000 but actual value is 1001.please kindly explain me sir a have been trying it since one month still i didnt get the correct answer..

1

There are 1 answers

0
Mark Wilkins On

It's not completely clear to me what your question is, but you seem to be confused as to why the value of n is printed as 101. At the risk of pointing out the obvious, what you have in a purely mathematical expression is:

n = (1.0 / .01) + 1

1 divided by .01 equals 100. So even eliminating the possibilities of floating point error, then the answer of 101 is correct. The value of 20 being printed (rather than the "expected" 21) because of floating point error. You can see this more clearly if you add this statement:

printf( "%.15f\n", L / dz );

On my machine, it prints this:

19.999999701976780

The cast to int results in computing the floor of the value (20.999... ends up as 20 when cast to the integer value).