4 ms·
Does anyone know of a good decimal float to string converter, preferably in c?
by kstenerud 8y ago
Does anyone know of a good decimal float to string converter, preferably in c?
- majewsky 8y agosprintf
- nwellnhof 8y agoThen main drawbacks of sprintf are that it doesn't allow to find the shortest decimal representation and that its output depends on the current locale, so you often need something like sprintf_l which isn't portable.
- Bromskloss 8y agoWhy is it not portable?
- rurban 8y agoBecause only very few libc's support it. There should also be a locale independent _sprintf_c somewhen.
- nwellnhof 8y agoC code for Ryu is available under the Apache2 and Boost licenses: https://github.com/ulfjack/ryu/tree/master/ryu https://github.com/ulfjack/ryu/tree/master/ryu Also have a look at Grisu3, Dragon4, dtoa, and the printf implementations of glibc and musl.
- stephencanon 8y agoThe two fastest options that I'm currently aware of, by a pretty wide margin, are Ryu and Swift's implementation (single C file, it's only in a C++ file to satisfy a build system quirk in Swift on linux: https://github.com/apple/swift/blob/master/stdlib/public/runtime/SwiftDtoa.cpp https://github.com/apple/swift/blob/master/stdlib/public/run..., Apache2 + run time exception). Ryu is somewhat faster for doubles, though SwiftDtoa has support for 80-bit and float out of the box, which is nice, and we have some further perf improvements planned. Both are considerably faster than any of the alternatives (some perf data in the original Swift PR: https://github.com/apple/swift/pull/15474 https://github.com/apple/swift/pull/15474), and both pass our fairly intensive test suite. musl's implementation is also interesting for reasons of simplicity.