Re: Rounding problems

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Paolo Saudin" <paolo(at)ecometer(dot)it>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Rounding problems
Date: 2009-05-03 14:52:45
Message-ID: 11625.1241362365@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Paolo Saudin" <paolo(at)ecometer(dot)it> writes:
> I have a problem with a query wich simple aggregate values. In the sample
> below I have two values, 1.3 and 1.4. Rounding their average with one
> decimals, should give 1.4.

You seem way overoptimistic about float4 values being exact. They are
not. The actual computation being done here is more like

regression=# select (1.3::real + 1.4::real) / 2 ;
?column?
------------------
1.34999990463257
(1 row)

If you want an exact sum with no roundoff error you should be storing
all your values as numeric (and taking the consequent speed and space
hit :-().

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Paolo Saudin 2009-05-03 15:24:46 R: Rounding problems
Previous Message Justin 2009-05-03 14:42:13 Re: Rounding problems