Re: strange sum behaviour

From: Chris Mair <chrisnospam(at)1006(dot)org>
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 19:23:04
Message-ID: 1156879384.3590.4.camel@ultra.home.lan
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


> 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.

0.1 cannot be represented exactly using floating point numbers
(the same way as 1/3 cannot be represented exactly using decimal
numbers). You're bound to suffer from round-off errors.

Use numeric for exact, decimal math.

Bye, Chris.

--

Chris Mair
http://www.1006.org

In response to

Browse pgsql-general by date

  From Date Subject
Next Message ing_enriquebarrios 2006-08-29 20:21:30 SQL Tuning
Previous Message Ron Johnson 2006-08-29 19:17:50 Re: Anonymous stored procedures