Re: exp() versus the POSIX standard

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: emre(at)hasegeli(dot)com
Cc: Darafei Komяpa Praliaskouski <me(at)komzpa(dot)net>, PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: exp() versus the POSIX standard
Date: 2020-06-13 23:12:27
Message-ID: 983260.1592089947@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Emre Hasegeli <emre(at)hasegeli(dot)com> writes:
>> No, no kind of GUC switch is needed. Just drop underflow/overflow checks. You'll get 0 or Infinity in expected places, and infinities are okay since 2007.

> This is out of scope of this thread.

Yeah, that. At the moment I'm just interested in making the float and
numeric functions give equivalent results for infinite inputs. If you
want to make a more general proposal about removing error checks, that
seems like a separate topic.

> I am not sure opening it to
> discussion on another thread would yield any result. Experienced
> developers like Tom appear to be in agreement of us needing to protect
> users from oddities of floating point numbers. (I am not.)

I think there's a pretty fundamental distinction between this behavior:

regression=# select exp('-inf'::float8);
exp
-----
0
(1 row)

and this one:

regression=# select exp('-1000'::float8);
ERROR: value out of range: underflow

In the first case, zero is the correct answer to any precision you care
to name. In the second case, zero is *not* the correct answer; we simply
cannot represent the correct answer (somewhere around 1e-434) as a float8.
Returning zero would represent 100% loss of accuracy. Now, there may well
be applications where you'd rather take the zero result and press on, but
I'd argue that they're subtle ones that you're not likely gonna be writing
in SQL.

Anyway, for now I propose the attached patch. The test cases inquire into
the edge-case behavior of pow() much more closely than we have done in the
past, and I wouldn't be a bit surprised if some of the older buildfarm
critters fail some of them. So my inclination is to try this only in
HEAD for starters. Even if we want to back-patch, I'd be hesitant to
put it in versions older than v12, where we started to require C99.

regards, tom lane

Attachment Content-Type Size
fix-exp-power-underflows-1.patch text/x-diff 6.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Masahiko Sawada 2020-06-13 23:29:43 Re: Transactions involving multiple postgres foreign servers, take 2
Previous Message Justin Pryzby 2020-06-13 22:51:21 Re: Warn when parallel restoring a custom dump without data offsets