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

Tan in awk?

1,822 views
Skip to first unread message

Darryl Lewis

unread,
May 2, 1997, 3:00:00 AM5/2/97
to

Is it possible to generate Tan (math function) in awk? I know that there
is sin and cos, but no tan.
Is it also possible to get the inverse function? ie inverse sin, inverse
cos , inverse tan ?

Thanks in advance

--
Darryl
Australia
IMAGINE INFO #####\ _ /##### AMOS INFO
Amiga 2000 #( )# | _( )__ | #( )# AmosPro + Compiler
Amiga 3000T ##### | /_ / | ##### APME (Get it!)
486DX2-66 #" "# | ___m/I_ //_____ | #" "# Turbo extension
Imagine 3.0 # O # |____#-x.\ /++m\ /.x-#____| # O # 3D extension
Intermediate #m.m# | /" \ ///###\\\ / "\ | #m.m# BBS extension
#####/ ######/ \###### \#####
Profession- Disk Jockey/System Administrator
http://www.dragonfire.net/~daz/home.htm


BC

unread,
May 2, 1997, 3:00:00 AM5/2/97
to

Darryl,
Yes you can generate a tan result, but you need to get a version that
has it in it. The orginal awk did not include this. I know that nawk
has this function and I'm pretty sure that gawk and mawk has it. What
type machine are you using it on? You can download gawk from the GNU
site or do a search on nawk and find somewhere to get it from.

Hope this helps,

Bob Cross
----------------------

Q. Richard Chen

unread,
May 3, 1997, 3:00:00 AM5/3/97
to

Darryl Lewis wrote:
>
> Is it possible to generate Tan (math function) in awk? I know that there
> is sin and cos, but no tan.
> Is it also possible to get the inverse function? ie inverse sin, inverse
> cos , inverse tan ?


If you are stuck with the old awk, there are still ways to do it.
A little math helps.
tan(x)=sin(x)/cos(x)
so tan can be obtained from sin and cos.

awk comes with arctan function. then arcsin and arccos can be
easily computed:
Let u = arcsin(x), then x = sin(u) and
tan(u)=sin(u)/cos(u)=sin(u)/sqrt(1-sin(u)^2)=x/sqrt(1-x^2)
so
u=arcsin(x)=arctan(x/sqrt(1-x^2))

arccos can be computed the same as above using arctan alone.


cheers

Steve Critchlow

unread,
May 3, 1997, 3:00:00 AM5/3/97
to

On 2 May 1997 15:44:41 GMT, Darryl Lewis <dar...@cafu.fl.net.au>
wrote:

>
>Is it possible to generate Tan (math function) in awk? I know that there
>is sin and cos, but no tan.

tan = sin / cos

>Is it also possible to get the inverse function? ie inverse sin, inverse
>cos , inverse tan ?

I would first check with a good book on trigonometry, to see if there
is some means of getting these values, using the functions you have
available. ISTR there is a polynomial series of inverse sin etc,
there certainly is for calculating sin in the first place.

If that fails, I would generate a table of say 90 entries, and
calculate the sin of each angle in the range of 0 to 90 degrees. Then
I would use linear interpolation to calculate the angle from a given
sin value.

I can't see either method being particularly efficient at the moment.

--
Steve

Maximilian Schulze

unread,
May 4, 1997, 3:00:00 AM5/4/97
to

Darryl Lewis <dar...@cafu.fl.net.au> writes:

> Is it possible to generate Tan (math function) in awk? I know that there
> is sin and cos, but no tan.

> Is it also possible to get the inverse function? ie inverse sin, inverse
> cos , inverse tan ?
>

Well, if you have sin(x) and cos(x), then it is extremely easy to compute
tan(x). The following is an awk solution for this:

function tan( x ) {
return( sin( x ) / cos( x ) )
}

You could calculate the inverse functions of the three basic trigonometric
functions by using their power series, but this might be very
inefficient. Try to get the source of the math library of a C compiler or
so; you might find there a more efficient solution (probably using some
sort of table lookup).

Hope this helps,

Max


--
----------------------------------------------------------------------
Bruno Maximilian Schulze Maximilia...@grenoble.rxrc.xerox.com
Multilingual Theory and Technology
Rank Xerox Research Centre Phone: +33 (0)4 76 61 50 86
6 chemin de Maupertuis Fax: +33 (0)4 76 61 50 99
F-38240 Meylan France URL: http://www.xerox.fr
----------------------------------------------------------------------

Maximilian Schulze

unread,
May 5, 1997, 3:00:00 AM5/5/97
to

"Q. Richard Chen" <ch...@fractal.eng.yale.edu> writes:

>
> Darryl Lewis wrote:
> >
> > Is it possible to generate Tan (math function) in awk? I know that there
> > is sin and cos, but no tan.
> > Is it also possible to get the inverse function? ie inverse sin, inverse
> > cos , inverse tan ?
>
>

> If you are stuck with the old awk, there are still ways to do it.

Please show me the _new_ awk that has a built-in tan(x) -- maybe
'wonder boy' tawk. As far as I know neither gawk nor mawk lack of this
function too.

> A little math helps.
> tan(x)=sin(x)/cos(x)
> so tan can be obtained from sin and cos.
>
> awk comes with arctan function.

> [...]
WRONG! awk (which I use equivalent for [gm]awk) have the atan2(y, x)
function. Have a look in the man page, e.g. gawk's:

atan2(y, x) returns the arctangent of y/x in radians.

This function isn't arctan(x). Right?

Jim Hart

unread,
May 5, 1997, 3:00:00 AM5/5/97
to

In message <5kd259$c...@kosh.fl.net.au> Darryl Lewis <dar...@cafu.fl.net.au> writes:
>
>Is it possible to generate Tan (math function) in awk? I know that there
>is sin and cos, but no tan.
>Is it also possible to get the inverse function? ie inverse sin, inverse
>cos , inverse tan ?
>
No offense, but you need to read a simple trig text book.
Tangent is the ratio of cosine and sine ( I forget which divides
into which). As for inverses, are you talking about arcsin or
inverse sine? The inverse of the sine is the cosine. arcsin is
"the angle whose sine is...", which requires lookup tables or a
scientific calculator. You should be able to get a trigonometry
book at your local library.

---
Jim Hart <jh...@h5.avcnet.org>
"Working together, we _can_ make a difference."
"Without order nothing can exist - without chaos nothing can evolve."
"If enough of us dream a new dream, humanity will change."

Patrick TJ McPhee

unread,
May 6, 1997, 3:00:00 AM5/6/97
to

In article <89744ede...@h5.avcnet.org>,
Jim Hart <jh...@h5.avcnet.org> wrote:
% In message <5kd259$c...@kosh.fl.net.au> Darryl Lewis <dar...@cafu.fl.net.au> writes:
% >
% >Is it possible to generate Tan (math function) in awk? I know that there
% >is sin and cos, but no tan.

% No offense, but you need to read a simple trig text book.
% Tangent is the ratio of cosine and sine ( I forget which divides
% into which).

I guess anyone who can't remember which divides into which really ought
to read a simple trig book. I mean, how can you live?

% As for inverses, are you talking about arcsin or
% inverse sine? The inverse of the sine is the cosine.

I'm posting because of this. The inverse of sine is not cosine.
sin(theta) = cos(theta-pi/2)
the values are always between 1 and -1.

You may recall
tan(theta) = sin(theta)/cos(theta)
and also that
sin^2(x) + cos^2(x) = 1 for all x

This means that if
asin(x) = theta
then
x = sin(theta)
= cos(theta)*tan(theta)
= sqrt(1-sin^2(theta))*tan(theta)
= sqrt(1-x^2)*tan(theta)
x/sqrt(1-x^2) = tan(theta)
theta = atan(x/sqrt(1-x^2))

On my machine, awk provides atan2, so that
asin(x) = atan2(x, sqrt(1-x**x))
provided -1 < x < 1.

I don't feel like figuring out acos, but it's very similar. Probably
you just reverse the arguments to atan2.

--
Patrick TJ McPhee
East York Canada
pt...@ican.net


Carlos Costa e Silva

unread,
May 7, 1997, 3:00:00 AM5/7/97
to

Maximilian Schulze wrote in article ...
:"Q. Richard Chen" <ch...@fractal.eng.yale.edu> writes:
:
:>
:> [...]


:WRONG! awk (which I use equivalent for [gm]awk) have the atan2(y, x)
:function. Have a look in the man page, e.g. gawk's:
:
: atan2(y, x) returns the arctangent of y/x in radians.
:
:This function isn't arctan(x). Right?

:

i tihink you want atan2(sin(r),cos(r))

y = sin(r), x = cos(r), arctan(r) = atan2( sin(r), cos(r) )

----
Carlos Costa e Silva <c...@minimal.pt>

Carlos Costa e Silva

unread,
May 7, 1997, 3:00:00 AM5/7/97
to

Q. Richard Chen

unread,
May 8, 1997, 3:00:00 AM5/8/97
to Carlos Costa e Silva

atan2(y,x) is arctan(y) if you put x=1. Use the freedom at your
disposal :-)


The above computation is wrong:
atan2(sin(r),cos(r)) = r
if r is in the first quadrant.


regards.


---

Carlos Costa e Silva

unread,
May 12, 1997, 3:00:00 AM5/12/97
to

Q. Richard Chen wrote in article <337273...@fractal.eng.yale.edu>...


:Carlos Costa e Silva wrote:
:>
:> Maximilian Schulze wrote in article ...
:> :"Q. Richard Chen" <ch...@fractal.eng.yale.edu> writes:
:> :
:> :>
:> :> [...]
:> :WRONG! awk (which I use equivalent for [gm]awk) have the atan2(y, x)
:> :function. Have a look in the man page, e.g. gawk's:
:> :
:> : atan2(y, x) returns the arctangent of y/x in radians.
:> :
:> :This function isn't arctan(x). Right?
:> :
:>
:> i tihink you want atan2(sin(r),cos(r))
:>
:> y = sin(r), x = cos(r), arctan(r) = atan2( sin(r), cos(r) )
:>
:> ----
:> Carlos Costa e Silva <c...@minimal.pt>
:
:atan2(y,x) is arctan(y) if you put x=1. Use the freedom at your
:disposal :-)

:

I assumed no gigo (garbage in, garbage out), only valid input:
If y <> 0 then x = 1 cannot be the cosine of any angle
and arctan(r) <> atan2( sin(r)/cos(r) ) because
either y <> sin(r) or x <> = cos(r)

.

:
:The above computation is wrong:


: atan2(sin(r),cos(r)) = r
:if r is in the first quadrant.
:
:
:regards.

:
:
:

If you look at the output of the following awk:
BEGIN {
PI = 3.14156;
TWOPI = 2 * PI;
inc = PI/8;
for(r = 0; r <= TWOPI; r += inc )
print r ":" atan2(sin(r),cos(r)) " r-TWOPI: " r-TWOPI;
}

0:0 r-TWOPI: -6.28312 first and second quadrant
0.392695:0.392695 r-TWOPI: -5.89043
0.78539:0.78539 r-TWOPI: -5.49773
1.17809:1.17809 r-TWOPI: -5.10503
1.57078:1.57078 r-TWOPI: -4.71234
1.96348:1.96348 r-TWOPI: -4.31965
2.35617:2.35617 r-TWOPI: -3.92695
2.74887:2.74887 r-TWOPI: -3.53425
3.14156:3.14156 r-TWOPI: -3.14156 third and tourth quadrant
3.53425:-2.74893 r-TWOPI: -2.74887
3.92695:-2.35624 r-TWOPI: -2.35617
4.31964:-1.96354 r-TWOPI: -1.96348
4.71234:-1.57085 r-TWOPI: -1.57078
5.10503:-1.17815 r-TWOPI: -1.17809
5.49773:-0.785455 r-TWOPI: -0.78539
5.89042:-0.39276 r-TWOPI: -0.392695
6.28312:-6.53072e-05 r-TWOPI: -1.77636e-15

Maybe your version of awk produces different results ?

Your's Carlos

0 new messages