NaN divided by zero should yield NaN

Started by Tom Laneover 5 years ago3 messageshackers
Jump to latest
#1Tom Lane
tgl@sss.pgh.pa.us

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

Attachments:

nan-over-zero-is-nan.patchtext/x-diff; charset=us-ascii; name=nan-over-zero-is-nan.patchDownload+22-2
#2Dean Rasheed
dean.a.rasheed@gmail.com
In reply to: Tom Lane (#1)
Re: NaN divided by zero should yield NaN

On Thu, 16 Jul 2020 at 20:29, Tom Lane <tgl@sss.pgh.pa.us> wrote:

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.

Agreed.

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)))

My guess is that the first would be better, since it would tell the
compiler that it's unlikely to need to do the NaN test, so it would be
kind of like doing

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

Regards,
Dean

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dean Rasheed (#2)
Re: NaN divided by zero should yield NaN

Dean Rasheed <dean.a.rasheed@gmail.com> writes:

On Thu, 16 Jul 2020 at 20:29, Tom Lane <tgl@sss.pgh.pa.us> wrote:

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)))

My guess is that the first would be better, since it would tell the
compiler that it's unlikely to need to do the NaN test,

Yeah, that's the straightforward way to think about it, but I've
found that gcc is sometimes less than straightforward ;-). Still,
there's no obvious reason to do it the second way, so I pushed the
first way.

regards, tom lane