Re: ROUND function ?? - Mailing list pgsql-sql

From Allan Engelhardt
Subject Re: ROUND function ??
Date
Msg-id 3BC3855A.9909FE71@cybaea.com
Whole thread Raw
In response to Re: ROUND function ??  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
Tom Lane wrote:

> [...]Because the IEEE float math standard says so.  Round-to-nearest-even
> is considered good practice.

I learn something new every day.  :-)

While it is true that IEEE 754 defaults to "round-to-nearest", which means rounding midway points to even, it is
slightlyconfusing that this is implemented in the math library (libc) by the  rint  function (subject to compiler
defaultsand  fesetround  calls) while the  round  functions ((ll|l)?round(f|l)?) of the library does indeed 'round
half-waycases away from zero to the nearest integer', which is also the mathematical behavior.  Try
 
   % info libc Arithmetic Rounding   % info libc Arithmetic 'Arithmetic Functions' 'Rounding Functions'

on you local friendly U*ix clone and you shall find enlightenment...


The SQL standard seems to leave it implementation dependent ('92, sec 4.4.1):

        An approximation obtained by rounding of a numerical value N for        an <exact numeric type> T is a value V
representablein T such        that the absolute value of the difference between N and the nu-        merical value of V
isnot greater than half the absolute value        of the difference between two successive numerical values repre-
 sentable in T. If there are more than one such values V, then it is        implementation-defined which one is taken.
 


So PostgreSQL does "the right thing" (again!).


Allan.


pgsql-sql by date:

Previous
From: "Josh Berkus"
Date:
Subject: Re: Loading current_user and current_timestamp using COPY
Next
From: Gerardo Perosio
Date:
Subject: SELECT FOR UPDATE CLAUSE