X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 173D69DC81A for ; Sun, 1 Jan 2006 21:27:47 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 79370-08 for ; Sun, 1 Jan 2006 21:27:50 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id A89E59DC80E for ; Sun, 1 Jan 2006 21:27:44 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id k021Rg0W012882 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 1 Jan 2006 18:27:45 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id k021RflI070314; Sun, 1 Jan 2006 18:27:41 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id k021RfC1070313; Sun, 1 Jan 2006 18:27:41 -0700 (MST) (envelope-from mfuhr) Date: Sun, 1 Jan 2006 18:27:41 -0700 From: Michael Fuhr To: George Pavlov Cc: pgsql-sql@postgresql.org Subject: Re: avg() with floating-point types Message-ID: <20060102012741.GA70122@winnie.fuhr.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.065 required=5 tests=[AWL=0.065] X-Spam-Score: 0.065 X-Spam-Level: X-Archive-Number: 200601/6 X-Sequence-Number: 23795 On Sun, Jan 01, 2006 at 04:40:18PM -0800, George Pavlov wrote: > The datatype of both city.latitude and postal_code.latitude is > number(16,12). > > This works, but I would like to understand why there is sometimes a > discrepancy between avg(pc.latitude) and what actually gets inserted > into the city table -- is it the usual floating-point discrepancy or is > there something I can do about it? E.g. after the above update: You've declared the numeric column as (16,12) so the inserted values are rounded to the 12th decimal place. Notice that that's where the values start to differ: > latitude | avg | diff > -----------------+---------------------+--------------------- > 36.709374333333 | 36.7093743333333333 | -0.0000000000003333 Is such a difference significant to your application? The distance discrepancy is on the order of tens of nanometers, which seems absurdly precise. With lat/lon coordinates five places after the decimal point is about meter-precision, which is probably more than adequate for whatever you're doing. Incidentally, if you're doing anything geospatial then you might want to check out PostGIS: http://www.postgis.org/ If you look around you should be able to find shapefiles with points or polygons for cities and postal codes. -- Michael Fuhr