pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: John Dickson <jdickson@tnsi.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #5975: Incorrect result from mod function with cast
Date: Wed, 13 Apr 2011 13:55:00 -0400
Message-ID: <9075.1302717300@sss.pgh.pa.us> (raw)
In-Reply-To: <201104130030.p3D0UCSZ076614@wwwmaster.postgresql.org>
References: <201104130030.p3D0UCSZ076614@wwwmaster.postgresql.org>

"John Dickson" <jdickson@tnsi.com> writes:
> The following SQL (executed on 8.3.11) shows a result one higher than the
> maximum permissible remainder when combined with a cast:

> select 1129590 % 66,
>   cast(1129590 as numeric(21, 0)) % 66;

>  ?column? | ?column?
> ----------+----------
>         0 |       66

This is basically a roundoff issue.  It's fixed in 8.4 and up, as a
result of this patch:

Author: Tom Lane <tgl@sss.pgh.pa.us>
Branch: master Release: REL8_4_BR [a0fad9762] 2008-04-04 18:45:36 +0000

    Re-implement division for numeric values using the traditional "schoolbook"
    algorithm.  This is a good deal slower than our old roundoff-error-prone
    code for long inputs, so we keep the old code for use in the transcendental
    functions, where everything is approximate anyway.  Also create a
    user-accessible function div(numeric, numeric) to provide access to the
    exact result of trunc(x/y) --- since the regular numeric / operator will
    round off its result, simply computing that expression in SQL doesn't
    reliably give the desired answer.  This fixes bug #3387 and various related
    corner cases, and improves the usefulness of PG for high-precision integer
    arithmetic.

We felt at the time that it was inappropriate to back-patch such a
behavioral change, and I doubt that decision is going to change now.
I'd suggest considering an update to 8.4 if this is a critical issue
for you.

			regards, tom lane



view thread (2+ messages)

Message-ID: <9075.1302717300@sss.pgh.pa.us>
Permalink:  ../9075.1302717300@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9075.1302717300@sss.pgh.pa.us

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jdickson@tnsi.com
  Subject: Re: BUG #5975: Incorrect result from mod function with cast
  In-Reply-To: <9075.1302717300@sss.pgh.pa.us>
  References: <201104130030.p3D0UCSZ076614@wwwmaster.postgresql.org> <9075.1302717300@sss.pgh.pa.us>

* 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