# couchdb-user mailing list archives

##### Site index · List index
Message view
Top
From Matteo Caprari <matteo.capr...@gmail.com>
Subject Re: sum function in reduce with precision problems?
Date Sat, 12 Dec 2009 14:45:13 GMT
```I'm writing a finance app and was looking for solutions to the
precision problem.

I found some stuff. I have not tested any of it as at first glance
this all thing feels overkill. There may be a more elegant solution.

bigdecimal.js part of gwt-math

BigDecimal support for JavaScript
http://stz-ida.de/html/oss/js_bigdecimal.html.en

Big Number
http://jsfromhell.com/classes/bignumber

On Sat, Dec 12, 2009 at 1:18 PM, Leo Simons <mail@leosimons.com> wrote:
> On 12/12/09 12:33 PM, Michael Franzkowiak wrote:
>>
>> CouchDB seems to think that 0.79 + 5.99 + 1.59 = 8.370000000000001
>
> So does most other software when using floating point arithmetic:
>
> \$ python -c 'print "%.15f" % (0.79 + 5.99 + 1.59)'
> 8.370000000000001
> \$ perl -e 'print sprintf("%.15f\n", 0.79 + 5.99 + 1.59);'
> 8.370000000000001
> \$ python -c 'print "%.20f" % (0.79 + 5.99 + 1.59)'
> 8.37000000000000099476
> \$ perl -e 'print sprintf("%.20f\n", 0.79 + 5.99 + 1.59);'
> 8.37000000000000099476
>
> This is standard stuff (
> http://docs.sun.com/source/806-3568/ncg_goldberg.html ).
>
>> I could always just multiply my numbers with the precision I need
>
>> and work with ints but I'm still curious to see this explained.
>
> What you really really want is a decimal type:
>
> \$ python -c 'from decimal import Decimal; print Decimal("0.79") +
> Decimal("5.99") + Decimal("1.59")'
> 8.37
>
> ...but there is no decimal type in javascript or json so also does not exist
> in CouchDB.
>
> Looks like you're representing money; just work with cents (stored as ints)
> and you will *probably* be fine, especially if you stay clear of divisions.
> I wouldn't try and implement my own finance package that way, though :)
>
> Alternatively, implement your own decimal type in javascript, or use a
> relational database :)
>
> ciao,
>
> Leo
>

--
:Matteo Caprari
matteo.caprari@gmail.com

```
Mime
View raw message