Next: , Previous: Setting Rounding Mode, Up: Arbitrary Precision Floats


15.4.3 Representing Floating-point Constants

Be wary of floating-point constants! When reading a floating-point constant from program source code, gawk uses the default precision, unless overridden by an assignment to the special variable PREC on the command line, to store it internally as a MPFR number. Changing the precision using PREC in the program text does not change the precision of a constant. If you need to represent a floating-point constant at a higher precision than the default and cannot use a command line assignment to PREC, you should either specify the constant as a string, or as a rational number, whenever possible. The following example illustrates the differences among various ways to print a floating-point constant:

     $ gawk -M 'BEGIN { PREC = 113; printf("%0.25f\n", 0.1) }'
     -| 0.1000000000000000055511151
     $ gawk -M -v PREC=113 'BEGIN { printf("%0.25f\n", 0.1) }'
     -| 0.1000000000000000000000000
     $ gawk -M 'BEGIN { PREC = 113; printf("%0.25f\n", "0.1") }'
     -| 0.1000000000000000000000000
     $ gawk -M 'BEGIN { PREC = 113; printf("%0.25f\n", 1/10) }'
     -| 0.1000000000000000000000000

In the first case, the number is stored with the default precision of 53 bits.