Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hr34q-0005U8-Vf for pgsql-bugs@arkaria.postgresql.org; Fri, 26 Jul 2019 16:28:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hr34p-0005Hb-DU for pgsql-bugs@arkaria.postgresql.org; Fri, 26 Jul 2019 16:28:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hr2s8-0005qf-4k for pgsql-bugs@lists.postgresql.org; Fri, 26 Jul 2019 16:15:08 +0000 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hr2s6-0007fm-5J for pgsql-bugs@lists.postgresql.org; Fri, 26 Jul 2019 16:15:07 +0000 Received: by mail-wr1-x42b.google.com with SMTP id 31so55061760wrm.1 for ; Fri, 26 Jul 2019 09:15:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=1/j6Ub5KL1zSL+HUVhZDSZYHGkXyy94ep+wYzPVBUxc=; b=rR4medovGV9fRcWYMflTlfAzx94w6TNwjzyuXbJs61EUddFvgW6caqPC2ctv+wY0Ke bGxokib/S2bXWA4vo9xEmciQ7oCI42h899nPuboagcpAY1r4ZfOYQpMrTPI5547DDDZf 8NX7ktZrFp2Il+WiYUx6i1kL4XBWWSmPNTeaS3sR9Bm9CpSotp/OaRnZhpAoYsLs4hhI f2QqiJDpYXlV75NJJO8H7nRJjrTE7M0kozSh8DCzArr1mpX5fC5AF9YVxrz+1Fx4hbNW +X5ZeWeC3dIRda/EViVPvBe1AaFminRLHDWJG+Z6W+AzkbK5yGywZ+ycvzVF2WTaCU8f r2HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=1/j6Ub5KL1zSL+HUVhZDSZYHGkXyy94ep+wYzPVBUxc=; b=A3dN03n/Ecf0Z/3qPV/U9LxpwdjyveJ64XZLmMdX2SjcnwiEyE2ERWYXPgYP5JN5SX 1NSliAdf4C4QtaPMP5buotV7VOgbWZak8ZO0XZIVJ/x6dhBEjVgutj2bcIUDAFR1DYyn ToX8LPfTZ19uqH34+yu/g785bfDduZSGeSrKX7Gjwtrk2hHqK5yXkH2SLiAIo9+2MwSq TsDCtk4ndUWCP6Lmd+wHcadFpa7lvstZrCrUF65W6eLE1vQuUrvEGSyXRKRoLLgC9xKr yXZrhqK9XdhNr2WVJg+oZ/ywdVDmkN/r1/lOG00Mwsi35PG/eBCXzy/FN7hswoYyzSCX N3LA== X-Gm-Message-State: APjAAAVwA0419hjRhbo54kZQ0FKrLZ4RcffW9ZBYeA1OrRNQaG5lt+vo wqEvu1O4QjUe5jm8He9fxv8= X-Google-Smtp-Source: APXvYqyqLHS0yGy7sJfRQQhhy25J9jyWmTdLTowXUz8fozJ5UJ89c58LjC67/LRrb2h4sGTuHnCu1Q== X-Received: by 2002:a5d:4205:: with SMTP id n5mr94467985wrq.52.1564157702459; Fri, 26 Jul 2019 09:15:02 -0700 (PDT) Received: from kermit.fritz.box (p578D2521.dip0.t-ipconnect.de. [87.141.37.33]) by smtp.gmail.com with ESMTPSA id y16sm110187812wrg.85.2019.07.26.09.15.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Jul 2019 09:15:01 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: BUG #15925: Loss of precision converting money to numeric From: Slawomir Chodnicki In-Reply-To: <12463.1564154254@sss.pgh.pa.us> Date: Fri, 26 Jul 2019 18:15:00 +0200 Cc: pgsql-bugs@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <15925-da9953e2674bb5c8@postgresql.org> <12463.1564154254@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > Hmm, yeah, anything approaching INT64_MAX has a problem. > The issue is that cash_numeric() does the equivalent of >=20 > SELECT 9223372036854775807::numeric / 100::numeric; >=20 > and if you try that by hand you indeed get >=20 > 92233720368547758 >=20 > 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: >=20 > * 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. >=20 > (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.) >=20 > 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