Re: math error or rounding problem Money type

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Mark Kirkwood" <markir(at)paradise(dot)net(dot)nz>
Cc: "Justin" <justin(at)emproshunts(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "PostgreSQL-development" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: math error or rounding problem Money type
Date: 2008-06-08 23:03:52
Message-ID: 87lk1fleav.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

"Mark Kirkwood" <markir(at)paradise(dot)net(dot)nz> writes:

> IFAIK (dimly recalling numerical analysis courses at university) SUM and ROUND
> can *never* be commuted. In general the recommended approach is to round as
> late as possible and as few times are possible - so your 1st query is the
> correct or best way to go.

I don't think "as late as possible" applies with money. If you were dealing
with approximate measurements you want to round as late as possible because
rounding is throwing away precision. But if you're dealing with money you're
dealing with exact quantities.

There is only going to be one correct time to round and that's whenever you're
creating an actual ledger item or order line item or whatever. Once you've
calculated how much interest to credit or whatever you have to make that
credit an exact number of cents and the $0.004 you lost or gained in rounding
never comes up again.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's PostGIS support!

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Gregory Stark 2008-06-08 23:07:21 Re: Overhauling GUCS
Previous Message Alvaro Herrera 2008-06-08 23:03:48 handling TOAST tables in autovacuum