Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version - Mailing list pgsql-bugs

From Michael Paquier
Subject Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
Date
Msg-id CAB7nPqRq94VT5WiYyCjSS+E_=mwWL49W-MXv5qgyR-8S91YY-w@mail.gmail.com
Whole thread Raw
In response to Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version  (Michael Paquier <michael.paquier@gmail.com>)
Responses Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version  (Dean Rasheed <dean.a.rasheed@gmail.com>)
Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version  (Pedro Gimeno <pgsql-004@personal.formauri.es>)
List pgsql-bugs
On Thu, Mar 26, 2015 at 11:15 AM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> On Thu, Mar 26, 2015 at 11:12 AM, Andrew Gierth wrote:
>>>>>>> "Michael" == Michael Paquier writes:
>>
>>  Michael> Indeed...
>>  Michael> =# select column1 AS double_value, cast(column1 AS INT) AS
>>  Michael> int_value
>>
>> The complication for numeric is that there's also the case of round(x,n)
>> and casting to numeric(m,n) to consider.
>
> OK, thanks for the reminder... I forgot this case.

Attached is a patch that adds nearest-to-even rounding for numeric
when dscale = 0. This gives the following results with the previous
query:
=# select column1 AS double_value, cast(column1 AS INT) AS int_value
                                                          FROM (VALUES
(-2.5::numeric),

(-1.5::numeric),

                       (-0.5::numeric),

                                              (0.5::numeric),


(1.5::numeric),

                        2.5::numeric)) t;
 double_value | int_value
--------------+-----------
         -2.5 |        -2
         -1.5 |        -2
         -0.5 |         0
          0.5 |         0
          1.5 |         2
          2.5 |         2
(6 rows)

For the case where dscale != 0 case, what would we actually want? I
have been staring at the code around round_var for some time but I
could not really get if we'd really want to change something there...
For example round(2.5::numeric, 1) should be 2.4 and not 2.5?
Regards.
--
Michael

Attachment

pgsql-bugs by date:

Previous
From: Jon Nelson
Date:
Subject: Re: minor: contrib/btree_gin/btree_gin.c uses DirectFunctionCall3(inet_in,..)
Next
From: Dean Rasheed
Date:
Subject: Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version