Re: convert real to numeric.

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: convert real to numeric.
Дата
Msg-id 30660.1539903493@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: convert real to numeric.  ("Peter J. Holzer" <hjp-pgsql@hjp.at>)
Ответы Re: convert real to numeric.  ("Peter J. Holzer" <hjp-pgsql@hjp.at>)
Список pgsql-general
"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


В списке pgsql-general по дате отправления:

Предыдущее
От: Steve Crawford
Дата:
Сообщение: Re: PGDG status and policy
Следующее
От: Mehman Jafarov
Дата:
Сообщение: Problem about partitioned table