home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuawk.zip / POSIX.STD < prev    next >
Text File  |  1995-12-27  |  4KB  |  110 lines

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