## Function trunc() behaves in unexpected manner with different data types

From: "Nathan M(dot) Davalos" Function trunc() behaves in unexpected manner with different data types 2011-02-24 19:01:21 2701CF596B80DC44815FDBFFF5881A1E0104BB63@exchange01.sharedmarketing.com (view raw or whole thread) 2011-02-24 19:01:21 from "Nathan M(dot) Davalos"  2011-02-24 19:31:22 from Merlin Moncure   2011-02-25 02:03:40 from Greg Stark    2011-02-25 14:46:29 from Merlin Moncure     2011-02-25 15:21:25 from Tom Lane      2011-02-25 15:28:25 from Merlin Moncure       2011-02-25 15:40:09 from Alvaro Herrera        2011-02-25 15:48:50 from Merlin Moncure         2011-02-25 16:30:08 from Merlin Moncure pgsql-bugs
```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.

```

### pgsql-bugs by date

 Next: From: Merlin Moncure Date: 2011-02-24 19:31:22 Subject: Re: Function trunc() behaves in unexpected manner with different data types Previous: From: Ross Barrett Date: 2011-02-24 16:36:34 Subject: BUG #5899: Memory corruption when running psql