From: Tom Lane <tgl@sss.pgh.pa.us>
To: Peter J. Holzer <hjp-pgsql@hjp.at>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: convert real to numeric.
Date: Thu, 18 Oct 2018 18:58:13 -0400
Message-ID: <30660.1539903493@sss.pgh.pa.us> (raw)
In-Reply-To: <20181018211328.jg373dpwc3dczhyo@hjp.at>
References: <CAM9F+O2m9nV7F95P3g5ymmxTTvdiB-s2F1KJqVZTKjE0ZvW2uw@mail.gmail.com>
<23387.1539872140@sss.pgh.pa.us>
<20181018211328.jg373dpwc3dczhyo@hjp.at>
"Peter J. Holzer" <hjp-pgsql@hjp.at> writes:
> On 2018-10-18 10:15:40 -0400, Tom Lane wrote:
>> You could ju-jitsu the system into duplicating that behavior by casting
>> to text (which invokes float4out) and then to numeric:
> I suggest casting first to float8 and then to numeric. The conversion
> from float4 to float8 is exact, and any rounding error introduced by the
> float8->numeric conversion is certainly much smaller than the
> uncertainty of the original float4 value.
I do not think that the OP will care for the results of that. The problem
is that now the output function will think that the result is worth
printing to 16 digits, and the last ten or so of those will be garbage.
As an example, even though the cited value happens to work nicely:
regression=# select '17637.75'::float4::float8;
float8
----------
17637.75
(1 row)
nearby ones don't:
regression=# select '17637.74'::float4::float8;
float8
-----------------
17637.740234375
(1 row)
Yeah, in some sense that's a valid representation of the stored float4,
but it likely has little to do with the originally presented value.
regards, tom lane
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, hjp-pgsql@hjp.at
Subject: Re: convert real to numeric.
In-Reply-To: <30660.1539903493@sss.pgh.pa.us>
References: <CAM9F+O2m9nV7F95P3g5ymmxTTvdiB-s2F1KJqVZTKjE0ZvW2uw@mail.gmail.com> <23387.1539872140@sss.pgh.pa.us> <20181018211328.jg373dpwc3dczhyo@hjp.at> <30660.1539903493@sss.pgh.pa.us>
* 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