lua-users home
lua-l archive

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


On Sun, Sep 4, 2016 at 8:20 PM, Diogo Mildenberger <diogo.milde@gmail.com> wrote:
 
Has someone any experience to share about implementing a system that deals with currency values? I have already been bitten by the problems described in http://floating-point-gui.de/ and I want to find a better way.

The two common approaches are fixed point, as you mentioned, and General Decimal Arithmetic, which is basically base-10 floating point. See: http://speleotrove.com/decimal/

Ten years ago I made a Lua binding for IBM's General Decimal Arithmetic library decNumber called ldecNumber

http://luarocks.org/modules/dougcurrie/ldecnumber

It does what you need, but is stuck at Lua 5.1. It depends on some Lua 5.1 features that were removed in 5.2, and would be a bit of work to redo. There have also been some updates to decNumber that ought to be incorporated if ldecNumber is updated. If you work on the library you might consider using decQuad (decimal128) if you can get by with 34 digits of precision; the present version of ldecNumber uses arbitrary precision decNumber set to 69 digits of precision.

e