mattiav27
mattiav27

Reputation: 685

Why do I get 0 as a result?

I am trying to write a complex program in C, but I get absurd results. I tracked the error in the definition of a constant. With this MWE I can reproduce the problem of my program:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main(){
      double Md=pow(10.,-9.);
     /*double Md= 0.000000001;*/
      printf("Md = %f\n",Md);
      return 0;
}

with both the definitions of Md I get 0 as a result. Why is it so?

Upvotes: 1

Views: 310

Answers (1)

Bathsheba
Bathsheba

Reputation: 234705

The default for %f is six decimal places, hence 0 is output.

Be assured, Md will be non-zero in your debugger.

printf("%.*lf\n", DBL_DIG, Md); is one way round this: DBL_DIG is introduced via <float.h>, although you'd be advised to get used to scientific notation

printf("%e\n", Md);

which will permit printing of arbitrarily sized floating point values.

Upvotes: 4

Related Questions