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

From Pedro Gimeno
Subject Re: Re: BUG #12885: The result of casting a double to an integer depends on the database version
Date
Msg-id 55159907.1060809@personal.formauri.es
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>)
List pgsql-bugs
Michael Paquier wrote, On 2015-03-27 13:32:

> Attached is a patch that adds nearest-to-even rounding for numeric
> when dscale = 0. This gives the following results with the previous
> query:

That produces some quite surprising results:

=# select '2.5'::numeric(9,0)::numeric(9,1),
         '2.25'::numeric(9,1)::numeric(9,2),
        '2.225'::numeric(9,2)::numeric(9,3),
       '2.2225'::numeric(9,3)::numeric(9,4),
      '2.22225'::numeric(9,4)::numeric(9,5);
 numeric | numeric | numeric | numeric | numeric
---------+---------+---------+---------+---------
     2.0 |    2.30 |   2.230 |  2.2230 | 2.22220
(1 row)

That rounding is inconsistent: since DEC_DIGITS is 4 by default, numbers
with a number of digits which is a multiple of 4 + 1 will be rounded to
nearest or even, and numbers with a different number of digits will be
rounded up on a tie. And I have not tested, but apparently when
DEC_DIGITS == 1 (array elements are single digits rather than packing
several) it always rounds down on a tie.

pgsql-bugs by date:

Previous
From: jurgen.soenen@gmail.com
Date:
Subject: BUG #12904: Installation postgres fails when Windows user name contains spaces
Next
From: Alejandro Guimaraens
Date:
Subject: problem with pgAdminIII from Spain