NaN divided by zero should yield NaN

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Cc: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>
Subject: NaN divided by zero should yield NaN
Date: 2020-07-16 19:29:45
Message-ID: 3421746.1594927785@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Dean Rasheed questioned this longstanding behavior:

regression=# SELECT 'nan'::float8 / '0'::float8;
ERROR: division by zero

After a bit of research I think he's right: per IEEE 754 this should
yield NaN, not an error. Accordingly I propose the attached patch.
This is probably not something to back-patch, though.

One thing that's not very clear to me is which of these spellings
is preferable:

if (unlikely(val2 == 0.0) && !isnan(val1))
if (unlikely(val2 == 0.0 && !isnan(val1)))

I think we can reject this variant:

if (unlikely(val2 == 0.0) && unlikely(!isnan(val1)))

since actually the second condition *is* pretty likely.
But I don't know which of the first two would give better
code. Andres, any thoughts?

regards, tom lane

Attachment Content-Type Size
nan-over-zero-is-nan.patch text/x-diff 3.0 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2020-07-16 19:38:19 Re: new heapcheck contrib module
Previous Message Pavel Stehule 2020-07-16 19:08:09 Re: calling procedures is slow and consumes extra much memory against calling function