Thread: pgsql: Fix NUMERIC modulus to properly truncate division in computation.

pgsql: Fix NUMERIC modulus to properly truncate division in computation.

From
momjian@svr1.postgresql.org (Bruce Momjian)
Date:
Log Message:
-----------
Fix NUMERIC modulus to properly truncate division in computation.
Division rounding was causing incorrect results.  Test case:

    test=> SELECT 12345678901234567890 % 123;
     ?column?
    ----------
           78
    (1 row)

Was returning -45.

Modified Files:
--------------
    pgsql/src/backend/utils/adt:
        numeric.c (r1.83 -> r1.84)
        (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/adt/numeric.c.diff?r1=1.83&r2=1.84)

Re: pgsql: Fix NUMERIC modulus to properly truncate

From
Christopher Kings-Lynne
Date:
Is this a backport?

Bruce Momjian wrote:
> Log Message:
> -----------
> Fix NUMERIC modulus to properly truncate division in computation.
> Division rounding was causing incorrect results.  Test case:
>
>     test=> SELECT 12345678901234567890 % 123;
>      ?column?
>     ----------
>            78
>     (1 row)
>
> Was returning -45.
>
> Modified Files:
> --------------
>     pgsql/src/backend/utils/adt:
>         numeric.c (r1.83 -> r1.84)
>         (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/adt/numeric.c.diff?r1=1.83&r2=1.84)
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
>       subscribe-nomail command to majordomo@postgresql.org so that your
>       message can get through to the mailing list cleanly

Re: pgsql: Fix NUMERIC modulus to properly truncate

From
Bruce Momjian
Date:
Christopher Kings-Lynne wrote:
> Is this a backport?

No, I don't think so.  It doesn't seem to be something that enough
people use to risk the change in behavior --- it might break something
that was working.  But, if folks want it backported we can do it.  It is
only a change to properly do modulus for numeric.

---------------------------------------------------------------------------


>
> Bruce Momjian wrote:
> > Log Message:
> > -----------
> > Fix NUMERIC modulus to properly truncate division in computation.
> > Division rounding was causing incorrect results.  Test case:
> >
> >     test=> SELECT 12345678901234567890 % 123;
> >      ?column?
> >     ----------
> >            78
> >     (1 row)
> >
> > Was returning -45.
> >
> > Modified Files:
> > --------------
> >     pgsql/src/backend/utils/adt:
> >         numeric.c (r1.83 -> r1.84)
> >         (http://developer.postgresql.org/cvsweb.cgi/pgsql/src/backend/utils/adt/numeric.c.diff?r1=1.83&r2=1.84)
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 3: if posting/reading through Usenet, please send an appropriate
> >       subscribe-nomail command to majordomo@postgresql.org so that your
> >       message can get through to the mailing list cleanly
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: pgsql: Fix NUMERIC modulus to properly truncate

From
Christopher Kings-Lynne
Date:
  > No, I don't think so.  It doesn't seem to be something that enough
> people use to risk the change in behavior --- it might break something
> that was working.  But, if folks want it backported we can do it.  It is
> only a change to properly do modulus for numeric.

Well, from my point of view it's an absolute mathematical error - i'd
backport it.  I can't see anyone relying on it :)

Chris

Re: pgsql: Fix NUMERIC modulus to properly truncate

From
Tom Lane
Date:
Christopher Kings-Lynne <chriskl@familyhealth.com.au> writes:
>> No, I don't think so.  It doesn't seem to be something that enough
>> people use to risk the change in behavior --- it might break something
>> that was working.  But, if folks want it backported we can do it.  It is
>> only a change to properly do modulus for numeric.

> Well, from my point of view it's an absolute mathematical error - i'd
> backport it.  I can't see anyone relying on it :)

Doesn't this patch break the basic theorem that

    a = trunc(a / b) * b + (a mod b)

?  If division rounds and mod doesn't, you've got pretty serious issues.

            regards, tom lane