lua-users home
lua-l archive

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


> Stupid question maybe, but is there a way (without having to write a C
> module) to serialize Lua numbers without loss of precision? Eg. I
> tried printing a 5/6 with tostring, string.format with all formats
> immaginable etc. but couldn't get back a clean 5/6.

If you are using double-precision Lua numbers (the default), print them
with %0.17g. This allows you to read them back without loss. However,
as explained in a previous answer, you'll still won't be able to represent
5/6 exactly. On the other hand, 5/6 does seem to be exact in the sense
that this code prints true:
	a=5/6
	b=6*a
	print(b==5)

This is not a contradiction...