Overengineering float serialization for no good reason
wejn.org·2d
🧪Binary Fuzzing
Preview
Report Post

Written on 2025-12-25

Problem statement

This is a quick exploration of how overengineering float serialization to be just right(™) instead of needlessly stripping digits off mantissa… wasn’t worth that much.

In other words, say you carefully write 15 lines of python (and some tests) to make 1.234567891e12 and -1.234567891e12 always serialize with max precision to a fixed buffer (say, 14 chars)1, and avoid this:

a = 1.234567891e12
"%.8g" %  a # => '1.2345679e+12'
"%.8g" % -a # => '-1.2345679e+12'

"%.9g" %  a # => '1.23456789e+12'

# well, shit... what to use, eh?

But… then you think about it for a while… and you realize you were a doofus for not simply phoning it in and just slapping f"{val:.8g}" in there instead.

Why?2 Read on…

Backgrou…

Similar Posts

Loading similar posts...