Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 7549 invoked from network); 12 Dec 2009 13:19:10 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 12 Dec 2009 13:19:10 -0000 Received: (qmail 47810 invoked by uid 500); 12 Dec 2009 13:19:09 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 47746 invoked by uid 500); 12 Dec 2009 13:19:09 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 47736 invoked by uid 99); 12 Dec 2009 13:19:09 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 12 Dec 2009 13:19:09 +0000 X-ASF-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00 X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [74.125.78.26] (HELO ey-out-2122.google.com) (74.125.78.26) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 12 Dec 2009 13:19:07 +0000 Received: by ey-out-2122.google.com with SMTP id 4so489181eyf.41 for ; Sat, 12 Dec 2009 05:18:45 -0800 (PST) Received: by 10.213.96.65 with SMTP id g1mr3006043ebn.44.1260623925115; Sat, 12 Dec 2009 05:18:45 -0800 (PST) Received: from acid.home (host81-153-231-112.range81-153.btcentralplus.com [81.153.231.112]) by mx.google.com with ESMTPS id 14sm1798177ewy.11.2009.12.12.05.18.42 (version=SSLv3 cipher=RC4-MD5); Sat, 12 Dec 2009 05:18:43 -0800 (PST) Message-ID: <4B239831.1050402@leosimons.com> Date: Sat, 12 Dec 2009 13:18:41 +0000 From: Leo Simons User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0 MIME-Version: 1.0 To: user@couchdb.apache.org Subject: Re: sum function in reduce with precision problems? References: <200912120801.49926.sh@widetrail.dk> <200912121155.04377.sh@widetrail.dk> <4B23849F.2040904@lymegreen.co.uk> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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