CINXE.COM

Math::Trig - trigonometric functions - Perldoc Browser

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Math::Trig - trigonometric functions - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/Math::Trig"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.22.0</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/Math::Trig">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/Math::Trig">5.40.1</a> <a class="dropdown-item" href="/5.40.0/Math::Trig">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/Math::Trig">5.38.3</a> <a class="dropdown-item" href="/5.38.2/Math::Trig">5.38.2</a> <a class="dropdown-item" href="/5.38.1/Math::Trig">5.38.1</a> <a class="dropdown-item" href="/5.38.0/Math::Trig">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/Math::Trig">5.36.3</a> <a class="dropdown-item" href="/5.36.2/Math::Trig">5.36.2</a> <a class="dropdown-item" href="/5.36.1/Math::Trig">5.36.1</a> <a class="dropdown-item" href="/5.36.0/Math::Trig">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/Math::Trig">5.34.3</a> <a class="dropdown-item" href="/5.34.2/Math::Trig">5.34.2</a> <a class="dropdown-item" href="/5.34.1/Math::Trig">5.34.1</a> <a class="dropdown-item" href="/5.34.0/Math::Trig">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/Math::Trig">5.32.1</a> <a class="dropdown-item" href="/5.32.0/Math::Trig">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/Math::Trig">5.30.3</a> <a class="dropdown-item" href="/5.30.2/Math::Trig">5.30.2</a> <a class="dropdown-item" href="/5.30.1/Math::Trig">5.30.1</a> <a class="dropdown-item" href="/5.30.0/Math::Trig">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/Math::Trig">5.28.3</a> <a class="dropdown-item" href="/5.28.2/Math::Trig">5.28.2</a> <a class="dropdown-item" href="/5.28.1/Math::Trig">5.28.1</a> <a class="dropdown-item" href="/5.28.0/Math::Trig">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/Math::Trig">5.26.3</a> <a class="dropdown-item" href="/5.26.2/Math::Trig">5.26.2</a> <a class="dropdown-item" href="/5.26.1/Math::Trig">5.26.1</a> <a class="dropdown-item" href="/5.26.0/Math::Trig">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/Math::Trig">5.24.4</a> <a class="dropdown-item" href="/5.24.3/Math::Trig">5.24.3</a> <a class="dropdown-item" href="/5.24.2/Math::Trig">5.24.2</a> <a class="dropdown-item" href="/5.24.1/Math::Trig">5.24.1</a> <a class="dropdown-item" href="/5.24.0/Math::Trig">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/Math::Trig">5.22.4</a> <a class="dropdown-item" href="/5.22.3/Math::Trig">5.22.3</a> <a class="dropdown-item" href="/5.22.2/Math::Trig">5.22.2</a> <a class="dropdown-item" href="/5.22.1/Math::Trig">5.22.1</a> <a class="dropdown-item active" href="/5.22.0/Math::Trig">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/Math::Trig">5.20.3</a> <a class="dropdown-item" href="/5.20.2/Math::Trig">5.20.2</a> <a class="dropdown-item" href="/5.20.1/Math::Trig">5.20.1</a> <a class="dropdown-item" href="/5.20.0/Math::Trig">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/Math::Trig">5.18.4</a> <a class="dropdown-item" href="/5.18.3/Math::Trig">5.18.3</a> <a class="dropdown-item" href="/5.18.2/Math::Trig">5.18.2</a> <a class="dropdown-item" href="/5.18.1/Math::Trig">5.18.1</a> <a class="dropdown-item" href="/5.18.0/Math::Trig">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/Math::Trig">5.16.3</a> <a class="dropdown-item" href="/5.16.2/Math::Trig">5.16.2</a> <a class="dropdown-item" href="/5.16.1/Math::Trig">5.16.1</a> <a class="dropdown-item" href="/5.16.0/Math::Trig">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/Math::Trig">5.14.4</a> <a class="dropdown-item" href="/5.14.3/Math::Trig">5.14.3</a> <a class="dropdown-item" href="/5.14.2/Math::Trig">5.14.2</a> <a class="dropdown-item" href="/5.14.1/Math::Trig">5.14.1</a> <a class="dropdown-item" href="/5.14.0/Math::Trig">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/Math::Trig">5.12.5</a> <a class="dropdown-item" href="/5.12.4/Math::Trig">5.12.4</a> <a class="dropdown-item" href="/5.12.3/Math::Trig">5.12.3</a> <a class="dropdown-item" href="/5.12.2/Math::Trig">5.12.2</a> <a class="dropdown-item" href="/5.12.1/Math::Trig">5.12.1</a> <a class="dropdown-item" href="/5.12.0/Math::Trig">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/Math::Trig">5.10.1</a> <a class="dropdown-item" href="/5.10.0/Math::Trig">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/Math::Trig">5.8.9</a> <a class="dropdown-item" href="/5.8.8/Math::Trig">5.8.8</a> <a class="dropdown-item" href="/5.8.7/Math::Trig">5.8.7</a> <a class="dropdown-item" href="/5.8.6/Math::Trig">5.8.6</a> <a class="dropdown-item" href="/5.8.5/Math::Trig">5.8.5</a> <a class="dropdown-item" href="/5.8.4/Math::Trig">5.8.4</a> <a class="dropdown-item" href="/5.8.3/Math::Trig">5.8.3</a> <a class="dropdown-item" href="/5.8.2/Math::Trig">5.8.2</a> <a class="dropdown-item" href="/5.8.1/Math::Trig">5.8.1</a> <a class="dropdown-item" href="/5.8.0/Math::Trig">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/Math::Trig">5.6.2</a> <a class="dropdown-item" href="/5.6.1/Math::Trig">5.6.1</a> <a class="dropdown-item" href="/5.6.0/Math::Trig">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/Math::Trig">5.005_04</a> <a class="dropdown-item" href="/5.005_03/Math::Trig">5.005_03</a> <a class="dropdown-item" href="/5.005_02/Math::Trig">5.005_02</a> <a class="dropdown-item" href="/5.005_01/Math::Trig">5.005_01</a> <a class="dropdown-item" href="/5.005/Math::Trig">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/Math::Trig">blead</a> <a class="dropdown-item" href="/5.41.10/Math::Trig">5.41.10</a> <a class="dropdown-item" href="/5.41.9/Math::Trig">5.41.9</a> <a class="dropdown-item" href="/5.41.8/Math::Trig">5.41.8</a> <a class="dropdown-item" href="/5.41.7/Math::Trig">5.41.7</a> <a class="dropdown-item" href="/5.41.6/Math::Trig">5.41.6</a> <a class="dropdown-item" href="/5.41.5/Math::Trig">5.41.5</a> <a class="dropdown-item" href="/5.41.4/Math::Trig">5.41.4</a> <a class="dropdown-item" href="/5.41.3/Math::Trig">5.41.3</a> <a class="dropdown-item" href="/5.41.2/Math::Trig">5.41.2</a> <a class="dropdown-item" href="/5.41.1/Math::Trig">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/Math::Trig">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/Math::Trig">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/Math::Trig">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/Math::Trig">5.39.10</a> <a class="dropdown-item" href="/5.39.9/Math::Trig">5.39.9</a> <a class="dropdown-item" href="/5.39.8/Math::Trig">5.39.8</a> <a class="dropdown-item" href="/5.39.7/Math::Trig">5.39.7</a> <a class="dropdown-item" href="/5.39.6/Math::Trig">5.39.6</a> <a class="dropdown-item" href="/5.39.5/Math::Trig">5.39.5</a> <a class="dropdown-item" href="/5.39.4/Math::Trig">5.39.4</a> <a class="dropdown-item" href="/5.39.3/Math::Trig">5.39.3</a> <a class="dropdown-item" href="/5.39.2/Math::Trig">5.39.2</a> <a class="dropdown-item" href="/5.39.1/Math::Trig">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/Math::Trig">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.22.0/perl">Perl</a> <a class="dropdown-item" href="/5.22.0/perlintro">Intro</a> <a class="dropdown-item" href="/5.22.0/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.22.0/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.22.0/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.0/perlop">Operators</a> <a class="dropdown-item" href="/5.22.0/functions">Functions</a> <a class="dropdown-item" href="/5.22.0/variables">Variables</a> <a class="dropdown-item" href="/5.22.0/modules">Modules</a> <a class="dropdown-item" href="/5.22.0/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.0/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.22.0/perlcommunity">Community</a> <a class="dropdown-item" href="/5.22.0/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.22.0/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.22.0/Math::Trig">Math::Trig</a> <div id="more"> (<a href="/5.22.0/Math::Trig.txt">source</a>, <a href="https://metacpan.org/pod/Math::Trig">CPAN</a>) </div> <div id="moduleversion">version 1.23</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.22.0. <a href="/Math::Trig">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#TRIGONOMETRIC-FUNCTIONS">TRIGONOMETRIC FUNCTIONS</a> <ul> <li> <a class="text-decoration-none" href="#ERRORS-DUE-TO-DIVISION-BY-ZERO">ERRORS DUE TO DIVISION BY ZERO</a> </li> <li> <a class="text-decoration-none" href="#SIMPLE-(REAL)-ARGUMENTS,-COMPLEX-RESULTS">SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#PLANE-ANGLE-CONVERSIONS">PLANE ANGLE CONVERSIONS</a> </li> <li> <a class="text-decoration-none" href="#RADIAL-COORDINATE-CONVERSIONS">RADIAL COORDINATE CONVERSIONS</a> <ul> <li> <a class="text-decoration-none" href="#COORDINATE-SYSTEMS">COORDINATE SYSTEMS</a> </li> <li> <a class="text-decoration-none" href="#3-D-ANGLE-CONVERSIONS">3-D ANGLE CONVERSIONS</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#GREAT-CIRCLE-DISTANCES-AND-DIRECTIONS">GREAT CIRCLE DISTANCES AND DIRECTIONS</a> <ul> <li> <a class="text-decoration-none" href="#great_circle_distance">great_circle_distance</a> </li> <li> <a class="text-decoration-none" href="#great_circle_direction">great_circle_direction</a> </li> <li> <a class="text-decoration-none" href="#great_circle_bearing">great_circle_bearing</a> </li> <li> <a class="text-decoration-none" href="#great_circle_destination">great_circle_destination</a> </li> <li> <a class="text-decoration-none" href="#great_circle_midpoint">great_circle_midpoint</a> </li> <li> <a class="text-decoration-none" href="#great_circle_waypoint">great_circle_waypoint</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#EXAMPLES">EXAMPLES</a> <ul> <li> <a class="text-decoration-none" href="#CAVEAT-FOR-GREAT-CIRCLE-FORMULAS">CAVEAT FOR GREAT CIRCLE FORMULAS</a> </li> <li> <a class="text-decoration-none" href="#Real-valued-asin-and-acos">Real-valued asin and acos</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#BUGS">BUGS</a> </li> <li> <a class="text-decoration-none" href="#AUTHORS">AUTHORS</a> </li> <li> <a class="text-decoration-none" href="#LICENSE">LICENSE</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>Math::Trig - trigonometric functions</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use Math::Trig; $x = tan(0.9); $y = acos(3.7); $z = asin(2.4); $halfpi = pi/2; $rad = deg2rad(120); # Import constants pi2, pip2, pip4 (2*pi, pi/2, pi/4). use Math::Trig &#39;:pi&#39;; # Import the conversions between cartesian/spherical/cylindrical. use Math::Trig &#39;:radial&#39;; # Import the great circle formulas. use Math::Trig &#39;:great_circle&#39;;</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p><code>Math::Trig</code> defines many trigonometric functions not defined by the core Perl which defines only the <code>sin()</code> and <code>cos()</code>. The constant <b>pi</b> is also defined as are a few convenience functions for angle conversions, and <i>great circle formulas</i> for spherical movement.</p> <h1 id="TRIGONOMETRIC-FUNCTIONS"><a class="permalink" href="#TRIGONOMETRIC-FUNCTIONS">#</a><a id="TRIGONOMETRIC"></a>TRIGONOMETRIC FUNCTIONS</h1> <p>The tangent</p> <dl> <dt id="tan"><a class="permalink" href="#tan">#</a><b>tan</b></dt> <dd> </dd> </dl> <p>The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot are aliases)</p> <p><b>csc</b>, <b>cosec</b>, <b>sec</b>, <b>sec</b>, <b>cot</b>, <b>cotan</b></p> <p>The arcus (also known as the inverse) functions of the sine, cosine, and tangent</p> <p><b>asin</b>, <b>acos</b>, <b>atan</b></p> <p>The principal value of the arc tangent of y/x</p> <p><b>atan2</b>(y, x)</p> <p>The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and acotan/acot are aliases). Note that atan2(0, 0) is not well-defined.</p> <p><b>acsc</b>, <b>acosec</b>, <b>asec</b>, <b>acot</b>, <b>acotan</b></p> <p>The hyperbolic sine, cosine, and tangent</p> <p><b>sinh</b>, <b>cosh</b>, <b>tanh</b></p> <p>The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch and cotanh/coth are aliases)</p> <p><b>csch</b>, <b>cosech</b>, <b>sech</b>, <b>coth</b>, <b>cotanh</b></p> <p>The area (also known as the inverse) functions of the hyperbolic sine, cosine, and tangent</p> <p><b>asinh</b>, <b>acosh</b>, <b>atanh</b></p> <p>The area cofunctions of the hyperbolic sine, cosine, and tangent (acsch/acosech and acoth/acotanh are aliases)</p> <p><b>acsch</b>, <b>acosech</b>, <b>asech</b>, <b>acoth</b>, <b>acotanh</b></p> <p>The trigonometric constant <b>pi</b> and some of handy multiples of it are also defined.</p> <p><b>pi, pi2, pi4, pip2, pip4</b></p> <h2 id="ERRORS-DUE-TO-DIVISION-BY-ZERO"><a class="permalink" href="#ERRORS-DUE-TO-DIVISION-BY-ZERO">#</a><a id="ERRORS"></a>ERRORS DUE TO DIVISION BY ZERO</h2> <p>The following functions</p> <pre><code class="plaintext">acoth acsc acsch asec asech atanh cot coth csc csch sec sech tan tanh</code></pre> <p>cannot be computed for all arguments because that would mean dividing by zero or taking logarithm of zero. These situations cause fatal runtime errors looking like this</p> <pre><code class="plaintext">cot(0): Division by zero. (Because in the definition of cot(0), the divisor sin(0) is 0) Died at ...</code></pre> <p>or</p> <pre><code class="plaintext">atanh(-1): Logarithm of zero. Died at...</code></pre> <p>For the <code>csc</code>, <code>cot</code>, <code>asec</code>, <code>acsc</code>, <code>acot</code>, <code>csch</code>, <code>coth</code>, <code>asech</code>, <code>acsch</code>, the argument cannot be <code>0</code> (zero). For the <code>atanh</code>, <code>acoth</code>, the argument cannot be <code>1</code> (one). For the <code>atanh</code>, <code>acoth</code>, the argument cannot be <code>-1</code> (minus one). For the <code>tan</code>, <code>sec</code>, <code>tanh</code>, <code>sech</code>, the argument cannot be <i>pi/2 + k * pi</i>, where <i>k</i> is any integer.</p> <p>Note that atan2(0, 0) is not well-defined.</p> <h2 id="SIMPLE-(REAL)-ARGUMENTS,-COMPLEX-RESULTS"><a class="permalink" href="#SIMPLE-(REAL)-ARGUMENTS,-COMPLEX-RESULTS">#</a><a id="SIMPLE"></a><a id="SIMPLE-REAL-ARGUMENTS-COMPLEX-RESULTS"></a>SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS</h2> <p>Please note that some of the trigonometric functions can break out from the <b>real axis</b> into the <b>complex plane</b>. For example <code>asin(2)</code> has no definition for plain real numbers but it has definition for complex numbers.</p> <p>In Perl terms this means that supplying the usual Perl numbers (also known as scalars, please see <a href="/5.22.0/perldata">perldata</a>) as input for the trigonometric functions might produce as output results that no more are simple real numbers: instead they are complex numbers.</p> <p>The <code>Math::Trig</code> handles this by using the <code>Math::Complex</code> package which knows how to handle complex numbers, please see <a href="/5.22.0/Math::Complex">Math::Complex</a> for more information. In practice you need not to worry about getting complex numbers as results because the <code>Math::Complex</code> takes care of details like for example how to display complex numbers. For example:</p> <pre><code>print asin(2), &quot;\n&quot;;</code></pre> <p>should produce something like this (take or leave few last decimals):</p> <pre><code class="plaintext">1.5707963267949-1.31695789692482i</code></pre> <p>That is, a complex number with the real part of approximately <code>1.571</code> and the imaginary part of approximately <code>-1.317</code>.</p> <h1 id="PLANE-ANGLE-CONVERSIONS"><a class="permalink" href="#PLANE-ANGLE-CONVERSIONS">#</a><a id="PLANE"></a>PLANE ANGLE CONVERSIONS</h1> <p>(Plane, 2-dimensional) angles may be converted with the following functions.</p> <dl> <dt id="deg2rad"><a class="permalink" href="#deg2rad">#</a>deg2rad</dt> <dd> <pre><code>$radians = deg2rad($degrees);</code></pre> </dd> <dt id="grad2rad"><a class="permalink" href="#grad2rad">#</a>grad2rad</dt> <dd> <pre><code>$radians = grad2rad($gradians);</code></pre> </dd> <dt id="rad2deg"><a class="permalink" href="#rad2deg">#</a>rad2deg</dt> <dd> <pre><code>$degrees = rad2deg($radians);</code></pre> </dd> <dt id="grad2deg"><a class="permalink" href="#grad2deg">#</a>grad2deg</dt> <dd> <pre><code>$degrees = grad2deg($gradians);</code></pre> </dd> <dt id="deg2grad"><a class="permalink" href="#deg2grad">#</a>deg2grad</dt> <dd> <pre><code>$gradians = deg2grad($degrees);</code></pre> </dd> <dt id="rad2grad"><a class="permalink" href="#rad2grad">#</a>rad2grad</dt> <dd> <pre><code>$gradians = rad2grad($radians);</code></pre> </dd> </dl> <p>The full circle is 2 <i>pi</i> radians or <i>360</i> degrees or <i>400</i> gradians. The result is by default wrapped to be inside the [0, {2pi,360,400}[ circle. If you don&#39;t want this, supply a true second argument:</p> <pre><code>$zillions_of_radians = deg2rad($zillions_of_degrees, 1); $negative_degrees = rad2deg($negative_radians, 1);</code></pre> <p>You can also do the wrapping explicitly by rad2rad(), deg2deg(), and grad2grad().</p> <dl> <dt id="rad2rad"><a class="permalink" href="#rad2rad">#</a>rad2rad</dt> <dd> <pre><code>$radians_wrapped_by_2pi = rad2rad($radians);</code></pre> </dd> <dt id="deg2deg"><a class="permalink" href="#deg2deg">#</a>deg2deg</dt> <dd> <pre><code>$degrees_wrapped_by_360 = deg2deg($degrees);</code></pre> </dd> <dt id="grad2grad"><a class="permalink" href="#grad2grad">#</a>grad2grad</dt> <dd> <pre><code>$gradians_wrapped_by_400 = grad2grad($gradians);</code></pre> </dd> </dl> <h1 id="RADIAL-COORDINATE-CONVERSIONS"><a class="permalink" href="#RADIAL-COORDINATE-CONVERSIONS">#</a><a id="RADIAL"></a>RADIAL COORDINATE CONVERSIONS</h1> <p><b>Radial coordinate systems</b> are the <b>spherical</b> and the <b>cylindrical</b> systems, explained shortly in more detail.</p> <p>You can import radial coordinate conversion functions by using the <code>:radial</code> tag:</p> <pre><code>use Math::Trig &#39;:radial&#39;; ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);</code></pre> <p><b>All angles are in radians</b>.</p> <h2 id="COORDINATE-SYSTEMS"><a class="permalink" href="#COORDINATE-SYSTEMS">#</a><a id="COORDINATE"></a>COORDINATE SYSTEMS</h2> <p><b>Cartesian</b> coordinates are the usual rectangular <i>(x, y, z)</i>-coordinates.</p> <p>Spherical coordinates, <i>(rho, theta, pi)</i>, are three-dimensional coordinates which define a point in three-dimensional space. They are based on a sphere surface. The radius of the sphere is <b>rho</b>, also known as the <i>radial</i> coordinate. The angle in the <i>xy</i>-plane (around the <i>z</i>-axis) is <b>theta</b>, also known as the <i>azimuthal</i> coordinate. The angle from the <i>z</i>-axis is <b>phi</b>, also known as the <i>polar</i> coordinate. The North Pole is therefore <i>0, 0, rho</i>, and the Gulf of Guinea (think of the missing big chunk of Africa) <i>0, pi/2, rho</i>. In geographical terms <i>phi</i> is latitude (northward positive, southward negative) and <i>theta</i> is longitude (eastward positive, westward negative).</p> <p><b>BEWARE</b>: some texts define <i>theta</i> and <i>phi</i> the other way round, some texts define the <i>phi</i> to start from the horizontal plane, some texts use <i>r</i> in place of <i>rho</i>.</p> <p>Cylindrical coordinates, <i>(rho, theta, z)</i>, are three-dimensional coordinates which define a point in three-dimensional space. They are based on a cylinder surface. The radius of the cylinder is <b>rho</b>, also known as the <i>radial</i> coordinate. The angle in the <i>xy</i>-plane (around the <i>z</i>-axis) is <b>theta</b>, also known as the <i>azimuthal</i> coordinate. The third coordinate is the <i>z</i>, pointing up from the <b>theta</b>-plane.</p> <h2 id="3-D-ANGLE-CONVERSIONS"><a class="permalink" href="#3-D-ANGLE-CONVERSIONS">#</a><a id="3"></a><a id="D-ANGLE-CONVERSIONS"></a>3-D ANGLE CONVERSIONS</h2> <p>Conversions to and from spherical and cylindrical coordinates are available. Please notice that the conversions are not necessarily reversible because of the equalities like <i>pi</i> angles being equal to <i>-pi</i> angles.</p> <dl> <dt id="cartesian_to_cylindrical"><a class="permalink" href="#cartesian_to_cylindrical">#</a>cartesian_to_cylindrical</dt> <dd> <pre><code>($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);</code></pre> </dd> <dt id="cartesian_to_spherical"><a class="permalink" href="#cartesian_to_spherical">#</a>cartesian_to_spherical</dt> <dd> <pre><code>($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);</code></pre> </dd> <dt id="cylindrical_to_cartesian"><a class="permalink" href="#cylindrical_to_cartesian">#</a>cylindrical_to_cartesian</dt> <dd> <pre><code>($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);</code></pre> </dd> <dt id="cylindrical_to_spherical"><a class="permalink" href="#cylindrical_to_spherical">#</a>cylindrical_to_spherical</dt> <dd> <pre><code>($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);</code></pre> <p>Notice that when <code>$z</code> is not 0 <code>$rho_s</code> is not equal to <code>$rho_c</code>.</p> </dd> <dt id="spherical_to_cartesian"><a class="permalink" href="#spherical_to_cartesian">#</a>spherical_to_cartesian</dt> <dd> <pre><code>($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);</code></pre> </dd> <dt id="spherical_to_cylindrical"><a class="permalink" href="#spherical_to_cylindrical">#</a>spherical_to_cylindrical</dt> <dd> <pre><code>($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);</code></pre> <p>Notice that when <code>$z</code> is not 0 <code>$rho_c</code> is not equal to <code>$rho_s</code>.</p> </dd> </dl> <h1 id="GREAT-CIRCLE-DISTANCES-AND-DIRECTIONS"><a class="permalink" href="#GREAT-CIRCLE-DISTANCES-AND-DIRECTIONS">#</a><a id="GREAT"></a>GREAT CIRCLE DISTANCES AND DIRECTIONS</h1> <p>A great circle is section of a circle that contains the circle diameter: the shortest distance between two (non-antipodal) points on the spherical surface goes along the great circle connecting those two points.</p> <h2 id="great_circle_distance"><a class="permalink" href="#great_circle_distance">#</a>great_circle_distance</h2> <p>You can compute spherical distances, called <b>great circle distances</b>, by importing the great_circle_distance() function:</p> <pre><code>use Math::Trig &#39;great_circle_distance&#39;; $distance = great_circle_distance($theta0, $phi0, $theta1, $phi1, [, $rho]);</code></pre> <p>The <i>great circle distance</i> is the shortest distance between two points on a sphere. The distance is in <code>$rho</code> units. The <code>$rho</code> is optional, it defaults to 1 (the unit sphere), therefore the distance defaults to radians.</p> <p>If you think geographically the <i>theta</i> are longitudes: zero at the Greenwhich meridian, eastward positive, westward negative -- and the <i>phi</i> are latitudes: zero at the North Pole, northward positive, southward negative. <b>NOTE</b>: this formula thinks in mathematics, not geographically: the <i>phi</i> zero is at the North Pole, not at the Equator on the west coast of Africa (Bay of Guinea). You need to subtract your geographical coordinates from <i>pi/2</i> (also known as 90 degrees).</p> <pre><code>$distance = great_circle_distance($lon0, pi/2 - $lat0, $lon1, pi/2 - $lat1, $rho);</code></pre> <h2 id="great_circle_direction"><a class="permalink" href="#great_circle_direction">#</a>great_circle_direction</h2> <p>The direction you must follow the great circle (also known as <i>bearing</i>) can be computed by the great_circle_direction() function:</p> <pre><code>use Math::Trig &#39;great_circle_direction&#39;; $direction = great_circle_direction($theta0, $phi0, $theta1, $phi1);</code></pre> <h2 id="great_circle_bearing"><a class="permalink" href="#great_circle_bearing">#</a>great_circle_bearing</h2> <p>Alias &#39;great_circle_bearing&#39; for &#39;great_circle_direction&#39; is also available.</p> <pre><code>use Math::Trig &#39;great_circle_bearing&#39;; $direction = great_circle_bearing($theta0, $phi0, $theta1, $phi1);</code></pre> <p>The result of great_circle_direction is in radians, zero indicating straight north, pi or -pi straight south, pi/2 straight west, and -pi/2 straight east.</p> <h2 id="great_circle_destination"><a class="permalink" href="#great_circle_destination">#</a>great_circle_destination</h2> <p>You can inversely compute the destination if you know the starting point, direction, and distance:</p> <pre><code>use Math::Trig &#39;great_circle_destination&#39;; # $diro is the original direction, # for example from great_circle_bearing(). # $distance is the angular distance in radians, # for example from great_circle_distance(). # $thetad and $phid are the destination coordinates, # $dird is the final direction at the destination. ($thetad, $phid, $dird) = great_circle_destination($theta, $phi, $diro, $distance);</code></pre> <p>or the midpoint if you know the end points:</p> <h2 id="great_circle_midpoint"><a class="permalink" href="#great_circle_midpoint">#</a>great_circle_midpoint</h2> <pre><code>use Math::Trig &#39;great_circle_midpoint&#39;; ($thetam, $phim) = great_circle_midpoint($theta0, $phi0, $theta1, $phi1);</code></pre> <p>The great_circle_midpoint() is just a special case of</p> <h2 id="great_circle_waypoint"><a class="permalink" href="#great_circle_waypoint">#</a>great_circle_waypoint</h2> <pre><code>use Math::Trig &#39;great_circle_waypoint&#39;; ($thetai, $phii) = great_circle_waypoint($theta0, $phi0, $theta1, $phi1, $way);</code></pre> <p>Where the $way is a value from zero ($theta0, $phi0) to one ($theta1, $phi1). Note that antipodal points (where their distance is <i>pi</i> radians) do not have waypoints between them (they would have an an &quot;equator&quot; between them), and therefore <code>undef</code> is returned for antipodal points. If the points are the same and the distance therefore zero and all waypoints therefore identical, the first point (either point) is returned.</p> <p>The thetas, phis, direction, and distance in the above are all in radians.</p> <p>You can import all the great circle formulas by</p> <pre><code>use Math::Trig &#39;:great_circle&#39;;</code></pre> <p>Notice that the resulting directions might be somewhat surprising if you are looking at a flat worldmap: in such map projections the great circles quite often do not look like the shortest routes -- but for example the shortest possible routes from Europe or North America to Asia do often cross the polar regions. (The common Mercator projection does <b>not</b> show great circles as straight lines: straight lines in the Mercator projection are lines of constant bearing.)</p> <h1 id="EXAMPLES"><a class="permalink" href="#EXAMPLES">#</a>EXAMPLES</h1> <p>To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N 139.8E) in kilometers:</p> <pre><code>use Math::Trig qw(great_circle_distance deg2rad); # Notice the 90 - latitude: phi zero is at the North Pole. sub NESW { deg2rad($_[0]), deg2rad(90 - $_[1]) } my @L = NESW( -0.5, 51.3); my @T = NESW(139.8, 35.7); my $km = great_circle_distance(@L, @T, 6378); # About 9600 km.</code></pre> <p>The direction you would have to go from London to Tokyo (in radians, straight north being zero, straight east being pi/2).</p> <pre><code>use Math::Trig qw(great_circle_direction); my $rad = great_circle_direction(@L, @T); # About 0.547 or 0.174 pi.</code></pre> <p>The midpoint between London and Tokyo being</p> <pre><code>use Math::Trig qw(great_circle_midpoint); my @M = great_circle_midpoint(@L, @T);</code></pre> <p>or about 69 N 89 E, in the frozen wastes of Siberia.</p> <p><b>NOTE</b>: you <b>cannot</b> get from A to B like this:</p> <pre><code class="plaintext">Dist = great_circle_distance(A, B) Dir = great_circle_direction(A, B) C = great_circle_destination(A, Dist, Dir)</code></pre> <p>and expect C to be B, because the bearing constantly changes when going from A to B (except in some special case like the meridians or the circles of latitudes) and in great_circle_destination() one gives a <b>constant</b> bearing to follow.</p> <h2 id="CAVEAT-FOR-GREAT-CIRCLE-FORMULAS"><a class="permalink" href="#CAVEAT-FOR-GREAT-CIRCLE-FORMULAS">#</a><a id="CAVEAT"></a>CAVEAT FOR GREAT CIRCLE FORMULAS</h2> <p>The answers may be off by few percentages because of the irregular (slightly aspherical) form of the Earth. The errors are at worst about 0.55%, but generally below 0.3%.</p> <h2 id="Real-valued-asin-and-acos"><a class="permalink" href="#Real-valued-asin-and-acos">#</a><a id="Real"></a>Real-valued asin and acos</h2> <p>For small inputs asin() and acos() may return complex numbers even when real numbers would be enough and correct, this happens because of floating-point inaccuracies. You can see these inaccuracies for example by trying theses:</p> <pre><code>print cos(1e-6)**2+sin(1e-6)**2 - 1,&quot;\n&quot;; printf &quot;%.20f&quot;, cos(1e-6)**2+sin(1e-6)**2,&quot;\n&quot;;</code></pre> <p>which will print something like this</p> <pre><code class="plaintext">-1.11022302462516e-16 0.99999999999999988898</code></pre> <p>even though the expected results are of course exactly zero and one. The formulas used to compute asin() and acos() are quite sensitive to this, and therefore they might accidentally slip into the complex plane even when they should not. To counter this there are two interfaces that are guaranteed to return a real-valued output.</p> <dl> <dt id="asin_real"><a class="permalink" href="#asin_real">#</a>asin_real</dt> <dd> <pre><code>use Math::Trig qw(asin_real); $real_angle = asin_real($input_sin);</code></pre> <p>Return a real-valued arcus sine if the input is between [-1, 1], <b>inclusive</b> the endpoints. For inputs greater than one, pi/2 is returned. For inputs less than minus one, -pi/2 is returned.</p> </dd> <dt id="acos_real"><a class="permalink" href="#acos_real">#</a>acos_real</dt> <dd> <pre><code>use Math::Trig qw(acos_real); $real_angle = acos_real($input_cos);</code></pre> <p>Return a real-valued arcus cosine if the input is between [-1, 1], <b>inclusive</b> the endpoints. For inputs greater than one, zero is returned. For inputs less than minus one, pi is returned.</p> </dd> </dl> <h1 id="BUGS"><a class="permalink" href="#BUGS">#</a>BUGS</h1> <p>Saying <code>use Math::Trig;</code> exports many mathematical routines in the caller environment and even overrides some (<code>sin</code>, <code>cos</code>). This is construed as a feature by the Authors, actually... ;-)</p> <p>The code is not optimized for speed, especially because we use <code>Math::Complex</code> and thus go quite near complex numbers while doing the computations even when the arguments are not. This, however, cannot be completely avoided if we want things like <code>asin(2)</code> to give an answer instead of giving a fatal runtime error.</p> <p>Do not attempt navigation using these formulas.</p> <p><a href="/5.22.0/Math::Complex">Math::Complex</a></p> <h1 id="AUTHORS"><a class="permalink" href="#AUTHORS">#</a>AUTHORS</h1> <p>Jarkko Hietaniemi &lt;<i>jhi!at!iki.fi</i>&gt;, Raphael Manfredi &lt;<i>Raphael_Manfredi!at!pobox.com</i>&gt;, Zefram &lt;zefram@fysh.org&gt;</p> <h1 id="LICENSE"><a class="permalink" href="#LICENSE">#</a>LICENSE</h1> <p>This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10