shep@allspice.lcs.mit.edu (Tim Shepard) (07/19/89)
I'm using 4.3BSD on a VAX. echo 1206669121 1206669122 | awk '{if ($1 == $2) print}' When I run this, it does print the line. It seems that the strings are being converted to numbers before the comparison. But on page USD:19-5 of the 4.3 BSD manuals (the ones printed by USENIX), it says, "Whether a field is deemed numeric or string depends on context; in ambiguous cases like if ($1 == $2) ... fields are treated as strings." The documentation seems to be wrong. I think the type coercion behaviour of awk is correct because in this case: echo 2.0 2 | awk '{if ($1 == $2) print)}' I do want the test to succeed. Other surprising results can be seen by running these tests: echo 1206669121 1206669122 | awk '{print $2-$1}' echo 1206669119 1206669120 | awk '{print $2-$1}' I have solved my problems by applying the patch below and building my private copy of awk (called "dawk") which uses doubles instead of floats for its internal representation of numbers. Maybe this patch should be considered for the 4.4 BSD awk? -Tim Shepard <shep@ptt.lcs.mit.edu> *** /usr/src/bin/awk/awk.def Sat Dec 8 16:33:31 1984 --- awk.def Mon Jul 10 11:12:04 1989 *************** *** 1,7 **** /* awk.def 4.3 83/12/09 */ #define hack int ! #define AWKFLOAT float #define xfree(a) { if(a!=NULL) { yfree(a); a=NULL;} } #define strfree(a) { if(a!=NULL && a!=EMPTY) { yfree(a);} a=EMPTY; } #define yfree free --- 1,7 ---- /* awk.def 4.3 83/12/09 */ #define hack int ! #define AWKFLOAT double #define xfree(a) { if(a!=NULL) { yfree(a); a=NULL;} } #define strfree(a) { if(a!=NULL && a!=EMPTY) { yfree(a);} a=EMPTY; } #define yfree free