1 # test rounding, accuracy, precicion and fallback, round_mode and mixing
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.
9 ###############################################################################
10 # test defaults and set/get
12 ok_undef (${"$mbi\::accuracy"});
13 ok_undef (${"$mbi\::precision"});
14 ok_undef ($mbi->accuracy());
15 ok_undef ($mbi->precision());
16 ok (${"$mbi\::div_scale"},40);
17 ok (${"$mbi\::round_mode"},'even');
18 ok ($mbi->round_mode(),'even');
20 ok_undef (${"$mbf\::accuracy"});
21 ok_undef (${"$mbf\::precision"});
22 ok_undef ($mbf->precision());
23 ok_undef ($mbf->precision());
24 ok (${"$mbf\::div_scale"},40);
25 ok (${"$mbf\::round_mode"},'even');
26 ok ($mbf->round_mode(),'even');
29 foreach my $class ($mbi,$mbf)
31 ok_undef ($class->accuracy());
32 ok_undef ($class->precision());
33 ok ($class->round_mode(),'even');
34 ok ($class->div_scale(),40);
36 ok ($class->div_scale(20),20);
37 $class->div_scale(40); ok ($class->div_scale(),40);
39 ok ($class->round_mode('odd'),'odd');
40 $class->round_mode('even'); ok ($class->round_mode(),'even');
42 ok ($class->accuracy(2),2);
43 $class->accuracy(3); ok ($class->accuracy(),3);
44 ok_undef ($class->accuracy(undef));
46 ok ($class->precision(2),2);
47 ok ($class->precision(-2),-2);
48 $class->precision(3); ok ($class->precision(),3);
49 ok_undef ($class->precision(undef));
53 foreach (qw/5 42 -1 0/)
55 ok (${"$mbf\::accuracy"} = $_,$_);
56 ok (${"$mbi\::accuracy"} = $_,$_);
58 ok_undef (${"$mbf\::accuracy"} = undef);
59 ok_undef (${"$mbi\::accuracy"} = undef);
62 foreach (qw/5 42 -1 0/)
64 ok (${"$mbf\::precision"} = $_,$_);
65 ok (${"$mbi\::precision"} = $_,$_);
67 ok_undef (${"$mbf\::precision"} = undef);
68 ok_undef (${"$mbi\::precision"} = undef);
73 ok (${"$mbf\::div_scale"} = $_,$_);
74 ok (${"$mbi\::div_scale"} = $_,$_);
76 # illegal values are possible for fallback due to no accessor
79 foreach (qw/odd even zero trunc +inf -inf/)
81 ok (${"$mbf\::round_mode"} = $_,$_);
82 ok (${"$mbi\::round_mode"} = $_,$_);
84 ${"$mbf\::round_mode"} = 'zero';
85 ok (${"$mbf\::round_mode"},'zero');
86 ok (${"$mbi\::round_mode"},'-inf'); # from above
88 ${"$mbi\::accuracy"} = undef;
89 ${"$mbi\::precision"} = undef;
91 $x = $mbf->new('123.456');
92 ok_undef ($x->accuracy());
93 ok ($x->accuracy(5),5);
94 ok_undef ($x->accuracy(undef),undef);
95 ok_undef ($x->precision());
96 ok ($x->precision(5),5);
97 ok_undef ($x->precision(undef),undef);
99 # see if MBF changes MBIs values
100 ok (${"$mbi\::accuracy"} = 42,42);
101 ok (${"$mbf\::accuracy"} = 64,64);
102 ok (${"$mbi\::accuracy"},42); # should be still 42
103 ok (${"$mbf\::accuracy"},64); # should be now 64
105 ###############################################################################
106 # see if creating a number under set A or P will round it
108 ${"$mbi\::accuracy"} = 4;
109 ${"$mbi\::precision"} = undef;
111 ok ($mbi->new(123456),123500); # with A
112 ${"$mbi\::accuracy"} = undef;
113 ${"$mbi\::precision"} = 3;
114 ok ($mbi->new(123456),123000); # with P
116 ${"$mbf\::accuracy"} = 4;
117 ${"$mbf\::precision"} = undef;
118 ${"$mbi\::precision"} = undef;
120 ok ($mbf->new('123.456'),'123.5'); # with A
121 ${"$mbf\::accuracy"} = undef;
122 ${"$mbf\::precision"} = -1;
123 ok ($mbf->new('123.456'),'123.5'); # with P from MBF, not MBI!
125 ${"$mbf\::precision"} = undef; # reset
127 ###############################################################################
128 # see if MBI leaves MBF's private parts alone
130 ${"$mbi\::precision"} = undef; ${"$mbf\::precision"} = undef;
131 ${"$mbi\::accuracy"} = 4; ${"$mbf\::accuracy"} = undef;
132 ok ($mbf->new('123.456'),'123.456');
133 ${"$mbi\::accuracy"} = undef; # reset
135 ###############################################################################
136 # see if setting accuracy/precision actually rounds the number
138 $x = $mbf->new('123.456'); $x->accuracy(4); ok ($x,'123.5');
139 $x = $mbf->new('123.456'); $x->precision(-2); ok ($x,'123.46');
141 $x = $mbi->new(123456); $x->accuracy(4); ok ($x,123500);
142 $x = $mbi->new(123456); $x->precision(2); ok ($x,123500);
144 ###############################################################################
145 # test actual rounding via round()
147 $x = $mbf->new('123.456');
148 ok ($x->copy()->round(5),'123.46');
149 ok ($x->copy()->round(4),'123.5');
150 ok ($x->copy()->round(5,2),'NaN');
151 ok ($x->copy()->round(undef,-2),'123.46');
152 ok ($x->copy()->round(undef,2),120);
154 $x = $mbi->new('123');
155 ok ($x->round(5,2),'NaN');
157 $x = $mbf->new('123.45000');
158 ok ($x->copy()->round(undef,-1,'odd'),'123.5');
160 # see if rounding is 'sticky'
161 $x = $mbf->new('123.4567');
162 $y = $x->copy()->bround(); # no-op since nowhere A or P defined
165 $y = $x->copy()->round(5);
166 ok ($y->accuracy(),5);
167 ok_undef ($y->precision()); # A has precedence, so P still unset
168 $y = $x->copy()->round(undef,2);
169 ok ($y->precision(),2);
170 ok_undef ($y->accuracy()); # P has precedence, so A still unset
172 # see if setting A clears P and vice versa
173 $x = $mbf->new('123.4567');
175 ok ($x->accuracy(4),4);
176 ok ($x->precision(-2),-2); # clear A
177 ok_undef ($x->accuracy());
179 $x = $mbf->new('123.4567');
181 ok ($x->precision(-2),-2);
182 ok ($x->accuracy(4),4); # clear P
183 ok_undef ($x->precision());
186 $x = $mbf->new(123.456); $x->accuracy(4); $x->precision(2);
187 $z = $x->copy(); ok_undef ($z->accuracy(),undef); ok ($z->precision(),2);
189 # does accuracy()/precision work on zeros?
190 foreach my $class ($mbi,$mbf)
192 $x = $class->bzero(); $x->accuracy(5); ok ($x->{_a},5);
193 $x = $class->bzero(); $x->precision(5); ok ($x->{_p},5);
194 $x = $class->new(0); $x->accuracy(5); ok ($x->{_a},5);
195 $x = $class->new(0); $x->precision(5); ok ($x->{_p},5);
197 $x = $class->bzero(); $x->round(5); ok ($x->{_a},5);
198 $x = $class->bzero(); $x->round(undef,5); ok ($x->{_p},5);
199 $x = $class->new(0); $x->round(5); ok ($x->{_a},5);
200 $x = $class->new(0); $x->round(undef,5); ok ($x->{_p},5);
202 # see if trying to increasing A in bzero() doesn't do something
203 $x = $class->bzero(); $x->{_a} = 3; $x->round(5); ok ($x->{_a},3);
206 ###############################################################################
207 # test wether operations round properly afterwards
208 # These tests are not complete, since they do not excercise every "return"
209 # statement in the op's. But heh, it's better than nothing...
211 $x = $mbf->new('123.456');
212 $y = $mbf->new('654.321');
213 $x->{_a} = 5; # $x->accuracy(5) would round $x straightaway
214 $y->{_a} = 4; # $y->accuracy(4) would round $x straightaway
216 $z = $x + $y; ok ($z,'777.8');
217 $z = $y - $x; ok ($z,'530.9');
218 $z = $y * $x; ok ($z,'80780');
219 $z = $x ** 2; ok ($z,'15241');
220 $z = $x * $x; ok ($z,'15241');
222 # not: $z = -$x; ok ($z,'-123.46'); ok ($x,'123.456');
223 $z = $x->copy(); $z->{_a} = 2; $z = $z / 2; ok ($z,62);
224 $x = $mbf->new(123456); $x->{_a} = 4;
225 $z = $x->copy; $z++; ok ($z,123500);
227 $x = $mbi->new(123456);
228 $y = $mbi->new(654321);
229 $x->{_a} = 5; # $x->accuracy(5) would round $x straightaway
230 $y->{_a} = 4; # $y->accuracy(4) would round $x straightaway
232 $z = $x + $y; ok ($z,777800);
233 $z = $y - $x; ok ($z,530900);
234 $z = $y * $x; ok ($z,80780000000);
235 $z = $x ** 2; ok ($z,15241000000);
236 # not yet: $z = -$x; ok ($z,-123460); ok ($x,123456);
237 $z = $x->copy; $z++; ok ($z,123460);
238 $z = $x->copy(); $z->{_a} = 2; $z = $z / 2; ok ($z,62000);
240 $x = $mbi->new(123400); $x->{_a} = 4;
241 ok ($x->bnot(),-123400); # not -1234001
243 # both babs() and bneg() don't need to round, since the input will already
244 # be rounded (either as $x or via new($string)), and they don't change the
245 # value. The two tests below peek at this by using _a (illegally) directly
246 $x = $mbi->new(-123401); $x->{_a} = 4; ok ($x->babs(),123401);
247 $x = $mbi->new(-123401); $x->{_a} = 4; ok ($x->bneg(),123401);
249 # test fdiv rounding to A and R (bug in v1.48 and maybe earlier versions)
250 $mbf->round_mode('even');
251 $x = $mbf->new('740.7')->fdiv('6',4,undef,'zero'); ok ($x,'123.4');
253 ###############################################################################
254 # test (also under Bare) that bfac() rounds at last step
256 ok ($mbi->new(12)->bfac(),'479001600');
257 ok ($mbi->new(12)->bfac(2),'480000000');
258 $x = $mbi->new(12); $x->accuracy(2); ok ($x->bfac(),'480000000');
259 $x = $mbi->new(13); $x->accuracy(2); ok ($x->bfac(),'6200000000');
260 $x = $mbi->new(13); $x->accuracy(3); ok ($x->bfac(),'6230000000');
261 $x = $mbi->new(13); $x->accuracy(4); ok ($x->bfac(),'6227000000');
262 # this does 1,2,3...9,10,11,12...20
263 $x = $mbi->new(20); $x->accuracy(1); ok ($x->bfac(),'2000000000000000000');
265 ###############################################################################
266 # test bsqrt) rounding to given A/P/R (bug prior to v1.60)
267 $x = $mbi->new('123456')->bsqrt(2,undef); ok ($x,'350'); # not 351
268 $x = $mbi->new('3')->bsqrt(2,undef); ok ($x->accuracy(),2);
270 $mbi->round_mode('even'); $x = $mbi->new('126025')->bsqrt(2,undef,'+inf');
271 ok ($x,'360'); # not 355 nor 350
273 $x = $mbi->new('126025')->bsqrt(undef,2); ok ($x,'400'); # not 355
276 ###############################################################################
277 # test mixed arguments
283 $z = $x + $y; ok ($z,12); ok (ref($z),$mbf);
284 $z = $x / $y; ok ($z,5); ok (ref($z),$mbf);
285 $z = $u * $y; ok ($z,5); ok (ref($z),$mbf);
287 $y = $mbi->new(12345);
288 $z = $u->copy()->bmul($y,2,undef,'odd'); ok ($z,31000);
289 $z = $u->copy()->bmul($y,3,undef,'odd'); ok ($z,30900);
290 $z = $u->copy()->bmul($y,undef,0,'odd'); ok ($z,30863);
291 $z = $u->copy()->bmul($y,undef,1,'odd'); ok ($z,30863);
292 $z = $u->copy()->bmul($y,undef,2,'odd'); ok ($z,30860);
293 $z = $u->copy()->bmul($y,undef,3,'odd'); ok ($z,30900);
294 $z = $u->copy()->bmul($y,undef,-1,'odd'); ok ($z,30862.5);
297 # $z = $y->copy()->bmul($u,2,0,'odd'); ok ($z,31000);
298 # $z = $y * $u; ok ($z,5); ok (ref($z),$mbi);
299 # $z = $y + $x; ok ($z,12); ok (ref($z),$mbi);
300 # $z = $y / $x; ok ($z,0); ok (ref($z),$mbi);
302 ###############################################################################
303 # rounding in bdiv with fallback and already set A or P
305 ${"$mbf\::accuracy"} = undef;
306 ${"$mbf\::precision"} = undef;
307 ${"$mbf\::div_scale"} = 40;
309 $x = $mbf->new(10); $x->{_a} = 4;
310 ok ($x->bdiv(3),'3.333');
311 ok ($x->{_a},4); # set's it since no fallback
313 $x = $mbf->new(10); $x->{_a} = 4; $y = $mbf->new(3);
314 ok ($x->bdiv($y),'3.333');
315 ok ($x->{_a},4); # set's it since no fallback
318 $x = $mbf->new(10); $x->{_p} = -2;
319 ok ($x->bdiv(3),'3.33');
321 # round in div with requested P
323 ok ($x->bdiv(3,undef,-2),'3.33');
325 # round in div with requested P greater than fallback
326 ${"$mbf\::div_scale"} = 5;
328 ok ($x->bdiv(3,undef,-8),'3.33333333');
329 ${"$mbf\::div_scale"} = 40;
331 $x = $mbf->new(10); $y = $mbf->new(3); $y->{_a} = 4;
332 ok ($x->bdiv($y),'3.333');
333 ok ($x->{_a},4); ok ($y->{_a},4); # set's it since no fallback
334 ok_undef ($x->{_p}); ok_undef ($y->{_p});
337 $x = $mbf->new(10); $y = $mbf->new(3); $y->{_p} = -2;
338 ok ($x->bdiv($y),'3.33');
341 ok_undef ($x->{_a}); ok_undef ($y->{_a});
343 ###############################################################################
344 # test whether bround(-n) fails in MBF (undocumented in MBI)
345 eval { $x = $mbf->new(1); $x->bround(-2); };
346 ok ($@ =~ /^bround\(\) needs positive accuracy/,1);
348 # test whether rounding to higher accuracy is no-op
349 $x = $mbf->new(1); $x->{_a} = 4;
351 $x->bround(6); # must be no-op
355 $x = $mbi->new(1230); $x->{_a} = 3;
357 $x->bround(6); # must be no-op
361 # bround(n) should set _a
362 $x->bround(2); # smaller works
366 # bround(-n) is undocumented and only used by MBF
367 # bround(-n) should set _a
368 $x = $mbi->new(12345);
373 # bround(-n) should set _a
374 $x = $mbi->new(12345);
379 # bround(-n) should set _a
380 $x = $mbi->new(12345); $x->{_a} = 5;
385 # bround(-n) should set _a
386 $x = $mbi->new(12345); $x->{_a} = 5;
391 # bround(-n) should be noop if n too big
392 $x = $mbi->new(12345);
394 ok ($x,'0'); # scale to "big" => 0
397 # bround(-n) should be noop if n too big
398 $x = $mbi->new(54321);
400 ok ($x,'100000'); # used by MBF to round 0.0054321 at 0.0_6_00000
403 # bround(-n) should be noop if n too big
404 $x = $mbi->new(54321); $x->{_a} = 5;
406 ok ($x,'100000'); # no-op
409 # bround(n) should set _a
410 $x = $mbi->new(12345); $x->{_a} = 5;
411 $x->bround(5); # must be no-op
415 # bround(n) should set _a
416 $x = $mbi->new(12345); $x->{_a} = 5;
417 $x->bround(6); # must be no-op
420 $x = $mbf->new('0.0061'); $x->bfround(-2); ok ($x,'0.01');
421 $x = $mbf->new('0.004'); $x->bfround(-2); ok ($x,'0.00');
422 $x = $mbf->new('0.005'); $x->bfround(-2); ok ($x,'0.00');
424 $x = $mbf->new('12345'); $x->bfround(2); ok ($x,'12340');
425 $x = $mbf->new('12340'); $x->bfround(2); ok ($x,'12340');
427 # MBI::bfround should clear A for negative P
428 $x = $mbi->new('1234'); $x->accuracy(3); $x->bfround(-2);
431 ###############################################################################
432 # rounding with already set precision/accuracy
434 $x = $mbf->new(1); $x->{_p} = -5;
437 # further rounding donw
438 ok ($x->bfround(-2),'1.00');
441 $x = $mbf->new(12345); $x->{_a} = 5;
442 ok ($x->bround(2),'12000');
445 $x = $mbf->new('1.2345'); $x->{_a} = 5;
446 ok ($x->bround(2),'1.2');
449 # mantissa/exponent format and A/P
450 $x = $mbf->new('12345.678'); $x->accuracy(4);
451 ok ($x,'12350'); ok ($x->{_a},4); ok_undef ($x->{_p});
452 ok_undef ($x->{_m}->{_a}); ok_undef ($x->{_e}->{_a});
453 ok_undef ($x->{_m}->{_p}); ok_undef ($x->{_e}->{_p});
455 # check for no A/P in case of fallback
457 $x = $mbf->new(100) / 3;
458 ok_undef ($x->{_a}); ok_undef ($x->{_p});
461 $x = $mbf->new(100) / 3; ($x,$y) = $x->bdiv(3);
462 ok_undef ($x->{_a}); ok_undef ($x->{_p});
463 ok_undef ($y->{_a}); ok_undef ($y->{_p});
465 ###############################################################################
466 # math with two numbers with differen A and P
468 $x = $mbf->new(12345); $x->accuracy(4); # '12340'
469 $y = $mbf->new(12345); $y->accuracy(2); # '12000'
470 ok ($x+$y,24000); # 12340+12000=> 24340 => 24000
472 $x = $mbf->new(54321); $x->accuracy(4); # '12340'
473 $y = $mbf->new(12345); $y->accuracy(3); # '12000'
474 ok ($x-$y,42000); # 54320+12300=> 42020 => 42000
476 $x = $mbf->new('1.2345'); $x->precision(-2); # '1.23'
477 $y = $mbf->new('1.2345'); $y->precision(-4); # '1.2345'
478 ok ($x+$y,'2.46'); # 1.2345+1.2300=> 2.4645 => 2.46
480 ###############################################################################
481 # round should find and use proper class
484 #ok ($x->round($Foo::accuracy),'a' x $Foo::accuracy);
485 #ok ($x->round(undef,$Foo::precision),'p' x $Foo::precision);
486 #ok ($x->bfround($Foo::precision),'p' x $Foo::precision);
487 #ok ($x->bround($Foo::accuracy),'a' x $Foo::accuracy);
489 ###############################################################################
490 # find out whether _find_round_parameters is doing what's it's supposed to do
492 ${"$mbi\::accuracy"} = undef;
493 ${"$mbi\::precision"} = undef;
494 ${"$mbi\::div_scale"} = 40;
495 ${"$mbi\::round_mode"} = 'odd';
498 my @params = $x->_find_round_parameters();
499 ok (scalar @params,1); # nothing to round
501 @params = $x->_find_round_parameters(1);
502 ok (scalar @params,4); # a=1
503 ok ($params[0],$x); # self
504 ok ($params[1],1); # a
505 ok_undef ($params[2]); # p
506 ok ($params[3],'odd'); # round_mode
508 @params = $x->_find_round_parameters(undef,2);
509 ok (scalar @params,4); # p=2
510 ok ($params[0],$x); # self
511 ok_undef ($params[1]); # a
512 ok ($params[2],2); # p
513 ok ($params[3],'odd'); # round_mode
515 eval { @params = $x->_find_round_parameters(undef,2,'foo'); };
516 ok ($@ =~ /^Unknown round mode 'foo'/,1);
518 @params = $x->_find_round_parameters(undef,2,'+inf');
519 ok (scalar @params,4); # p=2
520 ok ($params[0],$x); # self
521 ok_undef ($params[1]); # a
522 ok ($params[2],2); # p
523 ok ($params[3],'+inf'); # round_mode
525 @params = $x->_find_round_parameters(2,-2,'+inf');
526 ok (scalar @params,1); # error, A and P defined
527 ok ($params[0],$x); # self
529 ${"$mbi\::accuracy"} = 1;
530 @params = $x->_find_round_parameters(undef,-2);
531 ok (scalar @params,1); # error, A and P defined
532 ok ($params[0],$x); # self
534 ${"$mbi\::accuracy"} = undef;
535 ${"$mbi\::precision"} = 1;
536 @params = $x->_find_round_parameters(1,undef);
537 ok (scalar @params,1); # error, A and P defined
538 ok ($params[0],$x); # self
540 ${"$mbi\::precision"} = undef; # reset
542 ###############################################################################
543 # test whether bone/bzero take additional A & P, or reset it etc
545 foreach my $c ($mbi,$mbf)
547 $x = $c->new(2)->bzero(); ok_undef ($x->{_a}); ok_undef ($x->{_p});
548 $x = $c->new(2)->bone(); ok_undef ($x->{_a}); ok_undef ($x->{_p});
549 $x = $c->new(2)->binf(); ok_undef ($x->{_a}); ok_undef ($x->{_p});
550 $x = $c->new(2)->bnan(); ok_undef ($x->{_a}); ok_undef ($x->{_p});
552 $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->bnan();
553 ok_undef ($x->{_a}); ok_undef ($x->{_p});
554 $x = $c->new(2); $x->{_a} = 1; $x->{_p} = 2; $x->binf();
555 ok_undef ($x->{_a}); ok_undef ($x->{_p});
557 $x = $c->new(2,1); ok ($x->{_a},1); ok_undef ($x->{_p});
558 $x = $c->new(2,undef,1); ok_undef ($x->{_a}); ok ($x->{_p},1);
560 $x = $c->new(2,1)->bzero(); ok ($x->{_a},1); ok_undef ($x->{_p});
561 $x = $c->new(2,undef,1)->bzero(); ok_undef ($x->{_a}); ok ($x->{_p},1);
563 $x = $c->new(2,1)->bone(); ok ($x->{_a},1); ok_undef ($x->{_p});
564 $x = $c->new(2,undef,1)->bone(); ok_undef ($x->{_a}); ok ($x->{_p},1);
566 $x = $c->new(2); $x->bone('+',2,undef); ok ($x->{_a},2); ok_undef ($x->{_p});
567 $x = $c->new(2); $x->bone('+',undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2);
568 $x = $c->new(2); $x->bone('-',2,undef); ok ($x->{_a},2); ok_undef ($x->{_p});
569 $x = $c->new(2); $x->bone('-',undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2);
571 $x = $c->new(2); $x->bzero(2,undef); ok ($x->{_a},2); ok_undef ($x->{_p});
572 $x = $c->new(2); $x->bzero(undef,2); ok_undef ($x->{_a}); ok ($x->{_p},2);
575 ###############################################################################
576 # test whether bone/bzero honour globals
578 for my $c ($mbi,$mbf)
581 $x = $c->bone(); ok ($x->accuracy(),2);
582 $x = $c->bzero(); ok ($x->accuracy(),2);
586 $x = $c->bone(); ok ($x->precision(),-2);
587 $x = $c->bzero(); ok ($x->precision(),-2);
588 $c->precision(undef);
591 ###############################################################################
592 # check whether mixing A and P creates a NaN
594 # new with set accuracy/precision and with parameters
596 foreach my $c ($mbi,$mbf)
598 ok ($c->new(123,4,-3),'NaN'); # with parameters
599 ${"$c\::accuracy"} = 42;
600 ${"$c\::precision"} = 2;
601 ok ($c->new(123),'NaN'); # with globals
602 ${"$c\::accuracy"} = undef;
603 ${"$c\::precision"} = undef;
607 foreach my $class ($mbi,$mbf)
609 foreach (qw/add sub mul pow mod/)
610 #foreach (qw/add sub mul div pow mod/)
612 my $try = "my \$x = $class->new(1234); \$x->accuracy(5); ";
613 $try .= "my \$y = $class->new(12); \$y->precision(-3); ";
614 $try .= "\$x->b$_(\$y);";
616 print "# Tried: '$try'\n" if !ok ($rc, 'NaN');
621 foreach (qw/new bsqrt/)
623 my $try = 'my $x = $mbi->$_(1234,5,-3); ';
625 print "# Tried: '$try'\n" if !ok ($rc, 'NaN');
628 # see if $x->bsub(0) and $x->badd(0) really round
629 foreach my $class ($mbi,$mbf)
631 $x = $class->new(123); $class->accuracy(2); $x->bsub(0);
633 $class->accuracy(undef);
634 $x = $class->new(123); $class->accuracy(2); $x->badd(0);
636 $class->accuracy(undef);
639 ###############################################################################
640 # test whether shortcuts returning zero/one preserve A and P
642 my ($ans1,$f,$a,$p,$xp,$yp,$xa,$ya,$try,$ans,@args);
643 my $CALC = Math::BigInt->config()->{lib};
647 next if /^\s*(#|$)/; # skip comments and empty lines
650 $f = $_; next; # function
652 @args = split(/:/,$_,99);
653 my $ans = pop(@args);
655 ($x,$xa,$xp) = split (/,/,$args[0]);
656 $xa = $xa || ''; $xp = $xp || '';
657 $try = "\$x = $mbi->new('$x'); ";
658 $try .= "\$x->accuracy($xa); " if $xa ne '';
659 $try .= "\$x->precision($xp); " if $xp ne '';
661 ($y,$ya,$yp) = split (/,/,$args[1]);
662 $ya = $ya || ''; $yp = $yp || '';
663 $try .= "\$y = $mbi->new('$y'); ";
664 $try .= "\$y->accuracy($ya); " if $ya ne '';
665 $try .= "\$y->precision($yp); " if $yp ne '';
667 $try .= "\$x->$f(\$y);";
669 # print "trying $try\n";
671 # convert hex/binary targets to decimal
672 if ($ans =~ /^(0x0x|0b0b)/)
675 $ans = $mbi->new($ans)->bstr();
677 print "# Tried: '$try'\n" if !ok ($rc, $ans);
678 # check internal state of number objects
679 is_valid($rc,$f) if ref $rc;
681 # now check whether A and P are set correctly
682 # only one of $a or $p will be set (no crossing here)
683 $a = $xa || $ya; $p = $xp || $yp;
685 # print "Check a=$a p=$p\n";
686 # print "# Tried: '$try'\n";
689 if (!(ok ($x->{_a}, $a) && ok_undef ($x->{_p})))
691 print "# Check: A=$a and P=undef\n";
692 print "# Tried: '$try'\n";
697 if (!(ok ($x->{_p}, $p) && ok_undef ($x->{_a})))
699 print "# Check: A=undef and P=$p\n";
700 print "# Tried: '$try'\n";
708 ###############################################################################
709 ###############################################################################
710 # Perl 5.005 does not like ok ($x,undef)
716 ok (1,1) and return 1 if !defined $x;
718 print "# Called from ",join(' ',caller()),"\n";
722 ###############################################################################
723 # sub to check validity of a BigInt internally, to ensure that no op leaves a
724 # number object in an invalid state (f.i. "-0")
732 $e = 'Not a reference' if !ref($x);
735 $e = "Illegal sign $x->{sign} (expected: '+', '-', '-inf', '+inf' or 'NaN'"
736 if $e eq '0' && $x->{sign} !~ /^(\+|-|\+inf|-inf|NaN)$/;
738 $e = "-0 is invalid!" if $e ne '0' && $x->{sign} eq '-' && $x == 0;
739 $e = $CALC->_check($x->{value}) if $e eq '0';
741 # test done, see if error did crop up
742 ok (1,1), return if ($e eq '0');
744 ok (1,$e." after op '$f'");
749 # 123,,3 means 123 with precision 3 (A is undef)
750 # the A or P of the result is calculated automatically
805 # 250 ^ 4095 = 3845 => 3800
807 # 255 ^ 4100 = 4347 => 4300
810 # 255 ^ 4100 = 10fb => 4347 => 4300
819 # FF | FA = FF => 250