pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: levi@leviaul.com
Subject: BUG #16919: Unexpected precision loss in NUMERIC type during text cast, math operations
Date: Wed, 10 Mar 2021 20:41:32 +0000
Message-ID: <16919-64fff49d97341054@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      16919
Logged by:          Levi Aul
Email address:      levi@leviaul.com
PostgreSQL version: 13.2
Operating system:   Ubuntu 20.04.2 (x86_64), Linux kernel 5.4.0
Description:        

Simple operations on NUMERIC, supposedly an arbitrary-precision type, are
having unexpected results for me:

SELECT     '12345678901234567890123456789012345678901234567890'::numeric;
-- expected 12345678901234567890123456789012345678901234567890
--      got 12345678901234567890123456789012345678900000000000

SELECT (('123456789012345678901234567890'::numeric) *
('100000000000000000000'::numeric)) + ('12345678901234567890'::numeric);
-- expected 12345678901234567890123456789012345678901234567890
--      got 12345678901234567890123456789012345678900000000000

Correct results can be attained by replacing NUMERIC in the above with
pgmp's mpz type.

Also, this precision loss doesn't occur with the internal _numeric
array-member type:

SELECT    
'{12345678901234567890123456789012345678901234567890}'::numeric[];
-- expected {12345678901234567890123456789012345678901234567890}
--      got {12345678901234567890123456789012345678901234567890}

I've verified with debug_print_parse that nothing strange is going on with
implicit casts overriding the defaults; the native function numeric_in is
being called here. (And even if it weren't, having an overridden (text TO
numeric) cast wouldn't also be affecting the math-op result above, as it
operates upon apparently-lossless inputs while still achieving a lossy
result.)



view thread (4+ messages)  latest in thread

Message-ID: <16919-64fff49d97341054@postgresql.org>
Permalink:  ../16919-64fff49d97341054@postgresql.org/
Also on:    postgresql.org/message-id/16919-64fff49d97341054@postgresql.org

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, levi@leviaul.com
  Subject: Re: BUG #16919: Unexpected precision loss in NUMERIC type during text cast, math operations
  In-Reply-To: <16919-64fff49d97341054@postgresql.org>

* 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