Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [125]

By Root 860 0
with a division operator, use the quoted form.

Just as a literal quote in a quoted string must be protected by a backslash ("...\"..."), so must a literal slash in a slash-delimited regular expression (/...\/.../). When a literal backslash is needed in a regular expression, it too must be protected, but the quoted form requires an extra level of protection: "\\TeX" and /\TeX/ are regular expressions that each match a string containing \TeX.

Numbers and Numeric Expressions

All numbers in awk are represented as double-precision floating-point values, and we provide some of the details in the nearby sidebar. Although you do not have to become an expert in floating-point arithmetic, it is important to be aware of the limitations of computer arithmetic so that you do not expect more than the computer can deliver, and so that you can avoid some of the pitfalls.

* * *

More on Floating-Point Arithmetic


Virtually all platforms today conform to the 1985 IEEE 754 Standard for Binary Floating-Point Arithmetic. That standard defines a 32-bit single-precision format, a 64-bit double-precision format, and an optional extended-precision format, which is usually implemented in 80 or 128 bits. awk implementations use the 64-bit format (corresponding to the C datatype double), although in the interests of portability, the awk language specification is intentionally vague about the details. The POSIX awk specification says only that the arithmetic shall follow the ISO C Standard, which does not require any particular floating-point architecture.

IEEE 754 64-bit double-precision values have a sign bit, an 11-bit biased exponent, and a 53-bit significand whose leading bit is not stored. This permits representing numbers with up to about 16 decimal digits. The largest finite magnitude is about 10+308, and the smallest normalized nonzero magnitude is about 10-308. Most IEEE 754 implementations also support subnormal numbers, which extend the range down to about 10-324, but with a loss of precision: this gradual underflow to zero has several desirable numerical properties, but is usually irrelevant to nonnumerical software.

Because the sign bit is explicitly represented, IEEE 754 arithmetic supports both positive and negative zero. Many programming languages get this wrong, however, and awk is no exception: some implementations print a negative zero without its minus sign.

IEEE 754 arithmetic also includes two special values, Infinity and not-a-number (NaN). Both can be signed, but the sign of NaN is not significant. They are intended to allow nonstop computation on high-performance computers while still being able to record the occurrence of exceptional conditions. When a value is too big to represent, it is said to overflow, and the result is Infinity. When a value is not well-defined, such as Infinity - Infinity, or 0/0, the result is a NaN.

Infinity and NaN propagate in computations: Infinity + Infinity and Infinity * Infinity produce Infinity, and NaN combined with anything produces NaN.

Infinities of the same sign compare equal. NaN compares unequal to itself: the test (x != x) is true only if x is a NaN.

awk was developed before IEEE 754 arithmetic became widely available, so the language does not fully support Infinity and NaN. In particular, current awk implementations trap attempts to divide by zero, even though that operation is perfectly well-defined in IEEE 754 arithmetic.

* * *

Floating-point numbers may include a trailing power-of-10 exponent represented by the letter e (or E) and an optionally signed integer. For example, 0.03125, 3.125e-2, 3125e-5, and 0.003125E1 are equivalent representations of the value 1/32. Because all arithmetic in awk is floating-point arithmetic, the expression 1/32 can be written that way without fear that it will evaluate to zero, as happens in programming languages with integer datatypes.

There is no function for explicit conversion of a string to a number, but the awk idiom is simple: just add zero to the string. For example, s = "123", followed by n = 0 + s, assigns

Return Main Page Previous Page Next Page

®Online Book Reader