bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] inside awk script check that -M/--bignum given on command


From: Jannick
Subject: Re: [bug-gawk] inside awk script check that -M/--bignum given on command line?
Date: Fri, 5 Jan 2018 17:36:11 +0100

Hi Andy,

On Fri, 5 Jan 2018 07:56:06 -0500, Andrew J. Schorr wrote:

> On Fri, Jan 05, 2018 at 03:54:13AM +0100, Jannick wrote:
> > 1 - 'exit 1' (as given in the BEGIN section of the example code you
> > were quoting earlier from the gawk manual) is not an immediate exit,
> > but it makes gawk jump to the END section which it then bravely works
> > through and exits then (cf. 7.4.10 gawk manual, ed. 4.2). I know that
> > this is really nitty-gritty, but I thought I mention that as a tiny
> > contribution, since other users might wonder why a larger script with
> > a huge END section does not immediately exit if the MPFR test fails.
> 
> This is a general AWK issue. Since this particular example doesn't include
an
> END section, I don't know how to address your concerns. In my own scripts,
I
> would probably do something like this if I had an END section:
> 
> BEGIN {
>          if (! adequate_math_precision(fpbits)) {
>              print("Error: insufficient computation precision
available.\n" \
>                    "Try again with the -M argument?") > "/dev/stderr"
>              exit erc=1
>          }
> }
> 
> END {
>       if (erc)
>               exit erc
>       ...
> }
> 
> But I don't think it makes sense to add an END section to this snippet.
> That seems to me more confusing than helpful.
> 
> What do you propose?

I was thinking of something like the END section above. But agree, this
makes the snip look a bit too complicated than needed. As an alternative -
if anything at all - perhaps adding a very short note (probably in a
footnote) on the exit issue with reference to chapter 7.4.10?
 
> > 2 - The exact fpbits number in the example code is not really written
> > in stone as such.
> 
> I'm not sure what you mean by written in stone. The idea is that any given
> program may require a certain amount of precision to function correctly,
so
> the code should figure out what it needs and then ascertain whether gawk
is
> configured to provide sufficient accuracy. What are you driving at?

Sorry, I was not paying enough attention to the comment in
https://www.gnu.org/software/gawk/manual/html_node/Checking-for-MPFR.html
before defining fpbits. While quickly reading it the first time I was under
the (wrong) impression that fpbits = 123 is needed to run the check if -M is
enabled or not. But as you said, it is the amount of precision the program
needs. 

Based on Arnold's hint in a posting earlier today, gawk's command line flags
are now in PROCINFO["argv"]. Thus I ended up with the following snip to
solve my original problem (check if -M/--bignum is enabled or not), while
calling the function adequate_math_precision does what its name suggests.

BEGIN{
        if ( ! ( "mpfr_version" in PROCINFO ) )
        {
                print "script requires call of gawk with MPFR compiled in" >
"/dev/stderr"
                exit 1
        }
        if ( ! ("argv" in PROCINFO ) )
        {
                print "script requires call of gawk >= 4.2" > "/dev/stderr"
                exit 1
        }
        found_bignum=0
        for ( i in PROCINFO["argv"] )
                if ( PROCINFO["argv"][i] ~ /^(--bignum|-M)$/ ) # requires
separate call of '-M'
                        found_bignum++
        if ( ! found_bignum )
        {
                print "script requires command line flag '-M' or '--bignum'"
> "/dev/stderr"
                exit 1
        }
}
 
> Thanks,
> Andy

Many thanks again,
J.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]