lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi!

On 20/06/2023 13:18, Thijs Schreijer wrote:
Hey list,

I’m running into the following issue (minimal example of a problem in a JSONschema validator):

[snip]

Works. (I also tried “%d” instead of “%.0f”, but that rolled-over to a negative number)

Now all of this is probably highly system/compile-time-config dependent. I get that.
But keeping in mind that “value” that is passed in is represented in the system as a proper value, how can I format the number in the code such that does not loose any precision in it’s journey from; number => string => lua-parser => number ?

Not just for ints but also floats? (And independent of Lua version, above was on PuC-Rio Lua 5.1)

Any help is greatly appreciated.

regards
Thijs


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