Macro reorganization in the Makefile, so that $(LDLIBPTH) always
[p5sagit/p5-mst-13.2.git] / lib / Math / BigInt / t / mbimbf.inc
CommitLineData
61f5c3f5 1# test rounding, accuracy, precicion and fallback, round_mode and mixing
2# of classes
3
4# Make sure you always quote any bare floating-point values, lest 123.46 will
5# be stringified to 123.4599999999 due to limited float prevision.
6
56d9de68 7use strict;
61f5c3f5 8my ($x,$y,$z,$u,$rc);
9
10###############################################################################
11# test defaults and set/get
12
56d9de68 13{
14 no strict 'refs';
15 ok_undef (${"$mbi\::accuracy"});
16 ok_undef (${"$mbi\::precision"});
17 ok_undef ($mbi->accuracy());
18 ok_undef ($mbi->precision());
19 ok (${"$mbi\::div_scale"},40);
20 ok (${"$mbi\::round_mode"},'even');
21 ok ($mbi->round_mode(),'even');
22
23 ok_undef (${"$mbf\::accuracy"});
24 ok_undef (${"$mbf\::precision"});
25 ok_undef ($mbf->precision());
26 ok_undef ($mbf->precision());
27 ok (${"$mbf\::div_scale"},40);
28 ok (${"$mbf\::round_mode"},'even');
29 ok ($mbf->round_mode(),'even');
30}
61f5c3f5 31
32# accessors
33foreach my $class ($mbi,$mbf)
34 {
35 ok_undef ($class->accuracy());
36 ok_undef ($class->precision());
37 ok ($class->round_mode(),'even');
38 ok ($class->div_scale(),40);
39
40 ok ($class->div_scale(20),20);
41 $class->div_scale(40); ok ($class->div_scale(),40);
42
43 ok ($class->round_mode('odd'),'odd');
44 $class->round_mode('even'); ok ($class->round_mode(),'even');
45
46 ok ($class->accuracy(2),2);
47 $class->accuracy(3); ok ($class->accuracy(),3);
48 ok_undef ($class->accuracy(undef));
49
50 ok ($class->precision(2),2);
51 ok ($class->precision(-2),-2);
52 $class->precision(3); ok ($class->precision(),3);
53 ok_undef ($class->precision(undef));
54 }
55
56d9de68 56{
57 no strict 'refs';
58 # accuracy
59 foreach (qw/5 42 -1 0/)
60 {
61 ok (${"$mbf\::accuracy"} = $_,$_);
62 ok (${"$mbi\::accuracy"} = $_,$_);
63 }
64 ok_undef (${"$mbf\::accuracy"} = undef);
65 ok_undef (${"$mbi\::accuracy"} = undef);
61f5c3f5 66
56d9de68 67 # precision
68 foreach (qw/5 42 -1 0/)
69 {
70 ok (${"$mbf\::precision"} = $_,$_);
71 ok (${"$mbi\::precision"} = $_,$_);
72 }
73 ok_undef (${"$mbf\::precision"} = undef);
74 ok_undef (${"$mbi\::precision"} = undef);
61f5c3f5 75
56d9de68 76 # fallback
77 foreach (qw/5 42 1/)
78 {
79 ok (${"$mbf\::div_scale"} = $_,$_);
80 ok (${"$mbi\::div_scale"} = $_,$_);
81 }
82 # illegal values are possible for fallback due to no accessor
61f5c3f5 83
56d9de68 84 # round_mode
85 foreach (qw/odd even zero trunc +inf -inf/)
86 {
87 ok (${"$mbf\::round_mode"} = $_,$_);
88 ok (${"$mbi\::round_mode"} = $_,$_);
89 }
90 ${"$mbf\::round_mode"} = 'zero';
91 ok (${"$mbf\::round_mode"},'zero');
92 ok (${"$mbi\::round_mode"},'-inf'); # from above
93
2ab5f49d 94 # reset for further tests
56d9de68 95 ${"$mbi\::accuracy"} = undef;
96 ${"$mbi\::precision"} = undef;
2ab5f49d 97 ${"$mbf\::div_scale"} = 40;
56d9de68 98}
61f5c3f5 99
61f5c3f5 100# local copies
101$x = $mbf->new('123.456');
102ok_undef ($x->accuracy());
103ok ($x->accuracy(5),5);
104ok_undef ($x->accuracy(undef),undef);
105ok_undef ($x->precision());
106ok ($x->precision(5),5);
107ok_undef ($x->precision(undef),undef);
108
56d9de68 109{
110 no strict 'refs';
111 # see if MBF changes MBIs values
112 ok (${"$mbi\::accuracy"} = 42,42);
113 ok (${"$mbf\::accuracy"} = 64,64);
114 ok (${"$mbi\::accuracy"},42); # should be still 42
115 ok (${"$mbf\::accuracy"},64); # should be now 64
116}
61f5c3f5 117
118###############################################################################
119# see if creating a number under set A or P will round it
120
56d9de68 121{
122 no strict 'refs';
123 ${"$mbi\::accuracy"} = 4;
124 ${"$mbi\::precision"} = undef;
61f5c3f5 125
56d9de68 126 ok ($mbi->new(123456),123500); # with A
127 ${"$mbi\::accuracy"} = undef;
128 ${"$mbi\::precision"} = 3;
129 ok ($mbi->new(123456),123000); # with P
61f5c3f5 130
56d9de68 131 ${"$mbf\::accuracy"} = 4;
132 ${"$mbf\::precision"} = undef;
133 ${"$mbi\::precision"} = undef;
61f5c3f5 134
56d9de68 135 ok ($mbf->new('123.456'),'123.5'); # with A
136 ${"$mbf\::accuracy"} = undef;
137 ${"$mbf\::precision"} = -1;
138 ok ($mbf->new('123.456'),'123.5'); # with P from MBF, not MBI!
61f5c3f5 139
56d9de68 140 ${"$mbf\::precision"} = undef; # reset
141}
61f5c3f5 142
143###############################################################################
144# see if MBI leaves MBF's private parts alone
145
56d9de68 146{
147 no strict 'refs';
148 ${"$mbi\::precision"} = undef; ${"$mbf\::precision"} = undef;
149 ${"$mbi\::accuracy"} = 4; ${"$mbf\::accuracy"} = undef;
150 ok ($mbf->new('123.456'),'123.456');
151 ${"$mbi\::accuracy"} = undef; # reset
152}
61f5c3f5 153
154###############################################################################
155# see if setting accuracy/precision actually rounds the number
156
157$x = $mbf->new('123.456'); $x->accuracy(4); ok ($x,'123.5');
158$x = $mbf->new('123.456'); $x->precision(-2); ok ($x,'123.46');
159
160$x = $mbi->new(123456); $x->accuracy(4); ok ($x,123500);
161$x = $mbi->new(123456); $x->precision(2); ok ($x,123500);
162
163###############################################################################
164# test actual rounding via round()
165
166$x = $mbf->new('123.456');
167ok ($x->copy()->round(5),'123.46');
168ok ($x->copy()->round(4),'123.5');
169ok ($x->copy()->round(5,2),'NaN');
170ok ($x->copy()->round(undef,-2),'123.46');
b3abae2a 171ok ($x->copy()->round(undef,2),120);
61f5c3f5 172
173$x = $mbi->new('123');
174ok ($x->round(5,2),'NaN');
175
176$x = $mbf->new('123.45000');
177ok ($x->copy()->round(undef,-1,'odd'),'123.5');
178
179# see if rounding is 'sticky'
180$x = $mbf->new('123.4567');
181$y = $x->copy()->bround(); # no-op since nowhere A or P defined
182
183ok ($y,123.4567);
184$y = $x->copy()->round(5);
185ok ($y->accuracy(),5);
186ok_undef ($y->precision()); # A has precedence, so P still unset
187$y = $x->copy()->round(undef,2);
188ok ($y->precision(),2);
189ok_undef ($y->accuracy()); # P has precedence, so A still unset
190
191# see if setting A clears P and vice versa
192$x = $mbf->new('123.4567');
193ok ($x,'123.4567');
194ok ($x->accuracy(4),4);
195ok ($x->precision(-2),-2); # clear A
196ok_undef ($x->accuracy());
197
198$x = $mbf->new('123.4567');
199ok ($x,'123.4567');
200ok ($x->precision(-2),-2);
201ok ($x->accuracy(4),4); # clear P
202ok_undef ($x->precision());
203
204# does copy work?
205$x = $mbf->new(123.456); $x->accuracy(4); $x->precision(2);
206$z = $x->copy(); ok_undef ($z->accuracy(),undef); ok ($z->precision(),2);
207
56d9de68 208# does $x->bdiv($y,d) work when $d > div_scale?
209$x = $mbf->new('0.008'); $x->accuracy(8);
210
211for my $e ( 4, 8, 16, 32 )
212 {
213 print "# Tried: $x->bdiv(3,$e)\n"
214 unless ok (scalar $x->copy()->bdiv(3,$e), '0.002' . ('6' x ($e-2)) . '7');
215 }
216
61f5c3f5 217# does accuracy()/precision work on zeros?
56d9de68 218foreach my $c ($mbi,$mbf)
61f5c3f5 219 {
56d9de68 220 $x = $c->bzero(); $x->accuracy(5); ok ($x->{_a},5);
221 $x = $c->bzero(); $x->precision(5); ok ($x->{_p},5);
222 $x = $c->new(0); $x->accuracy(5); ok ($x->{_a},5);
223 $x = $c->new(0); $x->precision(5); ok ($x->{_p},5);
61f5c3f5 224
56d9de68 225 $x = $c->bzero(); $x->round(5); ok ($x->{_a},5);
226 $x = $c->bzero(); $x->round(undef,5); ok ($x->{_p},5);
227 $x = $c->new(0); $x->round(5); ok ($x->{_a},5);
228 $x = $c->new(0); $x->round(undef,5); ok ($x->{_p},5);
61f5c3f5 229
230 # see if trying to increasing A in bzero() doesn't do something
56d9de68 231 $x = $c->bzero(); $x->{_a} = 3; $x->round(5); ok ($x->{_a},3);
232 }
233
234###############################################################################
235# test whether an opp calls objectify properly or not (or at least does what
236# it should do given non-objects, w/ or w/o objectify())
237
238foreach my $c ($mbi,$mbf)
239 {
240# ${"$c\::precision"} = undef; # reset
241# ${"$c\::accuracy"} = undef; # reset
242
243 ok ($c->new(123)->badd(123),246);
244 ok ($c->badd(123,321),444);
245 ok ($c->badd(123,$c->new(321)),444);
246
247 ok ($c->new(123)->bsub(122),1);
248 ok ($c->bsub(321,123),198);
249 ok ($c->bsub(321,$c->new(123)),198);
250
251 ok ($c->new(123)->bmul(123),15129);
252 ok ($c->bmul(123,123),15129);
253 ok ($c->bmul(123,$c->new(123)),15129);
254
255# ok ($c->new(15129)->bdiv(123),123);
256# ok ($c->bdiv(15129,123),123);
257# ok ($c->bdiv(15129,$c->new(123)),123);
258
259 ok ($c->new(15131)->bmod(123),2);
260 ok ($c->bmod(15131,123),2);
261 ok ($c->bmod(15131,$c->new(123)),2);
262
263 ok ($c->new(2)->bpow(16),65536);
264 ok ($c->bpow(2,16),65536);
265 ok ($c->bpow(2,$c->new(16)),65536);
266
267# ok ($c->new(2**15)->brsft(1),2**14);
268# ok ($c->brsft(2**15,1),2**14);
269# ok ($c->brsft(2**15,$c->new(1)),2**14);
270
271 ok ($c->new(2**13)->blsft(1),2**14);
272 ok ($c->blsft(2**13,1),2**14);
273 ok ($c->blsft(2**13,$c->new(1)),2**14);
61f5c3f5 274 }
275
276###############################################################################
277# test wether operations round properly afterwards
278# These tests are not complete, since they do not excercise every "return"
279# statement in the op's. But heh, it's better than nothing...
280
281$x = $mbf->new('123.456');
282$y = $mbf->new('654.321');
283$x->{_a} = 5; # $x->accuracy(5) would round $x straightaway
284$y->{_a} = 4; # $y->accuracy(4) would round $x straightaway
285
286$z = $x + $y; ok ($z,'777.8');
287$z = $y - $x; ok ($z,'530.9');
288$z = $y * $x; ok ($z,'80780');
289$z = $x ** 2; ok ($z,'15241');
290$z = $x * $x; ok ($z,'15241');
291
292# not: $z = -$x; ok ($z,'-123.46'); ok ($x,'123.456');
293$z = $x->copy(); $z->{_a} = 2; $z = $z / 2; ok ($z,62);
294$x = $mbf->new(123456); $x->{_a} = 4;
295$z = $x->copy; $z++; ok ($z,123500);
296
297$x = $mbi->new(123456);
298$y = $mbi->new(654321);
299$x->{_a} = 5; # $x->accuracy(5) would round $x straightaway
300$y->{_a} = 4; # $y->accuracy(4) would round $x straightaway
301
302$z = $x + $y; ok ($z,777800);
303$z = $y - $x; ok ($z,530900);
304$z = $y * $x; ok ($z,80780000000);
305$z = $x ** 2; ok ($z,15241000000);
306# not yet: $z = -$x; ok ($z,-123460); ok ($x,123456);
307$z = $x->copy; $z++; ok ($z,123460);
308$z = $x->copy(); $z->{_a} = 2; $z = $z / 2; ok ($z,62000);
309
310$x = $mbi->new(123400); $x->{_a} = 4;
311ok ($x->bnot(),-123400); # not -1234001
312
313# both babs() and bneg() don't need to round, since the input will already
314# be rounded (either as $x or via new($string)), and they don't change the
315# value. The two tests below peek at this by using _a (illegally) directly
316$x = $mbi->new(-123401); $x->{_a} = 4; ok ($x->babs(),123401);
317$x = $mbi->new(-123401); $x->{_a} = 4; ok ($x->bneg(),123401);
318
319# test fdiv rounding to A and R (bug in v1.48 and maybe earlier versions)
320$mbf->round_mode('even');
321$x = $mbf->new('740.7')->fdiv('6',4,undef,'zero'); ok ($x,'123.4');
322
323###############################################################################
2ab5f49d 324# test that bop(0) does the same than bop(undef)
325
326$x = $mbf->new('1234567890');
327ok ($x->copy()->bsqrt(0),$x->copy()->bsqrt(undef));
328ok ($x->copy->bsqrt(0),'35136.41828644462161665823116758077037159');
329
330ok_undef ($x->{_a});
331
332# test that bsqrt() modifies $x and does not just return something else
333# (especially under BareCalc)
334$z = $x->bsqrt();
335ok ($z,$x); ok ($x,'35136.41828644462161665823116758077037159');
336
337$x = $mbf->new('1.234567890123456789');
338ok ($x->copy()->bpow('0.5',0),$x->copy()->bpow('0.5',undef));
339ok ($x->copy()->bpow('0.5',0),$x->copy()->bsqrt(undef));
340ok ($x->copy()->bpow('2',0),'1.524157875323883675019051998750190521');
341
342###############################################################################
f9a08e12 343# test (also under Bare) that bfac() rounds at last step
344
345ok ($mbi->new(12)->bfac(),'479001600');
346ok ($mbi->new(12)->bfac(2),'480000000');
347$x = $mbi->new(12); $x->accuracy(2); ok ($x->bfac(),'480000000');
348$x = $mbi->new(13); $x->accuracy(2); ok ($x->bfac(),'6200000000');
349$x = $mbi->new(13); $x->accuracy(3); ok ($x->bfac(),'6230000000');
350$x = $mbi->new(13); $x->accuracy(4); ok ($x->bfac(),'6227000000');
351# this does 1,2,3...9,10,11,12...20
352$x = $mbi->new(20); $x->accuracy(1); ok ($x->bfac(),'2000000000000000000');
353
354###############################################################################
355# test bsqrt) rounding to given A/P/R (bug prior to v1.60)
356$x = $mbi->new('123456')->bsqrt(2,undef); ok ($x,'350'); # not 351
357$x = $mbi->new('3')->bsqrt(2,undef); ok ($x->accuracy(),2);
358
359$mbi->round_mode('even'); $x = $mbi->new('126025')->bsqrt(2,undef,'+inf');
360ok ($x,'360'); # not 355 nor 350
361
362$x = $mbi->new('126025')->bsqrt(undef,2); ok ($x,'400'); # not 355
363
364
365###############################################################################
61f5c3f5 366# test mixed arguments
367
368$x = $mbf->new(10);
369$u = $mbf->new(2.5);
370$y = $mbi->new(2);
371
372$z = $x + $y; ok ($z,12); ok (ref($z),$mbf);
373$z = $x / $y; ok ($z,5); ok (ref($z),$mbf);
374$z = $u * $y; ok ($z,5); ok (ref($z),$mbf);
375
376$y = $mbi->new(12345);
377$z = $u->copy()->bmul($y,2,undef,'odd'); ok ($z,31000);
378$z = $u->copy()->bmul($y,3,undef,'odd'); ok ($z,30900);
379$z = $u->copy()->bmul($y,undef,0,'odd'); ok ($z,30863);
b3abae2a 380$z = $u->copy()->bmul($y,undef,1,'odd'); ok ($z,30863);
381$z = $u->copy()->bmul($y,undef,2,'odd'); ok ($z,30860);
382$z = $u->copy()->bmul($y,undef,3,'odd'); ok ($z,30900);
61f5c3f5 383$z = $u->copy()->bmul($y,undef,-1,'odd'); ok ($z,30862.5);
384
56d9de68 385my $warn = ''; $SIG{__WARN__} = sub { $warn = shift; };
386# these should warn, since '3.17' is a NaN in BigInt and thus >= returns undef
387$warn = ''; eval "\$z = 3.17 <= \$y"; ok ($z, 1);
388print "# Got: '$warn'\n" unless
389ok ($warn =~ /^Use of uninitialized value in numeric le \(<=\) at/);
390$warn = ''; eval "\$z = \$y >= 3.17"; ok ($z, 1);
391print "# Got: '$warn'\n" unless
392ok ($warn =~ /^Use of uninitialized value in numeric ge \(>=\) at/);
393
394# XXX TODO breakage:
61f5c3f5 395# $z = $y->copy()->bmul($u,2,0,'odd'); ok ($z,31000);
396# $z = $y * $u; ok ($z,5); ok (ref($z),$mbi);
397# $z = $y + $x; ok ($z,12); ok (ref($z),$mbi);
398# $z = $y / $x; ok ($z,0); ok (ref($z),$mbi);
399
400###############################################################################
401# rounding in bdiv with fallback and already set A or P
402
56d9de68 403{
404 no strict 'refs';
405 ${"$mbf\::accuracy"} = undef;
406 ${"$mbf\::precision"} = undef;
407 ${"$mbf\::div_scale"} = 40;
408}
61f5c3f5 409
56d9de68 410 $x = $mbf->new(10); $x->{_a} = 4;
411 ok ($x->bdiv(3),'3.333');
412 ok ($x->{_a},4); # set's it since no fallback
61f5c3f5 413
414$x = $mbf->new(10); $x->{_a} = 4; $y = $mbf->new(3);
415ok ($x->bdiv($y),'3.333');
416ok ($x->{_a},4); # set's it since no fallback
417
418# rounding to P of x
419$x = $mbf->new(10); $x->{_p} = -2;
420ok ($x->bdiv(3),'3.33');
421
422# round in div with requested P
423$x = $mbf->new(10);
424ok ($x->bdiv(3,undef,-2),'3.33');
425
426# round in div with requested P greater than fallback
56d9de68 427{
428 no strict 'refs';
429 ${"$mbf\::div_scale"} = 5;
430 $x = $mbf->new(10);
431 ok ($x->bdiv(3,undef,-8),'3.33333333');
432 ${"$mbf\::div_scale"} = 40;
433}
61f5c3f5 434
435$x = $mbf->new(10); $y = $mbf->new(3); $y->{_a} = 4;
436ok ($x->bdiv($y),'3.333');
437ok ($x->{_a},4); ok ($y->{_a},4); # set's it since no fallback
438ok_undef ($x->{_p}); ok_undef ($y->{_p});
439
440# rounding to P of y
441$x = $mbf->new(10); $y = $mbf->new(3); $y->{_p} = -2;
442ok ($x->bdiv($y),'3.33');
443ok ($x->{_p},-2);
444 ok ($y->{_p},-2);
445ok_undef ($x->{_a}); ok_undef ($y->{_a});
446
447###############################################################################
448# test whether bround(-n) fails in MBF (undocumented in MBI)
449eval { $x = $mbf->new(1); $x->bround(-2); };
450ok ($@ =~ /^bround\(\) needs positive accuracy/,1);
451
452# test whether rounding to higher accuracy is no-op
453$x = $mbf->new(1); $x->{_a} = 4;
454ok ($x,'1.000');
455$x->bround(6); # must be no-op
456ok ($x->{_a},4);
457ok ($x,'1.000');
458
459$x = $mbi->new(1230); $x->{_a} = 3;
460ok ($x,'1230');
461$x->bround(6); # must be no-op
462ok ($x->{_a},3);
463ok ($x,'1230');
464
465# bround(n) should set _a
466$x->bround(2); # smaller works
467ok ($x,'1200');
468ok ($x->{_a},2);
469
470# bround(-n) is undocumented and only used by MBF
471# bround(-n) should set _a
472$x = $mbi->new(12345);
473$x->bround(-1);
474ok ($x,'12300');
475ok ($x->{_a},4);
476
477# bround(-n) should set _a
478$x = $mbi->new(12345);
479$x->bround(-2);
480ok ($x,'12000');
481ok ($x->{_a},3);
482
483# bround(-n) should set _a
484$x = $mbi->new(12345); $x->{_a} = 5;
485$x->bround(-3);
486ok ($x,'10000');
487ok ($x->{_a},2);
488
489# bround(-n) should set _a
490$x = $mbi->new(12345); $x->{_a} = 5;
491$x->bround(-4);
492ok ($x,'0');
493ok ($x->{_a},1);
494
495# bround(-n) should be noop if n too big
496$x = $mbi->new(12345);
497$x->bround(-5);
498ok ($x,'0'); # scale to "big" => 0
499ok ($x->{_a},0);
500
501# bround(-n) should be noop if n too big
502$x = $mbi->new(54321);
503$x->bround(-5);
504ok ($x,'100000'); # used by MBF to round 0.0054321 at 0.0_6_00000
505ok ($x->{_a},0);
506
507# bround(-n) should be noop if n too big
508$x = $mbi->new(54321); $x->{_a} = 5;
509$x->bround(-6);
510ok ($x,'100000'); # no-op
511ok ($x->{_a},0);
512
513# bround(n) should set _a
514$x = $mbi->new(12345); $x->{_a} = 5;
515$x->bround(5); # must be no-op
516ok ($x,'12345');
517ok ($x->{_a},5);
518
519# bround(n) should set _a
520$x = $mbi->new(12345); $x->{_a} = 5;
521$x->bround(6); # must be no-op
522ok ($x,'12345');
523
b3abae2a 524$x = $mbf->new('0.0061'); $x->bfround(-2); ok ($x,'0.01');
525$x = $mbf->new('0.004'); $x->bfround(-2); ok ($x,'0.00');
526$x = $mbf->new('0.005'); $x->bfround(-2); ok ($x,'0.00');
527
528$x = $mbf->new('12345'); $x->bfround(2); ok ($x,'12340');
529$x = $mbf->new('12340'); $x->bfround(2); ok ($x,'12340');
61f5c3f5 530
531# MBI::bfround should clear A for negative P
532$x = $mbi->new('1234'); $x->accuracy(3); $x->bfround(-2);
533ok_undef ($x->{_a});
534
535###############################################################################
536# rounding with already set precision/accuracy
537
538$x = $mbf->new(1); $x->{_p} = -5;
539ok ($x,'1.00000');
540
541# further rounding donw
542ok ($x->bfround(-2),'1.00');
543ok ($x->{_p},-2);
544
545$x = $mbf->new(12345); $x->{_a} = 5;
546ok ($x->bround(2),'12000');
547ok ($x->{_a},2);
548
549$x = $mbf->new('1.2345'); $x->{_a} = 5;
550ok ($x->bround(2),'1.2');
551ok ($x->{_a},2);
552
553# mantissa/exponent format and A/P
554$x = $mbf->new('12345.678'); $x->accuracy(4);
555ok ($x,'12350'); ok ($x->{_a},4); ok_undef ($x->{_p});
556ok_undef ($x->{_m}->{_a}); ok_undef ($x->{_e}->{_a});
557ok_undef ($x->{_m}->{_p}); ok_undef ($x->{_e}->{_p});
558
559# check for no A/P in case of fallback
560# result
561$x = $mbf->new(100) / 3;
562ok_undef ($x->{_a}); ok_undef ($x->{_p});
563
564# result & reminder
565$x = $mbf->new(100) / 3; ($x,$y) = $x->bdiv(3);
566ok_undef ($x->{_a}); ok_undef ($x->{_p});
567ok_undef ($y->{_a}); ok_undef ($y->{_p});
568
569###############################################################################
570# math with two numbers with differen A and P
571
572$x = $mbf->new(12345); $x->accuracy(4); # '12340'
573$y = $mbf->new(12345); $y->accuracy(2); # '12000'
574ok ($x+$y,24000); # 12340+12000=> 24340 => 24000
575
576$x = $mbf->new(54321); $x->accuracy(4); # '12340'
577$y = $mbf->new(12345); $y->accuracy(3); # '12000'
578ok ($x-$y,42000); # 54320+12300=> 42020 => 42000
579
580$x = $mbf->new('1.2345'); $x->precision(-2); # '1.23'
581$y = $mbf->new('1.2345'); $y->precision(-4); # '1.2345'
582ok ($x+$y,'2.46'); # 1.2345+1.2300=> 2.4645 => 2.46
583
584###############################################################################
585# round should find and use proper class
586
587#$x = Foo->new();
588#ok ($x->round($Foo::accuracy),'a' x $Foo::accuracy);
589#ok ($x->round(undef,$Foo::precision),'p' x $Foo::precision);
590#ok ($x->bfround($Foo::precision),'p' x $Foo::precision);
591#ok ($x->bround($Foo::accuracy),'a' x $Foo::accuracy);
592
593###############################################################################
594# find out whether _find_round_parameters is doing what's it's supposed to do
56d9de68 595
596{
597 no strict 'refs';
598 ${"$mbi\::accuracy"} = undef;
599 ${"$mbi\::precision"} = undef;
600 ${"$mbi\::div_scale"} = 40;
601 ${"$mbi\::round_mode"} = 'odd';
602}
603
61f5c3f5 604$x = $mbi->new(123);
605my @params = $x->_find_round_parameters();
606ok (scalar @params,1); # nothing to round
607
608@params = $x->_find_round_parameters(1);
609ok (scalar @params,4); # a=1
610ok ($params[0],$x); # self
611ok ($params[1],1); # a
612ok_undef ($params[2]); # p
613ok ($params[3],'odd'); # round_mode
614
615@params = $x->_find_round_parameters(undef,2);
616ok (scalar @params,4); # p=2
617ok ($params[0],$x); # self
618ok_undef ($params[1]); # a
619ok ($params[2],2); # p
620ok ($params[3],'odd'); # round_mode
621
622eval { @params = $x->_find_round_parameters(undef,2,'foo'); };
623ok ($@ =~ /^Unknown round mode 'foo'/,1);
624
625@params = $x->_find_round_parameters(undef,2,'+inf');
626ok (scalar @params,4); # p=2
627ok ($params[0],$x); # self
628ok_undef ($params[1]); # a
629ok ($params[2],2); # p
630ok ($params[3],'+inf'); # round_mode
631
632@params = $x->_find_round_parameters(2,-2,'+inf');
633ok (scalar @params,1); # error, A and P defined
634ok ($params[0],$x); # self
635
56d9de68 636{
637 no strict 'refs';
638 ${"$mbi\::accuracy"} = 1;
639 @params = $x->_find_round_parameters(undef,-2);
640 ok (scalar @params,1); # error, A and P defined
641 ok ($params[0],$x); # self
642
643 ${"$mbi\::accuracy"} = undef;
644 ${"$mbi\::precision"} = 1;
645 @params = $x->_find_round_parameters(1,undef);
646 ok (scalar @params,1); # error, A and P defined
647 ok ($params[0],$x); # self
648
649 ${"$mbi\::precision"} = undef; # reset
650}
61f5c3f5 651
652###############################################################################
653# test whether bone/bzero take additional A & P, or reset it etc
654
f9a08e12 655foreach my $c ($mbi,$mbf)
61f5c3f5 656 {
f9a08e12 657 $x = $c->new(2)->bzero(); ok_undef ($x->{_a}); ok_undef ($x->{_p});
658 $x = $c->new(2)->bone(); ok_undef ($x->{_a}); ok_undef ($x->{_p});
659 $x = $c->new(2)->binf(); ok_undef ($x->{_a}); ok_undef ($x->{_p});
660 $x = $c->new(2)->bnan(); ok_undef ($x->{_a}); ok_undef ($x->{_p});
61f5c3f5 661
f9a08e12 662 $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->bnan();
61f5c3f5 663 ok_undef ($x->{_a}); ok_undef ($x->{_p});
f9a08e12 664 $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->binf();
61f5c3f5 665 ok_undef ($x->{_a}); ok_undef ($x->{_p});
666
f9a08e12 667 $x = $c->new(2,1); ok ($x->{_a},1); ok_undef ($x->{_p});
668 $x = $c->new(2,undef,1); ok_undef ($x->{_a}); ok ($x->{_p},1);
669
670 $x = $c->new(2,1)->bzero(); ok ($x->{_a},1); ok_undef ($x->{_p});
671 $x = $c->new(2,undef,1)->bzero(); ok_undef ($x->{_a}); ok ($x->{_p},1);
672
673 $x = $c->new(2,1)->bone(); ok ($x->{_a},1); ok_undef ($x->{_p});
674 $x = $c->new(2,undef,1)->bone(); ok_undef ($x->{_a}); ok ($x->{_p},1);
675
676 $x = $c->new(2); $x->bone('+',2,undef); ok ($x->{_a},2); ok_undef ($x->{_p});
677 $x = $c->new(2); $x->bone('+',undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2);
678 $x = $c->new(2); $x->bone('-',2,undef); ok ($x->{_a},2); ok_undef ($x->{_p});
679 $x = $c->new(2); $x->bone('-',undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2);
61f5c3f5 680
f9a08e12 681 $x = $c->new(2); $x->bzero(2,undef); ok ($x->{_a},2); ok_undef ($x->{_p});
682 $x = $c->new(2); $x->bzero(undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2);
683 }
684
685###############################################################################
686# test whether bone/bzero honour globals
61f5c3f5 687
f9a08e12 688for my $c ($mbi,$mbf)
689 {
690 $c->accuracy(2);
691 $x = $c->bone(); ok ($x->accuracy(),2);
692 $x = $c->bzero(); ok ($x->accuracy(),2);
693 $c->accuracy(undef);
694
695 $c->precision(-2);
696 $x = $c->bone(); ok ($x->precision(),-2);
697 $x = $c->bzero(); ok ($x->precision(),-2);
698 $c->precision(undef);
61f5c3f5 699 }
700
701###############################################################################
702# check whether mixing A and P creates a NaN
703
704# new with set accuracy/precision and with parameters
56d9de68 705{
706 no strict 'refs';
707 foreach my $c ($mbi,$mbf)
708 {
709 ok ($c->new(123,4,-3),'NaN'); # with parameters
710 ${"$c\::accuracy"} = 42;
711 ${"$c\::precision"} = 2;
712 ok ($c->new(123),'NaN'); # with globals
713 ${"$c\::accuracy"} = undef;
714 ${"$c\::precision"} = undef;
715 }
716}
61f5c3f5 717
718# binary ops
719foreach my $class ($mbi,$mbf)
720 {
721 foreach (qw/add sub mul pow mod/)
722 #foreach (qw/add sub mul div pow mod/)
723 {
724 my $try = "my \$x = $class->new(1234); \$x->accuracy(5); ";
725 $try .= "my \$y = $class->new(12); \$y->precision(-3); ";
726 $try .= "\$x->b$_(\$y);";
727 $rc = eval $try;
728 print "# Tried: '$try'\n" if !ok ($rc, 'NaN');
729 }
730 }
731
732# unary ops
733foreach (qw/new bsqrt/)
734 {
735 my $try = 'my $x = $mbi->$_(1234,5,-3); ';
736 $rc = eval $try;
737 print "# Tried: '$try'\n" if !ok ($rc, 'NaN');
738 }
739
28df3e88 740# see if $x->bsub(0) and $x->badd(0) really round
741foreach my $class ($mbi,$mbf)
742 {
743 $x = $class->new(123); $class->accuracy(2); $x->bsub(0);
744 ok ($x,120);
745 $class->accuracy(undef);
746 $x = $class->new(123); $class->accuracy(2); $x->badd(0);
747 ok ($x,120);
748 $class->accuracy(undef);
749 }
b3abae2a 750
61f5c3f5 751###############################################################################
752# test whether shortcuts returning zero/one preserve A and P
753
754my ($ans1,$f,$a,$p,$xp,$yp,$xa,$ya,$try,$ans,@args);
b3abae2a 755my $CALC = Math::BigInt->config()->{lib};
61f5c3f5 756while (<DATA>)
757 {
d614cd8b 758 chomp;
61f5c3f5 759 next if /^\s*(#|$)/; # skip comments and empty lines
760 if (s/^&//)
761 {
762 $f = $_; next; # function
763 }
764 @args = split(/:/,$_,99);
765 my $ans = pop(@args);
766
767 ($x,$xa,$xp) = split (/,/,$args[0]);
768 $xa = $xa || ''; $xp = $xp || '';
769 $try = "\$x = $mbi->new('$x'); ";
770 $try .= "\$x->accuracy($xa); " if $xa ne '';
771 $try .= "\$x->precision($xp); " if $xp ne '';
772
773 ($y,$ya,$yp) = split (/,/,$args[1]);
774 $ya = $ya || ''; $yp = $yp || '';
775 $try .= "\$y = $mbi->new('$y'); ";
776 $try .= "\$y->accuracy($ya); " if $ya ne '';
777 $try .= "\$y->precision($yp); " if $yp ne '';
778
779 $try .= "\$x->$f(\$y);";
780
781 # print "trying $try\n";
782 $rc = eval $try;
783 # convert hex/binary targets to decimal
784 if ($ans =~ /^(0x0x|0b0b)/)
785 {
786 $ans =~ s/^0[xb]//;
787 $ans = $mbi->new($ans)->bstr();
788 }
789 print "# Tried: '$try'\n" if !ok ($rc, $ans);
790 # check internal state of number objects
791 is_valid($rc,$f) if ref $rc;
792
793 # now check whether A and P are set correctly
794 # only one of $a or $p will be set (no crossing here)
795 $a = $xa || $ya; $p = $xp || $yp;
796
797 # print "Check a=$a p=$p\n";
b3abae2a 798 # print "# Tried: '$try'\n";
f9a08e12 799 if ($a ne '')
800 {
801 if (!(ok ($x->{_a}, $a) && ok_undef ($x->{_p})))
802 {
803 print "# Check: A=$a and P=undef\n";
804 print "# Tried: '$try'\n";
805 }
806 }
807 if ($p ne '')
808 {
809 if (!(ok ($x->{_p}, $p) && ok_undef ($x->{_a})))
810 {
811 print "# Check: A=undef and P=$p\n";
812 print "# Tried: '$try'\n";
813 }
814 }
61f5c3f5 815 }
816
817# all done
8181;
819
820###############################################################################
821###############################################################################
822# Perl 5.005 does not like ok ($x,undef)
823
824sub ok_undef
825 {
826 my $x = shift;
827
f9a08e12 828 ok (1,1) and return 1 if !defined $x;
61f5c3f5 829 ok ($x,'undef');
830 print "# Called from ",join(' ',caller()),"\n";
f9a08e12 831 return 0;
61f5c3f5 832 }
833
834###############################################################################
835# sub to check validity of a BigInt internally, to ensure that no op leaves a
836# number object in an invalid state (f.i. "-0")
837
838sub is_valid
839 {
840 my ($x,$f) = @_;
841
842 my $e = 0; # error?
843 # ok as reference?
844 $e = 'Not a reference' if !ref($x);
845
846 # has ok sign?
847 $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'"
848 if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/;
849
850 $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0;
851 $e = $CALC->_check($x->{value}) if $e eq '0';
852
853 # test done, see if error did crop up
854 ok (1,1), return if ($e eq '0');
855
856 ok (1,$e." after op '$f'");
857 }
858
859# format is:
860# x,A,P:x,A,P:result
861# 123,,3 means 123 with precision 3 (A is undef)
862# the A or P of the result is calculated automatically
863__DATA__
864&badd
61f5c3f5 865123,,:123,,:246
866123,3,:0,,:123
867123,,-3:0,,:123
868123,,:0,3,:123
869123,,:0,,-3:123
870&bmul
871123,,:1,,:123
872123,3,:0,,:0
873123,,-3:0,,:0
874123,,:0,3,:0
875123,,:0,,-3:0
876123,3,:1,,:123
877123,,-3:1,,:123
878123,,:1,3,:123
879123,,:1,,-3:123
8801,3,:123,,:123
8811,,-3:123,,:123
8821,,:123,3,:123
8831,,:123,,-3:123
884&bdiv
885123,,:1,,:123
886123,4,:1,,:123
887123,,:1,4,:123
888123,,:1,,-4:123
889123,,-4:1,,:123
8901,4,:123,,:0
8911,,:123,4,:0
8921,,:123,,-4:0
8931,,-4:123,,:0
f9a08e12 894&band
8951,,:3,,:1
8961234,1,:0,,:0
8971234,,:0,1,:0
8981234,,-1:0,,:0
8991234,,:0,,-1:0
9000xFF,,:0x10,,:0x0x10
9010xFF,2,:0xFF,,:250
9020xFF,,:0xFF,2,:250
9030xFF,,1:0xFF,,:250
9040xFF,,:0xFF,,1:250
905&bxor
9061,,:3,,:2
9071234,1,:0,,:1000
9081234,,:0,1,:1000
9091234,,3:0,,:1000
9101234,,:0,,3:1000
9110xFF,,:0x10,,:239
912# 250 ^ 255 => 5
9130xFF,2,:0xFF,,:5
9140xFF,,:0xFF,2,:5
9150xFF,,1:0xFF,,:5
9160xFF,,:0xFF,,1:5
917# 250 ^ 4095 = 3845 => 3800
9180xFF,2,:0xFFF,,:3800
919# 255 ^ 4100 = 4347 => 4300
9200xFF,,:0xFFF,2,:4300
9210xFF,,2:0xFFF,,:3800
922# 255 ^ 4100 = 10fb => 4347 => 4300
9230xFF,,:0xFFF,,2:4300
924&bior
9251,,:3,,:3
9261234,1,:0,,:1000
9271234,,:0,1,:1000
9281234,,3:0,,:1000
9291234,,:0,,3:1000
9300xFF,,:0x10,,:0x0xFF
931# FF | FA = FF => 250
932250,2,:0xFF,,:250
9330xFF,,:250,2,:250
9340xFF,,1:0xFF,,:250
9350xFF,,:0xFF,,1:250
936&bpow
9372,,:3,,:8
9382,,:0,,:1
9392,2,:0,,:1
9402,,:0,2,:1