You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Metin Akat <ak...@gmail.com> on 2009/09/01 10:58:15 UTC

Is fixed precision arithmetic possible in couchdb python?

/* Paste this in a code editor to have code highlighting */

/* Here is my Map function */
function(doc) {
  if (doc.type == "Transaction") {
    for (var i = 0; i < doc.accounts.length; i++) {
      emit(doc.accounts[i].account,
      doc.accounts[i].value *
      doc.accounts[i].operation
      );
    }
  }
}
/* Here the value is the money transferred to or from an account and
the operation an integer (1 or -1) showing the type (credit debit)
of the operation */

/* This is the reduce */
function(keys, values) {
  return sum(values);
  }


/* When I visit:
http://localhost:5984/nola7accounting/_design/accounting/_view/balance?group=true
I get this. The 13.05(9) is the sum of 5.03 + 8.03 (so is -13.05(9),
only it's negative). */
{"rows":[
{"key":"5cf15107d977498ec4003dca0d44c579","value":-13.059999999999999},
{"key":"b4b0c38efd4ff4c27fc63d24ae981f60","value":13.059999999999999}
]}

/* But when I visit:
http://localhost:5984/nola7accounting/_design/accounting/_view/balance
In the accounting world, this should return 0 as it is the sum of all
values (and the sum of all operations in a financial system must be
0),
 but here is what I get: */
{"rows":[
{"key":null,"value":-8.881784197001252e-16}
]}

/* OK, the above is is something close to 0, but the question is...
how good is this for a financial software system?
The situation is the same, no matter if I store floats or strings
(which would represent decimals). This is probably the result
of the weak typing in javascript (JSON).
I wrote this view following along the "Banking" chapter of the CouchDB
book, here:
http://books.couchdb.org/relax/receipts/banking
The example there is nice and working, but it works with whole numbers
(integers).
So, the first question is... Is there some way of working with fixed
precision numbers in CouchdB?
And the further question is...  How good is CouchDB (or any JSON
storage) for an accounting application?
 */

Re: Is fixed precision arithmetic possible in couchdb python?

Posted by Jason Davies <ja...@jasondavies.com>.
Also, if you are worried about loss of precision with large integers  
(with more than 15 digits) you should use a JavaScript port of  
BigInteger (or BigDecimal) for calculations and store the numbers as  
strings.  I'm successfully using BigDecimal for financial calculations  
at the moment.  It's probably overkill for most people but it works.   
See also http://issues.apache.org/jira/browse/COUCHDB-227 for some  
links about this issue.

Cheers,
--
Jason Davies

www.jasondavies.com

On 1 Sep 2009, at 13:47, Paul Davis wrote:

> Noah Slater had a post quite some time ago where he admonished the use
> of floats for money. He said to always use two integers and figure out
> the mat yourself. With floats is only a matter of time before you
> start missing pennies.
>
> It stuck with me so I thought I'd mention it.
> Paul
>
> On Tue, Sep 1, 2009 at 5:27 AM, Metin Akat<ak...@gmail.com>  
> wrote:
>> Hm, this makes sense. I guess I'll go this way. Thanks
>>
>> On Tue, Sep 1, 2009 at 12:18 PM, Robert Newson<robert.newson@gmail.com 
>> > wrote:
>>> Multiply your numbers by the amount of precision you need and use
>>> integers (*1000 for 3 d.p)? Using floating point to store money
>>> amounts seems fraught with rounding errors.
>>>
>>> B.
>>


Re: Is fixed precision arithmetic possible in couchdb python?

Posted by Paul Davis <pa...@gmail.com>.
Noah Slater had a post quite some time ago where he admonished the use
of floats for money. He said to always use two integers and figure out
the mat yourself. With floats is only a matter of time before you
start missing pennies.

It stuck with me so I thought I'd mention it.
Paul

On Tue, Sep 1, 2009 at 5:27 AM, Metin Akat<ak...@gmail.com> wrote:
> Hm, this makes sense. I guess I'll go this way. Thanks
>
> On Tue, Sep 1, 2009 at 12:18 PM, Robert Newson<ro...@gmail.com> wrote:
>> Multiply your numbers by the amount of precision you need and use
>> integers (*1000 for 3 d.p)? Using floating point to store money
>> amounts seems fraught with rounding errors.
>>
>> B.
>

Re: Is fixed precision arithmetic possible in couchdb python?

Posted by Metin Akat <ak...@gmail.com>.
Hm, this makes sense. I guess I'll go this way. Thanks

On Tue, Sep 1, 2009 at 12:18 PM, Robert Newson<ro...@gmail.com> wrote:
> Multiply your numbers by the amount of precision you need and use
> integers (*1000 for 3 d.p)? Using floating point to store money
> amounts seems fraught with rounding errors.
>
> B.

Re: Is fixed precision arithmetic possible in couchdb python?

Posted by Robert Newson <ro...@gmail.com>.
Multiply your numbers by the amount of precision you need and use
integers (*1000 for 3 d.p)? Using floating point to store money
amounts seems fraught with rounding errors.

B.

On Tue, Sep 1, 2009 at 9:58 AM, Metin Akat<ak...@gmail.com> wrote:
> /* Paste this in a code editor to have code highlighting */
>
> /* Here is my Map function */
> function(doc) {
>  if (doc.type == "Transaction") {
>    for (var i = 0; i < doc.accounts.length; i++) {
>      emit(doc.accounts[i].account,
>      doc.accounts[i].value *
>      doc.accounts[i].operation
>      );
>    }
>  }
> }
> /* Here the value is the money transferred to or from an account and
> the operation an integer (1 or -1) showing the type (credit debit)
> of the operation */
>
> /* This is the reduce */
> function(keys, values) {
>  return sum(values);
>  }
>
>
> /* When I visit:
> http://localhost:5984/nola7accounting/_design/accounting/_view/balance?group=true
> I get this. The 13.05(9) is the sum of 5.03 + 8.03 (so is -13.05(9),
> only it's negative). */
> {"rows":[
> {"key":"5cf15107d977498ec4003dca0d44c579","value":-13.059999999999999},
> {"key":"b4b0c38efd4ff4c27fc63d24ae981f60","value":13.059999999999999}
> ]}
>
> /* But when I visit:
> http://localhost:5984/nola7accounting/_design/accounting/_view/balance
> In the accounting world, this should return 0 as it is the sum of all
> values (and the sum of all operations in a financial system must be
> 0),
>  but here is what I get: */
> {"rows":[
> {"key":null,"value":-8.881784197001252e-16}
> ]}
>
> /* OK, the above is is something close to 0, but the question is...
> how good is this for a financial software system?
> The situation is the same, no matter if I store floats or strings
> (which would represent decimals). This is probably the result
> of the weak typing in javascript (JSON).
> I wrote this view following along the "Banking" chapter of the CouchDB
> book, here:
> http://books.couchdb.org/relax/receipts/banking
> The example there is nice and working, but it works with whole numbers
> (integers).
> So, the first question is... Is there some way of working with fixed
> precision numbers in CouchdB?
> And the further question is...  How good is CouchDB (or any JSON
> storage) for an accounting application?
>  */
>