blib/lib/Math/FakeDD.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary FakeDD.xs »
1 package Math::FakeDD;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use Math::MPFR qw(:mpfr);
Subroutine BEGIN: called CC=1 SCAR=2.6
6  
7 use Config;
8  
9 use 5.022; # for $Config{longdblkind}
10  
11 use constant MPFR_LIB_VERSION => MPFR_VERSION;
12  
13 use constant NAN_COMPARE_BUG => $Math::MPFR::VERSION < 4.23 ? 1 : 0;
14  
15 # The dd_repro() sub requires Math-MPFR-4.24
16 use constant M_MPFR_VER_OK => $Math::MPFR::VERSION >= 4.24 ? 1 : 0;
17  
18 use constant NV_IS_DOUBLE => $Config{nvsize} == 8 ? 1 : 0;
19  
20 use constant NV_IS_DOUBLEDOUBLE => $Config{nvsize} != 8 &&
21 ($Config{longdblkind} >=5 && $Config{longdblkind} <= 8) ? 1 : 0;
22  
23 use constant NV_IS_QUAD => $Config{nvtype} eq '__float128' ||
24 ($Config{nvtype} eq 'long double' && $Config{longdblkind} > 0
25 && $Config{longdblkind} < 3) ? 1 : 0;
26  
27 use constant NV_IS_80BIT_LD => $Config{nvtype} eq 'long double' &&
28 $Config{longdblkind} > 2 && $Config{longdblkind} < 5 ? 1 : 0;
29  
30 use constant M_FDD_DBL_MAX => Rmpfr_get_d(Math::MPFR->new('1.fffffffffffffp+1023', 16), 0);
31 use constant M_FDD_P2_970 => 2 ** 970;
32 use constant DBL_DENORM_MIN => 2 ** -1074;
33  
34 use overload
35 'abs' => \&dd_abs,
36 'atan2' => \&dd_atan2,
37 'bool' => \&dd_true,
38 'cos' => \&dd_cos,
39 'eq' => \&dd_streq,
40 'ne' => \&dd_strne,
41 'exp' => \&dd_exp,
42 'int' => \&dd_int,
43 'log' => \&dd_log,
44 'sin' => \&dd_sin,
45 'sqrt' => \&dd_sqrt,
46 '+' => \&dd_add,
47 '+=' => \&dd_add_eq,
48 '/' => \&dd_div,
49 '/=' => \&dd_div_eq,
50 '==' => \&dd_eq,
51 '>' => \&dd_gt,
52 '>=' => \&dd_gte,
53 '<' => \&dd_lt,
54 '<=' => \&dd_lte,
55 '*' => \&dd_mul,
56 '*=' => \&dd_mul_eq,
57 '!=' => \&dd_neq,
58 '**' => \&dd_pow,
59 '**=' => \&dd_pow_eq,
60 '<=>' => \&dd_spaceship,
61 '""' => \&dd_stringify,
62 '0+' => \&dd_numify,
63 '-' => \&dd_sub,
64 '-=' => \&dd_sub_eq,
65 '!' => \&dd_false,
66 '=' => \&overload_copy,
67 '++' => \&overload_inc,
68 '--' => \&overload_dec,
69 ;
70  
71 require Exporter;
72 *import = \&Exporter::import;
73 require DynaLoader;
74  
75 my $v = MPFR_VERSION_MAJOR;
76 if($v < 4) {
77 $Math::FakeDD::MPFR_PREC_MIN = 2;
78 warn "
79 This module requires that Math::MPFR was built against version 4
80 of the mpfr C library. You have only ", Rmpfr_get_version, ".\n",
81 " As a result, you will experience a fatal assertion error whenever
82 the mpfr library needs to use a 1-bit precision value.
83 Some test suite tests are skipped to avoid such an error.
84 The need to use a 1-bit precision value is fairly limited, so
85 we'll go ahead anyway. You have been warned !!\n";
86 }
87 else { $Math::FakeDD::MPFR_PREC_MIN = 1 }
88  
89 my @tags = qw(
90 NV_IS_DOUBLE NV_IS_DOUBLEDOUBLE NV_IS_QUAD NV_IS_80BIT_LD MPFR_LIB_VERSION
91 dd_abs dd_add dd_add_eq dd_assign dd_atan2 dd_catalan dd_cmp dd_clone dd_copy dd_cos dd_dec
92 dd_div dd_div_eq dd_dump dd_eq dd_euler dd_exp dd_exp2 dd_exp10 dd_frexp
93 dd_gt dd_gte dd_hex dd_inf dd_is_inf dd_is_nan dd_int dd_log dd_log2 dd_log10 dd_lt dd_lte
94 dd_mul dd_mul_eq dd_nan dd_neq
95 dd_nextup dd_nextdown dd_numify dd_pi dd_pow dd_pow_eq dd_repro dd_repro_test
96 dd_sin dd_spaceship dd_sqrt dd_streq dd_stringify dd_strne
97 dd_sub dd_sub_eq
98 dd2mpfr mpfr2dd mpfr_any_prec2dd mpfr2098
99 printx sprintx any2dd unpackx
100 ulp_exponent is_subnormal
101 );
102  
103 @Math::FakeDD::EXPORT_OK = (@tags);
104  
105 %Math::FakeDD::EXPORT_TAGS = (all => [@tags]);
106  
107 $Math::FakeDD::VERSION = '1.03';
108 Math::FakeDD->DynaLoader::bootstrap($Math::FakeDD::VERSION);
109  
110 sub dl_load_flags {0} # Prevent DynaLoader from complaining and croaking
111  
112 # Whenever dd_repro($obj) returns its string representation of
113 # the value of $obj, $Math::FakeDD::REPRO_PREC is set to the
114 # bit-precision that was used to derive that returned value.
115 # This variable will be set to zero if an Inf, NaN or zero
116 # $obj is passed to dd_repro().The initial value of -1 indicates
117 # that dd_repro() has not been called at all.
118  
119 $Math::FakeDD::REPRO_PREC = -1;
120 $Math::FakeDD::DD_MAX = Math::FakeDD->new(Rmpfr_get_d(Math::MPFR->new('1.fffffffffffffp+1023', 16), MPFR_RNDN))
121 + Rmpfr_get_d(Math::MPFR->new('1.fffffffffffffp+969', 16), MPFR_RNDN);
122  
123  
124  
125 sub new {
126  
127 my %h = ('msd' => 0.0, 'lsd' => 0.0, 'mpfr' => Rmpfr_init2(2098));
128 unless(@_) {
129 Rmpfr_set_zero($h{mpfr}, 1);
130 return bless(\%h);
131 }
132  
133 if(!ref($_[0]) && $_[0] eq "Math::FakeDD") {
134 # 'new' has been called as a method
135 shift;
136 unless(@_) {
137 Rmpfr_set_zero($h{mpfr}, 1);
138 return bless(\%h);
139 }
140 }
141  
142 die "Too many args given to new()" if @_ > 1;
143  
144 # If the remaining argument is a Math::FakeDD
145 # object then simply return a copy of it:
146 return shift
147 if ref($_[0]) eq "Math::FakeDD";
148  
149 return mpfr2dd(mpfr2098(shift));
150 }
151  
152 sub dd_repro {
153 die "Arg given to dd_repro() must be a Math::FakeDD object"
154 unless ref($_[0]) eq 'Math::FakeDD';
155  
156 unless(M_MPFR_VER_OK) {
157 warn "dd_repro() needs Math-MPFR-4.24 or later, but you have only $Math::MPFR::VERSION\n";
158 die "Please update Math::MPFR if you wish to call dd_repro()";
159 }
160  
161 my $arg = mpfr2dd($_[0]->{mpfr});
162 my $prec = 0;
163 if(dd_is_nan($arg)) {
164 $Math::FakeDD::REPRO_PREC = 0;
165 return 'NaN';
166 }
167  
168 if(dd_is_inf($arg)) {
169 $Math::FakeDD::REPRO_PREC = 0;
170 return'Inf' if $arg > 0;
171 return'-Inf';
172 }
173  
174 if($arg == 0) {
175 $Math::FakeDD::REPRO_PREC = 0;
176 return '0.0';
177 }
178  
179 if(NV_IS_DOUBLEDOUBLE) {
180 # A correction was made here in Math-FakeDD-0.09.
181 # However, it relies on nvtoa() being correct, and
182 # I don't have a machine on which I can verfiy that.
183 # (The replaced code also relied on correctness of nvtoa.)
184 $Math::FakeDD::REPRO_PREC = undef; # nvtoa() doesn't tell us the precision.
185 my $ret = dd2mpfr($arg->{msd});
186 Rmpfr_add($ret, $ret, dd2mpfr($arg->{lsd}), MPFR_RNDN);
187 return nvtoa(dd2mpfr(Rmpfr_get_NV($ret, MPFR_RNDN)));
188 }
189  
190 my $neg = 0;
191 # my $mpfr = dd2mpfr($arg); # old code
192 my $mpfr = $arg->{mpfr};
193  
194 if($mpfr < 0) {
195 Rmpfr_neg($mpfr, $mpfr, MPFR_RNDN);
196 $neg = 1;
197 }
198  
199 my $exp = Rmpfr_get_exp($mpfr);
200  
201 if($arg->{lsd} == 0) {
202 my $addon = 1074;
203 if( MPFR_LIB_VERSION < 262146 ) { # 4.0.1 or earlier
204 # Prior to mpfr-4.0.2, there are issues with precision < 2,
205 # but DBL_DENORM_MIN calls for a precision of one bit.
206 # We therefore return the hard coded value for this case.
207  
208 if($exp == -1073) {
209 # $mpfr is 2 ** -1074
210 my $ret = $neg ? '-5e-324' : '5e-324';
211 $Math::FakeDD::REPRO_PREC = 1;
212 return $ret;
213 }
214 }
215  
216 $prec = $addon + $exp;
217 Rmpfr_prec_round($mpfr, $prec, MPFR_RNDN);
218 $Math::FakeDD::REPRO_PREC = $prec;
219  
220 return '-' . mpfrtoa($mpfr, $prec + 1) if $neg;
221 return mpfrtoa($mpfr, $prec + 1);
222  
223 } # close $arg->{lsd} == 0
224  
225 my $m_msd = Rmpfr_init2(53);
226 my $m_lsd = Rmpfr_init2(53);
227  
228 Rmpfr_set_d($m_msd, $arg->{msd}, MPFR_RNDN);
229 Rmpfr_set_d($m_lsd, $arg->{lsd}, MPFR_RNDN);
230  
231 my $different_signs = 0; # will be set to 1 if one double < 0,
232 # and the other double > 0.
233  
234 if(abs($arg->{lsd}) >= 2 ** -1022) {
235 # lsd is not subnormal.
236 $prec = Rmpfr_get_exp($m_msd) - Rmpfr_get_exp($m_lsd) + 53;
237 if( ($arg->{lsd} < 0 && $arg->{msd} > 0) || ($arg->{msd} < 0 && $arg->{lsd} > 0) ) {
238 # $prec--; # originally, an *unqualified* decrement.
239 $prec-- if abs( (dd_frexp($arg->{msd}))[0] ) == 0.5; # MSD is a power of 2
240 $different_signs = 1; # one double < 0, the other > 0
241 }
242 my $mpfr_copy = Rmpfr_init2(2098);
243 Rmpfr_set($mpfr_copy, $mpfr, MPFR_RNDN);
244 Rmpfr_prec_round($mpfr_copy, $prec, MPFR_RNDN);
245 my $trial_repro = mpfrtoa($mpfr_copy);
246 my $trial_dd = Math::FakeDD->new($trial_repro);
247 if($trial_dd == $arg || ($neg == 1 && $trial_dd == abs($arg)) ) {
248 $Math::FakeDD::REPRO_PREC = $prec;
249 return '-' . $trial_repro if $neg;
250 return $trial_repro;
251 }
252  
253 $prec++;
254 # Might need to be incremented again if the 2 doubles have different sign.
255 }
256 else {
257 $prec = Rmpfr_get_exp($m_msd) + 1073; # $prec should be > 0
258 #$prec++ if $prec == 0; # Ensure $prec > 0
259  
260 my $mpfr_copy = Rmpfr_init2(2098);
261 Rmpfr_set($mpfr_copy, $mpfr, MPFR_RNDN);
262 Rmpfr_prec_round($mpfr_copy, $prec, MPFR_RNDN);
263 my $trial_repro = mpfrtoa($mpfr_copy);
264 my $trial_dd = Math::FakeDD->new($trial_repro);
265 if($trial_dd == $arg || ($neg == 1 && $trial_dd == abs($arg)) ) {
266 $Math::FakeDD::REPRO_PREC = $prec;
267 return '-' . $trial_repro if $neg;
268 return $trial_repro;
269 }
270  
271 $prec++;
272 }
273  
274 my $mpfr_orig = Rmpfr_init2(2098);
275 Rmpfr_set($mpfr_orig, $mpfr, MPFR_RNDN); # copy $mpfr to $mpfr_orig
276  
277 Rmpfr_prec_round($mpfr, $prec, MPFR_RNDN);
278  
279 if($different_signs) {
280 my $candidate = mpfrtoa($mpfr); # was mpfrtoa($mpfr, 53) - apparently unnecessary
281  
282 # Might fail either the "chop" test or
283 # the "round trip" test, but not both.
284  
285 if(abs($arg) != Math::FakeDD->new($candidate)) {
286 # First check whether decrementing the mantissa
287 # allows the round trip to succeed.
288  
289 my $ret = _decrement($candidate);
290  
291 if(abs($arg) == Math::FakeDD->new($ret)) {
292 $Math::FakeDD::REPRO_PREC = "< $prec";
293 return '-' . $ret if $neg;
294 return $ret;
295 }
296  
297 # Fails round trip - so we increment $prec. We then
298 # can't use $mpfr again as its precision has already
299 # been altered, so we use $mpfr_orig.
300  
301 $prec++;
302 Rmpfr_prec_round($mpfr_orig, $prec, MPFR_RNDN);
303 $Math::FakeDD::REPRO_PREC = $prec;
304 return '-' . mpfrtoa($mpfr_orig) if $neg; # was mpfrtoa($mpfr_orig, 53) - apparently unnecessary
305 return mpfrtoa($mpfr_orig); # was mpfrtoa($mpfr_orig, 53) - apparently unnecessary
306 }
307  
308 my $ret = _chop_test($candidate, $arg, 0);
309  
310 if($ret eq 'ok') {
311 $Math::FakeDD::REPRO_PREC = $prec;
312 return '-' . $candidate if $neg;
313 return $candidate;
314 }
315  
316 # The value we now return is the value calculated
317 # for precision $prec, but with the least significant
318 # mantissa digit removed.
319  
320 $Math::FakeDD::REPRO_PREC = "< $prec";
321 return '-' . $ret if $neg;
322 return $ret;
323  
324 } # close different signs
325  
326 # msd and lsd are either both >0, or both <0.
327 # We need to detect the (rare) cases that a chopped and
328 # then incremented mantissa passes the round trip.
329 # AFAIK, this can happen only if the LSD is an integer power of 2.
330 # Two examples: [0x1p+200 0x1p-549] & [0x1.ffffffffffff8p+999 0x1p-549].
331  
332 my $candidate = mpfrtoa($mpfr);
333 #my @frexp = dd_frexp($arg->{lsd});
334 if(abs( (dd_frexp($arg->{lsd}))[0]) == 0.5) { # LSD is an integer power of 2.
335  
336 my $ret = _chop_test($candidate, $arg, 1); # $ret will either be set to 'ok' (in which case
337 # we return $candidate), or $ret will be set to
338 # the correct value (in which case we return $ret).
339  
340 unless($ret eq 'ok') {
341 $Math::FakeDD::REPRO_PREC = "> $prec";
342 return '-' . $ret if $neg;
343 return $ret;
344 }
345 }
346  
347 $Math::FakeDD::REPRO_PREC = $prec;
348 return '-' . $candidate if $neg;
349 return $candidate;
350 }
351  
352 sub _decrement {
353 my $shift = shift;
354 my @r = split /e/i, $shift;
355  
356 # Remove all trailing zeroes from $r[0];
357  
358 if($r[0] =~ /\./) {
359 chop($r[0]) while $r[0] =~ /0$/;
360 }
361  
362 $r[0] =~ s/\.$//;
363 $r[1] = defined $r[1] ? $r[1] : 0;
364 while($r[0] =~ /0$/) {
365 chop $r[0];
366 $r[1]++;
367 }
368  
369 return $shift if length($r[0]) < 2;
370  
371 my $substitute = substr($r[0], -1, 1) - 1;
372 substr($r[0], -1, 1, "$substitute");
373  
374 my $ret = $r[1] ? $r[0] . 'e' . $r[1]
375 : $r[0];
376  
377 return $ret;
378 }
379  
380 sub _chop_test {
381 my @r = split /e/i, shift;
382 my $op = shift;
383  
384 # If $do_increment is set, then we are not interested
385 # in the result of the chop test. We are interested in the
386 # result of the incrmentation - which requires that we
387 # first perform the chop.
388  
389 my $do_increment = defined($_[0]) ? shift
390 : 0;
391  
392 # We remove from $r[0] any trailing mantissa zeroes, and then
393 # replace the least significant digit with zero.
394 # IOW, we effectively chop off the least siginificant digit, thereby
395 # rounding it down to the next lowest decimal precision.)
396 # This altered string should assign to a DoubleDouble value that is
397 # less than the given $op.
398  
399 chop($r[0]) while $r[0] =~ /0$/;
400 $r[0] =~ s/\.$//;
401 $r[1] = defined $r[1] ? $r[1] : 0;
402 while($r[0] =~ /0$/) {
403 chop $r[0];
404 $r[1]++;
405 }
406  
407 return 'ok' if length($r[0]) < 2; # chop test inapplicable.
408  
409 substr($r[0], -1, 1, '');
410  
411 $r[1]++ unless $r[0] =~ /\./;
412 $r[0] =~ s/\.$/.0/
413 unless $r[1];
414 $r[0] =~ s/\.$//;
415  
416 if(!$do_increment) {
417 # We are interested only in the chop test
418  
419 my $chopped = $r[1] ? $r[0] . 'e' . $r[1]
420 : $r[0];
421  
422 return 'ok' if Math::FakeDD->new($chopped) < abs($op); # chop test ok.
423 return $chopped;
424 }
425  
426 # We are not interested in the chop test - the "chop" was
427 # done only as the first step in the incrementation, and
428 # it's the result of the following incrementation that
429 # interests us. Now we want, in effect, to do:
430 # ++$r[0];
431 # This value should then assign to a DoubleDouble value
432 # that is greater than the given $op.
433  
434 if($r[0] =~ /\./) {
435 # We must remove the '.', do the string increment,
436 # and then reinsert the '.' in the appropriate place.
437 my @mantissa = split /\./, $r[0];
438 my $point_pos = -(length($mantissa[1]));
439 my $t = $mantissa[0] . $mantissa[1];
440 $t++;
441 substr($t, $point_pos, 0, '.');
442 $r[0] = $t;
443 }
444 else {
445 $r[0]++;
446 $r[1]++ while $r[0] =~ s/0$//;
447 }
448  
449 my $incremented = $r[1] ? $r[0] . 'e' . $r[1]
450 : $r[0];
451  
452 return $incremented if Math::FakeDD->new($incremented) == abs($op);
453 return 'ok';
454 }
455  
456 ############################
457 ############################
458  
459 sub dd_repro_test {
460 my ($repro, $op) = (shift, shift);
461 my $ret = 0;
462  
463 my($debug, $examine) = (0, 0);
464  
465 if(defined $_[0]) {
466 die "Too many args passed to dd_repro_test()"
467 if @_ > 2;
468  
469 for(@_) {
470 $debug = 1 if $_ =~ /debug/i;
471 $examine = 1 if $_ =~ /examine/i;
472 }
473 }
474  
475 print "OP: $op\nREPRO: $repro\n" if $debug;
476 $Math::FakeDD::examine{repro} = $repro if $examine;
477  
478 # Handle Infs, Nan, and Zero.
479 if(dd_is_nan($op)) {
480 if($examine) {
481 # Clear unset %Math::FakeDD::examine keys
482 $Math::FakeDD::examine{chop} = '';
483 $Math::FakeDD::examine{inc} = '';
484 }
485 return 15 if $repro eq 'NaN';
486 return 0;
487 }
488  
489 if(dd_is_inf($op)) {
490 if($examine) {
491 # Clear unset %Math::FakeDD::examine keys
492 $Math::FakeDD::examine{chop} = '';
493 $Math::FakeDD::examine{inc} = '';
494 }
495 return 15 if ($op > 0 && $repro eq 'Inf');
496 return 15 if ($op < 0 && $repro eq '-Inf');
497 return 0;
498 }
499  
500 if($op == 0) {
501 if($examine) {
502 # Clear unset %Math::FakeDD::examine keys
503 $Math::FakeDD::examine{chop} = '';
504 $Math::FakeDD::examine{inc} = '';
505 }
506 return 15 if ($repro eq '0.0' || $repro eq '-0.0');
507 return 0;
508 }
509  
510 $repro =~ s/^\-// if $op < 0; # TODO - remove this stipulation
511 $op = abs($op); # TODO - remove this stipulation
512  
513 $ret++ if Math::FakeDD->new($repro) == $op; # round trip ok
514  
515 my @r = split /e/i, $repro;
516  
517 # Increment $ret by 8 if and only if there are no errant trailing
518 # zeroes in $r[0] .
519  
520 if(!defined($r[1])) {
521 $ret += 8 if ($r[0] =~ /\.0$/ || $r[0] !~ /0$/);
522 $r[1] = 0; # define $r[1] by setting it to zero.
523 }
524 else {
525 $ret += 8 unless $r[0] =~ /0$/;
526 }
527  
528 # We remove from $repro any trailing mantissa zeroes, and then
529 # replace the least significant digit with zero.
530 # IOW, we effectively chop off the least siginificant digit, thereby
531 # rounding it down to the next lowest decimal precision.
532 # This altered string should assign to a DoubleDouble value that is
533 # less than the given $op.
534  
535 chop($r[0]) while $r[0] =~ /0$/;
536 $r[0] =~ s/\.$//;
537 while($r[0] =~ /0$/) {
538 chop $r[0];
539 $r[1]++;
540 }
541  
542 if(length($r[0]) < 2) { # chop test and increment test inapplicable.
543 if($examine) {
544 $Math::FakeDD::examine{chop} = '';
545 $Math::FakeDD::examine{inc} = '';
546 }
547 return $ret + 6;
548 }
549  
550 substr($r[0], -1, 1, '0');
551  
552  
553 my $chopped = $r[1] ? $r[0] . 'e' . $r[1]
554 : $r[0];
555  
556 print "CHOPPED:\n$chopped\n\n" if $debug;
557 $Math::FakeDD::examine{chop} = $chopped if $examine;
558  
559 $ret += 2 if Math::FakeDD->new($chopped) < abs($op); # chop test ok.
560  
561 # Now we derive a value that is $repro rounded up to the next lowest
562 # decimal representation.
563 # This value should assign to a DoubleDouble value that is greater
564 # than the given $op.
565  
566 if($r[0] =~ /\./) {
567 # We must remove the '.', do the string increment,
568 # and then reinsert the '.' in the appropriate place.
569 my @mantissa = split /\./, $r[0];
570 my $point_pos = -(length($mantissa[1]));
571 my $t = $mantissa[0] . $mantissa[1];
572 $t++ for 1..10;
573 substr($t, $point_pos, 0, '.');
574 $r[0] = $t;
575 }
576 else {
577 $r[0]++ for 1..10;
578 }
579  
580 unless ($r[0] gt $chopped) {
581 print "OP: $op\n" if $debug;
582 my $substitute = substr($r[0], -1, 1) + 1;
583 substr($r[0], -1, 1, "$substitute");
584 }
585  
586 my $incremented = defined($r[1]) ? $r[0] . 'e' . $r[1]
587 : $r[0];
588  
589 print "INCREMENTED:\n$incremented\n" if $debug;
590 $Math::FakeDD::examine{inc} = $incremented if $examine;
591  
592 $ret += 4 if Math::FakeDD->new($incremented) > abs($op); # increment test ok
593 return $ret;
594 }
595  
596 ############################
597 ############################
598  
599 sub dd_abs {
600 my $obj;
601 my $mpfr = Rmpfr_init2(2098);
602 if(ref($_[0]) eq 'Math::FakeDD') {
603 $obj = dd_copy($_[0]);
604 }
605 else {
606 $obj = mpfr2dd(mpfr2098($_[0]));
607 }
608  
609 if($obj->{msd} < 0) {
610 $obj->{msd} *= -1;
611 $obj->{lsd} *= -1;
612 $obj->{mpfr} *= -1;
613 }
614  
615 return $obj;
616 }
617  
618 sub dd_add {
619 # When dd_add is called via overloading of '+' a
620 # third argument (which we CAN ignore) will be provided
621 die "Wrong number of arguments given to dd_add()"
622 if @_ > 3;
623  
624 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
625  
626 if(ref($_[0]) eq 'Math::FakeDD') {
627 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
628 }
629 else {
630 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
631 }
632  
633 if(ref($_[0]) eq 'Math::FakeDD') {
634 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
635 }
636 else {
637 Rmpfr_set($rop2, mpfr2098(shift), MPFR_RNDN);
638 }
639  
640 Rmpfr_add($rop1, $rop1, $rop2, MPFR_RNDN);
641 return mpfr2dd($rop1);
642 }
643  
644 sub dd_add_4196 {
645 # Used only in testing.
646 die "Wrong number of arguments given to dd_add()"
647 if @_ > 2;
648  
649 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
650  
651 if(ref($_[0]) eq 'Math::FakeDD') {
652 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
653 }
654 else {
655 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
656 }
657  
658 if(ref($_[0]) eq 'Math::FakeDD') {
659 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
660 }
661 else {
662 Rmpfr_set($rop2, mpfr2098(shift), MPFR_RNDN);
663 }
664  
665 my $ret = Rmpfr_init2(4196);
666  
667 Rmpfr_add($ret, $rop1, $rop2, MPFR_RNDN);
668 return mpfr_any_prec2dd($ret);
669  
670 }
671  
672 sub dd_add_eq {
673 # When dd_add_eq is called via overloading of '+=' a
674 # third argument (which we CAN ignore) will be provided
675 die "Wrong number of arguments given to dd_add_eq()"
676 if @_ > 3;
677  
678 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
679  
680 if(ref($_[0]) eq 'Math::FakeDD') {
681 Rmpfr_set($rop1, $_[0]->{mpfr}, MPFR_RNDN);
682 }
683 else {
684 die "First arg to dd_add_eq must be a Math::FakeDD object";
685 }
686  
687 if(ref($_[1]) eq 'Math::FakeDD') {
688 Rmpfr_set($rop2, $_[1]->{mpfr}, MPFR_RNDN);
689 }
690 else {
691 Rmpfr_set($rop2, mpfr2098($_[1]), MPFR_RNDN);
692 }
693  
694 Rmpfr_add($rop1, $rop1, $rop2, MPFR_RNDN);
695  
696 if(@_ > 2) {
697 # dd_add_eq() has been called via
698 # Math::FakeDD overloading of '+='.
699 return mpfr2dd($rop1);
700 }
701  
702 dd_assign($_[0], mpfr2dd($rop1));
703 }
704  
705 sub dd_assign {
706 die "Wrong number of arguments given to dd_assign()"
707 unless @_ == 2;
708  
709 die "First arg to dd_assign must be a Math::FakeDD object"
710 unless ref($_[0]) eq 'Math::FakeDD';
711  
712 my $val = $_[1];
713 if(ref($val) eq 'Math::FakeDD') {
714 $_[0]->{msd} = $val->{msd};
715 $_[0]->{lsd} = $val->{lsd};
716 Rmpfr_set($_[0]->{mpfr}, $val->{mpfr}, MPFR_RNDN);
717 }
718 else {
719 my $obj = mpfr2dd(mpfr2098($val));
720 $_[0]->{msd} = $obj->{msd};
721 $_[0]->{lsd} = $obj->{lsd};
722 Rmpfr_set($_[0]->{mpfr}, $obj->{mpfr}, MPFR_RNDN);
723 }
724  
725 Rmpfr_set_d($_[0]->{mpfr}, $_[0]->{msd}, MPFR_RNDN);
726 Rmpfr_add_d($_[0]->{mpfr}, $_[0]->{mpfr}, $_[0]->{lsd}, MPFR_RNDN);
727 }
728  
729 sub dd_atan2 {
730  
731 # When dd_atan2 is called via overloading of 'atan2' a
732 # third argument (which we cannot ignore) will be provided
733 die "Wrong number of arguments given to dd_atan2()"
734 if @_ > 3;
735  
736 my ($rop1, $rop2);
737  
738 if(ref($_[0]) eq 'Math::FakeDD') {
739 $rop1 = dd2mpfr(shift);
740 }
741 else {
742 $rop1 = mpfr2098(shift);
743 }
744  
745 if(ref($_[0]) eq 'Math::FakeDD') {
746 $rop2 = dd2mpfr(shift);
747 }
748 else {
749 $rop2 = mpfr2098(shift);
750 }
751  
752 if(@_ && $_[0]) { # switch args
753 Rmpfr_atan2($rop2, $rop2, $rop1, MPFR_RNDN);
754 return mpfr2dd($rop2);
755 }
756  
757 Rmpfr_atan2($rop1, $rop1, $rop2, MPFR_RNDN);
758 return mpfr2dd($rop1);
759 }
760  
761 sub dd_catalan {
762 my $rop = Rmpfr_init2(2098);
763 Rmpfr_const_catalan($rop, MPFR_RNDN);
764 return mpfr2dd($rop);
765 }
766  
767  
768 sub dd_cmp {
769  
770 die "Wrong number of arguments given to dd_cmp()"
771 unless @_ == 2;
772  
773 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
774  
775 if(ref($_[0]) eq 'Math::FakeDD') {
776 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
777 }
778 else {
779 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
780 }
781  
782 if(ref($_[0]) eq 'Math::FakeDD') {
783 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
784 }
785 else {
786 Rmpfr_set($rop2, mpfr2098(shift), MPFR_RNDN);
787 }
788  
789 return $rop1 <=> $rop2; # "<=>" is "Math::MPFR::overload_spaceship"
790 # and will return undef if a NaN is involved.
791 }
792  
793 *dd_clone = \&dd_copy;
794 sub dd_copy {
795 die "Arg given to dd_clone or dd_copy must be a Math::FakeDD object"
796 unless ref($_[0]) eq 'Math::FakeDD';
797  
798 my $ret = Math::FakeDD->new();
799 dd_assign($ret, $_[0]);
800 return $ret;
801 }
802  
803 sub dd_cos {
804 my $mpfr = Rmpfr_init2(2098);
805  
806 if(ref($_[0]) eq 'Math::FakeDD') {
807 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
808 }
809 else {
810 my $new_obj = mpfr2dd(mpfr2098($_[0]));
811 Rmpfr_set($mpfr, $new_obj->{mpfr}, MPFR_RNDN);
812 }
813  
814 Rmpfr_cos($mpfr, $mpfr, MPFR_RNDN);
815 my $ret = mpfr2dd($mpfr);
816 return $ret;
817 }
818  
819 sub dd_dec {
820 die "Wrong arg given to dd_dec()"
821 unless ref($_[0]) eq 'Math::FakeDD';
822 my $mpfr = dd2mpfr(shift);
823  
824 if(!Rmpfr_regular_p($mpfr)) {
825 return '0.0' if Rmpfr_zero_p($mpfr);
826 return 'NaN' if Rmpfr_nan_p($mpfr);
827  
828 # must be an inf
829 return 'Inf' if $mpfr > 0;
830 return '-Inf';
831 }
832  
833 return decimalize($mpfr);
834 }
835  
836 sub dd_div {
837 # When dd_div is called via overloading of '/' a
838 # third argument (which we cannot ignore) will be provided
839 die "Wrong number of arguments given to dd_div()"
840 if @_ > 3;
841  
842 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
843  
844 if(ref($_[0]) eq 'Math::FakeDD') {
845 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
846 }
847 else {
848 Rmpfr_set($rop1,mpfr2098(shift), MPFR_RNDN);
849 }
850  
851 if(ref($_[0]) eq 'Math::FakeDD') {
852 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
853 }
854 else {
855 Rmpfr_set($rop2,mpfr2098(shift), MPFR_RNDN);
856 }
857  
858 if(@_ && $_[0]) { # switch args
859 Rmpfr_div($rop2, $rop2, $rop1, MPFR_RNDN);
860 return mpfr2dd($rop2);
861 }
862 Rmpfr_div($rop1, $rop1, $rop2, MPFR_RNDN);
863 return mpfr2dd($rop1);
864  
865 }
866  
867 sub dd_div_4196 {
868 # Used only in testing.
869 die "Wrong number of arguments given to dd_div()"
870 if @_ > 2;
871  
872 my ($rop1, $rop2);
873  
874 if(ref($_[0]) eq 'Math::FakeDD') {
875 $rop1 = dd2mpfr(shift);
876 }
877 else {
878 $rop1 = mpfr2098(shift);
879 }
880  
881 if(ref($_[0]) eq 'Math::FakeDD') {
882 $rop2 = dd2mpfr(shift);
883 }
884 else {
885 $rop2 = mpfr2098(shift);
886 }
887  
888 my $ret = Rmpfr_init2(4196);
889  
890 Rmpfr_div($ret, $rop1, $rop2, MPFR_RNDN);
891 return mpfr_any_prec2dd($ret);
892  
893 }
894  
895 sub dd_div_eq {
896 # When dd_div_eq is called via overloading of '/=' a
897 # third argument (which we CAN ignore) will be provided
898 die "Wrong number of arguments given to dd_div_eq()"
899 if @_ > 3;
900  
901 my ($rop1, $rop2);
902  
903 if(ref($_[0]) eq 'Math::FakeDD') {
904 $rop1 = dd2mpfr($_[0]);
905 }
906 else {
907 die "First arg to dd_div_eq must be a Math::FakeDD object";
908 }
909  
910 if(ref($_[1]) eq 'Math::FakeDD') {
911 $rop2 = dd2mpfr($_[1]);
912 }
913 else {
914 $rop2 = mpfr2098($_[1]);
915 }
916  
917 Rmpfr_div($rop1, $rop1, $rop2, MPFR_RNDN);
918  
919 if(@_ > 2) {
920 # dd_div_eq() has been called via
921 # Math::FakeDD overloading of '/='.
922 return mpfr2dd($rop1);
923 }
924 dd_assign($_[0], mpfr2dd($rop1));
925  
926 }
927  
928 sub dd_dump {
929 my $s1 = "[" . sprintf("%.17g", $_[0]->{msd}) . " " . sprintf("%.17g", $_[0]->{lsd}) . "]";
930 my $s2 = sprintx($_[0]);
931 my ($s3, $exp) = Rmpfr_deref2($_[0]->{mpfr}, 2, 0, MPFR_RNDN);
932 if($s3 =~ /^1/) {
933 $s3 =~ s/0+$//;
934 $s3 = "0." . $s3 . "E$exp";
935 }
936 return "$s1\n$s2\n$s3\n";
937 }
938  
939 sub dd_eq {
940  
941 # When dd_eq is called via overloading of '==' a
942 # third argument (which we CAN ignore) will be provided
943 die "Wrong number of args passed to dd_eq()"
944 if(@_ > 3);
945 my $cmp = dd_cmp(shift, shift);
946 return 0 if $cmp || !defined $cmp; # not equal
947 return 1; # equal
948 }
949  
950 sub dd_euler {
951 my $rop = Rmpfr_init2(2098);
952 Rmpfr_const_euler($rop, MPFR_RNDN);
953 return mpfr2dd($rop);
954 }
955  
956 sub dd_exp {
957 my $mpfr = Rmpfr_init2(2098);
958  
959 if(ref($_[0]) eq 'Math::FakeDD') {
960 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
961 }
962 else {
963 my $new_obj = mpfr2dd(mpfr2098($_[0]));
964 Rmpfr_set($mpfr, $new_obj->{mpfr}, MPFR_RNDN);
965 }
966  
967 Rmpfr_exp($mpfr, $mpfr, MPFR_RNDN);
968 my $ret = mpfr2dd($mpfr);
969 return $ret;
970 }
971  
972 sub dd_exp2 {
973 my $mpfr = Rmpfr_init2(2098);
974  
975 if(ref($_[0]) eq 'Math::FakeDD') {
976 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
977 }
978 else {
979 my $new_obj = mpfr2dd(mpfr2098($_[0]));
980 Rmpfr_set($mpfr, $new_obj->{mpfr}, MPFR_RNDN);
981 }
982  
983 Rmpfr_exp2($mpfr, $mpfr, MPFR_RNDN);
984 my $ret = mpfr2dd($mpfr);
985 return $ret;
986 }
987  
988 sub dd_exp10 {
989 my $mpfr = Rmpfr_init2(2098);
990  
991 if(ref($_[0]) eq 'Math::FakeDD') {
992 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
993 }
994 else {
995 my $new_obj = mpfr2dd(mpfr2098($_[0]));
996 Rmpfr_set($mpfr, $new_obj->{mpfr}, MPFR_RNDN);
997 }
998  
999 Rmpfr_exp10($mpfr, $mpfr, MPFR_RNDN);
1000 my $ret = mpfr2dd($mpfr);
1001 return $ret;
1002 }
1003  
1004 sub dd_gt {
1005  
1006 # When dd_gt is called via overloading of '>' a third
1007 # argument (which we cannot ignore) will be provided
1008 die "Wrong number of args passed to dd_gt()"
1009 if(@_ > 3);
1010  
1011 my $correction = 1;
1012 $correction = -1
1013 if (@_ == 3 && $_[2]);
1014  
1015 return 1 if $correction * dd_cmp(shift, shift) > 0; # greater than
1016 return 0; # not greater than
1017 }
1018  
1019 sub dd_gte {
1020  
1021 # When dd_gte is called via overloading of '>=' a third
1022 # argument (which we cannot ignore) will be provided
1023 die "Wrong number of args passed to dd_gte()"
1024 if(@_ > 3);
1025  
1026 my $correction = 1;
1027 $correction = -1
1028 if (@_ == 3 && $_[2]);
1029  
1030 return 0 if $correction * dd_cmp(shift, shift) < 0; # less than
1031 return 1; # greater than or equal
1032 }
1033  
1034 sub dd_hex {
1035 die "Wrong arg given to dd_dec()"
1036 unless ref($_[0]) eq 'Math::FakeDD';
1037  
1038 my $mpfr = dd2mpfr(shift);
1039  
1040 if(!Rmpfr_regular_p($mpfr)) {
1041 return '0x0p+0' if Rmpfr_zero_p($mpfr);
1042 return 'NaN' if Rmpfr_nan_p($mpfr);
1043  
1044 # must be an inf
1045 return 'Inf' if $mpfr > 0;
1046 return '-Inf';
1047 }
1048  
1049 my $buffer;
1050 Rmpfr_sprintf($buffer, "%Ra", $mpfr, 528);
1051  
1052 return $buffer;
1053 }
1054  
1055 sub dd_int {
1056 my $mpfr = Rmpfr_init2(2098);
1057 if(ref($_[0]) eq 'Math::FakeDD') { Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN) }
1058 else {
1059 Rmpfr_set($mpfr, mpfr2098($_[0]));
1060 }
1061  
1062 Rmpfr_trunc($mpfr, $mpfr);
1063 my $ret = mpfr2dd($mpfr);
1064 return $ret;
1065 }
1066  
1067 sub dd_log {
1068 my $mpfr = Rmpfr_init2(2098);
1069  
1070 if(ref($_[0]) eq 'Math::FakeDD') {
1071 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
1072 }
1073 else {
1074 my $new_obj = mpfr2dd(mpfr2098($_[0]));
1075 Rmpfr_set($mpfr, $new_obj->{mpfr}, MPFR_RNDN);
1076 }
1077  
1078 Rmpfr_log($mpfr, $mpfr, MPFR_RNDN);
1079 my $ret = mpfr2dd($mpfr);
1080 return $ret;
1081 }
1082  
1083 sub dd_log2 {
1084 my $mpfr = Rmpfr_init2(2098);
1085  
1086 if(ref($_[0]) eq 'Math::FakeDD') {
1087 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
1088 }
1089 else {
1090 my $new_obj = mpfr2dd(mpfr2098($_[0]));
1091 Rmpfr_set($mpfr, $new_obj->{mpfr}, MPFR_RNDN);
1092 }
1093  
1094 Rmpfr_log2($mpfr, $mpfr, MPFR_RNDN);
1095 my $ret = mpfr2dd($mpfr);
1096 return $ret;
1097 }
1098  
1099 sub dd_log10 {
1100 my $mpfr = Rmpfr_init2(2098);
1101  
1102 if(ref($_[0]) eq 'Math::FakeDD') {
1103 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
1104 }
1105 else {
1106 my $new_obj = mpfr2dd(mpfr2098($_[0]));
1107 Rmpfr_set($mpfr, $new_obj->{mpfr}, MPFR_RNDN);
1108 }
1109  
1110 Rmpfr_log10($mpfr, $mpfr, MPFR_RNDN);
1111 my $ret = mpfr2dd($mpfr);
1112 return $ret;
1113 }
1114  
1115 sub dd_lt {
1116  
1117 # When dd_gt is called via overloading of '<' a third
1118 # argument (which we cannot ignore) will be provided
1119 die "Wrong number of args passed to dd_lt()"
1120 if(@_ > 3);
1121  
1122 my $correction = 1;
1123 $correction = -1
1124 if (@_ == 3 && $_[2]);
1125  
1126 return 1 if $correction * dd_cmp(shift, shift) < 0; # less than
1127 return 0; # not less than
1128 }
1129  
1130 sub dd_lte {
1131  
1132 # When dd_lte is called via overloading of '>=' a third
1133 # argument (which we cannot ignore) will be provided
1134 die "Wrong number of args passed to dd_lte()"
1135 if(@_ > 3);
1136  
1137 my $correction = 1;
1138 $correction = -1
1139 if (@_ == 3 && $_[2]);
1140  
1141 return 0 if $correction * dd_cmp(shift, shift) > 0; # greater than
1142 return 1; # less than or equal
1143 }
1144  
1145 sub dd_mul {
1146 # When dd_mul is called via overloading of '*' a
1147 # third argument (which we CAN ignore) will be provided
1148 die "Wrong number of arguments given to dd_mul()"
1149 if @_ > 3;
1150  
1151 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
1152  
1153 if(ref($_[0]) eq 'Math::FakeDD') {
1154 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
1155 }
1156 else {
1157 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
1158 }
1159  
1160 if(ref($_[0]) eq 'Math::FakeDD') {
1161 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
1162 }
1163 else {
1164 Rmpfr_set($rop2, mpfr2098(shift), MPFR_RNDN);
1165 }
1166  
1167 Rmpfr_mul($rop1, $rop1, $rop2, MPFR_RNDN);
1168 return mpfr2dd($rop1);
1169  
1170 }
1171  
1172 sub dd_mul_4196 {
1173 # Used only in testing.
1174 die "Wrong number of arguments given to dd_mul()"
1175 if @_ > 2;
1176  
1177 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
1178  
1179 if(ref($_[0]) eq 'Math::FakeDD') {
1180 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
1181 }
1182 else {
1183 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
1184 }
1185  
1186 if(ref($_[0]) eq 'Math::FakeDD') {
1187 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
1188 }
1189 else {
1190 Rmpfr_set($rop2, mpfr2098(shift), MPFR_RNDN);
1191 }
1192  
1193 my $ret = Rmpfr_init2(4196);
1194  
1195 Rmpfr_mul($ret, $rop1, $rop2, MPFR_RNDN);
1196 return mpfr_any_prec2dd($ret);
1197  
1198 }
1199  
1200 sub dd_mul_eq {
1201 # When dd_mul_eq is called via overloading of '*=' a
1202 # third argument (which we CAN ignore) will be provided
1203 die "Wrong number of arguments given to dd_mul_eq()"
1204 if @_ > 3;
1205  
1206 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
1207  
1208 if(ref($_[0]) eq 'Math::FakeDD') {
1209 Rmpfr_set($rop1, $_[0]->{mpfr}, MPFR_RNDN);
1210 }
1211 else {
1212 die "First arg to dd_mul_eq must be a Math::FakeDD object";
1213 }
1214  
1215 if(ref($_[1]) eq 'Math::FakeDD') {
1216 Rmpfr_set($rop2, $_[1]->{mpfr}, MPFR_RNDN);
1217 }
1218 else {
1219 $rop2 = mpfr2098($_[1]);
1220 }
1221  
1222 Rmpfr_mul($rop1, $rop1, $rop2, MPFR_RNDN);
1223  
1224 if(@_ > 2) {
1225 # dd_mul_eq() has been called via
1226 # Math::FakeDD overloading of '*='.
1227 return mpfr2dd($rop1);
1228 }
1229  
1230 dd_assign($_[0], mpfr2dd($rop1));
1231 }
1232  
1233 sub dd_neq {
1234  
1235 # When dd_neq is called via overloading of '!=' a
1236 # third argument (which we CAN ignore) will be provided
1237 die "Wrong number of args passed to dd_neq()"
1238 if(@_ > 3);
1239 my $cmp = dd_cmp(shift, shift);
1240 return 1 if $cmp || !defined $cmp; # not equal
1241 return 0; # equal
1242 }
1243  
1244 sub dd_numify {
1245 # Mainly for '0+' overloading.
1246 # So far, I've only ever seen '0+' overloading get invoked
1247 # by Test::More if (and only if) a test performing a
1248 # comparisons that involved a Math::FakeDD object failed.
1249  
1250 die "Argument passed to dd_numify must be a Math::FakeDD object"
1251 unless ref($_[0]) eq 'Math::FakeDD';
1252  
1253 my $ret = Rmpfr_init2(2098);
1254 Rmpfr_set($ret, $_[0]->{mpfr}, MPFR_RNDN);
1255 return $ret;
1256 }
1257  
1258 sub dd_pi {
1259 my $rop = Rmpfr_init2(2098);
1260 Rmpfr_const_pi($rop, MPFR_RNDN);
1261 return mpfr2dd($rop);
1262 }
1263  
1264 sub dd_pow {
1265 # When dd_pow is called via overloading of '**' a
1266 # third argument (which we cannot ignore) will be provided
1267 die "Wrong number of arguments given to dd_pow()"
1268 if @_ > 3;
1269  
1270 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
1271  
1272 if(ref($_[0]) eq 'Math::FakeDD') {
1273 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
1274 }
1275 else {
1276 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
1277 }
1278  
1279 if(ref($_[0]) eq 'Math::FakeDD') {
1280 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
1281 }
1282 else {
1283 Rmpfr_set($rop2, mpfr2098(shift), MPFR_RNDN);
1284 }
1285  
1286 if(@_ && $_[0]) { # switch args
1287 Rmpfr_pow($rop2, $rop2, $rop1, MPFR_RNDN);
1288 return mpfr2dd($rop2);
1289 }
1290 Rmpfr_pow($rop1, $rop1, $rop2, MPFR_RNDN);
1291 return mpfr2dd($rop1);
1292  
1293 }
1294  
1295 sub dd_pow_eq {
1296 # When dd_pow_eq is called via overloading of '**=' a
1297 # third argument (which we CAN ignore) will be provided
1298 die "Wrong number of arguments given to dd_pow_eq()"
1299 if @_ > 3;
1300  
1301 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
1302  
1303 if(ref($_[0]) eq 'Math::FakeDD') {
1304 Rmpfr_set($rop1, $_[0]->{mpfr}, MPFR_RNDN);
1305 }
1306 else {
1307 die "First arg to dd_pow_eq must be a Math::FakeDD object";
1308 }
1309  
1310 if(ref($_[1]) eq 'Math::FakeDD') {
1311 Rmpfr_set($rop2, $_[1]->{mpfr}, MPFR_RNDN);
1312 }
1313 else {
1314 Rmpfr_set($rop2, mpfr2098($_[1]), MPFR_RNDN);
1315 }
1316  
1317 Rmpfr_pow($rop1, $rop1, $rop2, MPFR_RNDN);
1318  
1319 if(@_ > 2) {
1320 # dd_pow_eq() has been called via
1321 # Math::FakeDD overloading of '**='.
1322 return mpfr2dd($rop1);
1323 }
1324  
1325 dd_assign($_[0], mpfr2dd($rop1));
1326 }
1327  
1328 sub dd_sin {
1329 my $mpfr = Rmpfr_init2(2098);
1330  
1331 if(ref($_[0]) eq 'Math::FakeDD') {
1332 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
1333 }
1334 else {
1335 my $new_obj = mpfr2dd(mpfr2098($_[0]));
1336 Rmpfr_set($mpfr, $new_obj->{mpfr}, MPFR_RNDN);
1337 }
1338  
1339 Rmpfr_sin($mpfr, $mpfr, MPFR_RNDN);
1340 my $ret = mpfr2dd($mpfr);
1341 return $ret;
1342 }
1343  
1344 sub dd_spaceship {
1345  
1346 # When dd_spaceship is called via overloading of '<=>' a
1347 # third argument (which we cannot ignore) will be provided
1348 die "Wrong number of args passed to dd_spaceship()"
1349 if(@_ > 3);
1350  
1351 my $correction = 1;
1352 $correction = -1
1353 if (@_ == 3 && $_[2]);
1354  
1355 my($arg1, $arg2) = (shift, shift);
1356  
1357 if(NAN_COMPARE_BUG) { # Fixed in Math-MPFR-4.23 and later.
1358 if(dd_is_nan(Math::FakeDD->new($arg1)) || dd_is_nan(Math::FakeDD->new($arg2))) {
1359 return undef;
1360 }
1361 }
1362  
1363 my $cmp = dd_cmp($arg1, $arg2);
1364 return $correction * $cmp if defined $cmp;
1365 return $cmp;
1366 }
1367  
1368 sub dd_sqrt {
1369 my $mpfr = Rmpfr_init2(2098);
1370 if(ref($_[0]) eq 'Math::FakeDD') {
1371 Rmpfr_set($mpfr, $_[0]->{mpfr}, MPFR_RNDN);
1372 }
1373 else {
1374 Rmpfr_set($mpfr, mpfr2098(shift), MPFR_RNDN);
1375 }
1376  
1377 Rmpfr_sqrt($mpfr, $mpfr, MPFR_RNDN);
1378 my $ret = mpfr2dd($mpfr);
1379 return $ret;
1380 }
1381  
1382 sub dd_streq {
1383 # Provided only because Test::More can pull in code
1384 # that assumes that overloading of 'eq' exists.
1385 # This is the function that overloaded 'eq' calls.
1386 die "Wrong arg given to dd_streq()"
1387 unless ref($_[0]) eq 'Math::FakeDD';
1388  
1389 my($arg1, $arg2) = (shift, shift);
1390 return 1 if "$arg1" eq "$arg2";
1391 return 0;
1392 }
1393  
1394 sub dd_strne {
1395 # Provided only because dd_streq() is provided.
1396 die "Wrong arg given to dd_strne()"
1397 unless ref($_[0]) eq 'Math::FakeDD';
1398  
1399 my($arg1, $arg2) = (shift, shift);
1400 return 1 if "$arg1" ne "$arg2";
1401 return 0;
1402 }
1403  
1404 sub dd_stringify {
1405 die "Wrong arg given to dd_stringify()"
1406 unless ref($_[0]) eq 'Math::FakeDD';
1407  
1408 my $self = shift;
1409  
1410 return "[" . nvtoa($self->{msd}) . " " . nvtoa($self->{lsd}) . "]"
1411 if NV_IS_DOUBLE;
1412  
1413 return "[0.0 0.0]" if($self->{msd} == 0 && $self->{lsd} == 0); # Don't look at the exponent of a
1414 # Math::MPFR object whose value is 0.
1415  
1416 # If $Config{nvsize} != 8, an assertion failure was making
1417 # it difficult for this sub to work with infinities. So,
1418 # we now simply detect the inf and return the appropriate
1419 # string, irrespective of the value of $Config{nvsize}.
1420 if(dd_is_inf($self)) {
1421 return "[Inf 0.0]" if $self > 0;
1422 return "[-Inf 0.0]";
1423 }
1424 my($mpfrm, $mpfrl) = (Rmpfr_init2(53), Rmpfr_init2(53));
1425 Rmpfr_set_d($mpfrm, $self->{msd}, MPFR_RNDN);
1426 Rmpfr_set_d($mpfrl, $self->{lsd}, MPFR_RNDN);
1427 my $expm = Rmpfr_get_exp($mpfrm);
1428  
1429 # Deal with the possibility that the absolute value of one (and only
1430 # one) of the 2 doubles could be subnormal - ie less that 2 ** -1022.
1431  
1432 if($expm < -1021) { # msd is subnormal
1433 Rmpfr_prec_round($mpfrm, 1074 + $expm, MPFR_RNDN);
1434 return "[" . mpfrtoa($mpfrm, 53) . " " . '0.0' . "]";
1435 }
1436  
1437 if($self->{lsd}) { # Avoid the case that lsd is 0 !!!
1438 my $expl = Rmpfr_get_exp($mpfrl);
1439 if($expl < -1021) { # lsd is subnormal
1440 Rmpfr_prec_round($mpfrl, 1074 + $expl, MPFR_RNDN);
1441 return "[" . mpfrtoa($mpfrm) . " " . mpfrtoa($mpfrl, 53) . "]";
1442 }
1443 }
1444 return "[" . mpfrtoa($mpfrm) . " " . mpfrtoa($mpfrl) . "]";
1445 }
1446  
1447 sub dd_sub {
1448 # When dd_sub is called via overloading of '-' a
1449 # third argument (which we cannot ignore) will be provided
1450 die "Wrong number of arguments given to dd_sub()"
1451 if @_ > 3;
1452  
1453 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
1454  
1455 if(ref($_[0]) eq 'Math::FakeDD') {
1456 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
1457 }
1458 else {
1459 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
1460 }
1461  
1462 if(ref($_[0]) eq 'Math::FakeDD') {
1463 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
1464 }
1465 else {
1466 Rmpfr_set($rop2, mpfr2098(shift), MPFR_RNDN);
1467 }
1468  
1469 if(@_ && $_[0]) { # switch args
1470 Rmpfr_sub($rop2, $rop2, $rop1, MPFR_RNDN);
1471 return mpfr2dd($rop2);
1472 }
1473  
1474 Rmpfr_sub($rop1, $rop1, $rop2, MPFR_RNDN);
1475 return mpfr2dd($rop1);
1476 }
1477  
1478 sub dd_sub_4196 {
1479 # Used only in testing.
1480 die "Wrong number of arguments given to dd_sub()"
1481 if @_ > 2;
1482  
1483 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
1484  
1485 if(ref($_[0]) eq 'Math::FakeDD') {
1486 Rmpfr_set($rop1, shift->{mpfr}, MPFR_RNDN);
1487 }
1488 else {
1489 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
1490 }
1491  
1492 if(ref($_[0]) eq 'Math::FakeDD') {
1493 Rmpfr_set($rop2, shift->{mpfr}, MPFR_RNDN);
1494 }
1495 else {
1496 Rmpfr_set($rop1, mpfr2098(shift), MPFR_RNDN);
1497 }
1498  
1499 my $ret = Rmpfr_init2(4196);
1500  
1501 Rmpfr_sub($ret, $rop1, $rop2, MPFR_RNDN);
1502 return mpfr_any_prec2dd($ret);
1503 }
1504  
1505 sub dd_sub_eq {
1506 # When dd_sub_eq is called via overloading of '-=' a
1507 # third argument (which we CAN ignore) will be provided
1508 die "Wrong number of arguments given to dd_sub_eq()"
1509 if @_ > 3;
1510  
1511 my ($rop1, $rop2) = (Rmpfr_init2(2098), Rmpfr_init2(2098));
1512  
1513 if(ref($_[0]) eq 'Math::FakeDD') {
1514 Rmpfr_set($rop1, $_[0]->{mpfr}, MPFR_RNDN);
1515 }
1516 else {
1517 die "First arg to dd_sub_eq must be a Math::FakeDD object";
1518 }
1519  
1520 if(ref($_[1]) eq 'Math::FakeDD') {
1521 Rmpfr_set($rop2, $_[1]->{mpfr}, MPFR_RNDN);
1522 }
1523 else {
1524 Rmpfr_set($rop2, mpfr2098($_[1]), MPFR_RNDN);
1525 }
1526  
1527 Rmpfr_sub($rop1, $rop1, $rop2, MPFR_RNDN);
1528  
1529 if(@_ > 2) {
1530 # dd_sub_eq() has been called via
1531 # Math::FakeDD overloading of '-='.
1532 return mpfr2dd($rop1);
1533 }
1534  
1535 dd_assign($_[0], mpfr2dd($rop1));
1536  
1537 }
1538  
1539 sub dd2mpfr {
1540 my $self = shift;
1541 #my $ret = Rmpfr_init2(2098);
1542 #Rmpfr_add($ret, mpfr2098($self->{msd}), mpfr2098($self->{lsd}), MPFR_RNDN);
1543 #return $ret;
1544 return $self->{mpfr};
1545 }
1546  
1547 sub mpfr2dd {
1548 my %h;
1549  
1550 die "Arg given to mpfr2dd() must be a Math::MPFR object"
1551 unless ref($_[0]) eq 'Math::MPFR';
1552  
1553 # mpfr2dd() will handle an argument of any precision - but if the
1554 # precision is not 2098, then it's probably a mistake. So let's
1555 # disallow it until it becomes evident that it should be permitted.
1556  
1557 die "Precision of Math::MPFR object passed to mpfr2dd() must be 2098"
1558 unless Rmpfr_get_prec($_[0]) == 2098;
1559  
1560 my $mpfr = Rmpfr_init2(2098);
1561 Rmpfr_set($mpfr, shift, MPFR_RNDN);
1562 my $msd = Rmpfr_get_d($mpfr, MPFR_RNDN);
1563  
1564 # $msd could be an Inf or Zero, even though $mpfr was not.
1565 # Also cater for the possibility that $msd is Nan - though
1566 # I don't think that can happen.
1567 if(!$msd || _is_nan($msd) || _is_inf($msd)) { # $msd is zero, nan, or inf.
1568 $h{msd} = $msd;
1569 $h{lsd} = 0.0;
1570 $h{mpfr} = Rmpfr_init2(2098);
1571 Rmpfr_set_d($h{mpfr}, $h{msd}, MPFR_RNDN);
1572 return bless(\%h);
1573 }
1574  
1575 Rmpfr_sub_d($mpfr, $mpfr, $msd, MPFR_RNDN);
1576 my $lsd = Rmpfr_get_d($mpfr, MPFR_RNDN);
1577  
1578 # If abs($msd) is DBL_MAX && abs($lsd) is 2**970
1579 # && $msd has the same sign as $lsd, then return
1580 # an Inf that has the same sign as $msd.
1581 # This is a bit murky. See:
1582 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61399
1583 if(_return_inf($msd, $lsd)) {
1584 if($msd < 0 && $lsd < 0) { return dd_inf(-1) }
1585 if($msd > 0 && $lsd > 0) { return dd_inf() }
1586 }
1587  
1588 $h{msd} = $msd;
1589 $h{lsd} = $lsd;
1590 $h{mpfr} = Rmpfr_init2(2098);
1591 Rmpfr_set_d($h{mpfr}, $h{msd}, MPFR_RNDN);
1592 Rmpfr_add_d($h{mpfr}, $h{mpfr}, $h{lsd}, MPFR_RNDN);
1593 return bless(\%h);
1594 }
1595  
1596 sub mpfr_any_prec2dd {
1597 # Converts a Math::MPFR object of any
1598 # precision to a Math::FakeDD object.
1599 my %h;
1600  
1601 die "Arg given to mpfr2dd() must be a Math::MPFR object"
1602 unless ref($_[0]) eq 'Math::MPFR';
1603  
1604 my $prec_in = Rmpfr_get_prec($_[0]);
1605 my $mpfr_prec = $prec_in <= 2098 ? 2098 : $prec_in;
1606  
1607 my $mpfr = Rmpfr_init2($mpfr_prec);
1608 Rmpfr_set($mpfr, shift, MPFR_RNDN);
1609  
1610 my $msd = Rmpfr_get_d($mpfr, MPFR_RNDN);
1611 if($msd == 0 || $msd != $msd || $msd / $msd != 1) { # $msd is zero, nan, or inf.
1612 $h{msd} = $msd;
1613 $h{lsd} = 0;
1614 $h{mpfr} = Rmpfr_init2(2098);
1615 Rmpfr_set_d($h{mpfr}, $h{msd}, MPFR_RNDN);
1616 return bless(\%h);
1617 }
1618  
1619 Rmpfr_sub_d($mpfr, $mpfr, $msd, MPFR_RNDN);
1620 my $lsd = Rmpfr_get_d($mpfr, MPFR_RNDN);
1621 $h{msd} = $msd;
1622 $h{lsd} = $lsd;
1623 $h{mpfr} = Rmpfr_init2(2098);
1624 Rmpfr_set_d($h{mpfr}, $h{msd}, MPFR_RNDN);
1625 Rmpfr_add_d($h{mpfr}, $h{mpfr}, $h{lsd}, MPFR_RNDN);
1626 return bless(\%h);
1627 }
1628  
1629 sub mpfr2098 {
1630 # Set the argument to a 2098-bit precision Math::MPFR object.
1631 my $ret = Rmpfr_init2(2098);
1632 my $itsa = Math::MPFR::_itsa($_[0]);
1633  
1634 # Arg must be one of PV (string), IV (integer), UV (unsigned integer),
1635 # NV (perl floating point type) or Math::MPFR object.
1636  
1637 die "Invalid arg ($itsa) passed internally to mpfr2098()"
1638 unless ($itsa > 0 && $itsa <= 5);
1639  
1640 if($itsa == 5) { # Math::MPFR object
1641 Rmpfr_set($ret, $_[0], MPFR_RNDN);
1642 return $ret;
1643 }
1644  
1645 my $arg = shift;
1646  
1647 if($itsa == 4) { # PV
1648 my $turnoff = 0;
1649 if(!$Math::MPFR::NNW) {
1650 $Math::MPFR::NNW = 1; # warn if $arg contains non-numeric character(s).
1651 $turnoff = 1;
1652 }
1653 Rmpfr_set_str($ret, $arg, 0, MPFR_RNDN);
1654 $Math::MPFR::NNW = 0 if $turnoff; # $Math::MPFR::NNW retains initial setting.
1655 return $ret;
1656 }
1657  
1658 if($itsa == 3) { # NV
1659 Rmpfr_set_NV($ret, $arg, MPFR_RNDN);
1660 return $ret;
1661 }
1662  
1663 Rmpfr_set_IV($ret, $arg, MPFR_RNDN); # IV/UV
1664 return $ret;
1665  
1666 }
1667  
1668 sub overload_copy {
1669 # Not exported
1670 my $ret = Math::FakeDD->new();
1671 dd_assign($ret, $_[0]);
1672 return $ret;
1673 }
1674  
1675 sub overload_inc {
1676 # Not exported
1677 my $rop1 = dd2mpfr($_[0]);
1678 Rmpfr_add_ui($rop1, $rop1, 1, MPFR_RNDN);
1679 dd_assign($_[0], mpfr2dd($rop1));
1680 }
1681  
1682 sub overload_dec {
1683 # Not exported
1684 my $rop1 = dd2mpfr($_[0]);
1685 Rmpfr_sub_ui($rop1, $rop1, 1, MPFR_RNDN);
1686 dd_assign($_[0], mpfr2dd($rop1));
1687 }
1688  
1689 sub oload {
1690 # Not exported.
1691 # Return a list of the operator-function pairs for the overloaded
1692 # operators and the respective functions that they call.
1693  
1694 my %h = (
1695 'abs' => 'dd_abs',
1696 'atan2' => 'dd_atan2',
1697 'bool' => 'dd_true',
1698 'cos' => 'dd_cos',
1699 'eq' => 'dd_streq',
1700 'ne' => 'dd_strne',
1701 'exp' => 'dd_exp',
1702 'int' => 'dd_int',
1703 'log' => 'dd_log',
1704 'sin' => 'dd_sin',
1705 'sqrt' => 'dd_sqrt',
1706 '+' => 'dd_add',
1707 '+=' => 'dd_add_eq',
1708 '/' => 'dd_div',
1709 '/=' => 'dd_div_eq',
1710 '==' => 'dd_eq',
1711 '>' => 'dd_gt',
1712 '>=' => 'dd_gte',
1713 '<' => 'dd_lt',
1714 '<=' => 'dd_lte',
1715 '*' => 'dd_mul',
1716 '*=' => 'dd_mul_eq',
1717 '!=' => 'dd_neq',
1718 '**' => 'dd_pow',
1719 '**=' => 'dd_pow_eq',
1720 '<=>' => 'dd_spaceship',
1721 '""' => 'dd_stringify',
1722 '0+' => 'dd_numify',
1723 '-' => 'dd_sub',
1724 '-=' => 'dd_sub_eq',
1725 '!' => 'dd_false',
1726 '=' => 'overload_copy',
1727 '++' => 'overload_inc',
1728 '--' => 'overload_dec',
1729 );
1730  
1731 return %h
1732 }
1733  
1734 sub dd_true {
1735 die "Not a Math::FakeDD object passed to dd_true()"
1736 unless ref($_[0]) eq 'Math::FakeDD';
1737  
1738 return 1 if(dd2mpfr(shift)); # Uses Math::MPFR overloading of 'bool'
1739 return 0;
1740 }
1741  
1742 sub dd_false {
1743 die "Not a Math::FakeDD object passed to dd_false()"
1744 unless ref($_[0]) eq 'Math::FakeDD';
1745  
1746 return 1 if !(dd2mpfr(shift)); # Uses Math::MPFR overloading of '!'
1747 return 0;
1748 }
1749  
1750 sub dd_is_inf {
1751 die "Not a Math::FakeDD object passed to dd_is_inf()"
1752 unless ref($_[0]) eq 'Math::FakeDD';
1753  
1754 return 1 if Rmpfr_inf_p(Math::MPFR->new($_[0]->{msd}));
1755 return 0;
1756 }
1757  
1758 sub dd_is_nan {
1759 die "Not a Math::FakeDD object passed to dd_is_nan()"
1760 unless ref($_[0]) eq 'Math::FakeDD';
1761  
1762 return 1 if Rmpfr_nan_p(Math::MPFR->new($_[0]->{msd}));
1763 return 0;
1764 }
1765  
1766 sub dd_inf {
1767 my $inf = Math::MPFR->new();
1768 Rmpfr_set_inf($inf, defined($_[0]) ? shift : 0); # Will be -Inf only if $_[0] < 0
1769 my %h = (msd => Rmpfr_get_d($inf, MPFR_RNDN), lsd => 0.0);
1770 $h{mpfr} = Rmpfr_init2(2098);
1771 Rmpfr_set_d($h{mpfr}, $h{msd}, MPFR_RNDN);
1772 return bless \%h;
1773 }
1774  
1775 sub dd_nan {
1776 my %h = (msd => Rmpfr_get_d(Math::MPFR->new(), MPFR_RNDN), lsd => 0.0);
1777 $h{mpfr} = Rmpfr_init2(2098);
1778 return bless \%h;
1779 }
1780  
1781 sub printx {
1782 print sprintx(shift);
1783 }
1784  
1785 sub sprintx {
1786 if(ref($_[0]) eq 'Math::FakeDD') {
1787 my $self = shift;
1788 return "[" . sprintf("%a", $self->{msd}) . " " . sprintf("%a", $self->{lsd}) . "]";
1789 }
1790 die "Wrong arg given to sprintx()";
1791 }
1792  
1793 sub any2dd {
1794 my $mpfr = Rmpfr_init2(2098);
1795 Rmpfr_set_ui($mpfr, 0, MPFR_RNDN);
1796 for(@_) {
1797 $mpfr += $_;
1798 }
1799 return mpfr2dd($mpfr);
1800 }
1801  
1802 sub unpackx {
1803 if(ref($_[0]) eq 'Math::FakeDD') {
1804 my $self = shift;
1805 return "[" . unpack("H*", pack("d>", $self->{msd})) . " " . unpack("H*", pack("d>", $self->{lsd})) . "]";
1806 }
1807 die "Wrong arg given to unpackx()";
1808 }
1809  
1810 #sub tz_test {
1811 # # Detect any unwanted trailing zeroes
1812 # # in values returned by nvtoa().
1813 #
1814 # my $s = shift;
1815 # my @r = split /e/i, $s;
1816 #
1817 # if(!defined($r[1])) {
1818 # return 1 if $r[0] =~ /\.0$/; # pass
1819 # return 0 if $r[0] =~ /0$/; # fail
1820 # }
1821 #
1822 # return 0 if $r[0] =~ /0$/; # fail (for our formatting convention)
1823 # return 1; # pass
1824 #}
1825  
1826 sub dd_nextup {
1827 die "Arg given to dd_nextup() must be a Math::FakeDD object"
1828 unless ref($_[0]) eq 'Math::FakeDD';
1829 my $dd = shift;
1830 return Math::FakeDD->new(2 ** -1074) if $dd == 0;
1831 return dd_nan() if dd_is_nan($dd);
1832 if(dd_is_inf($dd)) {
1833 return dd_inf() if $dd > 0;
1834 return -$Math::FakeDD::DD_MAX;
1835 }
1836  
1837 # We now need to check the first 12 bits of the lsd
1838 my $is_neg = 0;
1839 my $leading_12_bits = hex(substr(unpack("H*", pack("d>", $dd->{lsd})), 0, 3));
1840 $is_neg = 1 if $leading_12_bits > 2048; # ignore signed zero
1841 my $raw_exponent = $leading_12_bits & 2047;
1842 unless($raw_exponent) {
1843 # The lsd can never be an inf or a nan
1844 # Therefore must be a subnormal (or zero),
1845 # so we return $dd plus DBL_DENORM_MIN
1846 return $dd + DBL_DENORM_MIN;
1847 }
1848  
1849 my $exp = $raw_exponent - 1023 - 52;
1850 my $pow = 2 ** $exp;
1851  
1852 # Check that $dd + $pow > $dd.
1853 while($dd + $pow == $dd) {
1854 $pow *= 2;
1855 }
1856  
1857 my $keep = $pow;
1858  
1859 while($dd + $pow > $dd) {
1860 $keep = $pow;
1861 die "Error (bug) in dd_nextup(" . sprintx($dd) . ")" unless $keep;
1862 $pow /= 2;
1863 }
1864  
1865 return $dd + $keep;
1866 }
1867  
1868 sub dd_nextdown {
1869 die "Arg given to dd_nextdown() must be a Math::FakeDD object"
1870 unless ref($_[0]) eq 'Math::FakeDD';
1871 my $dd = shift;
1872 return Math::FakeDD->new(-(2 ** -1074)) if $dd == 0;
1873 return dd_nan() if dd_is_nan($dd);
1874 if(dd_is_inf($dd)) {
1875 return dd_inf(-1) if $dd < 0;
1876 return $Math::FakeDD::DD_MAX;
1877 }
1878  
1879 # We now need to check the first 12 bits of the lsd
1880 my $is_neg = 0;
1881 my $leading_12_bits = hex(substr(unpack("H*", pack("d>", $dd->{lsd})), 0, 3));
1882 $is_neg = 1 if $leading_12_bits > 2048; # ignore signed zero
1883 my $raw_exponent = $leading_12_bits & 2047;
1884 unless($raw_exponent) {
1885 # The lsd can never be an inf or a nan.
1886 # Therefore must be a subnormal (or zero),
1887 # so we return $dd minus DBL_DENORM_MIN
1888 return $dd - DBL_DENORM_MIN;
1889 }
1890  
1891 my $exp = $raw_exponent - 1023 - 52;
1892 my $pow = 2 ** $exp;
1893  
1894 # Check that $dd - $pow < $dd.
1895 while($dd - $pow == $dd) {
1896 $pow *= 2;
1897 }
1898  
1899 my $keep = $pow;
1900  
1901 while($dd - $pow < $dd) {
1902 $keep = $pow;
1903 die "Error (bug) in dd_nextup(" . sprintx($dd) . ")" unless $keep;
1904 $pow /= 2;
1905 }
1906  
1907 return $dd - $keep;
1908 }
1909  
1910 sub ulp_exponent {
1911 my $dd = shift;
1912 my $exp;
1913 if($_[0]) {
1914 # If a second (and true) argument was provided
1915 $exp = hex(substr(unpack("H*", pack("d>", $dd->{msd})), 0, 3)) & 2047;
1916 }
1917 else {
1918 $exp = hex(substr(unpack("H*", pack("d>", $dd->{lsd})), 0, 3)) & 2047;
1919 }
1920 return $exp - 1075 if $exp;
1921 return -1074;
1922 }
1923  
1924 sub is_subnormal {
1925 # Takes an NV as its argument.
1926 my $d = shift;
1927 die "Bad argument passed to is_subnormal()"
1928 unless Math::MPFR::_itsa($d) == 3;
1929 # NOTE: # We return 1 if $d == 0.
1930 return 1 if((hex(substr(unpack("H*", pack("d>", $d)), 0, 3)) & 2047) == 0);
1931 return 0;
1932 }
1933  
1934 sub _is_nan {
1935 # Test with a copy of the arg to
1936 # avoid altering the arg's numeric flags
1937 my $t = shift;
1938 return 1 if $t != $t;
1939 return 0;
1940 }
1941  
1942 sub _is_inf {
1943 # Test with a copy of the arg to
1944 # avoid altering the arg's numeric flags
1945 my $t = shift;
1946 return 1 if $t && $t / $t != 1;
1947 return 0;
1948 }
1949  
1950 sub _return_inf {
1951 my ($msd, $lsd) = (shift, shift);
1952 return 1 if(abs($msd) == M_FDD_DBL_MAX && abs($lsd) == M_FDD_P2_970);
1953 return 0;
1954 }
1955  
1956 1;
↑ Summary FakeDD.xs »