Next: , Previous: , Up: Arbitrary Precision Arithmetic   [Contents][Index]


16.6 How To Check If MPFR Is Available

Occasionally, you might like to be able to check if gawk was invoked with the -M option, enabling arbitrary-precision arithmetic. You can do so with the following function, contributed by Andrew Schorr:

# adequate_math_precision --- return true if we have enough bits

function adequate_math_precision(n)
{
    return (1 != (1+(1/(2^(n-1)))))
}

Here is code that invokes the function in order to check if arbitrary-precision arithmetic is available:

BEGIN {
    # How many bits of mantissa precision are required
    # for this program to function properly?
    fpbits = 123

    # We hope that we were invoked with MPFR enabled. If so, the
    # following statement should configure calculations to our desired
    # precision.
    PREC = fpbits

    if (! adequate_math_precision(fpbits)) {
        print("Error: insufficient computation precision available.\n" \
              "Try again with the -M argument?") > "/dev/stderr"
        # Note: you may need to set a flag here to bail out of END rules
        exit 1
    }
}

Please be aware that exit will jump to the END rules, if present (see section The exit Statement).