Previous: Unexpected Results, Up: Floating Point Issues
Historically, awk has converted any non-numeric looking string to the numeric value zero, when required. Furthermore, the original definition of the language and the original POSIX standards specified that awk only understands decimal numbers (base 10), and not octal (base 8) or hexadecimal numbers (base 16).
As of this writing (February, 2007), changes in the language of the current POSIX standard can be interpreted to imply that awk should support additional features. These features are:
The first problem is that both of these are clear changes to historical practice:
The second problem is that the gawk maintainer feels that this
interpretation of the standard, which requires a certain amount of
“language lawyering” to arrive at in the first place, was not intended
by the standard developers, either. In other words, “we see how you
got where you are, but we don't think that that's where you want to be.”
Nevertheless, on systems that support IEEE floating point, it seems reasonable to provide some way to support NaN and Infinity values. The solution implemented in gawk, as of version 3.1.6, is as follows:
strtod() function, and if it successfuly returns a numeric value,
that is what's used. By definition, the results are not portable across
different systems.1
They are also a little surprising:
$ echo nanny | gawk --posix '{ print $1 + 0 }'
-| nan
$ echo 0xDeadBeef | gawk --posix '{ print $1 + 0 }'
-| 3735928559
$ echo nanny | gawk '{ print $1 + 0 }'
-| 0
$ echo +nan | gawk '{ print $1 + 0 }'
-| nan
$ echo 0xDeadBeef | gawk '{ print $1 + 0 }'
-| 0
gawk does ignore case distinction in the four special values. Thus ‘+nan’ and ‘+NaN’ are the same.