Re: floating point output

From: Luca Ferrari <fluca1978(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Rob Sargent <robjsargent(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: floating point output
Date: 2019-09-05 06:48:11
Message-ID: CAKoxK+53nMQbj4ZdxzMjtXHZa-Uac+kEGnmVduic8mVWi0EjrQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Sep 5, 2019 at 6:14 AM Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> wrote:
> Some examples would help explain your concerns.

I guess the problem is with extra_float_digits. If greater then zero,
the string representation of a real is at its minimum, that is only
significative digits are there:

restoredb=# set extra_float_digits to 1;
SET
restoredb=# select 2.001230::real;
float4
---------
2.00123
(1 row)

If lower than zero, the output switch to "precise" mode that is
extra_float_digits are subtracted from the number of available digits
for a number:

restoredb=# set extra_float_digits to -2;
SET
restoredb=# select 2.001230::real;
float4
--------
2.001
(1 row)

restoredb=# set extra_float_digits to -3;
SET
restoredb=# select 2.001230::real;
float4
--------
2
(1 row)

However, this has some more obscure to me behaviors when the value is
greater than 1:

restoredb=# set extra_float_digits to 1;
SET
restoredb=# select 2.1::real;
float4
--------
2.1
(1 row)

restoredb=# set extra_float_digits to 2;
SET
restoredb=# select 2.1::real;
float4
-----------
2.0999999
(1 row)

>
> Bottom line, if you want precision use numeric.

Yeah, totally agree. I would also add that numeric is a little more documented.

Luca

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sonam Sharma 2019-09-05 08:53:01 Uninstall postgres
Previous Message Luca Ferrari 2019-09-05 06:33:07 Re: PG11.2 - wal_level =minimal max_wal_senders = 0