gawk: Checking for MPFR
1
1 15.6 How To Check If MPFR Is Available
1 ======================================
1
1 Occasionally, you might like to be able to check if 'gawk' was invoked
1 with the '-M' option, enabling arbitrary-precision arithmetic. You can
1 do so with the following function, contributed by Andrew Schorr:
1
1 # adequate_math_precision --- return true if we have enough bits
1
1 function adequate_math_precision(n)
1 {
1 return (1 != (1+(1/(2^(n-1)))))
1 }
1
1 Here is code that invokes the function in order to check if
1 arbitrary-precision arithmetic is available:
1
1 BEGIN {
1 # How many bits of mantissa precision are required
1 # for this program to function properly?
1 fpbits = 123
1
1 # We hope that we were invoked with MPFR enabled. If so, the
1 # following statement should configure calculations to our desired
1 # precision.
1 PREC = fpbits
1
1 if (! adequate_math_precision(fpbits)) {
1 print("Error: insufficient computation precision available.\n" \
1 "Try again with the -M argument?") > "/dev/stderr"
1 # Note: you may need to set a flag here to bail out of END rules
1 exit 1
1 }
1 }
1
1 Please be aware that 'exit' will jump to the 'END' rules, if present
1 (⇒Exit Statement).
1