Next: POSIX Floating Point Problems, Previous: String Conversion Precision, Up: Floating Point Issues [Contents][Index]

Unlike numbers in the abstract sense (such as what you studied in high school or college arithmetic), numbers stored in computers are limited in certain ways. They cannot represent an infinite number of digits, nor can they always represent things exactly. In particular, floating-point numbers cannot always represent values exactly. Here is an example:

$awk '{ printf("%010d\n", $1 * 100) }'515.79 -| 0000051579 515.80 -| 0000051579 515.81 -| 0000051580 515.82 -| 0000051582Ctrl-d

This shows that some values can be represented exactly,
whereas others are only approximated. This is not a “bug”
in `awk`

, but simply an artifact of how computers
represent numbers.

NOTE:It cannot be emphasized enough that the behavior just described is fundamental to modern computers. You will see this kind of thing happen inanyprogramming language using hardware floating-point numbers. It isnota bug in`gawk`

, nor is it something that can be “just fixed.”

Another peculiarity of floating-point numbers on modern systems is that they often have more than one representation for the number zero! In particular, it is possible to represent “minus zero” as well as regular, or “positive” zero.

This example shows that negative and positive zero are distinct values when stored internally, but that they are in fact equal to each other, as well as to “regular” zero:

$gawk 'BEGIN { mz = -0 ; pz = 0>printf "-0 = %g, +0 = %g, (-0 == +0) -> %d\n", mz, pz, mz == pz>printf "mz == 0 -> %d, pz == 0 -> %d\n", mz == 0, pz == 0>}'-| -0 = -0, +0 = 0, (-0 == +0) -> 1 -| mz == 0 -> 1, pz == 0 -> 1

It helps to keep this in mind should you process numeric data that contains negative zero values; the fact that the zero is negative is noted and can affect comparisons.