for instance:
#include <stdio.h>
int main(int argc, char **argv) {
my_printf("%f\n", 1.233239208938208);
my_printf("%f\n", 1.23);
return (0);
};
I hope output is
1.233239208938208
1.23
How should I do?
for instance:
#include <stdio.h>
int main(int argc, char **argv) {
my_printf("%f\n", 1.233239208938208);
my_printf("%f\n", 1.23);
return (0);
};
I hope output is
1.233239208938208
1.23
How should I do?
Assuming my_printf()
works like printf()
, include the precision as commneted by @Weather Vane. If the precision field is not specified, 6 is used.
int main(void) {
printf("%f\n", 1.233239208938208);
printf("%f\n", 1.23);
printf("%.*f\n", 15, 1.233239208938208);
printf("%.*f\n", 2, 1.23);
printf("%.15f\n", 1.233239208938208);
printf("%.2f\n", 1.23);
// Alternatively use %g which avoids trailing zeros
printf("%.16g\n", 1.233239208938208);
printf("%.16g\n", 1.23);
return 0;
}
Output
1.233239
1.230000
1.233239208938208
1.23
1.233239208938208
1.23
1.233239208938208
1.23
When printing double
to high precision, consider that issues begin past DBL_DIG
places.
See Printf width specifier to maintain precision of floating-point value
Further detail: 1.23
is a double
. Typical double
, internally, do not take on the exact value as expressed in decimal text due to binary floating point representation. Instead, the closest double
to 1.23
may be exactly 1.229999999999999982236431605997495353221893310546875
.