[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Possible improvements to the LNUM patch?
- From: glen@...
- Date: Mon, 14 Apr 2008 15:06:09 -0600 (MDT)
> 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.
Recently I wrote a bookkeeping application that does Quicken-like stuff.
It is much faster than Quicken for data entry, but has only primitive
output; no charts, etc.
The easiest way to solve the problem is just to use floats, and when
inputting data make sure that the input strings do not have more than 2
digits past the decimal. When outputting, just use string.format("%10.2f")
or whatever.
You will not have accuracy problems until you have a great many
transactions. I wrote a quick lua program to see what the worst case would
be, and it will never happen with my program unless you have (I forget how
many - an extremely large number) of transactions.
Yes, the data is not exact, but you really don't care. One caveat: when
doing compares you may need to have an epsilon. For example, in one place
I need to make sure that the sum of some amounts is zero. I simply make
sure that the absolute value of the sum is less than a very small number;
iirc I use e-10.
The next easiest way is to simply use cents. This requires that input data
be checked and converted. This method will easily handle amounts as large
as the deficit created by the current administration (and congress, who is
just as guilty as Bush because they just rubber stamp all his requests).
Oops- political ranting not appropriate on this forum. Still, the answers
about financial data in Lua are, I hope, useful to someone. IMHO it is
wasted effort to create a special data type, at least without checking to
see if a float will work for you. The integer part of a float is something
like 48 bits (I forget - look it up) which will allow exact storage of
numbers that I will bet are large enough for any financial application.
Glen.