Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 0BDA81337B94 for ; Thu, 24 Feb 2011 15:13:42 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 60283-01 for ; Thu, 24 Feb 2011 19:13:34 +0000 (UTC) X-Greylist: delayed 00:12:10.79011 by SQLgrey-1.7.6 Received: from exchange01.sharedmarketing.com (exchange01.sharedmarketing.com [67.217.132.12]) by mail.postgresql.org (Postfix) with ESMTP id 179A81337B90 for ; Thu, 24 Feb 2011 15:13:33 -0400 (AST) Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01CBD455.3A12F31E" X-MimeOLE: Produced By Microsoft Exchange V6.5 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> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Function trunc() behaves in unexpected manner with different data types thread-index: AcvUVTnQoURIpHAXRgyCkKNzyL27nA== From: "Nathan M. Davalos" To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.909 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/205 X-Sequence-Number: 29477 This is a multi-part message in MIME format. ------_=_NextPart_001_01CBD455.3A12F31E Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable 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: =20 select trunc( ((cast(2183.68 as numeric) - cast(1 as numeric)) )*100) /100=20 yields 2184.68 =20 select trunc(((cast(2183.68 as numeric) - cast(1 as double precision) ))*100) /100 yields 2184.67 =20 select trunc(cast(2184.68 as double precision) *100)=20 yields 218467 instead of 218468 =20 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. =20 I already made sure everything we're using is just defined as numeric to avoid the issue. ------_=_NextPart_001_01CBD455.3A12F31E Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

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.

------_=_NextPart_001_01CBD455.3A12F31E--