Thread: BUG #3467: Sum strange behaviour

BUG #3467: Sum strange behaviour

From
"Alex"
Date:
The following bug has been logged online:

Bug reference:      3467
Logged by:          Alex
Email address:      AVShutko@mail.khstu.ru
PostgreSQL version: 8.2.4
Operating system:   FreeBSD 6.2-RELEASE-p4
Description:        Sum strange behaviour
Details:

I have a database with some float numbers (all of them are 2 digits
presision). When I do select sum() I get this: 96.3100000000001

Here are the numbers:

 4.29
 4.29
 0.29
 14.3
 1.43
 14.3
 1.43
 1.43
 7.15
 2.94
 0.14
 5.56
 1.43
 0.29
 0.59
 1.43
 1.43
 1.43
 1.43
21.45
 1.43
 0.14
 2.15
 1.43
 1.43
 1.43
 1.43
 0.59
 0.22
 1.43
 1.18

Is this bug or future ?

Re: BUG #3467: Sum strange behaviour

From
Heikki Linnakangas
Date:
Alex wrote:
> I have a database with some float numbers (all of them are 2 digits
> presision). When I do select sum() I get this: 96.3100000000001
> ...
> Is this bug or future ?

A feature. Floating point numbers can't generally represent a decimal
number exactly, so you get some rounding errors. Usually the errors are
small enough that they don't show up, but sometimes they do, like in
this example. I would recommend using NUMERIC instead of floats for
anything where the input data is in decimal format.

--
  Heikki Linnakangas
  EnterpriseDB   http://www.enterprisedb.com