2 # Trigonometric functions, mostly inherited from Math::Complex.
3 # -- Jarkko Hietaniemi, April 1997
4 # -- Raphael Manfredi, September 1996 (indirectly: because of Math::Complex)
12 use Math::Complex qw(:trig);
14 use vars qw($VERSION $PACKAGE
22 my @angcnv = qw(rad2deg rad2grad
26 @EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}},
29 use constant pi2 => 2 * pi;
30 use constant DR => pi2/360;
31 use constant RD => 360/pi2;
32 use constant DG => 400/360;
33 use constant GD => 360/400;
34 use constant RG => 400/pi2;
35 use constant GR => pi2/400;
38 # Truncating remainder.
42 # Oh yes, POSIX::fmod() would be faster. Possibly. If it is available.
43 $_[0] - $_[1] * int($_[0] / $_[1]);
50 sub rad2deg ($) { remt(RD * $_[0], 360) }
52 sub deg2rad ($) { remt(DR * $_[0], pi2) }
54 sub grad2deg ($) { remt(GD * $_[0], 360) }
56 sub deg2grad ($) { remt(DG * $_[0], 400) }
58 sub rad2grad ($) { remt(RG * $_[0], 400) }
60 sub grad2rad ($) { remt(GR * $_[0], pi2) }
64 Math::Trig - trigonometric functions
80 C<Math::Trig> defines many trigonometric functions not defined by the
81 core Perl which defines only the C<sin()> and C<cos()>. The constant
82 B<pi> is also defined as are a few convenience functions for angle
85 =head1 TRIGONOMETRIC FUNCTIONS
91 The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot
94 csc cosec sec cot cotan
96 The arcus (also known as the inverse) functions of the sine, cosine,
101 The principal value of the arc tangent of y/x
105 The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc
106 and acotan/acot are aliases)
108 acsc acosec asec acot acotan
110 The hyperbolic sine, cosine, and tangent
114 The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch
115 and cotanh/coth are aliases)
117 csch cosech sech coth cotanh
119 The arcus (also known as the inverse) functions of the hyperbolic
120 sine, cosine, and tangent
124 The arcus cofunctions of the hyperbolic sine, cosine, and tangent
125 (acsch/acosech and acoth/acotanh are aliases)
127 acsch acosech asech acoth acotanh
129 The trigonometric constant B<pi> is also defined.
133 =head2 ERRORS DUE TO DIVISION BY ZERO
135 The following functions
152 cannot be computed for all arguments because that would mean dividing
153 by zero or taking logarithm of zero. These situations cause fatal
154 runtime errors looking like this
156 cot(0): Division by zero.
157 (Because in the definition of cot(0), the divisor sin(0) is 0)
162 atanh(-1): Logarithm of zero.
165 For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>,
166 C<asech>, C<acsch>, the argument cannot be C<0> (zero). For the
167 C<atanh>, C<acoth>, the argument cannot be C<1> (one). For the
168 C<atanh>, C<acoth>, the argument cannot be C<-1> (minus one). For the
169 C<tan>, C<sec>, C<tanh>, C<sech>, the argument cannot be I<pi/2 + k *
170 pi>, where I<k> is any integer.
172 =head2 SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS
174 Please note that some of the trigonometric functions can break out
175 from the B<real axis> into the B<complex plane>. For example
176 C<asin(2)> has no definition for plain real numbers but it has
177 definition for complex numbers.
179 In Perl terms this means that supplying the usual Perl numbers (also
180 known as scalars, please see L<perldata>) as input for the
181 trigonometric functions might produce as output results that no more
182 are simple real numbers: instead they are complex numbers.
184 The C<Math::Trig> handles this by using the C<Math::Complex> package
185 which knows how to handle complex numbers, please see L<Math::Complex>
186 for more information. In practice you need not to worry about getting
187 complex numbers as results because the C<Math::Complex> takes care of
188 details like for example how to display complex numbers. For example:
192 should produce something like this (take or leave few last decimals):
194 1.5707963267949-1.31695789692482i
196 That is, a complex number with the real part of approximately C<1.571>
197 and the imaginary part of approximately C<-1.317>.
199 =head1 ANGLE CONVERSIONS
201 (Plane, 2-dimensional) angles may be converted with the following functions.
203 $radians = deg2rad($degrees);
204 $radians = grad2rad($gradians);
206 $degrees = rad2deg($radians);
207 $degrees = grad2deg($gradians);
209 $gradians = deg2grad($degrees);
210 $gradians = rad2grad($radians);
212 The full circle is 2 I<pi> radians or I<360> degrees or I<400> gradians.
216 Saying C<use Math::Trig;> exports many mathematical routines in the
217 caller environment and even overrides some (C<sin>, C<cos>). This is
218 construed as a feature by the Authors, actually... ;-)
220 The code is not optimized for speed, especially because we use
221 C<Math::Complex> and thus go quite near complex numbers while doing
222 the computations even when the arguments are not. This, however,
223 cannot be completely avoided if we want things like C<asin(2)> to give
224 an answer instead of giving a fatal runtime error.
228 Jarkko Hietaniemi <F<jhi@iki.fi>> and
229 Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>>.