Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use GMP/MPFR in GAWK...

34 views
Skip to first unread message

Kenny McCormack

unread,
Feb 2, 2016, 9:37:23 AM2/2/16
to
My versions of GAWK have GMP/MPFR compiled in, but I have to admit that I
don't really know how to use it. I won't go into details, but I found the
sections in the GAWK documentation cryptic at best.

Here's my basic problem: I want to calculate and display 2^^61 - 1.

Here's the basic idea in GAWK:

$ gawk4 '{ OFMT="%.30g";print 2**$1-1 }'
61
2305843009213693952
^C
$

Well, this is obviously wrong, since we know the result should be odd.

Is it possible to get the right result, using GMP/MPFR (in GAWK) ?

--
Watching ConservaLoons playing with statistics and facts is like watching a
newborn play with a computer. Endlessly amusing, but totally unproductive.

Janis Papanagnou

unread,
Feb 2, 2016, 9:44:02 AM2/2/16
to
On 02.02.2016 15:37, Kenny McCormack wrote:
> My versions of GAWK have GMP/MPFR compiled in, but I have to admit that I
> don't really know how to use it. I won't go into details, but I found the
> sections in the GAWK documentation cryptic at best.
>
> Here's my basic problem: I want to calculate and display 2^^61 - 1.
>
> Here's the basic idea in GAWK:
>
> $ gawk4 '{ OFMT="%.30g";print 2**$1-1 }'
> 61
> 2305843009213693952
> ^C
> $
>
> Well, this is obviously wrong, since we know the result should be odd.
>
> Is it possible to get the right result, using GMP/MPFR (in GAWK) ?
>

$ gawk -M '{ OFMT="%.30g";print 2**$1-1 }'
61
2305843009213693951
10
1023
99
633825300114114700748351602687


Janis

Kenny McCormack

unread,
Feb 2, 2016, 9:57:17 AM2/2/16
to
In article <n8qfbh$4up$1...@news.m-online.net>,
Interesting. So, that's all you have to do?

Somehow, the description in the dox makes it sound like you have to do a
lot more faffling about to get the right answers.

A few other comments/questions:

1) One wonders why it isn't the default? Why *should* we have to
remember to put in "-M" ?

2) FWIW, ISTM that you don't need the OFMT stuff now, if you are using
"-M".

3) How do I turn "-M" mode on from within a GAWK source file?
I.e., just as we can use things like "-i" and "-l" on the command
line, or we can us "@include" or "@load" inside the source code, is
there a similar trick to use inside the code to turn on "-M" mode?

--
(The Republican mind, in a nutshell)
You believe things that are incomprehensible, inconsistent, impossible
because we have commanded you to believe them; go then and do what is
unjust because we command it. Such people show admirable reasoning. Truly,
whoever is able to make you absurd is able to make you unjust. If the
God-given understanding of your mind does not resist a demand to believe
what is impossible, then you will not resist a demand to do wrong to that
God-given sense of justice in your heart. As soon as one faculty of your
soul has been dominated, other faculties will follow as well. And from this
derives all those crimes of religion which have overrun the world.

(Alternative condensed translation)
"Those who can make you believe absurdities, can make you commit atrocities".

Janis Papanagnou

unread,
Feb 2, 2016, 10:45:21 AM2/2/16
to
On 02.02.2016 15:57, Kenny McCormack wrote:
> In article <n8qfbh$4up$1...@news.m-online.net>,
> Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>> On 02.02.2016 15:37, Kenny McCormack wrote:
>>> My versions of GAWK have GMP/MPFR compiled in, but I have to admit that I
>>> don't really know how to use it. I won't go into details, but I found the
>>> sections in the GAWK documentation cryptic at best.
>>>
>>> Here's my basic problem: I want to calculate and display 2^^61 - 1.
>>>
>>> Here's the basic idea in GAWK:
>>>
>>> $ gawk4 '{ OFMT="%.30g";print 2**$1-1 }'
>>> 61
>>> 2305843009213693952
>>> ^C
>>> $
>>>
>>> Well, this is obviously wrong, since we know the result should be odd.
>>>
>>> Is it possible to get the right result, using GMP/MPFR (in GAWK) ?
>>>
>>
>> $ gawk -M '{ OFMT="%.30g";print 2**$1-1 }'
>> 61
>> 2305843009213693951
>> 10
>> 1023
>> 99
>> 633825300114114700748351602687
>
> Interesting.

Frankly, I'm quite scared about the effect that omitting -M will produce
(off-by-one?) results. That's a subtle effect, and (if not having -M as
default) an error message would be more what I'd expect.[*]

> So, that's all you have to do?

That's the thing I memorized about gawk's MP-arithmetic. And the option
to explicitly define the precision using the variable PREC.

>
> Somehow, the description in the dox makes it sound like you have to do a
> lot more faffling about to get the right answers.
>
> A few other comments/questions:
>
> 1) One wonders why it isn't the default? Why *should* we have to
> remember to put in "-M" ?

Can't tell. Maybe the visible design effect of some internal optimization?

>
> 2) FWIW, ISTM that you don't need the OFMT stuff now, if you are using
> "-M".
>
> 3) How do I turn "-M" mode on from within a GAWK source file?
> I.e., just as we can use things like "-i" and "-l" on the command
> line, or we can us "@include" or "@load" inside the source code, is
> there a similar trick to use inside the code to turn on "-M" mode?

While I seem to recall to have done something like that (using @load) in
the past I'm not sure now (and too lazy to check the documents); maybe it
has been removed in recent versions, or maybe I was just misremembering.

Janis

[*] Actually since I noticed many years ago that even the bc command will
silently produce wrong results (depending on the precision used)[**] I'm
very reluctant to use MP in any tool without double checking the results.

[**] I seem to recall that POSIX guarantees only 99 digits accuracy, but
larger results are accepted (e.g. in GNU's bc), but correct [only] until
some margin; no error messages there either. (Don't know if that has ever
been fixed.)

Kenny McCormack

unread,
Feb 2, 2016, 11:30:14 AM2/2/16
to
In article <n8qiug$5oo$1...@news.m-online.net>,
Janis Papanagnou <janis_pa...@hotmail.com> wrote:
...
>> 3) How do I turn "-M" mode on from within a GAWK source file?
>> I.e., just as we can use things like "-i" and "-l" on the command
>> line, or we can us "@include" or "@load" inside the source code, is
>> there a similar trick to use inside the code to turn on "-M" mode?
>
>While I seem to recall to have done something like that (using @load) in
>the past I'm not sure now (and too lazy to check the documents); maybe it
>has been removed in recent versions, or maybe I was just misremembering.

My specific interest here is noting that:

$ cat zotz
#!/bin/gawk4 -M -f
{ print 2**$1 - 1 }
$ ./zotz

doesn't work (produces the GAWK usage message).

So, I need a command to put in the GAWK source to turn on GMP/MPFS mode.

--

Prayer has no place in the public schools, just like facts
have no place in organized religion.
-- Superintendent Chalmers

Hermann Peifer

unread,
Feb 2, 2016, 3:58:33 PM2/2/16
to
The largest integer one can *safely* store as double-precision
floating-point value is 2^53-1.

# This is why there is some precision loss here
# echo 61 |gawk '{ print 2**$1-3, 2**$1+3 }'
2305843009213693952 2305843009213693952

# Arbitrary-precision integers are enabled by using -M
echo 61 |gawk -M '{ print 2**$1-3, 2**$1+3 }'
2305843009213693949 2305843009213693955

See
http://www.gnu.org/software/gawk/manual/gawk.html#Arbitrary-Precision-Integers

Hermann

Kenny McCormack

unread,
Feb 2, 2016, 4:23:25 PM2/2/16
to
In article <n8qlik$e4a$1...@news.xmission.com>,
Kenny McCormack <gaz...@shell.xmission.com> wrote:
>In article <n8qiug$5oo$1...@news.m-online.net>,
>Janis Papanagnou <janis_pa...@hotmail.com> wrote:
>...
>>> 3) How do I turn "-M" mode on from within a GAWK source file?
>>> I.e., just as we can use things like "-i" and "-l" on the command
>>> line, or we can us "@include" or "@load" inside the source code, is
>>> there a similar trick to use inside the code to turn on "-M" mode?
>>
>>While I seem to recall to have done something like that (using @load) in
>>the past I'm not sure now (and too lazy to check the documents); maybe it
>>has been removed in recent versions, or maybe I was just misremembering.
>
>My specific interest here is noting that:
>
> $ cat zotz
> #!/bin/gawk4 -M -f
> { print 2**$1 - 1 }
> $ ./zotz
>
>doesn't work (produces the GAWK usage message).
>
>So, I need a command to put in the GAWK source to turn on GMP/MPFS mode.

Actually, changing it to:

#!/bin/gawk4 -Mf

works. Still, it'd be nice to be able to do it directly in the source
code.

It'd also be nice to know why some systems don't allow more than one option
to be passed on the #! line - and which specific systems these are.

--
https://en.wikipedia.org/wiki/Mansplaining

It describes comp.lang.c to a T!

Janis Papanagnou

unread,
Feb 2, 2016, 5:42:49 PM2/2/16
to
On 02.02.2016 22:23, Kenny McCormack wrote:
> [...]
> It'd also be nice to know why some systems don't allow more than one option
> to be passed on the #! line - and which specific systems these are.

You probably find the answer here:

http://www.in-ulm.de/~mascheck/various/shebang/

Specifically in paragraph "Splitting arguments" and in the table at the end
of the page.

Janis

0 new messages