Skip site navigation (1) Skip section navigation (2)

Re: BUG #2117: inconsistency in sum

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Adam Kolany <dr(dot)a(dot)kolany(at)wp(dot)pl>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #2117: inconsistency in sum
Date: 2005-12-15 21:31:47
Message-ID: 20051215213147.GA45921@winnie.fuhr.org (view raw or flat)
Thread:
Lists: pgsql-bugs
[Please copy the mailing list on replies.]

On Thu, Dec 15, 2005 at 07:32:54PM +0100, Adam Kolany wrote:
> Michael Fuhr napisa?(a):
> >On Thu, Dec 15, 2005 at 11:03:55AM +0000, Adam Kolany wrote:
> >>stoff=> select sum(wplyw),sum(wydatek), sum(wplyw)-sum(wydatek) from
> >>rachunki where okres<6;
> >> sum  |   sum   | ?column? 
> >>-------+---------+----------
> >>73745 | 6712.55 |  67032.5
> >>(1 row)
> >
> >What data types are wplyw and wydatek?
> >
> they were float(2)
> 
> casting them into numeric helped, so I have changed the types of the to 
> numeric, instead of float(2)
> 
> this is however a bug, I think.

float(2) gives you a real, aka float4 (32-bit floating point).  As
the documentation points out, that type has a precision of 6 decimal
digits; you seem to object to the rounding:

test=> SELECT 73745::float8 - 6712.55::float8;
 ?column? 
----------
 67032.45
(1 row)

test=> SELECT 67032.45::float4;
 float4  
---------
 67032.5
(1 row)

test=> SELECT 73745::float4 - 6712.55::float4;
 ?column? 
----------
  67032.5
(1 row)

That's arguably not a bug: you've requested a low-precision data
type so you have to expect discrepancies around that 6th digit of
precision.  If you need greater precision then use a double precision
(float8) type, or if you need exact precision (e.g., for handling
money) then use numeric.

-- 
Michael Fuhr

In response to

pgsql-bugs by date

Next:From: Jon KeatingDate: 2005-12-15 23:29:33
Subject: Re: BUG #2116: Searching text fields does not work in EUC_JP
Previous:From: Pierre GirardDate: 2005-12-15 20:56:27
Subject: Solaris cc compiler on amd: PostgreSQL does not have native spinlock support on this platform

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group