Re: strange sum behaviour

From: Seneca Cunningham <tentra(at)gmail(dot)com>
To: Andrew Baerg <andrew(dot)baerg(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: strange sum behaviour
Date: 2006-08-29 18:04:25
Message-ID: FFDED977-A4DF-4B2B-B210-D1E577F11B87@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 29-Aug-2006, at 13:13 :48, Andrew Baerg wrote:

> Hi,
>
> I am getting strange results from the sum function as follows:
>
> corp=# select amount from acc_trans where trans_id=19721 and
> chart_id=10019;
> amount
> ---------
> 4.88
> 117.1
> -121.98
> (3 rows)
>
> corp=# select sum(amount) from acc_trans where trans_id=19721 and
> chart_id=10019;
> sum
> ----------------------
> -1.4210854715202e-14
> (1 row)
>
>
> amount is defined as double precision. I noticed that if I cast amount
> as numeric, the sum comes out 0 as expected.
>

Don't use floats or doubles for financial data, use numeric types.
Your double sum looks to be a normal error for floats in that situation.

Take a look at <http://www2.hursley.ibm.com/decimal/decifaq.html> for
some information about why you should be using numeric for your
amount column.

--
Seneca Cunningham
tentra(at)gmail(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andrej Ricnik-Bay 2006-08-29 18:12:35 Re: [GENERAL] Shared Objects (Dynamic loading)
Previous Message Enrique Alejandro Barrios 2006-08-29 17:59:42 SQL Tuning