4 ms·
Similar approach, I think, by Russ Cox: https://research.swtch.com/ftoa https://research.swtch.com/ftoa Starts with an int->string conversion, then does the re
by to3m 9y ago
Similar approach, I think, by Russ Cox: https://research.swtch.com/ftoa https://research.swtch.com/ftoa
Starts with an int->string conversion, then does the rest with string manipulation - strings, of course, being bignums with a somewhat wasteful BCD representation.
- userbinator 9y agoWhy are some converters more complicated than this? Because you can make them a little faster. On my laptop, glibc's sprintf(buf, "%.50e", M_PI) is about 15 times faster than an equivalent print using the code above, because the implementation of sprintf uses much more sophisticated mathematics to speed the conversion. He missed an opportunity to name the article "Floating Point to Decimal Conversion is Easy and Not Fast" (in reference to https://swtch.com/~rsc/regexp/regexp1.html https://swtch.com/~rsc/regexp/regexp1.html) (When did he start writing articles in Go? IMHO the syntax is close enough to C to make skimming through easy enough, but confusing to look at in detail for the majority who will have likely far more experience with C than Go.)