This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
patch for documentation error in FileCache.pm
[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
5cd24f17 7# -- Jarkko Hietaniemi, March-April 1997
5aabfad6 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
0e505df1 26 if (/^&(.+)/) {
89d0527d
RM
27 $op = $1;
28 next;
29 }
0e505df1 30 elsif (/^\{(.+)\}/) {
89d0527d
RM
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
5cd24f17
PP
52# test the divbyzeros
53
0e505df1
JH
54sub test_dbz {
55 for my $op (@_) {
56 $test++;
57
58# push(@script, qq(print "# '$op'\n";));
59 push(@script, qq(eval '$op';));
60 push(@script, qq(print 'not ' unless (\$@ =~ /Division by zero/);));
61 push(@script, qq(print "ok $test\n";));
62 }
63}
64
5cd24f17
PP
65test_dbz(
66 'i/0',
67# 'tan(pi/2)', # may succeed thanks to floating point inaccuracies
68# 'sec(pi/2)', # may succeed thanks to floating point inaccuracies
69 'csc(0)',
70 'cot(0)',
71 'atan(i)',
72 'asec(0)',
73 'acsc(0)',
74 'acot(i)',
75# 'tanh(pi/2)', # may succeed thanks to floating point inaccuracies
76# 'sech(pi/2)', # may succeed thanks to floating point inaccuracies
77 'csch(0)',
78 'coth(0)',
79 'atanh(1)',
80 'asech(0)',
81 'acsch(0)',
82 'acoth(1)'
83 );
84
0e505df1
JH
85# test the 0**0
86
87sub test_ztz {
88 $test++;
89
90# push(@script, qq(print "# 0**0\n";));
91 push(@script, qq(eval 'cplx(0)**cplx(0)';));
92 push(@script, qq(print 'not ' unless (\$@ =~ /zero raised to the/);));
93 push(@script, qq(print "ok $test\n";));
94}
95
96test_ztz;
97
98# test the bad roots
99
100sub test_broot {
5cd24f17
PP
101 for my $op (@_) {
102 $test++;
103
0e505df1
JH
104# push(@script, qq(print "# root(2, $op)\n";));
105 push(@script, qq(eval 'root(2, $op)';));
106 push(@script, qq(print 'not ' unless (\$@ =~ /root must be/);));
5cd24f17
PP
107 push(@script, qq(print "ok $test\n";));
108 }
109}
110
0e505df1
JH
111test_broot(qw(-3 -2.1 0 0.99));
112
89d0527d 113print "1..$test\n";
fe82cf6e 114eval join '', @script;
89d0527d
RM
115die $@ if $@;
116
0e505df1
JH
117sub abop {
118 my ($op) = @_;
119
120 push(@script, qq(print "# $op=\n";));
121}
122
89d0527d
RM
123sub test {
124 my ($op, $z, @args) = @_;
0e505df1 125 my ($baop) = 0;
89d0527d
RM
126 $test++;
127 my $i;
0e505df1 128 $baop = 1 if ($op =~ s/;=$//);
89d0527d
RM
129 for ($i = 0; $i < @args; $i++) {
130 $val = value($args[$i]);
fe82cf6e 131 push @script, "\$z$i = $val;\n";
89d0527d
RM
132 }
133 if (defined $z) {
134 $args = "'$op'"; # Really the value
0c721ce2 135 $try = "abs(\$z0 - \$z1) <= $eps ? \$z1 : \$z0";
fe82cf6e
CS
136 push @script, "\$res = $try; ";
137 push @script, "check($test, $args[0], \$res, \$z$#args, $args);\n";
89d0527d
RM
138 } else {
139 my ($try, $args);
140 if (@args == 2) {
141 $try = "$op \$z0";
142 $args = "'$args[0]'";
143 } else {
144 $try = ($op =~ /^\w/) ? "$op(\$z0, \$z1)" : "\$z0 $op \$z1";
145 $args = "'$args[0]', '$args[1]'";
146 }
fe82cf6e
CS
147 push @script, "\$res = $try; ";
148 push @script, "check($test, '$try', \$res, \$z$#args, $args);\n";
0e505df1
JH
149 if (@args > 2 and $baop) { # binary assignment ops
150 $test++;
151 # check the op= works
152 push @script, <<EOB;
153{
154 my \$za = cplx(ref \$z0 ? \@{\$z0->cartesian} : (\$z0, 0));
155
156 my (\$z1r, \$z1i) = ref \$z1 ? \@{\$z1->cartesian} : (\$z1, 0);
157
158 my \$zb = cplx(\$z1r, \$z1i);
159
160 \$za $op= \$zb;
161 my (\$zbr, \$zbi) = \@{\$zb->cartesian};
162
163 check($test, '\$z0 $op= \$z1', \$za, \$z$#args, $args);
164EOB
165 $test++;
166 # check that the rhs has not changed
167 push @script, qq(print "not " unless (\$zbr == \$z1r and \$zbi == \$z1i););
168 push @script, qq(print "ok $test\n";);
169 push @script, "}\n";
170 }
89d0527d
RM
171 }
172}
173
174sub set {
175 my ($set, $setref, $valref) = @_;
176 @{$setref} = ();
177 @{$valref} = ();
178 my @set = split(/;\s*/, $set);
179 my @res;
180 my $i;
181 for ($i = 0; $i < @set; $i++) {
182 push(@{$valref}, $set[$i]);
183 my $val = value($set[$i]);
fe82cf6e
CS
184 push @script, "\$s$i = $val;\n";
185 push @{$setref}, "\$s$i";
89d0527d
RM
186 }
187}
188
189sub value {
190 local ($_) = @_;
191 if (/^\s*\((.*),(.*)\)/) {
192 return "cplx($1,$2)";
193 }
194 elsif (/^\s*\[(.*),(.*)\]/) {
195 return "cplxe($1,$2)";
196 }
197 elsif (/^\s*'(.*)'/) {
198 my $ex = $1;
199 $ex =~ s/\bz\b/$target/g;
200 $ex =~ s/\br\b/abs($target)/g;
201 $ex =~ s/\bt\b/arg($target)/g;
202 $ex =~ s/\ba\b/Re($target)/g;
203 $ex =~ s/\bb\b/Im($target)/g;
204 return $ex;
205 }
206 elsif (/^\s*"(.*)"/) {
207 return "\"$1\"";
208 }
209 return $_;
210}
211
212sub check {
213 my ($test, $try, $got, $expected, @z) = @_;
0c721ce2
JH
214
215# print "# @_\n";
216
217 if ("$got" eq "$expected"
218 ||
219 ($expected =~ /^-?\d/ && $got == $expected)
220 ||
221 (abs($got - $expected) < $eps)
222 ) {
89d0527d
RM
223 print "ok $test\n";
224 } else {
225 print "not ok $test\n";
226 my $args = (@z == 1) ? "z = $z[0]" : "z0 = $z[0], z1 = $z[1]";
227 print "# '$try' expected: '$expected' got: '$got' for $args\n";
228 }
229}
230__END__
0e505df1 231&+;=
89d0527d
RM
232(3,4):(3,4):(6,8)
233(-3,4):(3,-4):(0,0)
234(3,4):-3:(0,4)
2351:(4,2):(5,2)
236[2,0]:[2,pi]:(0,0)
237
238&++
239(2,1):(3,1)
240
0e505df1 241&-;=
89d0527d
RM
242(2,3):(-2,-3)
243[2,pi/2]:[2,-(pi)/2]
2442:[2,0]:(0,0)
245[3,0]:2:(1,0)
2463:(4,5):(-1,-5)
247(4,5):3:(1,5)
0e505df1 248(2,1):(3,5):(-1,-4)
89d0527d
RM
249
250&--
251(1,2):(0,2)
252[2,pi]:[3,pi]
253
0e505df1 254&*;=
89d0527d
RM
255(0,1):(0,1):(-1,0)
256(4,5):(1,0):(4,5)
257[2,2*pi/3]:(1,0):[2,2*pi/3]
2582:(0,1):(0,2)
259(0,1):3:(0,3)
260(0,1):(4,1):(-1,4)
261(2,1):(4,-1):(9,2)
262
0e505df1 263&/;=
89d0527d
RM
264(3,4):(3,4):(1,0)
265(4,-5):1:(4,-5)
2661:(0,1):(0,-1)
267(0,6):(0,2):(3,0)
268(9,2):(4,-1):(2,1)
269[4,pi]:[2,pi/2]:[2,pi/2]
270[2,pi/2]:[4,pi]:[0.5,-(pi)/2]
271
0e505df1
JH
272&**;=
273(2,0):(3,0):(8,0)
274(3,0):(2,0):(9,0)
275(2,3):(4,0):(-119,-120)
ace5de91
GS
276(0,0):(1,0):(0,0)
277(0,0):(2,3):(0,0)
278(1,0):(0,0):(1,0)
279(1,0):(1,0):(1,0)
280(1,0):(2,3):(1,0)
281(2,3):(0,0):(1,0)
282(2,3):(1,0):(2,3)
0e505df1 283
0c721ce2
JH
284&Re
285(3,4):3
286(-3,4):-3
287[1,pi/2]:0
288
289&Im
290(3,4):4
291(3,-4):-4
292[1,pi/2]:1
293
89d0527d
RM
294&abs
295(3,4):5
296(-3,4):5
297
0c721ce2
JH
298&arg
299[2,0]:0
300[-2,0]:pi
301
89d0527d
RM
302&~
303(4,5):(4,-5)
304(-3,4):(-3,-4)
305[2,pi/2]:[2,-(pi)/2]
306
307&<
308(3,4):(1,2):0
309(3,4):(3,2):0
310(3,4):(3,8):1
311(4,4):(5,129):1
312
313&==
314(3,4):(4,5):0
315(3,4):(3,5):0
316(3,4):(2,4):0
317(3,4):(3,4):1
318
319&sqrt
0c721ce2 320-9:(0,3)
89d0527d
RM
321(-100,0):(0,10)
322(16,-30):(5,-3)
323
324&stringify_cartesian
325(-100,0):"-100"
326(0,1):"i"
327(4,-3):"4-3i"
328(4,0):"4"
329(-4,0):"-4"
330(-2,4):"-2+4i"
331(-2,-1):"-2-i"
332
333&stringify_polar
334[-1, 0]:"[1,pi]"
335[1, pi/3]:"[1,pi/3]"
336[6, -2*pi/3]:"[6,-2pi/3]"
337[0.5, -9*pi/11]:"[0.5,-9pi/11]"
338
339{ (4,3); [3,2]; (-3,4); (0,2); [2,1] }
340
341|'z + ~z':'2*Re(z)'
342|'z - ~z':'2*i*Im(z)'
343|'z * ~z':'abs(z) * abs(z)'
344
f4837644 345{ (2,3); [3,2]; (-3,2); (0,2); 3; 1.2; (-3, 0); (-2, -1); [2,1] }
89d0527d 346
0c721ce2
JH
347|'(root(z, 4))[1] ** 4':'z'
348|'(root(z, 5))[3] ** 5':'z'
349|'(root(z, 8))[7] ** 8':'z'
89d0527d 350|'abs(z)':'r'
0c721ce2
JH
351|'acot(z)':'acotan(z)'
352|'acsc(z)':'acosec(z)'
353|'acsc(z)':'asin(1 / z)'
354|'asec(z)':'acos(1 / z)'
89d0527d 355|'cbrt(z)':'cbrt(r) * exp(i * t/3)'
89d0527d 356|'cos(acos(z))':'z'
89d0527d 357|'cos(z) ** 2 + sin(z) ** 2':1
89d0527d 358|'cos(z)':'cosh(i*z)'
0c721ce2
JH
359|'cosh(z) ** 2 - sinh(z) ** 2':1
360|'cot(acot(z))':'z'
361|'cot(z)':'1 / tan(z)'
362|'cot(z)':'cotan(z)'
363|'csc(acsc(z))':'z'
364|'csc(z)':'1 / sin(z)'
365|'csc(z)':'cosec(z)'
89d0527d 366|'exp(log(z))':'z'
0c721ce2
JH
367|'exp(z)':'exp(a) * exp(i * b)'
368|'ln(z)':'log(z)'
369|'log(exp(z))':'z'
370|'log(z)':'log(r) + i*t'
89d0527d 371|'log10(z)':'log(z) / log(10)'
89d0527d 372|'logn(z, 2)':'log(z) / log(2)'
0c721ce2
JH
373|'logn(z, 3)':'log(z) / log(3)'
374|'sec(asec(z))':'z'
375|'sec(z)':'1 / cos(z)'
376|'sin(asin(z))':'z'
377|'sin(i * z)':'i * sinh(z)'
378|'sqrt(z) * sqrt(z)':'z'
379|'sqrt(z)':'sqrt(r) * exp(i * t/2)'
380|'tan(atan(z))':'z'
381|'z**z':'exp(z * log(z))'
89d0527d 382
0c721ce2 383{ (1,1); [1,0.5]; (-2, -1); 2; -3; (-1,0.5); (0,0.5); 0.5; (2, 0); (-1, -2) }
89d0527d 384
89d0527d 385|'cosh(acosh(z))':'z'
0c721ce2
JH
386|'coth(acoth(z))':'z'
387|'coth(z)':'1 / tanh(z)'
388|'coth(z)':'cotanh(z)'
389|'csch(acsch(z))':'z'
390|'csch(z)':'1 / sinh(z)'
391|'csch(z)':'cosech(z)'
392|'sech(asech(z))':'z'
393|'sech(z)':'1 / cosh(z)'
394|'sinh(asinh(z))':'z'
89d0527d 395|'tanh(atanh(z))':'z'
89d0527d 396
0c721ce2 397{ (0.2,-0.4); [1,0.5]; -1.2; (-1,0.5); 0.5; (1.1, 0) }
89d0527d 398
89d0527d 399|'acos(cos(z)) ** 2':'z * z'
89d0527d 400|'acosh(cosh(z)) ** 2':'z * z'
0c721ce2
JH
401|'acoth(z)':'acotanh(z)'
402|'acoth(z)':'atanh(1 / z)'
403|'acsch(z)':'acosech(z)'
404|'acsch(z)':'asinh(1 / z)'
405|'asech(z)':'acosh(1 / z)'
406|'asin(sin(z))':'z'
407|'asinh(sinh(z))':'z'
408|'atan(tan(z))':'z'
89d0527d
RM
409|'atanh(tanh(z))':'z'
410
0c721ce2
JH
411&sin
412( 2, 3):( 9.15449914691143, -4.16890695996656)
413(-2, 3):( -9.15449914691143, -4.16890695996656)
414(-2,-3):( -9.15449914691143, 4.16890695996656)
415( 2,-3):( 9.15449914691143, 4.16890695996656)
416
417&cos
418( 2, 3):( -4.18962569096881, -9.10922789375534)
419(-2, 3):( -4.18962569096881, 9.10922789375534)
420(-2,-3):( -4.18962569096881, -9.10922789375534)
421( 2,-3):( -4.18962569096881, 9.10922789375534)
422
423&tan
424( 2, 3):( -0.00376402564150, 1.00323862735361)
425(-2, 3):( 0.00376402564150, 1.00323862735361)
426(-2,-3):( 0.00376402564150, -1.00323862735361)
427( 2,-3):( -0.00376402564150, -1.00323862735361)
428
429&sec
430( 2, 3):( -0.04167496441114, 0.09061113719624)
431(-2, 3):( -0.04167496441114, -0.09061113719624)
432(-2,-3):( -0.04167496441114, 0.09061113719624)
433( 2,-3):( -0.04167496441114, -0.09061113719624)
434
435&csc
436( 2, 3):( 0.09047320975321, 0.04120098628857)
437(-2, 3):( -0.09047320975321, 0.04120098628857)
438(-2,-3):( -0.09047320975321, -0.04120098628857)
439( 2,-3):( 0.09047320975321, -0.04120098628857)
440
441&cot
442( 2, 3):( -0.00373971037634, -0.99675779656936)
443(-2, 3):( 0.00373971037634, -0.99675779656936)
444(-2,-3):( 0.00373971037634, 0.99675779656936)
445( 2,-3):( -0.00373971037634, 0.99675779656936)
446
447&asin
448( 2, 3):( 0.57065278432110, 1.98338702991654)
449(-2, 3):( -0.57065278432110, 1.98338702991654)
450(-2,-3):( -0.57065278432110, -1.98338702991654)
451( 2,-3):( 0.57065278432110, -1.98338702991654)
452
453&acos
454( 2, 3):( 1.00014354247380, -1.98338702991654)
455(-2, 3):( 2.14144911111600, -1.98338702991654)
456(-2,-3):( 2.14144911111600, 1.98338702991654)
457( 2,-3):( 1.00014354247380, 1.98338702991654)
458
459&atan
460( 2, 3):( 1.40992104959658, 0.22907268296854)
461(-2, 3):( -1.40992104959658, 0.22907268296854)
462(-2,-3):( -1.40992104959658, -0.22907268296854)
463( 2,-3):( 1.40992104959658, -0.22907268296854)
464
465&asec
466( 2, 3):( 1.42041072246703, 0.23133469857397)
467(-2, 3):( 1.72118193112276, 0.23133469857397)
468(-2,-3):( 1.72118193112276, -0.23133469857397)
469( 2,-3):( 1.42041072246703, -0.23133469857397)
470
471&acsc
472( 2, 3):( 0.15038560432786, -0.23133469857397)
473(-2, 3):( -0.15038560432786, -0.23133469857397)
474(-2,-3):( -0.15038560432786, 0.23133469857397)
475( 2,-3):( 0.15038560432786, 0.23133469857397)
476
477&acot
478( 2, 3):( 0.16087527719832, -0.22907268296854)
479(-2, 3):( -0.16087527719832, -0.22907268296854)
480(-2,-3):( -0.16087527719832, 0.22907268296854)
481( 2,-3):( 0.16087527719832, 0.22907268296854)
482
483&sinh
484( 2, 3):( -3.59056458998578, 0.53092108624852)
485(-2, 3):( 3.59056458998578, 0.53092108624852)
486(-2,-3):( 3.59056458998578, -0.53092108624852)
487( 2,-3):( -3.59056458998578, -0.53092108624852)
488
489&cosh
490( 2, 3):( -3.72454550491532, 0.51182256998738)
491(-2, 3):( -3.72454550491532, -0.51182256998738)
492(-2,-3):( -3.72454550491532, 0.51182256998738)
493( 2,-3):( -3.72454550491532, -0.51182256998738)
494
495&tanh
496( 2, 3):( 0.96538587902213, -0.00988437503832)
497(-2, 3):( -0.96538587902213, -0.00988437503832)
498(-2,-3):( -0.96538587902213, 0.00988437503832)
499( 2,-3):( 0.96538587902213, 0.00988437503832)
500
501&sech
502( 2, 3):( -0.26351297515839, -0.03621163655877)
503(-2, 3):( -0.26351297515839, 0.03621163655877)
504(-2,-3):( -0.26351297515839, -0.03621163655877)
505( 2,-3):( -0.26351297515839, 0.03621163655877)
506
507&csch
508( 2, 3):( -0.27254866146294, -0.04030057885689)
509(-2, 3):( 0.27254866146294, -0.04030057885689)
510(-2,-3):( 0.27254866146294, 0.04030057885689)
511( 2,-3):( -0.27254866146294, 0.04030057885689)
512
513&coth
514( 2, 3):( 1.03574663776500, 0.01060478347034)
515(-2, 3):( -1.03574663776500, 0.01060478347034)
516(-2,-3):( -1.03574663776500, -0.01060478347034)
517( 2,-3):( 1.03574663776500, -0.01060478347034)
518
519&asinh
520( 2, 3):( 1.96863792579310, 0.96465850440760)
521(-2, 3):( -1.96863792579310, 0.96465850440761)
522(-2,-3):( -1.96863792579310, -0.96465850440761)
523( 2,-3):( 1.96863792579310, -0.96465850440760)
524
525&acosh
526( 2, 3):( 1.98338702991654, 1.00014354247380)
527(-2, 3):( -1.98338702991653, -2.14144911111600)
528(-2,-3):( -1.98338702991653, 2.14144911111600)
529( 2,-3):( 1.98338702991654, -1.00014354247380)
530
531&atanh
532( 2, 3):( 0.14694666622553, 1.33897252229449)
533(-2, 3):( -0.14694666622553, 1.33897252229449)
534(-2,-3):( -0.14694666622553, -1.33897252229449)
535( 2,-3):( 0.14694666622553, -1.33897252229449)
536
537&asech
538( 2, 3):( 0.23133469857397, -1.42041072246703)
539(-2, 3):( -0.23133469857397, 1.72118193112276)
540(-2,-3):( -0.23133469857397, -1.72118193112276)
541( 2,-3):( 0.23133469857397, 1.42041072246703)
542
543&acsch
544( 2, 3):( 0.15735549884499, -0.22996290237721)
545(-2, 3):( -0.15735549884499, -0.22996290237721)
546(-2,-3):( -0.15735549884499, 0.22996290237721)
547( 2,-3):( 0.15735549884499, 0.22996290237721)
548
549&acoth
550( 2, 3):( 0.14694666622553, -0.23182380450040)
551(-2, 3):( -0.14694666622553, -0.23182380450040)
552(-2,-3):( -0.14694666622553, 0.23182380450040)
553( 2,-3):( 0.14694666622553, 0.23182380450040)
554
555# eof