I want to be able print out a double precision number s.t it is identical to the original decimal number in C language.
char buf[1000];
double d = ______;
For a double, I would do
snprintf(buf, sizeof(buf), "%.17g", .1);
But it will print out on doing : printf("%s", buf);
0.10000000000000001
There is a last 1
as a rounding error
It prints out the closest decimal representation double precision value if you try to represent .1
. Casting .1
to double, you get a rounding error. Casting back to decimal, you will get another rounding error.
Is there a way to fix this?