Re: floating point output - Mailing list pgsql-general

From Luca Ferrari
Subject Re: floating point output
Date
Msg-id CAKoxK+53nMQbj4ZdxzMjtXHZa-Uac+kEGnmVduic8mVWi0EjrQ@mail.gmail.com
Whole thread Raw
In response to Re: floating point output  (Adrian Klaver <adrian.klaver@aklaver.com>)
List pgsql-general
On Thu, Sep 5, 2019 at 6:14 AM Adrian Klaver <adrian.klaver@aklaver.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



pgsql-general by date:

Previous
From: Luca Ferrari
Date:
Subject: Re: PG11.2 - wal_level =minimal max_wal_senders = 0
Next
From: Sonam Sharma
Date:
Subject: Uninstall postgres