Re: How can I accurately infer the output of a SELECT statement FROM a table with floating-point data type?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: 毛毛 <krave(at)163(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: How can I accurately infer the output of a SELECT statement FROM a table with floating-point data type?
Date: 2023-10-19 04:12:51
Message-ID: 3259196.1697688771@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

=?GBK?B?w6vDqw==?= <krave(at)163(dot)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.
Keep in mind that the nominal precision, such as 6 digits for
float4, is a *lower bound* on the number of decimal digits
that will be stored accurately, whereas supporting exact
round-trip I/O requires an *upper bound* number of digits.

In short, no, there is not a trivial way to predict the
number of digits emitted. If you have a problem with that,
maybe you should be using type numeric instead.

You can find more info by looking around for info about the
Ryū float output algorithm, eg here:

https://github.com/ulfjack/ryu

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2023-10-19 04:14:47 Re: How can I accurately infer the output of a SELECT statement FROM a table with floating-point data type?
Previous Message 毛毛 2023-10-19 03:45:54 How can I accurately infer the output of a SELECT statement FROM a table with floating-point data type?