pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Allan Engelhardt <allane@cybaea.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: lattim@hotmail.com
Subject: Re: ROUND function ??
Date: Wed, 10 Oct 2001 00:16:42 +0100
Message-ID: <3BC3855A.9909FE71@cybaea.com> (raw)
References: <21186.1002602561@sss.pgh.pa.us>

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 slightly confusing that this is implemented in the math library (libc) by the  rint  function (subject to compiler defaults and  fesetround  calls) while the  round  functions ((ll|l)?round(f|l)?) of the library does indeed 'round half-way cases 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 representable in T such
         that the absolute value of the difference between N and the nu-
         merical value of V is not 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.



view thread (4+ messages)

Message-ID: <3BC3855A.9909FE71@cybaea.com>
Permalink:  ../3BC3855A.9909FE71@cybaea.com/
Also on:    postgresql.org/message-id/3BC3855A.9909FE71@cybaea.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: allane@cybaea.com, tgl@sss.pgh.pa.us, lattim@hotmail.com
  Subject: Re: ROUND function ??
  In-Reply-To: <3BC3855A.9909FE71@cybaea.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox