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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help