From: Nathan M. Davalos <n.davalos@sharedmarketing.com>
To: pgsql-bugs@postgresql.org
Subject: Function trunc() behaves in unexpected manner with different data types
Date: Thu, 24 Feb 2011 13:01:21 -0600
Message-ID: <2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.com> (raw)
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
select trunc(((cast(2183.68 as numeric) - cast(1 as double precision)
))*100) /100
yields 2184.67
select trunc(cast(2184.68 as double precision) *100)
yields 218467 instead of 218468
This only happens on certain ranges of numbers. Doing the same thing
with the number 3183.68 yields the same result in both cases. It only
appears to happen when a number is declared as a double and there is no
number past the last significant digit or the number past the last
significant digit is a zero AND falls within a certain range of numbers.
For instance select trunc(cast(2184.681 as double precision) *100)
yields 218468, but select trunc(cast(2184.680 as double precision) *100)
yields 218467.
I already made sure everything we're using is just defined as numeric to
avoid the issue.
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: n.davalos@sharedmarketing.com
Subject: Re: Function trunc() behaves in unexpected manner with different data types
In-Reply-To: <2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.com>
References: <2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.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