summaryrefslogtreecommitdiffstats
path: root/contrib/awk/POSIX.STD
blob: 04d31fcf32b6b598da2ce3714e1a6f758f7805ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
March 2001:

It looks like the revised 1003.2 standard will actually follow the
rules given below.  Hallelujah!

October 1998:

The 1003.2 work has been at a stand-still for ages. Who knows if or
when a new revision will actually happen...

August 1995:

Although the published 1003.2 standard contained the incorrect
comparison rules of 11.2 draft as described below, no actual implementation
of awk (that I know of) actually used those rules.

A revision of the 1003.2 standard is in progress, and in the May 1995
draft, the rules were fixed (based on my submissions for interpretation
requests) to match the description given below. Thus, the next version
of the standard will have a correct description of the comparison
rules.

June 1992:

Right now, the numeric vs. string comparisons are screwed up in draft
11.2.  What prompted me to check it out was the note in gnu.bug.utils
which observed that gawk was doing the comparison  $1 == "000"
numerically.  I think that we can agree that intuitively, this should
be done as a string comparison.  Version 2.13.2 of gawk follows the
current POSIX draft.  Following is how I (now) think this
stuff should be done. 

1.  A numeric literal or the result of a numeric operation has the NUMERIC
    attribute.

2.  A string literal or the result of a string operation has the STRING
    attribute.

3.  Fields, getline input, FILENAME, ARGV elements, ENVIRON elements and the
    elements of an array created by split() that are numeric strings
    have the STRNUM attribute.  Otherwise, they have the STRING attribute.
    Uninitialized variables also have the STRNUM attribute.

4.  Attributes propagate across assignments, but are not changed by
    any use.  (Although a use may cause the entity to acquire an additional
    value such that it has both a numeric and string value -- this leaves the
    attribute unchanged.)

When two operands are compared, either string comparison or numeric comparison
may be used, depending on the attributes of the operands, according to the
following (symmetric) matrix:

	+----------------------------------------------
	|	STRING		NUMERIC		STRNUM
--------+----------------------------------------------
	|
STRING	|	string		string		string
	|
NUMERIC	|	string		numeric		numeric
	|
STRNUM	|	string		numeric		numeric
--------+----------------------------------------------

So, the following program should print all OKs.

echo '0e2 0a 0 0b
0e2 0a 0 0b' |
$AWK '
NR == 1 {
	num = 0
	str = "0e2"

	print ++test ": " (	(str == "0e2")	? "OK" : "OOPS" )
	print ++test ": " (	("0e2" != 0)	? "OK" : "OOPS" )
	print ++test ": " (	("0" != $2)	? "OK" : "OOPS" )
	print ++test ": " (	("0e2" == $1)	? "OK" : "OOPS" )

	print ++test ": " (	(0 == "0")	? "OK" : "OOPS" )
	print ++test ": " (	(0 == num)	? "OK" : "OOPS" )
	print ++test ": " (	(0 != $2)	? "OK" : "OOPS" )
	print ++test ": " (	(0 == $1)	? "OK" : "OOPS" )

	print ++test ": " (	($1 != "0")	? "OK" : "OOPS" )
	print ++test ": " (	($1 == num)	? "OK" : "OOPS" )
	print ++test ": " (	($2 != 0)	? "OK" : "OOPS" )
	print ++test ": " (	($2 != $1)	? "OK" : "OOPS" )
	print ++test ": " (	($3 == 0)	? "OK" : "OOPS" )
	print ++test ": " (	($3 == $1)	? "OK" : "OOPS" )
	print ++test ": " (	($2 != $4)	? "OK" : "OOPS"	) # 15
}
{
	a = "+2"
	b = 2
	if (NR % 2)
		c = a + b
	print ++test ": " (	(a != b)	? "OK" : "OOPS" ) # 16 and 22

	d = "2a"
	b = 2
	if (NR % 2)
		c = d + b
	print ++test ": " (	(d != b)	? "OK" : "OOPS" )

	print ++test ": " (	(d + 0 == b)	? "OK" : "OOPS" )

	e = "2"
	print ++test ": " (	(e == b "")	? "OK" : "OOPS" )

	a = "2.13"
	print ++test ": " (	(a == 2.13)	? "OK" : "OOPS" )

	a = "2.130000"
	print ++test ": " (	(a != 2.13)	? "OK" : "OOPS" )

	if (NR == 2) {
		CONVFMT = "%.6f"
		print ++test ": " (	(a == 2.13)	? "OK" : "OOPS" )
	}
}'
OpenPOWER on IntegriCloud