I am trying to convert a double to a string in a native NT application, i.e. an application that only depends on ntdll.dll
. Unfortunately, ntdll's version of vsnprintf
does not support %f
et al., forcing me to implement the conversion on my own.
The aforementioned ntdll.dll
exports only a few of the math.h
functions (floor
, ceil
, log
, pow
, ...). However, I am reasonably sure that I can implement any of the unavailable math.h
functions if necessary.
There is an implementation of floating point conversion in GNU's libc, but the code is extremely dense and difficult to comprehent (the GNU indentation style does not help here).
I've already implemented the conversion by normalizing the number (i.e. multiplying/dividing the number by 10 until it's in the interval [1, 10)
) and then generating each digit by cutting the integral part off with modf
and multiplying the fractional part by 10. This works, but there is a loss of precision (only the first 15 digits are correct). The loss of precision is, of course, inherent to the algorithm.
I'd settle with 17 digits, but an algorithm that would be able to generate an arbitrary number of digits correctly would be preferred.
Could you please suggest an algorithm or point me to a good resource?