c - Strange behaviour of the pow function -


while running following lines of code:

int i,a;      for(i=0;i<=4;i++)   {         a=pow(10,i);         printf("%d\t",a);     }    

i surprised see output, comes out 1 10 99 1000 9999 instead of 1 10 100 1000 10000.

what possible reason?

note
if think it's floating point inaccuracy in above loop when i = 2, values stored in variable a 99.

but if write instead

a=pow(10,2); 

now value of comes out 100. how possible?

i can't spell c, can tell why.

you have set a int. pow() generates floating point number, in cases may hair less 100 or 10000 (as see here.)

then stuff integer, truncates integer. lose fractional part. oops. if needed integer result, round may better way operation.

be careful there, large enough powers, error may large enough still cause failure, giving don't expect. remember floating point numbers carry precision.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -