summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/perl/lib/Math/Trig.pm
blob: a1cbb0723403eaf03e52f843450a9e33fe6a6e61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#
# Trigonometric functions, mostly inherited from Math::Complex.
# -- Jarkko Hietaniemi, April 1997
# -- Raphael Manfredi, September 1996 (indirectly: because of Math::Complex)
#

require Exporter;
package Math::Trig;

use strict;

use Math::Complex qw(:trig);

use vars qw($VERSION $PACKAGE
	    @ISA
	    @EXPORT);

@ISA = qw(Exporter);

$VERSION = 1.00;

my @angcnv = qw(rad2deg rad2grad
	     deg2rad deg2grad
	     grad2rad grad2deg);

@EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}},
	   @angcnv);

use constant pi2 => 2 * pi;
use constant DR  => pi2/360;
use constant RD  => 360/pi2;
use constant DG  => 400/360;
use constant GD  => 360/400;
use constant RG  => 400/pi2;
use constant GR  => pi2/400;

#
# Truncating remainder.
#

sub remt ($$) {
    # Oh yes, POSIX::fmod() would be faster. Possibly. If it is available.
    $_[0] - $_[1] * int($_[0] / $_[1]);
}

#
# Angle conversions.
#

sub rad2deg ($)  { remt(RD * $_[0], 360) }

sub deg2rad ($)  { remt(DR * $_[0], pi2) }

sub grad2deg ($) { remt(GD * $_[0], 360) }

sub deg2grad ($) { remt(DG * $_[0], 400) }

sub rad2grad ($) { remt(RG * $_[0], 400) }

sub grad2rad ($) { remt(GR * $_[0], pi2) }

=head1 NAME

Math::Trig - trigonometric functions

=head1 SYNOPSIS

	use Math::Trig;
	
	$x = tan(0.9);
	$y = acos(3.7);
	$z = asin(2.4);
	
	$halfpi = pi/2;

	$rad = deg2rad(120);

=head1 DESCRIPTION

C<Math::Trig> defines many trigonometric functions not defined by the
core Perl which defines only the C<sin()> and C<cos()>.  The constant
B<pi> is also defined as are a few convenience functions for angle
conversions.

=head1 TRIGONOMETRIC FUNCTIONS

The tangent

	tan

The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot
are aliases)

	csc cosec sec cot cotan

The arcus (also known as the inverse) functions of the sine, cosine,
and tangent

	asin acos atan

The principal value of the arc tangent of y/x

	atan2(y, x)

The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc
and acotan/acot are aliases)

	acsc acosec asec acot acotan

The hyperbolic sine, cosine, and tangent

	sinh cosh tanh

The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch
and cotanh/coth are aliases)

	csch cosech sech coth cotanh

The arcus (also known as the inverse) functions of the hyperbolic
sine, cosine, and tangent

	asinh acosh atanh

The arcus cofunctions of the hyperbolic sine, cosine, and tangent
(acsch/acosech and acoth/acotanh are aliases)

	acsch acosech asech acoth acotanh

The trigonometric constant B<pi> is also defined.

	$pi2 = 2 * pi;

=head2 ERRORS DUE TO DIVISION BY ZERO

The following functions

	tan
	sec
	csc
	cot
	asec
	acsc
	tanh
	sech
	csch
	coth
	atanh
	asech
	acsch
	acoth

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

	cot(0): Division by zero.
	(Because in the definition of cot(0), the divisor sin(0) is 0)
	Died at ...

or

	atanh(-1): Logarithm of zero.
	Died at...

For the C<csc>, C<cot>, C<asec>, C<acsc>, C<acot>, C<csch>, C<coth>,
C<asech>, C<acsch>, the argument cannot be C<0> (zero).  For the
C<atanh>, C<acoth>, the argument cannot be C<1> (one).  For the
C<atanh>, C<acoth>, the argument cannot be C<-1> (minus one).  For the
C<tan>, C<sec>, C<tanh>, C<sech>, the argument cannot be I<pi/2 + k *
pi>, where I<k> is any integer.

=head2 SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS

Please note that some of the trigonometric functions can break out
from the B<real axis> into the B<complex plane>. For example
C<asin(2)> has no definition for plain real numbers but it has
definition for complex numbers.

In Perl terms this means that supplying the usual Perl numbers (also
known as scalars, please see L<perldata>) as input for the
trigonometric functions might produce as output results that no more
are simple real numbers: instead they are complex numbers.

The C<Math::Trig> handles this by using the C<Math::Complex> package
which knows how to handle complex numbers, please see L<Math::Complex>
for more information. In practice you need not to worry about getting
complex numbers as results because the C<Math::Complex> takes care of
details like for example how to display complex numbers. For example:

	print asin(2), "\n";
    
should produce something like this (take or leave few last decimals):

	1.5707963267949-1.31695789692482i

That is, a complex number with the real part of approximately C<1.571>
and the imaginary part of approximately C<-1.317>.

=head1 ANGLE CONVERSIONS

(Plane, 2-dimensional) angles may be converted with the following functions.

	$radians  = deg2rad($degrees);
	$radians  = grad2rad($gradians);
	
	$degrees  = rad2deg($radians);
	$degrees  = grad2deg($gradians);
	
	$gradians = deg2grad($degrees);
	$gradians = rad2grad($radians);

The full circle is 2 I<pi> radians or I<360> degrees or I<400> gradians.

=head1 BUGS

Saying C<use Math::Trig;> exports many mathematical routines in the
caller environment and even overrides some (C<sin>, C<cos>).  This is
construed as a feature by the Authors, actually... ;-)

The code is not optimized for speed, especially because we use
C<Math::Complex> 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 C<asin(2)> to give
an answer instead of giving a fatal runtime error.

=head1 AUTHORS

Jarkko Hietaniemi <F<jhi@iki.fi>> and 
Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>>.

=cut

# eof