[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Possible improvements to the LNUM patch?
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Mon, 14 Apr 2008 14:40:06 -0400
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.
>
> Is there a way, using the LNUM patch, to circumvent this? I have
> thought about using an int64 for numbers and associating a second
> number to indicate where the decimal point is located. Another option
> would be to use BCD like some databases do (ORACLE for instance) to
> deal with arbitrary precision decimals.
>
> What do you think? Is LNUM the way to go (with some improvements), or
> should I instead use some available BCD library? (or implement it by
> myself).
I think using a custom number format is the way to go, since it will allow very natural programming in Lua (userdata have different semantics than numbers in Lua).
For the way to replace the number format you can have a look at LNUM and either use it or do the customization work yourself (if learning a bit about Lua internals is of some interest to you).
For the number format, I'd recommend a fixed point BCD format. The fixed point is because the financial data doesn't have arbitrary sub-unit precision (iirc. it's one cent or one tenth of a cent, so 2 or 3 BCD digits), and because even the biggest numbers will require precision down to the cent (or whatever the smallest value is). With a 64bits integer you get 16 BCD digits (which may not be enough if you're a trillionnaire), with 128 bits you can get a comfortable 32 BCD digits. You will have to replace basic number operations inside the Lua interpreter (either through LNUM or macros in luaconf.h). You can use a third-party BCD library to save some time.
My 2 cents.