From: Merlin Moncure <mmoncure@gmail.com>
To: Nathan M. Davalos <n.davalos@sharedmarketing.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Function trunc() behaves in unexpected manner with different data types
Date: Thu, 24 Feb 2011 13:31:22 -0600
Message-ID: <AANLkTimWoBDGLN282PLRkg2eCuZTS6KJCEH_m0uQHrb-@mail.gmail.com> (raw)
In-Reply-To: <2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.com>
References: <2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.com>
On Thu, Feb 24, 2011 at 1:01 PM, Nathan M. Davalos
<n.davalos@sharedmarketing.com> wrote:
> I ran into something interesting with using trunc() and different data
> types:
>
> The following is a simplified from the statement we’re using and produces
> the same results:
>
> select trunc( ((cast(2183.68 as numeric) - cast(1 as numeric)) )*100) /100
>
> yields 2184.68
the root issue I think here is that the string version of the double
precision math is approximated:
postgres=# create table v as select floor(2183.68::float8 * 100) as v;
postgres=# select * from v;
v
--------
218367
(1 row)
postgres=# select floor(v) from v;
floor
--------
218367
postgres=# insert into v select 218368;
INSERT 0 1
(1 row)
postgres=# 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: mmoncure@gmail.com, n.davalos@sharedmarketing.com
Subject: Re: Function trunc() behaves in unexpected manner with different data types
In-Reply-To: <AANLkTimWoBDGLN282PLRkg2eCuZTS6KJCEH_m0uQHrb-@mail.gmail.com>
References: <2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.com> <AANLkTimWoBDGLN282PLRkg2eCuZTS6KJCEH_m0uQHrb-@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox