Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the full length of a double with printf in C

Tags:

c

printf

How can I use printf in C to display the full length of a double.

Consider

int main(){

double x = 0.00000000000001;

printf("x: %f \n", x);

}

The result I get is "i: 0.000000", however from what I read %f is the correct format specifier and doubles should have 15-17 significant figures therefore I don't understand why I am getting a rounded result. I have also tried things like %15f and %lf.

I am using Ubuntu if that helps.

like image 771
User Avatar asked Dec 14 '25 01:12

User


2 Answers

From http://linux.die.net/man/3/printf:

f, F

The double argument is rounded and converted to decimal notation in the style [-]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as 6; if the precision is explicitly zero, no decimal-point character appears. If a decimal point appears, at least one digit appears before it.

like image 156
Peter - Reinstate Monica Avatar answered Dec 16 '25 19:12

Peter - Reinstate Monica


If you want to output 15 digits after the decimal points, it should be %.15f instead of %15f:

printf("i: %.15f \n", i);
like image 40
Yu Hao Avatar answered Dec 16 '25 19:12

Yu Hao