groff
[Top][All Lists]
Advanced

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

[Groff] Arithmetic in groff


From: Joachim Walsdorff
Subject: [Groff] Arithmetic in groff
Date: Tue, 20 Oct 2009 20:41:39 +0200
User-agent: Thunderbird 2.0.0.23 (Windows/20090812)

Here is a little macro to enhance the restricted groff-arithmetics. It uses the unix bc-command for arbitrary precision calculations (see the bc-manpage). Maybe it's easier to use for people (like me) who are not familiar with pic to use its numerical engine like suggested by Ted:

.de calc
.  sy  echo -n ".ds *calc " > /tmp/calc; echo "\\$*" | bc -l >> /tmp/calc
.  pso cat /tmp/calc; rm /tmp/calc
..

The argument of calc is the expression to be calculated; string *calc returns the result. groff must be called with -U, because the register .U is readonly and therefore it is not possible to change between safe/unsafe-mode inside groff.

Examples:

.calc scale=5; 10^2      \" scale=5: 5 digits right from the decimal point.
.tm \*[*calc] => 100.00000

The second example should yield the same result:

.calc scale=5; e(2*l(10))\" e=exponential function, l=natural logarithm.
.tm \*[*calc] => 99.99898

This result is not exact, since bc /truncates/ (without rounding) the values of e(2) and l (10) to 5 digits (value of scale).

Joachim



reply via email to

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