Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [126]

By Root 878 0
the number 123 to n.

Non-numeric strings are coerced to numbers by converting as much of the string that looks like a number: "+123ABC" converts to 123, and "ABC", "ABC123", and "" all convert to 0.

The limited precision of floating-point numbers means that some values cannot be represented exactly: the order of evaluation is significant (floating-point arithmetic is not associative), and computed results are normally rounded to the nearest representable number.

The limited range of floating-point numbers means that very small or very large numbers are not representable. On modern systems, such values are converted to zero and infinity.

Even though all numeric computations in awk are done in floating-point arithmetic, integer values can be represented exactly, provided that they are not too large. With IEEE 754 arithmetic, the 53-bit significand limits integers to at most 253 = 9,007,199,254,740,992. That number is large enough that few text processing applications that involve counting things are likely to reach it.

Numeric operators in awk are similar to those in several other programming languages. We collect them in Table 9-1Table 9-1.

Table 9-2. Numeric operators in awk (in decreasing precedence)

Operator

Description

++ --

Increment and decrement (either prefix or postfix)

^ **

Exponentiate (right-associative)

! + -

Not, unary plus, unary minus

* / %

Multiply, divide, remainder

+ -

Add, subtract

< <= = = <= != > >=

Com pare

&&

Logical AND (short-circuit)

||

Logical OR (short-circuit)

? :

Ternary conditional

= += -= *= /= %= ^= **=

Assign (right -associative)

Like most programming languages, awk allows parentheses to control evaluation order. Few people can reliably remember operator precedence, especially if they work with multiple languages: when in doubt, parenthesize!

The increment and decrement operators work like those in the shell, described in Section 6.1.3. In isolation, n++ and ++n are equivalent. However, because they have the side effect of updating the variable as well as returning a value, ambiguities in evaluation order can arise when they are used more than once in the same statement. For example, the result of an expression like n++ + ++n is implementation defined. Despite such ambiguities, the increment and decrement operators receive wide use in programming languages that have them.

Exponentiation raises the left operand to the power given by the right operand. Thus, n^3 and n**3 both mean the cube of n. The two operator names are equivalent, but come from different ancestor languages. C programmers should note that awk's ^ operator is different from C's, despite the similarity of major parts of awk and C.

Exponentiation and assignment are the only operators in awk that are right-associative: thus, a^b^c^d means a^(b^(c^d)), whereas a/b/c/d means ((a/b)/c)/d. These associativity rules are common to most other programming languages, and are conventional in mathematics.

In the original awk specification, the result of the remainder operator is implementation-defined when either operand is negative. POSIX awk requires that it behave like the ISO Standard C function fmod( ). This in turn requires that if x % y is representable, then the expression has the sign of x, and magnitude less than y. All awk implementations that we tested follow the POSIX mandate.

Just as in the shell, the logical operators && and || are short-circuiting forms of AND and OR: they evaluate their righthand operand only if needed.

The operator in the next-to-last row in the table is the ternary short-circuiting conditional operator. If the first operand is nonzero (true), the result is the second operand; otherwise, it is the third operand. Only one of the second and third operands is evaluated. Thus, in awk, you can write a compact assignment a = (u > w) ? x^3 : y^7 that in other programming languages might require something like this:

if (u > w) then

a = x^3

else

a = y^7

endif

The assignment operators are perhaps unusual

Return Main Page Previous Page Next Page

®Online Book Reader