groff
[Top][All Lists]
Advanced

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

Re: [Groff] Floating point arithmetic in groff


From: Ted Harding
Subject: Re: [Groff] Floating point arithmetic in groff
Date: Tue, 31 Jan 2006 15:05:44 -0000 (GMT)

On 31-Jan-06 M Bianchi wrote:
> On Tue, Jan 31, 2006 at 07:38:54AM +0100, Werner LEMBERG wrote:
>> > Is anyone aware of a means by which floating point arithmetic
>> > and formating can be computed either within groff or in a
>> > preprocessor (ala eqn, tbl, etc.)?
>> 
>> Not that I know of.  Why do you need it?
> 
> I use  groff -mm  for all my documents, and sometimes have tables
>  that are
>       Quantity   Each   Total
> 
> and it would be nice to simply be able to use floating number
> registers to do the arithmetic.

Unfortunately groff's numner registers are integer-valued!

Also, groff's table-formatting resources do not include a
built-in facility to make calculations (spreadsheet-style)
using the table entries (unlike some word processors).

There are a few approaches to what you want to do.

One would be to run your source file through 'awk' before
groff sees it, and let 'awk' fill in the calculated values.

Within groff, you can use 'pic' which has a good numerical
engine built in to it. Again, there are diferent ways of doing
this, but a straightforward one is as follows.

Suppose you have a file "temp.pic", say, which contains
two columns, one for "Quantity" and the second for "Each",
like

   7 13.5
  21 35.5
   9 11.99
  27 46.25

Then you can write a troff source file "temp.tr" as follows:

.P
.PS
total=0
gtotal=0
define mktotals {
total = $1*$2
gtotal = gtotal + total
command sprintf("%.0f#%.2f#%.2f",$1,$2,total)
}
.PE
Here is your itemised bill.
.PS
command ".TS"
command "center tab(#);"
command "c c c."
command "Quantity#Each#Total"
command "_"
command ".T&"
command "n n n."
copy "temp.pic" thru mktotals
command "="
command sprintf("##%.2f",gtotal)
command ".TE"
.PE


Finally, run this through 'pic' and then troff as follows
(see below for comment on the 'awk' section of the pipeline):

pic temp.tr | awk '/^\.lf/{next}; {print $0}' | groff -t -mm > temp.ps

which will produce a PS file which contains what you would want
in this case. Using the "ascii" device with the command

pic temp.tr | awk '/^\.lf/{next}; {print $0}' | groff -Tascii -t -mm

produces the output:

                                   - 1 -



       Here is your itemised bill.
                        Quantity   Each     Total
                        ---------------------------
                            7      13.50     94.50
                           21      35.50    745.50
                            9      11.99    107.91
                           27      46.25   1248.75
                        ---------------------------
                                           2196.66



The reason that "| awk '/^\.lf/{next}; {print $0}" is there
in the pipeline is that the ".lf" lines output by 'pic'
seem to confuse 'tbl'; the 'awk' step removes them.

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <address@hidden>
Fax-to-email: +44 (0)870 094 0861
Date: 31-Jan-06                                       Time: 15:05:41
------------------------------ XFMail ------------------------------




reply via email to

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