| 1 | March 2001: | 
|---|
| 2 |  | 
|---|
| 3 | It looks like the revised 1003.2 standard will actually follow the | 
|---|
| 4 | rules given below.  Hallelujah! | 
|---|
| 5 |  | 
|---|
| 6 | October 1998: | 
|---|
| 7 |  | 
|---|
| 8 | The 1003.2 work has been at a stand-still for ages. Who knows if or | 
|---|
| 9 | when a new revision will actually happen... | 
|---|
| 10 |  | 
|---|
| 11 | August 1995: | 
|---|
| 12 |  | 
|---|
| 13 | Although the published 1003.2 standard contained the incorrect | 
|---|
| 14 | comparison rules of 11.2 draft as described below, no actual implementation | 
|---|
| 15 | of awk (that I know of) actually used those rules. | 
|---|
| 16 |  | 
|---|
| 17 | A revision of the 1003.2 standard is in progress, and in the May 1995 | 
|---|
| 18 | draft, the rules were fixed (based on my submissions for interpretation | 
|---|
| 19 | requests) to match the description given below. Thus, the next version | 
|---|
| 20 | of the standard will have a correct description of the comparison | 
|---|
| 21 | rules. | 
|---|
| 22 |  | 
|---|
| 23 | June 1992: | 
|---|
| 24 |  | 
|---|
| 25 | Right now, the numeric vs. string comparisons are screwed up in draft | 
|---|
| 26 | 11.2.  What prompted me to check it out was the note in gnu.bug.utils | 
|---|
| 27 | which observed that gawk was doing the comparison  $1 == "000" | 
|---|
| 28 | numerically.  I think that we can agree that intuitively, this should | 
|---|
| 29 | be done as a string comparison.  Version 2.13.2 of gawk follows the | 
|---|
| 30 | current POSIX draft.  Following is how I (now) think this | 
|---|
| 31 | stuff should be done. | 
|---|
| 32 |  | 
|---|
| 33 | 1.  A numeric literal or the result of a numeric operation has the NUMERIC | 
|---|
| 34 | attribute. | 
|---|
| 35 |  | 
|---|
| 36 | 2.  A string literal or the result of a string operation has the STRING | 
|---|
| 37 | attribute. | 
|---|
| 38 |  | 
|---|
| 39 | 3.  Fields, getline input, FILENAME, ARGV elements, ENVIRON elements and the | 
|---|
| 40 | elements of an array created by split() that are numeric strings | 
|---|
| 41 | have the STRNUM attribute.  Otherwise, they have the STRING attribute. | 
|---|
| 42 | Uninitialized variables also have the STRNUM attribute. | 
|---|
| 43 |  | 
|---|
| 44 | 4.  Attributes propagate across assignments, but are not changed by | 
|---|
| 45 | any use.  (Although a use may cause the entity to acquire an additional | 
|---|
| 46 | value such that it has both a numeric and string value -- this leaves the | 
|---|
| 47 | attribute unchanged.) | 
|---|
| 48 |  | 
|---|
| 49 | When two operands are compared, either string comparison or numeric comparison | 
|---|
| 50 | may be used, depending on the attributes of the operands, according to the | 
|---|
| 51 | following (symmetric) matrix: | 
|---|
| 52 |  | 
|---|
| 53 | +---------------------------------------------- | 
|---|
| 54 | |       STRING          NUMERIC         STRNUM | 
|---|
| 55 | --------+---------------------------------------------- | 
|---|
| 56 | | | 
|---|
| 57 | STRING  |       string          string          string | 
|---|
| 58 | | | 
|---|
| 59 | NUMERIC |       string          numeric         numeric | 
|---|
| 60 | | | 
|---|
| 61 | STRNUM  |       string          numeric         numeric | 
|---|
| 62 | --------+---------------------------------------------- | 
|---|
| 63 |  | 
|---|
| 64 | So, the following program should print all OKs. | 
|---|
| 65 |  | 
|---|
| 66 | echo '0e2 0a 0 0b | 
|---|
| 67 | 0e2 0a 0 0b' | | 
|---|
| 68 | $AWK ' | 
|---|
| 69 | NR == 1 { | 
|---|
| 70 | num = 0 | 
|---|
| 71 | str = "0e2" | 
|---|
| 72 |  | 
|---|
| 73 | print ++test ": " (     (str == "0e2")  ? "OK" : "OOPS" ) | 
|---|
| 74 | print ++test ": " (     ("0e2" != 0)    ? "OK" : "OOPS" ) | 
|---|
| 75 | print ++test ": " (     ("0" != $2)     ? "OK" : "OOPS" ) | 
|---|
| 76 | print ++test ": " (     ("0e2" == $1)   ? "OK" : "OOPS" ) | 
|---|
| 77 |  | 
|---|
| 78 | print ++test ": " (     (0 == "0")      ? "OK" : "OOPS" ) | 
|---|
| 79 | print ++test ": " (     (0 == num)      ? "OK" : "OOPS" ) | 
|---|
| 80 | print ++test ": " (     (0 != $2)       ? "OK" : "OOPS" ) | 
|---|
| 81 | print ++test ": " (     (0 == $1)       ? "OK" : "OOPS" ) | 
|---|
| 82 |  | 
|---|
| 83 | print ++test ": " (     ($1 != "0")     ? "OK" : "OOPS" ) | 
|---|
| 84 | print ++test ": " (     ($1 == num)     ? "OK" : "OOPS" ) | 
|---|
| 85 | print ++test ": " (     ($2 != 0)       ? "OK" : "OOPS" ) | 
|---|
| 86 | print ++test ": " (     ($2 != $1)      ? "OK" : "OOPS" ) | 
|---|
| 87 | print ++test ": " (     ($3 == 0)       ? "OK" : "OOPS" ) | 
|---|
| 88 | print ++test ": " (     ($3 == $1)      ? "OK" : "OOPS" ) | 
|---|
| 89 | print ++test ": " (     ($2 != $4)      ? "OK" : "OOPS" ) # 15 | 
|---|
| 90 | } | 
|---|
| 91 | { | 
|---|
| 92 | a = "+2" | 
|---|
| 93 | b = 2 | 
|---|
| 94 | if (NR % 2) | 
|---|
| 95 | c = a + b | 
|---|
| 96 | print ++test ": " (     (a != b)        ? "OK" : "OOPS" ) # 16 and 22 | 
|---|
| 97 |  | 
|---|
| 98 | d = "2a" | 
|---|
| 99 | b = 2 | 
|---|
| 100 | if (NR % 2) | 
|---|
| 101 | c = d + b | 
|---|
| 102 | print ++test ": " (     (d != b)        ? "OK" : "OOPS" ) | 
|---|
| 103 |  | 
|---|
| 104 | print ++test ": " (     (d + 0 == b)    ? "OK" : "OOPS" ) | 
|---|
| 105 |  | 
|---|
| 106 | e = "2" | 
|---|
| 107 | print ++test ": " (     (e == b "")     ? "OK" : "OOPS" ) | 
|---|
| 108 |  | 
|---|
| 109 | a = "2.13" | 
|---|
| 110 | print ++test ": " (     (a == 2.13)     ? "OK" : "OOPS" ) | 
|---|
| 111 |  | 
|---|
| 112 | a = "2.130000" | 
|---|
| 113 | print ++test ": " (     (a != 2.13)     ? "OK" : "OOPS" ) | 
|---|
| 114 |  | 
|---|
| 115 | if (NR == 2) { | 
|---|
| 116 | CONVFMT = "%.6f" | 
|---|
| 117 | print ++test ": " (     (a == 2.13)     ? "OK" : "OOPS" ) | 
|---|
| 118 | } | 
|---|
| 119 | }' | 
|---|