bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Special Comparisons: nan == nan


From: arnold
Subject: Re: [bug-gawk] Special Comparisons: nan == nan
Date: Mon, 07 Aug 2017 12:04:04 -0600
User-agent: Heirloom mailx 12.4 7/29/08

"Andrew J. Schorr" <address@hidden> wrote:

> Hi,
>
> Thanks for your bug reports.
>
> Regarding #1:
>
> On Sun, Aug 06, 2017 at 05:59:59PM -0700, Daniel Pettet wrote:
> > 1.  A NaN is never equal to anything - including another NaN.  The
> > following illustrate the problem
> > 
> > BEGIN {
> >     nan = 0 * 1e400
> >     print nan               # nan
> >     print nan == nan        # 1     should be 0
> > }
>
> This is caused by 2 issues in the code.
>
> The eval.c:cmp_nodes function has this check at the top:
>
>         if (t1 == t2)
>                 return 0;
>
> So it has a special check for comparing a value to itself,
> and always returns that they are equal.
>
> If one disables that check and runs through the normal comparison logic, it
> still returns true because of this code in node.c:cmp_awknums:
>
>         /*
>          * This routine is also used to sort numeric array indices or values.
>          * For the purposes of sorting, NaN is considered greater than
>          * any other value, and all NaN values are considered equivalent and 
> equal.
>          * This isn't in compliance with IEEE standard, but compliance w.r.t. 
> NaN
>          * comparison at the awk level is a different issue, and needs to be 
> dealt
>          * with in the interpreter for each opcode seperately.
>          */
>
>         if (isnan(t1->numbr))
>                 return ! isnan(t2->numbr);
>
> And this logic is mirrored in the MPFR version in mpfr.c:mpg_cmp.
> So there are a couple of reasons why your test behaves the way
> it does. I'm not sure whether or how to address your concern. I don't
> imagine that too many people are using gawk with calculations involving
> NaN that depend on these subtle behaviors. Arnold?
>
> I haven't had time yet to look at issue #2.
>
> Regards,
> Andy

This is an interesting issue. I don't think POSIX addresses it
specifically.  Here's is what a bunch of awk do:

        $ for i in nawk mawk gawk mksawk 'busybox awk'
        > do echo === $i
        > $i -f nan.awk
        > done
        === nawk
        -nan
        1
        === mawk
        mawk: nan.awk: line 2: 1e400 : decimal overflow
        === gawk
        -nan
        1
        === mksawk
        -nan
        1
        === busybox awk
        -nan
        0

So I'm in good company at the moment. I want to think about this
one for a little bit.

Thanks,

Arnold



reply via email to

[Prev in Thread] Current Thread [Next in Thread]