This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make L<perltrap> refer to L<perldelta>
[perl5.git] / t / lib / complex.t
CommitLineData
89d0527d
RM
1#!./perl
2
3# $RCSfile$
4#
5aabfad6
PP
5# Regression tests for the Math::Complex pacakge
6# -- Raphael Manfredi, September 1996
7# -- Jarkko Hietaniemi, March 1997
8
89d0527d
RM
9BEGIN {
10 chdir 't' if -d 't';
11 @INC = '../lib';
12}
5aabfad6 13
89d0527d
RM
14use Math::Complex;
15
16$test = 0;
17$| = 1;
fe82cf6e 18@script = ();
5aabfad6 19my $eps = 1e-11;
89d0527d
RM
20
21while (<DATA>) {
0c721ce2
JH
22 s/^\s+//;
23 next if $_ eq '' || /^\#/;
24 chomp;
25 $test_set = 0; # Assume not a test over a set of values
89d0527d
RM
26 if (/^&(.*)/) {
27 $op = $1;
28 next;
29 }
30 elsif (/^\{(.*)\}/) {
31 set($1, \@set, \@val);
32 next;
33 }
34 elsif (s/^\|//) {
0c721ce2 35 $test_set = 1; # Requests we loop over the set...
89d0527d
RM
36 }
37 my @args = split(/:/);
0c721ce2 38 if ($test_set == 1) {
89d0527d
RM
39 my $i;
40 for ($i = 0; $i < @set; $i++) {
0c721ce2
JH
41 # complex number
42 $target = $set[$i];
43 # textual value as found in set definition
44 $zvalue = $val[$i];
89d0527d
RM
45 test($zvalue, $target, @args);
46 }
47 } else {
48 test($op, undef, @args);
49 }
50}
51
52print "1..$test\n";
fe82cf6e 53eval join '', @script;
89d0527d
RM
54die $@ if $@;
55
56sub test {
57 my ($op, $z, @args) = @_;
58 $test++;
59 my $i;
60 for ($i = 0; $i < @args; $i++) {
61 $val = value($args[$i]);
fe82cf6e 62 push @script, "\$z$i = $val;\n";
89d0527d
RM
63 }
64 if (defined $z) {
65 $args = "'$op'"; # Really the value
0c721ce2 66 $try = "abs(\$z0 - \$z1) <= $eps ? \$z1 : \$z0";
fe82cf6e
CS
67 push @script, "\$res = $try; ";
68 push @script, "check($test, $args[0], \$res, \$z$#args, $args);\n";
89d0527d
RM
69 } else {
70 my ($try, $args);
71 if (@args == 2) {
72 $try = "$op \$z0";
73 $args = "'$args[0]'";
74 } else {
75 $try = ($op =~ /^\w/) ? "$op(\$z0, \$z1)" : "\$z0 $op \$z1";
76 $args = "'$args[0]', '$args[1]'";
77 }
fe82cf6e
CS
78 push @script, "\$res = $try; ";
79 push @script, "check($test, '$try', \$res, \$z$#args, $args);\n";
89d0527d
RM
80 }
81}
82
83sub set {
84 my ($set, $setref, $valref) = @_;
85 @{$setref} = ();
86 @{$valref} = ();
87 my @set = split(/;\s*/, $set);
88 my @res;
89 my $i;
90 for ($i = 0; $i < @set; $i++) {
91 push(@{$valref}, $set[$i]);
92 my $val = value($set[$i]);
fe82cf6e
CS
93 push @script, "\$s$i = $val;\n";
94 push @{$setref}, "\$s$i";
89d0527d
RM
95 }
96}
97
98sub value {
99 local ($_) = @_;
100 if (/^\s*\((.*),(.*)\)/) {
101 return "cplx($1,$2)";
102 }
103 elsif (/^\s*\[(.*),(.*)\]/) {
104 return "cplxe($1,$2)";
105 }
106 elsif (/^\s*'(.*)'/) {
107 my $ex = $1;
108 $ex =~ s/\bz\b/$target/g;
109 $ex =~ s/\br\b/abs($target)/g;
110 $ex =~ s/\bt\b/arg($target)/g;
111 $ex =~ s/\ba\b/Re($target)/g;
112 $ex =~ s/\bb\b/Im($target)/g;
113 return $ex;
114 }
115 elsif (/^\s*"(.*)"/) {
116 return "\"$1\"";
117 }
118 return $_;
119}
120
121sub check {
122 my ($test, $try, $got, $expected, @z) = @_;
0c721ce2
JH
123
124# print "# @_\n";
125
126 if ("$got" eq "$expected"
127 ||
128 ($expected =~ /^-?\d/ && $got == $expected)
129 ||
130 (abs($got - $expected) < $eps)
131 ) {
89d0527d
RM
132 print "ok $test\n";
133 } else {
134 print "not ok $test\n";
135 my $args = (@z == 1) ? "z = $z[0]" : "z0 = $z[0], z1 = $z[1]";
136 print "# '$try' expected: '$expected' got: '$got' for $args\n";
137 }
138}
139__END__
140&+
141(3,4):(3,4):(6,8)
142(-3,4):(3,-4):(0,0)
143(3,4):-3:(0,4)
1441:(4,2):(5,2)
145[2,0]:[2,pi]:(0,0)
146
147&++
148(2,1):(3,1)
149
150&-
151(2,3):(-2,-3)
152[2,pi/2]:[2,-(pi)/2]
1532:[2,0]:(0,0)
154[3,0]:2:(1,0)
1553:(4,5):(-1,-5)
156(4,5):3:(1,5)
157
158&--
159(1,2):(0,2)
160[2,pi]:[3,pi]
161
162&*
163(0,1):(0,1):(-1,0)
164(4,5):(1,0):(4,5)
165[2,2*pi/3]:(1,0):[2,2*pi/3]
1662:(0,1):(0,2)
167(0,1):3:(0,3)
168(0,1):(4,1):(-1,4)
169(2,1):(4,-1):(9,2)
170
171&/
172(3,4):(3,4):(1,0)
173(4,-5):1:(4,-5)
1741:(0,1):(0,-1)
175(0,6):(0,2):(3,0)
176(9,2):(4,-1):(2,1)
177[4,pi]:[2,pi/2]:[2,pi/2]
178[2,pi/2]:[4,pi]:[0.5,-(pi)/2]
179
0c721ce2
JH
180&Re
181(3,4):3
182(-3,4):-3
183[1,pi/2]:0
184
185&Im
186(3,4):4
187(3,-4):-4
188[1,pi/2]:1
189
89d0527d
RM
190&abs
191(3,4):5
192(-3,4):5
193
0c721ce2
JH
194&arg
195[2,0]:0
196[-2,0]:pi
197
89d0527d
RM
198&~
199(4,5):(4,-5)
200(-3,4):(-3,-4)
201[2,pi/2]:[2,-(pi)/2]
202
203&<
204(3,4):(1,2):0
205(3,4):(3,2):0
206(3,4):(3,8):1
207(4,4):(5,129):1
208
209&==
210(3,4):(4,5):0
211(3,4):(3,5):0
212(3,4):(2,4):0
213(3,4):(3,4):1
214
215&sqrt
0c721ce2 216-9:(0,3)
89d0527d
RM
217(-100,0):(0,10)
218(16,-30):(5,-3)
219
220&stringify_cartesian
221(-100,0):"-100"
222(0,1):"i"
223(4,-3):"4-3i"
224(4,0):"4"
225(-4,0):"-4"
226(-2,4):"-2+4i"
227(-2,-1):"-2-i"
228
229&stringify_polar
230[-1, 0]:"[1,pi]"
231[1, pi/3]:"[1,pi/3]"
232[6, -2*pi/3]:"[6,-2pi/3]"
233[0.5, -9*pi/11]:"[0.5,-9pi/11]"
234
235{ (4,3); [3,2]; (-3,4); (0,2); [2,1] }
236
237|'z + ~z':'2*Re(z)'
238|'z - ~z':'2*i*Im(z)'
239|'z * ~z':'abs(z) * abs(z)'
240
0c721ce2 241{ (2,3); [3,2]; (-3,2); (0,2); 3; 1.2; -3; (-3, 0); (-2, -1); [2,1] }
89d0527d 242
0c721ce2
JH
243|'(root(z, 4))[1] ** 4':'z'
244|'(root(z, 5))[3] ** 5':'z'
245|'(root(z, 8))[7] ** 8':'z'
89d0527d 246|'abs(z)':'r'
0c721ce2
JH
247|'acot(z)':'acotan(z)'
248|'acsc(z)':'acosec(z)'
249|'acsc(z)':'asin(1 / z)'
250|'asec(z)':'acos(1 / z)'
89d0527d 251|'cbrt(z)':'cbrt(r) * exp(i * t/3)'
89d0527d 252|'cos(acos(z))':'z'
89d0527d 253|'cos(z) ** 2 + sin(z) ** 2':1
89d0527d 254|'cos(z)':'cosh(i*z)'
0c721ce2
JH
255|'cosh(z) ** 2 - sinh(z) ** 2':1
256|'cot(acot(z))':'z'
257|'cot(z)':'1 / tan(z)'
258|'cot(z)':'cotan(z)'
259|'csc(acsc(z))':'z'
260|'csc(z)':'1 / sin(z)'
261|'csc(z)':'cosec(z)'
89d0527d 262|'exp(log(z))':'z'
0c721ce2
JH
263|'exp(z)':'exp(a) * exp(i * b)'
264|'ln(z)':'log(z)'
265|'log(exp(z))':'z'
266|'log(z)':'log(r) + i*t'
89d0527d 267|'log10(z)':'log(z) / log(10)'
89d0527d 268|'logn(z, 2)':'log(z) / log(2)'
0c721ce2
JH
269|'logn(z, 3)':'log(z) / log(3)'
270|'sec(asec(z))':'z'
271|'sec(z)':'1 / cos(z)'
272|'sin(asin(z))':'z'
273|'sin(i * z)':'i * sinh(z)'
274|'sqrt(z) * sqrt(z)':'z'
275|'sqrt(z)':'sqrt(r) * exp(i * t/2)'
276|'tan(atan(z))':'z'
277|'z**z':'exp(z * log(z))'
89d0527d 278
0c721ce2 279{ (1,1); [1,0.5]; (-2, -1); 2; -3; (-1,0.5); (0,0.5); 0.5; (2, 0); (-1, -2) }
89d0527d 280
89d0527d 281|'cosh(acosh(z))':'z'
0c721ce2
JH
282|'coth(acoth(z))':'z'
283|'coth(z)':'1 / tanh(z)'
284|'coth(z)':'cotanh(z)'
285|'csch(acsch(z))':'z'
286|'csch(z)':'1 / sinh(z)'
287|'csch(z)':'cosech(z)'
288|'sech(asech(z))':'z'
289|'sech(z)':'1 / cosh(z)'
290|'sinh(asinh(z))':'z'
89d0527d 291|'tanh(atanh(z))':'z'
89d0527d 292
0c721ce2 293{ (0.2,-0.4); [1,0.5]; -1.2; (-1,0.5); 0.5; (1.1, 0) }
89d0527d 294
89d0527d 295|'acos(cos(z)) ** 2':'z * z'
89d0527d 296|'acosh(cosh(z)) ** 2':'z * z'
0c721ce2
JH
297|'acoth(z)':'acotanh(z)'
298|'acoth(z)':'atanh(1 / z)'
299|'acsch(z)':'acosech(z)'
300|'acsch(z)':'asinh(1 / z)'
301|'asech(z)':'acosh(1 / z)'
302|'asin(sin(z))':'z'
303|'asinh(sinh(z))':'z'
304|'atan(tan(z))':'z'
89d0527d
RM
305|'atanh(tanh(z))':'z'
306
0c721ce2
JH
307&sin
308( 2, 3):( 9.15449914691143, -4.16890695996656)
309(-2, 3):( -9.15449914691143, -4.16890695996656)
310(-2,-3):( -9.15449914691143, 4.16890695996656)
311( 2,-3):( 9.15449914691143, 4.16890695996656)
312
313&cos
314( 2, 3):( -4.18962569096881, -9.10922789375534)
315(-2, 3):( -4.18962569096881, 9.10922789375534)
316(-2,-3):( -4.18962569096881, -9.10922789375534)
317( 2,-3):( -4.18962569096881, 9.10922789375534)
318
319&tan
320( 2, 3):( -0.00376402564150, 1.00323862735361)
321(-2, 3):( 0.00376402564150, 1.00323862735361)
322(-2,-3):( 0.00376402564150, -1.00323862735361)
323( 2,-3):( -0.00376402564150, -1.00323862735361)
324
325&sec
326( 2, 3):( -0.04167496441114, 0.09061113719624)
327(-2, 3):( -0.04167496441114, -0.09061113719624)
328(-2,-3):( -0.04167496441114, 0.09061113719624)
329( 2,-3):( -0.04167496441114, -0.09061113719624)
330
331&csc
332( 2, 3):( 0.09047320975321, 0.04120098628857)
333(-2, 3):( -0.09047320975321, 0.04120098628857)
334(-2,-3):( -0.09047320975321, -0.04120098628857)
335( 2,-3):( 0.09047320975321, -0.04120098628857)
336
337&cot
338( 2, 3):( -0.00373971037634, -0.99675779656936)
339(-2, 3):( 0.00373971037634, -0.99675779656936)
340(-2,-3):( 0.00373971037634, 0.99675779656936)
341( 2,-3):( -0.00373971037634, 0.99675779656936)
342
343&asin
344( 2, 3):( 0.57065278432110, 1.98338702991654)
345(-2, 3):( -0.57065278432110, 1.98338702991654)
346(-2,-3):( -0.57065278432110, -1.98338702991654)
347( 2,-3):( 0.57065278432110, -1.98338702991654)
348
349&acos
350( 2, 3):( 1.00014354247380, -1.98338702991654)
351(-2, 3):( 2.14144911111600, -1.98338702991654)
352(-2,-3):( 2.14144911111600, 1.98338702991654)
353( 2,-3):( 1.00014354247380, 1.98338702991654)
354
355&atan
356( 2, 3):( 1.40992104959658, 0.22907268296854)
357(-2, 3):( -1.40992104959658, 0.22907268296854)
358(-2,-3):( -1.40992104959658, -0.22907268296854)
359( 2,-3):( 1.40992104959658, -0.22907268296854)
360
361&asec
362( 2, 3):( 1.42041072246703, 0.23133469857397)
363(-2, 3):( 1.72118193112276, 0.23133469857397)
364(-2,-3):( 1.72118193112276, -0.23133469857397)
365( 2,-3):( 1.42041072246703, -0.23133469857397)
366
367&acsc
368( 2, 3):( 0.15038560432786, -0.23133469857397)
369(-2, 3):( -0.15038560432786, -0.23133469857397)
370(-2,-3):( -0.15038560432786, 0.23133469857397)
371( 2,-3):( 0.15038560432786, 0.23133469857397)
372
373&acot
374( 2, 3):( 0.16087527719832, -0.22907268296854)
375(-2, 3):( -0.16087527719832, -0.22907268296854)
376(-2,-3):( -0.16087527719832, 0.22907268296854)
377( 2,-3):( 0.16087527719832, 0.22907268296854)
378
379&sinh
380( 2, 3):( -3.59056458998578, 0.53092108624852)
381(-2, 3):( 3.59056458998578, 0.53092108624852)
382(-2,-3):( 3.59056458998578, -0.53092108624852)
383( 2,-3):( -3.59056458998578, -0.53092108624852)
384
385&cosh
386( 2, 3):( -3.72454550491532, 0.51182256998738)
387(-2, 3):( -3.72454550491532, -0.51182256998738)
388(-2,-3):( -3.72454550491532, 0.51182256998738)
389( 2,-3):( -3.72454550491532, -0.51182256998738)
390
391&tanh
392( 2, 3):( 0.96538587902213, -0.00988437503832)
393(-2, 3):( -0.96538587902213, -0.00988437503832)
394(-2,-3):( -0.96538587902213, 0.00988437503832)
395( 2,-3):( 0.96538587902213, 0.00988437503832)
396
397&sech
398( 2, 3):( -0.26351297515839, -0.03621163655877)
399(-2, 3):( -0.26351297515839, 0.03621163655877)
400(-2,-3):( -0.26351297515839, -0.03621163655877)
401( 2,-3):( -0.26351297515839, 0.03621163655877)
402
403&csch
404( 2, 3):( -0.27254866146294, -0.04030057885689)
405(-2, 3):( 0.27254866146294, -0.04030057885689)
406(-2,-3):( 0.27254866146294, 0.04030057885689)
407( 2,-3):( -0.27254866146294, 0.04030057885689)
408
409&coth
410( 2, 3):( 1.03574663776500, 0.01060478347034)
411(-2, 3):( -1.03574663776500, 0.01060478347034)
412(-2,-3):( -1.03574663776500, -0.01060478347034)
413( 2,-3):( 1.03574663776500, -0.01060478347034)
414
415&asinh
416( 2, 3):( 1.96863792579310, 0.96465850440760)
417(-2, 3):( -1.96863792579310, 0.96465850440761)
418(-2,-3):( -1.96863792579310, -0.96465850440761)
419( 2,-3):( 1.96863792579310, -0.96465850440760)
420
421&acosh
422( 2, 3):( 1.98338702991654, 1.00014354247380)
423(-2, 3):( -1.98338702991653, -2.14144911111600)
424(-2,-3):( -1.98338702991653, 2.14144911111600)
425( 2,-3):( 1.98338702991654, -1.00014354247380)
426
427&atanh
428( 2, 3):( 0.14694666622553, 1.33897252229449)
429(-2, 3):( -0.14694666622553, 1.33897252229449)
430(-2,-3):( -0.14694666622553, -1.33897252229449)
431( 2,-3):( 0.14694666622553, -1.33897252229449)
432
433&asech
434( 2, 3):( 0.23133469857397, -1.42041072246703)
435(-2, 3):( -0.23133469857397, 1.72118193112276)
436(-2,-3):( -0.23133469857397, -1.72118193112276)
437( 2,-3):( 0.23133469857397, 1.42041072246703)
438
439&acsch
440( 2, 3):( 0.15735549884499, -0.22996290237721)
441(-2, 3):( -0.15735549884499, -0.22996290237721)
442(-2,-3):( -0.15735549884499, 0.22996290237721)
443( 2,-3):( 0.15735549884499, 0.22996290237721)
444
445&acoth
446( 2, 3):( 0.14694666622553, -0.23182380450040)
447(-2, 3):( -0.14694666622553, -0.23182380450040)
448(-2,-3):( -0.14694666622553, 0.23182380450040)
449( 2,-3):( 0.14694666622553, 0.23182380450040)
450
451# eof