[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
**Subject**: **Re: Formatting numbers without precision loss**
**From**: Thijs Schreijer <thijs@...>
**Date**: Tue, 20 Jun 2023 13:58:02 +0200

> Just a hunch, since it passed too much time from when I looked into this. I think the only fireproof way to serialize a float number (assuming an IEEE754 implementation) is using exponential binary notation ("%a" format specifier), but the presence of that depends on the underlying C runtime (IIRC Lua supported that format specifier since v5.2).
>
> In other words, there always will be some float number whose binary representation will be approximate by any amount of decimal figures in a decimal representation.
>
> There was some "good enough" approximation: IIRC %.14f worked well for 64bit double precision IEEE754 floats. "Well" means that for most (all?) possible numbers the approximation was under some epsilon.
>
> Cheers!
>
> -- Lorenzo
Yes, my further digging also led to “%a”; see https://stackoverflow.com/questions/4826842/the-format-specifier-a-for-printf-in-c for example.
I guess a runtime test for support of that specifier could tell the lib whether it can use it.
Remaining question is then; what’s the best workaround if that is not available?
Thijs