summaryrefslogtreecommitdiff
path: root/lib/Math/Trig.pm
blob: 7c3570c840350dd27e26cb393c89ad294863b40b (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
234
235
236
237
238
239
240
241
242
#
# Trigonometric functions, mostly inherited from Math::Complex.
# -- Jarkko Hietaniemi, April 1997
#

require Exporter;
package Math::Trig;

use strict;

use Math::Complex qw(:trig);

use vars qw($VERSION $PACKAGE
	    @ISA
	    @EXPORT
	    $pi2 $DR $RD $DG $GD $RG $GR);

@ISA = qw(Exporter);

$VERSION = 1.00;

my @angcnv = qw(rad_to_deg rad_to_grad
	     deg_to_rad deg_to_grad
	     grad_to_rad grad_to_dec);

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

sub pi2 () {
    $pi2 = 2 * pi unless ($pi2);
    $pi2;
}

sub DR () {
    $DR = pi2/360 unless ($DR);
    $DR;
}

sub RD () {
    $RD = 360/pi2 unless ($RD);
    $RD;
}

sub DG () {
    $DG = 400/360 unless ($DG);
    $DG;
}

sub GD () {
    $GD = 360/400 unless ($GD);
    $GD;
}

sub RG () {
    $RG = 400/pi2 unless ($RG);
    $RG;
}

sub GR () {
    $GR = pi2/400 unless ($GR);
    $GR;
}

#
# Truncating remainder.
#

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

#
# Angle conversions.
#

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

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

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

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

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

sub grad_to_rad ($) {
    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 = deg_to_rad(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 SIMPLE 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 E<1.571>
and the imaginary part of approximately E<-1.317>.

=head1 ANGLE CONVERSIONS

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

	$radians  = deg_to_rad($degrees);
	$radians  = grad_to_rad($gradians);
	
	$degrees  = rad_to_deg($radians);
	$degrees  = grad_to_deg($gradians);
	
	$gradians = deg_to_grad($degrees);
	$gradians = rad_to_grad($radians);

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

=head1

The following functions

	tan
	sec
	csc
	cot
	atan
	acot
	tanh
	sech
	csch
	coth
	atanh
	asech
	acsch
	acoth

cannot be computed for all arguments because that would mean dividing
by zero. These situations cause fatal runtime errors looking like this

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

=cut

# eof