From: Slawomir Chodnicki <slawomir.chodnicki@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15925: Loss of precision converting money to numeric
Date: Fri, 26 Jul 2019 18:15:00 +0200
Message-ID: <A6E2C5F9-7437-41B1-9F18-4E6C35981F6B@gmail.com> (raw)
In-Reply-To: <12463.1564154254@sss.pgh.pa.us>
References: <15925-da9953e2674bb5c8@postgresql.org>
<12463.1564154254@sss.pgh.pa.us>
> Hmm, yeah, anything approaching INT64_MAX has a problem.
> The issue is that cash_numeric() does the equivalent of
>
> SELECT 9223372036854775807::numeric / 100::numeric;
>
> and if you try that by hand you indeed get
>
> 92233720368547758
>
> because select_div_scale() has decided that it need not produce
> any fractional digits. We can force its hand by making the input
> have the required number of fractional digits *before* dividing,
> which is a bit weird on its face but gets the job done, per the
> comment therein:
>
> * The result scale of a division isn't specified in any SQL standard. For
> * PostgreSQL we select a result scale that will give at least
> * NUMERIC_MIN_SIG_DIGITS significant digits, so that numeric gives a
> * result no less accurate than float8; but use a scale not less than
> * either input's display scale.
>
> (NUMERIC_MIN_SIG_DIGITS is 16, whence the problem for a 17-digit result.
> Maybe we should consider raising that, but I'm hesitant to consider such
> a far-reaching change just to make cash_numeric happy.)
>
> I intend to apply the attached patch.
Thanks Tom,
the response is illuminating. And a same-day patch is legendary.
Thank you for your work.
Slawo
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-bugs@postgresql.org
Cc: slawomir.chodnicki@gmail.com, tgl@sss.pgh.pa.us, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15925: Loss of precision converting money to numeric
In-Reply-To: <A6E2C5F9-7437-41B1-9F18-4E6C35981F6B@gmail.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