Next: , Up: Floating Point Issues   [Contents][Index]


15.1.1.1 The String Value Can Lie

Internally, awk keeps both the numeric value (double precision floating-point) and the string value for a variable. Separately, awk keeps track of what type the variable has (see Typing and Comparison), which plays a role in how variables are used in comparisons.

It is important to note that the string value for a number may not reflect the full value (all the digits) that the numeric value actually contains. The following program, values.awk, illustrates this:

{
   sum = $1 + $2
   # see it for what it is
   printf("sum = %.12g\n", sum)
   # use CONVFMT
   a = "<" sum ">"
   print "a =", a
   # use OFMT
   print "sum =", sum
}

This program shows the full value of the sum of $1 and $2 using printf, and then prints the string values obtained from both automatic conversion (via CONVFMT) and from printing (via OFMT).

Here is what happens when the program is run:

$ echo 3.654321 1.2345678 | awk -f values.awk
-| sum = 4.8888888
-| a = <4.88889>
-| sum = 4.88889

This makes it clear that the full numeric value is different from what the default string representations show.

CONVFMT’s default value is "%.6g", which yields a value with at most six significant digits. For some applications, you might want to change it to specify more precision. On most modern machines, most of the time, 17 digits is enough to capture a floating-point number’s value exactly.89


Footnotes

(89)

Pathological cases can require up to 752 digits (!), but we doubt that you need to worry about this.