Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 3976E1337BC8 for ; Thu, 24 Feb 2011 15:31:30 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 06193-03 for ; Thu, 24 Feb 2011 19:31:22 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-qw0-f46.google.com (mail-qw0-f46.google.com [209.85.216.46]) by mail.postgresql.org (Postfix) with ESMTP id 99F431337B97 for ; Thu, 24 Feb 2011 15:31:22 -0400 (AST) Received: by qwd7 with SMTP id 7so703598qwd.19 for ; Thu, 24 Feb 2011 11:31:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=PAq/60r2LRNuX7YS8k7eg/xB50DmtSnllMzjAWIQ568=; b=js6F//U4etV4cVdCM4i0DsHlnkk+iqao2AaZXZprUhmMAydUB5z/Gr83aANLhLs5xr yxDYp1nKedDUIbJpnXR7m/QGjVhKK5VqHZJhWLSFlanTEnheCWqgfk9beAe1kY55//J7 LPMn2C6IIziAd8wzApQf93RavQfe/b1yydubQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=Gxv9SiSRXGAIxsJ9d4LUZXsGSe6mnqHXBRVJSCTa5vaWjeqkIMjecTpVmYgBwp+DGP dgtDIRoUIDxJqiXCPwWAiVgkQMvwO1wPxsGj00YXKoPxG4ypwe5SXxmr72FmxHHBPTHq IS8l0XXpBymsIB17vYj9VYBkCTT6z7Os6vzR0= MIME-Version: 1.0 Received: by 10.229.75.18 with SMTP id w18mr1084861qcj.95.1298575882264; Thu, 24 Feb 2011 11:31:22 -0800 (PST) Received: by 10.229.225.212 with HTTP; Thu, 24 Feb 2011 11:31:22 -0800 (PST) In-Reply-To: <2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.com> References: <2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.com> Date: Thu, 24 Feb 2011 13:31:22 -0600 Message-ID: Subject: Re: Function trunc() behaves in unexpected manner with different data types From: Merlin Moncure To: "Nathan M. Davalos" Cc: pgsql-bugs@postgresql.org Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201102/206 X-Sequence-Number: 29478 On Thu, Feb 24, 2011 at 1:01 PM, Nathan M. Davalos wrote: > I ran into something interesting with using trunc() and different data > types: > > The following is a simplified from the statement we=92re using and produc= es > the same results: > > select trunc( ((cast(2183.68 as numeric) - cast(1 as numeric)) )*100) /10= 0 > > =A0=A0yields 2184.68 the root issue I think here is that the string version of the double precision math is approximated: postgres=3D# create table v as select floor(2183.68::float8 * 100) as v; postgres=3D# select * from v; v -------- 218367 (1 row) postgres=3D# select floor(v) from v; floor -------- 218367 postgres=3D# insert into v select 218368; INSERT 0 1 (1 row) postgres=3D# select distinct v from v; v -------- 218368 218368 (2 rows) As you can see, even though the string versions are the same, the internal representation is different. You could dump the data and restore it and get different results. Also the text/binary protocols would send different data to the client. I don't know if this is a bug in postgresql floating point implementation or not: i think the backend would either have to print 218367.999999999999999ish number or spend the time to look for these cases and round them internally. Floating point is a headache :-). merlin