I'm on environment that has no printf()
or any equivalent, so I'm writing it myself. But I have no idea how to perform such a conversion of float
types. I tried to seen how gcc does it, but it's really hard to understand.

- 17,007
- 37
- 111
- 185
-
"Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results." What have you tried so far? What isn't working about it? You mention "a conversion of `float` types", but what do you need to convert? Aren't you just trying to write a method that takes a float and writes some output? – Joshua Taylor Oct 12 '13 at 18:09
-
@JoshuaTaylor: I haven't posted my code because it totally wrong.. but I do. Wait a second. – The Mask Oct 12 '13 at 18:10
-
...the conversion is from float to ASCII – The Mask Oct 12 '13 at 18:11
-
Thanks! Also, as an answer already mentions, can you use _other_ library code, e.g., something that take a float and produces a string? You mention that you can't use `printf`, but what are the _actual_ constraints on what you do and don't have? Also, as I see mentioned in a comment to one answer, what _will_ you have available? What API are you using to create output at all? `putc`, perhaps? What part of the standard C libraries can we assume that you have? – Joshua Taylor Oct 12 '13 at 18:11
-
Re: "the conversion is from float to ASCII" so it the actual question how to get a _string_ containing a representation of the `float`? I ask only because it's possible that there are some ways (e.g., OS routines) that would _output_ a float for you, but not give you `char*` to it. – Joshua Taylor Oct 12 '13 at 18:18
-
What is the target system? – ouah Oct 12 '13 at 18:21
-
@JoshuaTaylor: By float I mean one like this `x = 3.14;` declared as `float x;` Which OS' routines are you speaking? I'm curious npw. But it need to be portable, only `putc()` equivalent that's machine-dependent. – The Mask Oct 12 '13 at 18:43
-
Oh, I don't know of an OS routine that does it; my point was that (to me) conversion implies that you need to put in a `float` _and get something back_, whereas the title says "print a float on stdout" which doesn't (to me) imply that you need a string back. And saying "ASCII" could be misleading, too. What if you're on a system using [EBCDIC](https://en.wikipedia.org/wiki/EBCDIC)? :) I'm just pointing out that, in general, there's a difference between getting a string representation of a float and having a representation of a float be sent to stdout. – Joshua Taylor Oct 12 '13 at 18:46
2 Answers
Floating-point formatting is very easy to get wrong. Writing a simplistic implementation that works for "most" numbers is deceptively easy, but it is likely to break on very large numbers, very small numbers, and numbers close to zero, not to mention IEEE754 subnormals, infinities and NaN. It might also get wrong the trailing decimals, failing to provide a string representation that allows reproducing the float bit-by-bit.
Fortunately, there are libraries out there that implement the work of formatting floating-point numbers, either for education, for embedded systems, or to improve on some aspect of standard-library formatting. If possible, I recommend that you incorporate David Gay's dtoa
library, which has been extensively tested in Python and elsewhere.

- 141,790
- 18
- 296
- 355
-
Very easy to get wrong really... that's why I'm asking for help and didn't provide any code in the thread... I wrote a small routine that seemd works fine by taking inspiration from other printf()'s for embbed systems, but it showed be totally wrong with various `float` numbers that I tested. Thanks for link. I'm going to check out – The Mask Oct 12 '13 at 18:50
You can take a look at musl libc implementation. musl
is a lightweight libc.
In fmt_fp
function defined in src/stdio/vfprintf.c
, they are basically converting a float to a string for fprintf
conversion specifiers like f
.
If you search on the internet with keyword ftoa
, you will find some other implementations of functions converting a float
to a string.

- 142,963
- 15
- 272
- 331
-
2For convenience, here's a link [src/stdio/vfprintf.c, line 208](http://git.musl-libc.org/cgit/musl/tree/src/stdio/vfprintf.c#n208). – Joshua Taylor Oct 12 '13 at 18:16