7 decimal digits precision for real

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

7 decimal digits precision for real

apt.postgresql.org Repository Update
The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/12/datatype-numeric.html
Description:

When  I try to run
`insert into employees (r_id) values (3.919192199);`
where r_id is of type real, I see it stores as 3.9191923 and not 6.
Am I understanding anything wrong ? isn't the range specified is 6 digits
precision?
Reply | Threaded
Open this post in threaded view
|

Re: 7 decimal digits precision for real

Bruce Momjian
On Thu, Jul 30, 2020 at 10:31:49PM +0000, PG Doc comments form wrote:

> The following documentation comment has been logged on the website:
>
> Page: https://www.postgresql.org/docs/12/datatype-numeric.html
> Description:
>
> When  I try to run
> `insert into employees (r_id) values (3.919192199);`
> where r_id is of type real, I see it stores as 3.9191923 and not 6.
> Am I understanding anything wrong ? isn't the range specified is 6 digits
> precision?

Those extra digits are not guaraneteed to be precise, but usually are
close to accurate, so we include, them.  You can diable that with
extra_float_digits = 0:

        SELECT '3.919192199'::real;
          float4
        -----------
         3.9191923
       
        SHOW extra_float_digits;
         extra_float_digits
        --------------------
         1
       
        SET extra_float_digits = 0;
        SELECT '3.919192199'::real;
         float4
        ---------
         3.91919

--
  Bruce Momjian  <[hidden email]>        https://momjian.us
  EnterpriseDB                             https://enterprisedb.com

  The usefulness of a cup is in its emptiness, Bruce Lee