📄 ch02_13.htm
字号:
<HTML><HEAD><TITLE>Recipe 2.12. Calculating More Trigonometric Functions (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:30:16Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch02_01.htm"TITLE="2. Numbers"><LINKREL="prev"HREF="ch02_12.htm"TITLE="2.11. Doing Trigonometry in Degrees, not Radians"><LINKREL="next"HREF="ch02_14.htm"TITLE="2.13. Taking Logarithms"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_12.htm"TITLE="2.11. Doing Trigonometry in Degrees, not Radians"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.11. Doing Trigonometry in Degrees, not Radians"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch02_01.htm"TITLE="2. Numbers"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_14.htm"TITLE="2.13. Taking Logarithms"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.13. Taking Logarithms"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch02-chap02_calculating_0">2.12. Calculating More Trigonometric Functions</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-881">Problem</A></H3><PCLASS="para">You want to calculate values for trigonometric functions like sine, tangent, or arc-cosine.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-887">Solution</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch02-idx-1000008069-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008069-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008069-2"></A>Perl provides only <CODECLASS="literal">sin</CODE>, <CODECLASS="literal">cos</CODE>, and <CODECLASS="literal">atan2</CODE> as standard functions. From these, you can derive <CODECLASS="literal">tan</CODE> and the other trig functions:</P><PRECLASS="programlisting">sub tan { my $theta = shift; return sin($theta)/cos($theta);}</PRE><PCLASS="para">The <ACLASS="indexterm"NAME="ch02-idx-1000008077-0"></A>POSIX module provides a wider range of trig functions:</P><PRECLASS="programlisting">use POSIX;$y = acos(3.7);</PRE><PCLASS="para">The Math::Trig module provides a complete set of functions and supports operations on or resulting in complex numbers:</P><PRECLASS="programlisting">use Math::Trig;$y = acos(3.7);</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-919">Discussion</A></H3><PCLASS="para">The <CODECLASS="literal">tan</CODE> function will cause a division-by-zero exception when <CODECLASS="literal">$theta</CODE> is <IMGCLASS="graphic"SRC="figs/pid2.gif"ALT="Graphic">, <IMGCLASS="graphic"SRC="figs/3pid2.gif"ALT="Graphic">, and so on, because the cosine is 0 for these values. Similarly, <CODECLASS="literal">tan</CODE> and many other functions from Math::Trig may generate the same error. To trap these, use <CODECLASS="literal">eval</CODE>:</P><PRECLASS="programlisting">eval { $y = tan($pi/2);} or return undef;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1000006099">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">sin</CODE>, <CODECLASS="literal">cos</CODE>, and <CODECLASS="literal">atan2</CODE> functions in <EMCLASS="emphasis">perlfunc</EM> (1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; we talk about trigonometry in the context of imaginary numbers in <ACLASS="xref"HREF="ch02_16.htm"TITLE="Using Complex Numbers">Recipe 2.15</A>; we talk about the use of <CODECLASS="literal">eval</CODE> to catch exceptions in <ACLASS="xref"HREF="ch10_13.htm"TITLE="Handling Exceptions">Recipe 10.12</A> <ACLASS="indexterm"NAME="ch02-idx-1000008070-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008070-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008070-2"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_12.htm"TITLE="2.11. Doing Trigonometry in Degrees, not Radians"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.11. Doing Trigonometry in Degrees, not Radians"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_14.htm"TITLE="2.13. Taking Logarithms"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.13. Taking Logarithms"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">2.11. Doing Trigonometry in Degrees, not Radians</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">2.13. Taking Logarithms</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -