Rounding to even for numeric data type

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Rounding to even for numeric data type
Date: 2015-03-27 21:44:40
Message-ID: CAB7nPqRVCbvYAWL++vCfyzOUujEay21bcLbBk_Mb=+2JX7+e5A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi all,

A couple of days ago a bug has showed up regarding rounding of float here:
http://www.postgresql.org/message-id/flat/20150320194337(dot)2573(dot)72944(at)wrigleys(dot)postgresql(dot)org#20150320194337(dot)2573(dot)72944@wrigleys.postgresql.org
The result being that the version of rint() shipped in src/port was
not IEEE compliant when rounding to even (MSVC < 2013 at least using
it), leading to inconsistent results depending on if the platform uses
src/port's rint() or the platform's one.

During this thread, Tom has raised as well that rounding for numeric
is not that IEEE-compliant:
http://www.postgresql.org/message-id/22366.1427313454@sss.pgh.pa.us

For example:
=# SELECT round(2.5::numeric), round(1.5::numeric),
round(0.5::numeric), round(-2.5::numeric);
round | round | round | round
-------+-------+-------+-------
3 | 2 | 1 | -3
(1 row)
=# SELECT round(2.5::float), round(1.5::float), round(0.5::float),
round(-2.5::float);
round | round | round | round
-------+-------+-------+-------
2 | 2 | 0 | -2
(1 row)

It sounds appealing to switch the default behavior to something that
is more IEEE-compliant, and not only for scale == 0. Now one can argue
as well that changing the default is risky for existing applications,
or the other way around that other RDBMs (?) are more compliant than
us for their equivalent numeric data type, and people get confused
when switching to Postgres.
An idea, from Dean, would be to have a new specific version for
round() able to do compliant IEEE rounding to even as well...

Opinions?
Regards,
--
Michael

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andreas Karlsson 2015-03-27 23:14:50 Re: Index-only scans with btree_gist
Previous Message Dima Ivanovskiy 2015-03-27 21:41:42 Re[2]: [HACKERS] GSoC 2015: SP-GIST for geometrical objects