gawk: Inexact representation
1
1 15.4.1.1 Many Numbers Cannot Be Represented Exactly
1 ...................................................
1
1 So, before you start to write any code, you should think about what you
1 really want and what's really happening. Consider the two numbers in
1 the following example:
1
1 x = 0.875 # 1/2 + 1/4 + 1/8
1 y = 0.425
1
1 Unlike the number in 'y', the number stored in 'x' is exactly
1 representable in binary because it can be written as a finite sum of one
1 or more fractions whose denominators are all powers of two. When 'gawk'
1 reads a floating-point number from program source, it automatically
1 rounds that number to whatever precision your machine supports. If you
1 try to print the numeric content of a variable using an output format
1 string of '"%.17g"', it may not produce the same number as you assigned
1 to it:
1
1 $ gawk 'BEGIN { x = 0.875; y = 0.425
1 > printf("%0.17g, %0.17g\n", x, y) }'
1 -| 0.875, 0.42499999999999999
1
1 Often the error is so small you do not even notice it, and if you do,
1 you can always specify how much precision you would like in your output.
1 Usually this is a format string like '"%.15g"', which, when used in the
1 previous example, produces an output identical to the input.
1