2 # Trigonometric functions, mostly inherited from Math::Complex.
3 # -- Jarkko Hietaniemi, April 1997
11 use Math::Complex qw(:trig);
13 use vars qw($VERSION $PACKAGE
16 $pi2 $DR $RD $DG $GD $RG $GR);
22 my @angcnv = qw(rad_to_deg rad_to_grad
23 deg_to_rad deg_to_grad
24 grad_to_rad grad_to_dec);
26 @EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}},
30 $pi2 = 2 * pi unless ($pi2);
35 $DR = pi2/360 unless ($DR);
40 $RD = 360/pi2 unless ($RD);
45 $DG = 400/360 unless ($DG);
50 $GD = 360/400 unless ($GD);
55 $RG = 400/pi2 unless ($RG);
60 $GR = pi2/400 unless ($GR);
65 # Truncating remainder.
69 # Oh yes, POSIX::fmod() would be faster. Possibly. If it is available.
70 $_[0] - $_[1] * int($_[0] / $_[1]);
78 remt(RD * $_[0], 360);
82 remt(DR * $_[0], pi2);
86 remt(GD * $_[0], 360);
90 remt(DG * $_[0], 400);
94 remt(RG * $_[0], 400);
98 remt(GR * $_[0], pi2);
103 Math::Trig - trigonometric functions
115 $rad = deg_to_rad(120);
119 C<Math::Trig> defines many trigonometric functions not defined by the
120 core Perl (which defines only the C<sin()> and C<cos()>. The constant
121 B<pi> is also defined as are a few convenience functions for angle
124 =head1 TRIGONOMETRIC FUNCTIONS
130 The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot
133 csc cosec sec cot cotan
135 The arcus (also known as the inverse) functions of the sine, cosine,
140 The principal value of the arc tangent of y/x
144 The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc
145 and acotan/acot are aliases)
147 acsc acosec asec acot acotan
149 The hyperbolic sine, cosine, and tangent
153 The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch
154 and cotanh/coth are aliases)
156 csch cosech sech coth cotanh
158 The arcus (also known as the inverse) functions of the hyperbolic
159 sine, cosine, and tangent
163 The arcus cofunctions of the hyperbolic sine, cosine, and tangent
164 (acsch/acosech and acoth/acotanh are aliases)
166 acsch acosech asech acoth acotanh
168 The trigonometric constant B<pi> is also defined.
172 =head2 SIMPLE 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 E<1.571>
197 and the imaginary part of approximately E<-1.317>.
199 =head1 ANGLE CONVERSIONS
201 (Plane, 2-dimensional) angles may be converted with the following functions.
203 $radians = deg_to_rad($degrees);
204 $radians = grad_to_rad($gradians);
206 $degrees = rad_to_deg($radians);
207 $degrees = grad_to_deg($gradians);
209 $gradians = deg_to_grad($degrees);
210 $gradians = rad_to_grad($radians);
212 The full circle is 2 B<pi> radians or E<360> degrees or E<400> gradians.
216 The following functions
233 cannot be computed for all arguments because that would mean dividing
234 by zero. These situations cause fatal runtime errors looking like this
236 cot(0): Division by zero.
237 (Because in the definition of cot(0), sin(0) is 0)