Re: stddev returns 0 when there is one row

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Douglas Trainor <trainor(at)uic(dot)edu>
Cc: Joe Conway <mail(at)joeconway(dot)com>, Manfred Koizar <mkoi-pg(at)aon(dot)at>, Bruno Wolff III <bruno(at)wolff(dot)to>, pgsql-general(at)postgresql(dot)org
Subject: Re: stddev returns 0 when there is one row
Date: 2003-04-21 01:34:10
Message-ID: 21603.1050888850@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Douglas Trainor <trainor(at)uic(dot)edu> writes:
> For example, both of these calculations produce answers of 0 (zero)
> but they should produce answers of 1 (one):

> =stdev(80000000,80000001,80000002)
> =var(80000000,80000001,80000002)

Looks like roundoff error to me. That's pushing the limit of what you
can hope to do in float8 math. Postgres gets the right answer with
NUMERIC data, but not with FLOAT8:

regression=# create table foo (f1 float8, f2 numeric, f3 int);
CREATE TABLE
regression=# insert into foo values(80000000, 80000000, 80000000);
INSERT 291676 1
regression=# insert into foo values(80000001, 80000001, 80000001);
INSERT 291677 1
regression=# insert into foo values(80000002, 80000002, 80000002);
INSERT 291678 1
regression=# select * from foo;
f1 | f2 | f3
----------+----------+----------
80000000 | 80000000 | 80000000
80000001 | 80000001 | 80000001
80000002 | 80000002 | 80000002
(3 rows)

regression=# select stddev(f1), variance(f1) from foo;
stddev | variance
------------------+------------------
1.15470053837925 | 1.33333333333333
(1 row)

regression=# select stddev(f2), variance(f2) from foo;
stddev | variance
------------------------+------------------------
1.00000000000000000000 | 1.00000000000000000000
(1 row)

regression=# select stddev(f3), variance(f3) from foo;
stddev | variance
------------------------+------------------------
1.00000000000000000000 | 1.00000000000000000000
(1 row)

(The integer case uses NUMERIC arithmetic under the hood.)

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-04-21 01:53:20 Re: [SQL] Yet Another (Simple) Case of Index not used
Previous Message Douglas Trainor 2003-04-21 01:00:16 Re: stddev returns 0 when there is one row