First of all, this is not a floating point newbie question. I know results of floating point arithmetic (not to mention transcendental functions) usually cannot be represent
I see you’ve accepted an answer already, but here are a couple of open source implementations of this conversion you might want to look at:
David Gay’s dtoa()
function in dtoa.c
: http://www.netlib.org/fp/dtoa.c.
The function ___printf_fp()
in the /stdio-common/printf_fp.c
file in Glibc (http://ftp.gnu.org/gnu/glibc/glibc-2.11.2.tar.gz, for example).
Both will print as many digits as you ask for in a %f
-type printf
, as I’ve written about at: