gawk: Floating point summary
1
1 15.8 Summary
1 ============
1
1 * Most computer arithmetic is done using either integers or
1 floating-point values. Standard 'awk' uses double-precision
1 floating-point values.
1
1 * In the early 1990s Barbie mistakenly said, "Math class is tough!"
1 Although math isn't tough, floating-point arithmetic isn't the same
1 as pencil-and-paper math, and care must be taken:
1
1 - Not all numbers can be represented exactly.
1
1 - Comparing values should use a delta, instead of being done
1 directly with '==' and '!='.
1
1 - Errors accumulate.
1
1 - Operations are not always truly associative or distributive.
1
1 * Increasing the accuracy can help, but it is not a panacea.
1
1 * Often, increasing the accuracy and then rounding to the desired
1 number of digits produces reasonable results.
1
1 * Use '-M' (or '--bignum') to enable MPFR arithmetic. Use 'PREC' to
1 set the precision in bits, and 'ROUNDMODE' to set the IEEE 754
1 rounding mode.
1
1 * With '-M', 'gawk' performs arbitrary-precision integer arithmetic
1 using the GMP library. This is faster and more space-efficient
1 than using MPFR for the same calculations.
1
1 * There are several areas with respect to floating-point numbers
1 where 'gawk' disagrees with the POSIX standard. It pays to be
1 aware of them.
1
1 * Overall, there is no need to be unduly suspicious about the results
1 from floating-point arithmetic. The lesson to remember is that
1 floating-point arithmetic is always more complex than arithmetic
1 using pencil and paper. In order to take advantage of the power of
1 floating-point arithmetic, you need to know its limitations and
1 work within them. For most casual use of floating-point
1 arithmetic, you will often get the expected result if you simply
1 round the display of your final results to the correct number of
1 significant decimal digits.
1
1 * As general advice, avoid presenting numerical data in a manner that
1 implies better precision than is actually the case.
1