Win32 update (two patches)
[p5sagit/p5-mst-13.2.git] / t / lib / complex.t
CommitLineData
89d0527d 1#!./perl
2
3# $RCSfile$
4#
5aabfad6 5# Regression tests for the Math::Complex pacakge
6# -- Raphael Manfredi, September 1996
5cd24f17 7# -- Jarkko Hietaniemi, March-April 1997
5aabfad6 8
89d0527d 9BEGIN {
10 chdir 't' if -d 't';
11 @INC = '../lib';
12}
5aabfad6 13
89d0527d 14use Math::Complex;
15
16$test = 0;
17$| = 1;
fe82cf6e 18@script = ();
5aabfad6 19my $eps = 1e-11;
89d0527d 20
21while (<DATA>) {
0c721ce2 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 27 $op = $1;
28 next;
29 }
0e505df1 30 elsif (/^\{(.+)\}/) {
89d0527d 31 set($1, \@set, \@val);
32 next;
33 }
34 elsif (s/^\|//) {
0c721ce2 35 $test_set = 1; # Requests we loop over the set...
89d0527d 36 }
37 my @args = split(/:/);
0c721ce2 38 if ($test_set == 1) {
89d0527d 39 my $i;
40 for ($i = 0; $i < @set; $i++) {
0c721ce2 41 # complex number
42 $target = $set[$i];
43 # textual value as found in set definition
44 $zvalue = $val[$i];
89d0527d 45 test($zvalue, $target, @args);
46 }
47 } else {
48 test($op, undef, @args);
49 }
50}
51
5cd24f17 52# test the divbyzeros
53
0e505df1 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 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 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 101 for my $op (@_) {
102 $test++;
103
0e505df1 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 107 push(@script, qq(print "ok $test\n";));
108 }
109}
110
0e505df1 111test_broot(qw(-3 -2.1 0 0.99));
112
89d0527d 113print "1..$test\n";
fe82cf6e 114eval join '', @script;
89d0527d 115die $@ if $@;
116
0e505df1 117sub abop {
118 my ($op) = @_;
119
120 push(@script, qq(print "# $op=\n";));
121}
122
89d0527d 123sub test {
124 my ($op, $z, @args) = @_;
0e505df1 125 my ($baop) = 0;
89d0527d 126 $test++;
127 my $i;
0e505df1 128 $baop = 1 if ($op =~ s/;=$//);
89d0527d 129 for ($i = 0; $i < @args; $i++) {
130 $val = value($args[$i]);
fe82cf6e 131 push @script, "\$z$i = $val;\n";
89d0527d 132 }
133 if (defined $z) {
134 $args = "'$op'"; # Really the value
0c721ce2 135 $try = "abs(\$z0 - \$z1) <= $eps ? \$z1 : \$z0";
fe82cf6e 136 push @script, "\$res = $try; ";
137 push @script, "check($test, $args[0], \$res, \$z$#args, $args);\n";
89d0527d 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 147 push @script, "\$res = $try; ";
148 push @script, "check($test, '$try', \$res, \$z$#args, $args);\n";
0e505df1 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 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 184 push @script, "\$s$i = $val;\n";
185 push @{$setref}, "\$s$i";
89d0527d 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 214
215# print "# @_\n";
216
217 if ("$got" eq "$expected"
218 ||
219 ($expected =~ /^-?\d/ && $got == $expected)
220 ||
221 (abs($got - $expected) < $eps)
222 ) {
89d0527d 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 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 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 249
250&--
251(1,2):(0,2)
252[2,pi]:[3,pi]
253
0e505df1 254&*;=
89d0527d 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 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 272&**;=
273(2,0):(3,0):(8,0)
274(3,0):(2,0):(9,0)
275(2,3):(4,0):(-119,-120)
276
0c721ce2 277&Re
278(3,4):3
279(-3,4):-3
280[1,pi/2]:0
281
282&Im
283(3,4):4
284(3,-4):-4
285[1,pi/2]:1
286
89d0527d 287&abs
288(3,4):5
289(-3,4):5
290
0c721ce2 291&arg
292[2,0]:0
293[-2,0]:pi
294
89d0527d 295&~
296(4,5):(4,-5)
297(-3,4):(-3,-4)
298[2,pi/2]:[2,-(pi)/2]
299
300&<
301(3,4):(1,2):0
302(3,4):(3,2):0
303(3,4):(3,8):1
304(4,4):(5,129):1
305
306&==
307(3,4):(4,5):0
308(3,4):(3,5):0
309(3,4):(2,4):0
310(3,4):(3,4):1
311
312&sqrt
0c721ce2 313-9:(0,3)
89d0527d 314(-100,0):(0,10)
315(16,-30):(5,-3)
316
317&stringify_cartesian
318(-100,0):"-100"
319(0,1):"i"
320(4,-3):"4-3i"
321(4,0):"4"
322(-4,0):"-4"
323(-2,4):"-2+4i"
324(-2,-1):"-2-i"
325
326&stringify_polar
327[-1, 0]:"[1,pi]"
328[1, pi/3]:"[1,pi/3]"
329[6, -2*pi/3]:"[6,-2pi/3]"
330[0.5, -9*pi/11]:"[0.5,-9pi/11]"
331
332{ (4,3); [3,2]; (-3,4); (0,2); [2,1] }
333
334|'z + ~z':'2*Re(z)'
335|'z - ~z':'2*i*Im(z)'
336|'z * ~z':'abs(z) * abs(z)'
337
0c721ce2 338{ (2,3); [3,2]; (-3,2); (0,2); 3; 1.2; -3; (-3, 0); (-2, -1); [2,1] }
89d0527d 339
0c721ce2 340|'(root(z, 4))[1] ** 4':'z'
341|'(root(z, 5))[3] ** 5':'z'
342|'(root(z, 8))[7] ** 8':'z'
89d0527d 343|'abs(z)':'r'
0c721ce2 344|'acot(z)':'acotan(z)'
345|'acsc(z)':'acosec(z)'
346|'acsc(z)':'asin(1 / z)'
347|'asec(z)':'acos(1 / z)'
89d0527d 348|'cbrt(z)':'cbrt(r) * exp(i * t/3)'
89d0527d 349|'cos(acos(z))':'z'
89d0527d 350|'cos(z) ** 2 + sin(z) ** 2':1
89d0527d 351|'cos(z)':'cosh(i*z)'
0c721ce2 352|'cosh(z) ** 2 - sinh(z) ** 2':1
353|'cot(acot(z))':'z'
354|'cot(z)':'1 / tan(z)'
355|'cot(z)':'cotan(z)'
356|'csc(acsc(z))':'z'
357|'csc(z)':'1 / sin(z)'
358|'csc(z)':'cosec(z)'
89d0527d 359|'exp(log(z))':'z'
0c721ce2 360|'exp(z)':'exp(a) * exp(i * b)'
361|'ln(z)':'log(z)'
362|'log(exp(z))':'z'
363|'log(z)':'log(r) + i*t'
89d0527d 364|'log10(z)':'log(z) / log(10)'
89d0527d 365|'logn(z, 2)':'log(z) / log(2)'
0c721ce2 366|'logn(z, 3)':'log(z) / log(3)'
367|'sec(asec(z))':'z'
368|'sec(z)':'1 / cos(z)'
369|'sin(asin(z))':'z'
370|'sin(i * z)':'i * sinh(z)'
371|'sqrt(z) * sqrt(z)':'z'
372|'sqrt(z)':'sqrt(r) * exp(i * t/2)'
373|'tan(atan(z))':'z'
374|'z**z':'exp(z * log(z))'
89d0527d 375
0c721ce2 376{ (1,1); [1,0.5]; (-2, -1); 2; -3; (-1,0.5); (0,0.5); 0.5; (2, 0); (-1, -2) }
89d0527d 377
89d0527d 378|'cosh(acosh(z))':'z'
0c721ce2 379|'coth(acoth(z))':'z'
380|'coth(z)':'1 / tanh(z)'
381|'coth(z)':'cotanh(z)'
382|'csch(acsch(z))':'z'
383|'csch(z)':'1 / sinh(z)'
384|'csch(z)':'cosech(z)'
385|'sech(asech(z))':'z'
386|'sech(z)':'1 / cosh(z)'
387|'sinh(asinh(z))':'z'
89d0527d 388|'tanh(atanh(z))':'z'
89d0527d 389
0c721ce2 390{ (0.2,-0.4); [1,0.5]; -1.2; (-1,0.5); 0.5; (1.1, 0) }
89d0527d 391
89d0527d 392|'acos(cos(z)) ** 2':'z * z'
89d0527d 393|'acosh(cosh(z)) ** 2':'z * z'
0c721ce2 394|'acoth(z)':'acotanh(z)'
395|'acoth(z)':'atanh(1 / z)'
396|'acsch(z)':'acosech(z)'
397|'acsch(z)':'asinh(1 / z)'
398|'asech(z)':'acosh(1 / z)'
399|'asin(sin(z))':'z'
400|'asinh(sinh(z))':'z'
401|'atan(tan(z))':'z'
89d0527d 402|'atanh(tanh(z))':'z'
403
0c721ce2 404&sin
405( 2, 3):( 9.15449914691143, -4.16890695996656)
406(-2, 3):( -9.15449914691143, -4.16890695996656)
407(-2,-3):( -9.15449914691143, 4.16890695996656)
408( 2,-3):( 9.15449914691143, 4.16890695996656)
409
410&cos
411( 2, 3):( -4.18962569096881, -9.10922789375534)
412(-2, 3):( -4.18962569096881, 9.10922789375534)
413(-2,-3):( -4.18962569096881, -9.10922789375534)
414( 2,-3):( -4.18962569096881, 9.10922789375534)
415
416&tan
417( 2, 3):( -0.00376402564150, 1.00323862735361)
418(-2, 3):( 0.00376402564150, 1.00323862735361)
419(-2,-3):( 0.00376402564150, -1.00323862735361)
420( 2,-3):( -0.00376402564150, -1.00323862735361)
421
422&sec
423( 2, 3):( -0.04167496441114, 0.09061113719624)
424(-2, 3):( -0.04167496441114, -0.09061113719624)
425(-2,-3):( -0.04167496441114, 0.09061113719624)
426( 2,-3):( -0.04167496441114, -0.09061113719624)
427
428&csc
429( 2, 3):( 0.09047320975321, 0.04120098628857)
430(-2, 3):( -0.09047320975321, 0.04120098628857)
431(-2,-3):( -0.09047320975321, -0.04120098628857)
432( 2,-3):( 0.09047320975321, -0.04120098628857)
433
434&cot
435( 2, 3):( -0.00373971037634, -0.99675779656936)
436(-2, 3):( 0.00373971037634, -0.99675779656936)
437(-2,-3):( 0.00373971037634, 0.99675779656936)
438( 2,-3):( -0.00373971037634, 0.99675779656936)
439
440&asin
441( 2, 3):( 0.57065278432110, 1.98338702991654)
442(-2, 3):( -0.57065278432110, 1.98338702991654)
443(-2,-3):( -0.57065278432110, -1.98338702991654)
444( 2,-3):( 0.57065278432110, -1.98338702991654)
445
446&acos
447( 2, 3):( 1.00014354247380, -1.98338702991654)
448(-2, 3):( 2.14144911111600, -1.98338702991654)
449(-2,-3):( 2.14144911111600, 1.98338702991654)
450( 2,-3):( 1.00014354247380, 1.98338702991654)
451
452&atan
453( 2, 3):( 1.40992104959658, 0.22907268296854)
454(-2, 3):( -1.40992104959658, 0.22907268296854)
455(-2,-3):( -1.40992104959658, -0.22907268296854)
456( 2,-3):( 1.40992104959658, -0.22907268296854)
457
458&asec
459( 2, 3):( 1.42041072246703, 0.23133469857397)
460(-2, 3):( 1.72118193112276, 0.23133469857397)
461(-2,-3):( 1.72118193112276, -0.23133469857397)
462( 2,-3):( 1.42041072246703, -0.23133469857397)
463
464&acsc
465( 2, 3):( 0.15038560432786, -0.23133469857397)
466(-2, 3):( -0.15038560432786, -0.23133469857397)
467(-2,-3):( -0.15038560432786, 0.23133469857397)
468( 2,-3):( 0.15038560432786, 0.23133469857397)
469
470&acot
471( 2, 3):( 0.16087527719832, -0.22907268296854)
472(-2, 3):( -0.16087527719832, -0.22907268296854)
473(-2,-3):( -0.16087527719832, 0.22907268296854)
474( 2,-3):( 0.16087527719832, 0.22907268296854)
475
476&sinh
477( 2, 3):( -3.59056458998578, 0.53092108624852)
478(-2, 3):( 3.59056458998578, 0.53092108624852)
479(-2,-3):( 3.59056458998578, -0.53092108624852)
480( 2,-3):( -3.59056458998578, -0.53092108624852)
481
482&cosh
483( 2, 3):( -3.72454550491532, 0.51182256998738)
484(-2, 3):( -3.72454550491532, -0.51182256998738)
485(-2,-3):( -3.72454550491532, 0.51182256998738)
486( 2,-3):( -3.72454550491532, -0.51182256998738)
487
488&tanh
489( 2, 3):( 0.96538587902213, -0.00988437503832)
490(-2, 3):( -0.96538587902213, -0.00988437503832)
491(-2,-3):( -0.96538587902213, 0.00988437503832)
492( 2,-3):( 0.96538587902213, 0.00988437503832)
493
494&sech
495( 2, 3):( -0.26351297515839, -0.03621163655877)
496(-2, 3):( -0.26351297515839, 0.03621163655877)
497(-2,-3):( -0.26351297515839, -0.03621163655877)
498( 2,-3):( -0.26351297515839, 0.03621163655877)
499
500&csch
501( 2, 3):( -0.27254866146294, -0.04030057885689)
502(-2, 3):( 0.27254866146294, -0.04030057885689)
503(-2,-3):( 0.27254866146294, 0.04030057885689)
504( 2,-3):( -0.27254866146294, 0.04030057885689)
505
506&coth
507( 2, 3):( 1.03574663776500, 0.01060478347034)
508(-2, 3):( -1.03574663776500, 0.01060478347034)
509(-2,-3):( -1.03574663776500, -0.01060478347034)
510( 2,-3):( 1.03574663776500, -0.01060478347034)
511
512&asinh
513( 2, 3):( 1.96863792579310, 0.96465850440760)
514(-2, 3):( -1.96863792579310, 0.96465850440761)
515(-2,-3):( -1.96863792579310, -0.96465850440761)
516( 2,-3):( 1.96863792579310, -0.96465850440760)
517
518&acosh
519( 2, 3):( 1.98338702991654, 1.00014354247380)
520(-2, 3):( -1.98338702991653, -2.14144911111600)
521(-2,-3):( -1.98338702991653, 2.14144911111600)
522( 2,-3):( 1.98338702991654, -1.00014354247380)
523
524&atanh
525( 2, 3):( 0.14694666622553, 1.33897252229449)
526(-2, 3):( -0.14694666622553, 1.33897252229449)
527(-2,-3):( -0.14694666622553, -1.33897252229449)
528( 2,-3):( 0.14694666622553, -1.33897252229449)
529
530&asech
531( 2, 3):( 0.23133469857397, -1.42041072246703)
532(-2, 3):( -0.23133469857397, 1.72118193112276)
533(-2,-3):( -0.23133469857397, -1.72118193112276)
534( 2,-3):( 0.23133469857397, 1.42041072246703)
535
536&acsch
537( 2, 3):( 0.15735549884499, -0.22996290237721)
538(-2, 3):( -0.15735549884499, -0.22996290237721)
539(-2,-3):( -0.15735549884499, 0.22996290237721)
540( 2,-3):( 0.15735549884499, 0.22996290237721)
541
542&acoth
543( 2, 3):( 0.14694666622553, -0.23182380450040)
544(-2, 3):( -0.14694666622553, -0.23182380450040)
545(-2,-3):( -0.14694666622553, 0.23182380450040)
546( 2,-3):( 0.14694666622553, 0.23182380450040)
547
548# eof