pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: How can I accurately infer the output of a SELECT statement FROM a table with floating-point data type?
Date: Sat, 21 Oct 2023 22:21:17 +0200
Message-ID: <20231021202117.75dmanjjnfhapyxd@hjp.at> (raw)
In-Reply-To: <3259196.1697688771@sss.pgh.pa.us>
References: <10b02a08.28b4.18b460a2f09.Coremail.krave@163.com>
	<3259196.1697688771@sss.pgh.pa.us>

On 2023-10-19 00:12:51 -0400, Tom Lane wrote:
> =?GBK?B?w6vDqw==?= <krave@163.com> writes:
> > Do we have a rule by follow which one can accurately info the output of a SELECT statment FROM a table with floating-point data type?
> 
> The goal of our floating-point output functions is to produce
> the shortest representation from which the stored value could
> be recovered exactly.  It's not unusual for that to require
> two or three decimal digits more than the nominal precision.

To illustrate this:

A real has 24 bits of mantissa. 12345.123456789 is between 2**13 and
2**14, so there are 10 bits left for the fraction. Or in other words,
the number must be approximated as a multiple of 1/1024.

The closest we can get is 12345+124/1024 = 12345.123046875

12345.123 would obviously be rounded to the same number, so it's close
enough and additional digits aren't necessary.

But 12345.12 would be rounded to 12345+123/1024 = 12345.1201171875.
That's different, so 7 digits are not enough in this case.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (5+ messages)

Message-ID: <20231021202117.75dmanjjnfhapyxd@hjp.at>
Permalink:  ../20231021202117.75dmanjjnfhapyxd@hjp.at/
Also on:    postgresql.org/message-id/20231021202117.75dmanjjnfhapyxd@hjp.at

 · 

reply

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: hjp-pgsql@hjp.at
  Subject: Re: How can I accurately infer the output of a SELECT statement FROM a table with floating-point data type?
  In-Reply-To: <20231021202117.75dmanjjnfhapyxd@hjp.at>
  References: <10b02a08.28b4.18b460a2f09.Coremail.krave@163.com> <3259196.1697688771@sss.pgh.pa.us> <20231021202117.75dmanjjnfhapyxd@hjp.at>

* 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