lua-users home
lua-l archive

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


Aladdin Lampé wrote:

I would like to use Lua in a "financial" context, where using
"doubles" for floating point decimal numbers is not enough regarding
to precision. Indeed, dealing with large monetary values may trigger
overflows in the calculations... I certainly do not want this to
happen.

<long winded response>

In my pbLua application, I had the opposite problem. I had compiled
Lua to be strictly 32 bit integers and had a strange hybrid mix
of single precision float and integers that occasionally gave
strange results if the programmer was not careful.

In the end, I made a userdata library that implemented float as
its own data type and then added a metatable with the built-in
operations for +-/* , the comparisons, as well as a tostring()
and concat() capability.

It was not that hard and once again made me appreciate the care
and thought that has gone into making the Lua C API a thing
of frightening simplicity and beauty.

</long winded response>

I would suggest having a look at creating your own userata for
high precision monetary values, and implementing a metatable
with overrides for the basic Lua operations.

You may also end up having to add functions to create instances
of the type etc. See the C API and userdata secions in the Blue PiL
for details.

Ralph