bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: [Gawk] Bug in arithmetic comparisons


From: Seb
Subject: Re: [Gawk] Bug in arithmetic comparisons
Date: Sat, 11 Sep 2010 23:20:59 +0200

Le Sat, 11 Sep 2010 14:12:45 -0400
John Cowan <address@hidden> a écrit:

> Seb scripsit:
> 
> > I think there is a bug in the comparisons that gawk (3.1.8) makes in numeric
> > context. Here is an example of what I get :
> > 
> > $ echo "12.10 12.10" | awk 'BEGIN{print ($1 == $2) ? 1 : 0}'
> >   1
> > $ echo "12.10 12.1" | awk 'BEGIN{print ($1 == $2) ? 1 : 0}'
> >   1
> > $ echo "12.10 12.1" | awk 'BEGIN{print ($1 == $2+0) ? 1 : 0}'
> >   0
> 
> Because you are inside a BEGIN block, the $1 and $2 variables are not yet
> set to anything, so you are comparing '' to '' in the first two examples,
> and '' to '0' in the third.  If you omit the BEGIN, you get 1 in each
> example, as expected.

My bad, you're right. My report is flawed, and finally there is no bug at all. I
was drawn to think there was one, due to a different behaviour of the
Kernighan's implementation :

$ echo "1.10 1.10" | gawk '{ x = substr($0,1,4); print (x+0 == x) ? 1 : 0}'
  0
$ echo "1.10 1.10" | LANG=C oawk '{ x=substr($0,1,4); print (x+0 == x) ? 1:0}'
  1

Above, the substr() makes gawk to consider "x" as a pure string, while it's 
still
possible to force the Kernighan's AWK to make a numeric comparison. The
documentation of gawk clearly says a numeric vs. string comparison is in
string context, so there is nothing to argue. Sorry for the noise and thanks
all for pointing me my error. :)

Take care,
Seb.



reply via email to

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