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

Inverse trigonometric functions in awk

3,219 views
Skip to first unread message

Melroy

unread,
Jul 2, 2003, 4:57:09 PM7/2/03
to
do asin, acos exist in awk,gawk or nawk?
from internet search I would have thought they would.
But they don't seem to exist when i try to use them.
when I type
awk '{print acos($1) ,$2}' effarea.dat
I get the message:
awk: cmd. line:1: (FILENAME=effarea.dat FNR=1) fatal: function `acos' not defined

same thing happened when I used nawk or gawk.
Is there a way to do this?
Melroy

Charles Demas

unread,
Jul 2, 2003, 6:41:50 PM7/2/03
to
In article <1b7c3dda.0307...@posting.google.com>,

Use mathematical identities such as:

(sin a)**2 + (cos a)**2 = 1
tan a = (sin a)/(cos a)

so with a value v that is the cosine of an angle a,

tan(a) = sin(a)/cos(a) = sqrt(1 - (v)*(v))/(v)

tan(a) = sqrt((1/(v*v)) - 1)

a = atan2(sqrt(1/(v*v)) -1)

you can so the same thing starting with sine instead of cosine

Make sure you don't divide by zero. :-)


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
de...@theworld.com | \___/ | http://world.std.com/~cpd

Tapani Tarvainen

unread,
Jul 3, 2003, 3:52:06 AM7/3/03
to
melroy...@hotmail.com (Melroy) writes:

> do asin, acos exist in awk,gawk or nawk?

No. They are easily defined, however:

function asin(x) { return atan2(x, sqrt(1-x*x)) }
function acos(x) { return atan2(sqrt(1-x*x), x) }
function atan(x) { return atan2(x,1) }

Beware of rounding errors with arguments close to 1.

--
Tapani Tarvainen

0 new messages