A
A. L.
Consider following code segment:
#1: double pi = 3.141592653589;
#2: printf("%lf\n", pi);
#3: printf("%1.12lf\n", pi);
#4: printf("%1.15lf\n", pi);
The above code outputs as following:
3.141593
3.141592653589
3.141592653589000
The default output precision of printf is 6, so #2 outputs '3.141593'
with the effect of rounding error. To get the complete string
representation of pi, the precision should be explicitly specified as a
number equal or greater than pi's real precision, e.g., 12 in '%1.12lf'
of #3. But when specified precision is greater than real precision,
just like #4, the annoying trailing zeros are padded.
My question is that how to get the complete/precise string
representation of a float number using C standard library, but not in a
style of printf in which the output precision should be specified as a
great number (it will bring out superfluous trailing zeros).
Could somebody here give me some advices?
Thank you very much.
#1: double pi = 3.141592653589;
#2: printf("%lf\n", pi);
#3: printf("%1.12lf\n", pi);
#4: printf("%1.15lf\n", pi);
The above code outputs as following:
3.141593
3.141592653589
3.141592653589000
The default output precision of printf is 6, so #2 outputs '3.141593'
with the effect of rounding error. To get the complete string
representation of pi, the precision should be explicitly specified as a
number equal or greater than pi's real precision, e.g., 12 in '%1.12lf'
of #3. But when specified precision is greater than real precision,
just like #4, the annoying trailing zeros are padded.
My question is that how to get the complete/precise string
representation of a float number using C standard library, but not in a
style of printf in which the output precision should be specified as a
great number (it will bring out superfluous trailing zeros).
Could somebody here give me some advices?
Thank you very much.