|
Home > Archive > PostgreSQL Discussion > May 2005 > Re: Modulus operator returns negative values / numeric
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Modulus operator returns negative values / numeric
|
|
| Paul Tillotson 2005-05-26, 9:23 am |
| Tom Lane wrote:
>Paul Tillotson <pntil@shentel.net> writes:
>
>
>
>
>
>
>
>
>
>
>
>
>or (3) increase the calculation precision (rscale), as suggested by
>Alvaro's message.
>
>Possibly that cannot work, but I haven't seen a proof.
>
>
>
I don't think that will work. Before switching round_var() to
trunc_var() at the end of div_var(), I tried recompiling it to say
div_var(var1, var2, &tmp, rscale + 1);
instead of
div_var(var1, var2, &tmp, rscale);
Around line 4129 in mod_var(). (Which would perform the division with
one extra decimal place when calculating a modulus.) It fixed the case
that Alvaro used as a test, but I was still able to get a negative
modulus by trying other values.
I think that adding digits to rscale will cause the negative modulus to
become more rare, but it will always be possible to do get it. For
example, 12345678901234567898
0 / 123 is
100371373180768844.6341 (rounded to 4 decimal places.)
If you divide with no extra decimal places you get 45 at the tens' and
ones' digits. If you divide with one extra decimal place, you get 44.6,
which is truncated to 44.
But suppose that dividing that gave you
100371373180768844.999997
In that case, you would need to work it to at least 6 extra places
before truncation would actually give you the expected 44 rather than
45, because even when working it to 5 decimal places, the carry
propagation would eventually carry into the ones digit, changing the 4
to a 5.
In other words, no arbitrary number of extra decimal places when calling
div_var() will be always sufficient to prevent rounding up at some other
decimal place.
>
>I cannot believe that that won't create problems at least as bad as it
>solves. Have you even tried the regression tests on this?
>
> regards, tom lane
>
>
>
>
<sheepish grin> No. Can you tell me how to do that?
Paul
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match
| |
| Alvaro Herrera 2005-05-26, 11:23 am |
| On Thu, May 26, 2005 at 08:56:34AM -0400, Paul Tillotson wrote:
> Tom Lane wrote:
>
[color=darkred]
> In other words, no arbitrary number of extra decimal places when calling
> div_var() will be always sufficient to prevent rounding up at some other
> decimal place.
No, an arbitrary number won't do. I found I could make it work by
adding as much extra decimals as digits in the divisor. At least it
worked for these test cases I made up. (Attached)
[color=darkred]
> <sheepish grin> No. Can you tell me how to do that?
make installcheck in src/test/regress
--
Alvaro Herrera (<alvherre[a]surnet.cl> )
"Cómo ponemos nuestros dedos en la arcilla del otro. Eso es la amistad; jugar
al alfarero y ver qué formas se pueden sacar del otro" (C. Halloway en
La Feria de las Tinieblas, R. Bradbury)
|
|
|
|
|