BUG #1577: round(dp) function does not round to nearest integer properly

Started by Roman Schayukabout 21 years ago4 messagesbugs
Jump to latest
#1Roman Schayuk
rschayuk@rogers.com

The following bug has been logged online:

Bug reference: 1577
Logged by: Roman Schayuk
Email address: rschayuk@rogers.com
PostgreSQL version: 7.2.2
Operating system: RedHat Linux 8.0 x86
Description: round(dp) function does not round to nearest integer
properly
Details:

SQL command:
gap=> select round(42.5);
round
-------
42
(1 row)
Result has to be 43

In contrast round(numeric, int) function works properly:
gap=> select round(42.5,0);
round
-------
43
(1 row)

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Roman Schayuk (#1)
Re: BUG #1577: round(dp) function does not round to nearest integer properly

"Roman Schayuk" <rschayuk@rogers.com> writes:

gap=> select round(42.5);
round
-------
42
(1 row)
Result has to be 43

No, the above is perfectly correct and in fact required by the IEEE
standard for floating-point arithmetic (because round-to-nearest-even
is the default rounding mode).

If anything needs to be changed here, it's the behavior of round(numeric).

regards, tom lane

#3Russell Smith
russell@pws.com.au
In reply to: Tom Lane (#2)
Re: BUG #1577: round(dp) function does not round to nearest integer properly

On Mon, 11 Apr 2005 05:57 am, Tom Lane wrote:

"Roman Schayuk" <rschayuk@rogers.com> writes:

gap=> select round(42.5);
round
-------
42
(1 row)
Result has to be 43

No, the above is perfectly correct and in fact required by the IEEE
standard for floating-point arithmetic (because round-to-nearest-even
is the default rounding mode).

That is rounding of unrepresentable numbers, is it not? at least
http://www.ee.ucla.edu/~vandenbe/103/flpt.pdf suggests that to me as I couldn't find
the IEEE spec about this. I'm sure somebody has it at hand, but I don't.

Either way 8.0.1 round differently;

test=# select version();
version
--------------------------------------------------------------------------------------------------------------------------
PostgreSQL 8.0.1 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3.2 20031218 (Gentoo Linux 3.3.2-r5, propolice-3.3-7)
(1 row)

test=# select round(42.5);
round
-------
43
(1 row)

test=# select round(42.5,0);
round
-------
43
(1 row)

If anything needs to be changed here, it's the behavior of round(numeric).

So I would say that the round behaviour has changed as Tom has suggested, but which is correct?

7.2, or 8.0?

Regards

Russell Smith

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Russell Smith (#3)
Re: BUG #1577: round(dp) function does not round to nearest integer properly

Russell Smith <russell@pws.com.au> writes:

Either way 8.0.1 round differently;

The rounding is the same. The difference from 7.2 is that the unadorned
literal "42.5" is now considered to be numeric whereas 7.2 treated it as
float8. This changed quite some time ago (in 7.3 it looks like).

regards, tom lane