blib/lib/Math/NV.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 NV.xs »
1 ## This file generated by InlineX::C2XS (version 0.22) using Inline::C (version 0.53)
2 package Math::NV;
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Math::MPFR qw(:mpfr);
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use 5.010;
7  
8 require Exporter;
9 *import = \&Exporter::import;
10 require DynaLoader;
11  
12 $Math::NV::VERSION = '2.02';
13  
14 DynaLoader::bootstrap Math::NV $Math::NV::VERSION;
15  
16 @Math::NV::EXPORT = ();
17 @Math::NV::EXPORT_OK = qw(
18 nv nv_type mant_dig ld2binary ld_str2binary is_eq
19 bin2val Cprintf Csprintf nv_mpfr is_eq_mpfr
20 set_C set_mpfr is_inexact
21 );
22  
23 %Math::NV::EXPORT_TAGS = (all => [qw(
24 nv nv_type mant_dig ld2binary ld_str2binary is_eq
25 bin2val Cprintf Csprintf nv_mpfr is_eq_mpfr
26 set_C set_mpfr is_inexact
27 )]);
28  
29 if($Math::MPFR::VERSION < 4.07) {
30 die " Math-MPFR version needs to be 4.07 or later\n This is only Math-MPFR-$Math::MPFR::VERSION\n";
31 }
32  
33 ## max NV finite values ##
34 # double : 1.7976931348623157e+308
35 #long double: 1.18973149535723176502e4932
36 # __float128: 1.18973149535723176508575932662800702e4932
37  
38 ## normal min values ##
39 # double : (2 ** - 1022) : 0.1E-1021 : 2.2250738585072014e-308
40 # long double: (2 ** -16382) : 0.1E-16381 : 3.36210314311209350626e-4932
41 # __float128 : (2 ** -16382) : 0.1E-16381 : 3.36210314311209350626267781732175260e-4932
42  
43 $Math::NV::DBL_MIN = Math::MPFR->new(2 ** -1022);
44 $Math::NV::LDBL_MIN = Math::MPFR->new(2 ** -16382);
45 $Math::NV::FLT128_MIN = $Math::NV::LDBL_MIN;
46  
47 ## denorm_min values ##
48 # double : (2 ** -1074) : 0.1E-1073 : 4.9406564584124654e-324
49 # long double: (2 ** -16445) : 0.1E-16444 : 3.64519953188247460253e-4951
50 # __float128 : (2 ** -16494) : 0.1E-16493 : 6.47517511943802511092443895822764655e-4966
51  
52 $Math::NV::DBL_DENORM_MIN = Math::MPFR->new(2);
53 Rmpfr_div_2ui($Math::NV::DBL_DENORM_MIN, $Math::NV::DBL_DENORM_MIN, 1075, 0); # (2 ** -1074)
54 $Math::NV::LDBL_DENORM_MIN = Math::MPFR->new(2);
55 Rmpfr_div_2ui($Math::NV::LDBL_DENORM_MIN, $Math::NV::LDBL_DENORM_MIN, 16446, 0); # (2 ** -16445)
56 $Math::NV::FLT128_DENORM_MIN = Math::MPFR->new(2);
57 Rmpfr_div_2ui($Math::NV::FLT128_DENORM_MIN, $Math::NV::FLT128_DENORM_MIN, 16495, 0); # (2 ** -16494)
58  
59 $Math::NV::DBL_DENORM_MIN_MIN = Math::MPFR->new();
60 $Math::NV::LDBL_DENORM_MIN_MIN = Math::MPFR->new();
61 $Math::NV::FLT128_DENORM_MIN_MIN = Math::MPFR->new();
62  
63 # For all x, DENORM_MIN_MIN < x < DENORM_MIN, x should round to DENORM_MIN when subnormalized.
64 # For all x, x <= DENORM_MIN_MIN, x is subnormalized to 0.
65  
66 Rmpfr_div_2ui($Math::NV::DBL_DENORM_MIN_MIN, $Math::NV::DBL_DENORM_MIN, 1, MPFR_RNDN); # (2 ** -1075)
67 Rmpfr_div_2ui($Math::NV::LDBL_DENORM_MIN_MIN, $Math::NV::LDBL_DENORM_MIN, 1, MPFR_RNDN); # (2 ** -16446)
68 Rmpfr_div_2ui($Math::NV::FLT128_DENORM_MIN_MIN, $Math::NV::FLT128_DENORM_MIN, 1, MPFR_RNDN); # (2 ** -16495)
69  
70 %Math::NV::DENORM_MIN = ('0' => Math::MPFR->new(0),
71 '53' => $Math::NV::DBL_DENORM_MIN,
72 '64' => $Math::NV::LDBL_DENORM_MIN,
73 '106' => $Math::NV::DBL_DENORM_MIN,
74 '113' => $Math::NV::FLT128_DENORM_MIN,
75 '53MIN' => $Math::NV::DBL_DENORM_MIN_MIN,
76 '64MIN' => $Math::NV::LDBL_DENORM_MIN_MIN,
77 '106MIN' => $Math::NV::DBL_DENORM_MIN_MIN,
78 '113MIN' => $Math::NV::FLT128_DENORM_MIN_MIN,
79 );
80  
81 if(Math::MPFR::_ld_subnormal_bug()) {
82 $Math::NV::_ld_subnormal_bug = 1;
83 }
84 else {
85 $Math::NV::_ld_subnormal_bug = 0;
86 }
87  
88 # With mpfr-3.1.5 and earlier, the ternary value returned
89 # by mpfr_strtofr is unreliable - thereby making that function
90 # unusable with mpfr_subnormalize.
91 $Math::NV::mpfr_strtofr_bug = MPFR_VERSION() <= 196869 ? 1 : 0;
92  
93 $Math::NV::no_warn = 0; # set to 1 to disable warning about non-string argument
94 # set to 2 to disable output of the 2 non-matching values
95 # set to 3 to disable both of the above
96  
97 # %_itsa is utilised in the formulation of the diagnostic message
98 # when it's detected that the provided arg is not a string.
99  
100 my %_itsa = (
101 1 => 'UV',
102 2 => 'IV',
103 3 => 'NV',
104 4 => 'string',
105 0 => 'unknown',
106 );
107  
108 sub dl_load_flags {0} # Prevent DynaLoader from complaining and croaking
109  
110 sub ld2binary {
111 my @ret = _ld2binary($_[0]);
112 my $prec = pop(@ret);
113 my $exp = pop(@ret);
114 my $mantissa = join '', @ret;
115 return ($mantissa, $exp, $prec);
116 }
117  
118 sub ld_str2binary {
119 my @ret = _ld_str2binary($_[0]);
120 my $prec = pop(@ret);
121 my $exp = pop(@ret);
122 my $mantissa = join '', @ret;
123 return ($mantissa, $exp, $prec);
124 }
125  
126 sub bin2val {
127 my($mantissa, $exp, $prec) = (shift, shift, shift);
128 my $sign = $mantissa =~ /^\-/ ? '-' : '';
129 # Remove everything upto and including the radix point
130 # as it now contains no useful information.
131 $mantissa =~ s/.+\.//;
132 # For our purposes the values $prec and $exp need
133 # to be reduced by 1.
134 $exp--;
135  
136 # Perl bugs make the following (commented out) code unreliable,
137 # so we now hand the calculations over to C.
138 # (And there's no need to decrement $prec.)
139 #$prec--;
140 #for(0..$prec) {
141 # if(substr($mantissa, $_, 1)) {$val += 2**$exp}
142 # $exp--;
143 #}
144 my @mantissa = split //, $mantissa;
145 my $val = _bin2val($prec, $exp, \@mantissa);
146 $sign eq '-' ? return -$val : return $val;
147 }
148  
149 sub is_eq {
150 unless($Math::NV::no_warn & 1) {
151 my $itsa = $_[0];
152 $itsa = _itsa($itsa); # make sure that $_[0] has POK flag set && all numeric flags unset
153 warn "Argument given to is_eq() is $_itsa{$itsa}, not a string - probably not what you want"
154 if $itsa != 4;
155 }
156 my $nv = $_[0];
157 my $check = nv($_[0]);
158 return 1 if $nv == $check;
159 unless($Math::NV::no_warn & 2) {
160 if(mant_dig() == 64) {
161 # pack/unpack like to deliver irrelevant (ie ignored) leading bytes
162 # if NV is 80-bit long double
163 my $first = scalar(reverse(unpack("h*", pack("F<", $nv))));
164 $first = substr($first, length($first) - 20, 20);
165 my $second = scalar(reverse(unpack("h*", pack("F<", $check))));
166 $second = substr($second, length($second) - 20, 20);
167 warn "\nIn is_eq:\nperl: $first vs C: $second\n";
168 if($] > 5.02) {
169 warn "perl: ", sprintf("%a", $nv), " vs mpfr: ", sprintf("%a", $check), "\n";
170 }
171 }
172 else {
173 warn "\nIn is_eq:\nperl: ",
174 scalar(reverse(unpack("h*", pack("F<", $nv)))), " vs C: ",
175 scalar(reverse(unpack("h*", pack("F<", $check)))), "\n";
176 if($] > 5.02) {
177 warn "perl: ", sprintf("%a", $nv), " vs mpfr: ", sprintf("%a", $check), "\n";
178 }
179 }
180 }
181 return 0;
182 }
183  
184 sub is_eq_mpfr {
185  
186 unless($Math::NV::no_warn & 1) {
187 my $itsa = $_[0];
188 $itsa = _itsa($itsa); # make sure that $_[0] has POK flag set && all numeric flags unset
189 warn "Argument given to is_eq() is $_itsa{$itsa}, not a string - probably not what you want"
190 if $itsa != 4;
191 }
192  
193 my $fr;
194 my $nv = $_[0];
195 my $bits = mant_dig();
196 $bits = 2098 if $bits == 106;
197  
198 if($bits == 2098) {
199 $fr = Rmpfr_init2($bits);
200 Rmpfr_strtofr($fr, $nv, 0, 0);
201 }
202 else { # OPEN ELSE 1
203  
204 $fr = Rmpfr_init2($bits);
205 my $inex = Rmpfr_strtofr($fr, $nv, 0, 0);
206  
207 unless($Math::NV::mpfr_strtofr_bug) {
208 $fr = _subnormalize($_[0], $bits);
209 }
210 else {
211 $fr = Rmpfr_init2($bits);
212 Rmpfr_strtofr($fr, $nv, 0, 0);
213 my $fr_bits = get_relevant_prec($fr); # check for subnormality
214  
215 Rmpfr_set($fr, get_subnormal($_[0], $fr_bits, $bits, $fr), 0);
216 }
217 } # CLOSE ELSE1
218  
219 if($nv == Rmpfr_get_NV($fr, 0)) {return 1}
220  
221 # Values don't match
222  
223 unless($Math::NV::no_warn & 2) {
224 if($bits == 64) {
225 # pack/unpack like to deliver irrelevant (ie ignored) leading bytes
226 # if NV is 80-bit long double
227 my $first = scalar(reverse(unpack("h*", pack("F<", $nv))));
228 $first = substr($first, length($first) - 20, 20);
229 my $second = scalar(reverse(unpack("h*", pack("F<", Rmpfr_get_NV($fr, 0)))));
230 $second = substr($second, length($second) - 20, 20);
231 warn "\nIn is_eq_mpfr: $_[0]\nperl: $first vs mpfr: $second\n";
232 if($] > 5.02) {
233 warn "perl: ", sprintf("%a", $nv), " vs mpfr: ", sprintf("%a", Rmpfr_get_NV($fr, 0)), "\n";
234 }
235 }
236 else {
237 warn "\nIn is_eq_mpfr: $_[0]\nperl: ",
238 scalar(reverse(unpack("h*", pack("F<", $nv)))), " vs mpfr: ",
239 scalar(reverse(unpack("h*", pack("F<", Rmpfr_get_NV($fr, 0))))), "\n";
240 if($] > 5.02) {
241 warn "perl: ", sprintf("%a", $nv), " vs mpfr: ", sprintf("%a", Rmpfr_get_NV($fr, 0)), "\n";
242 }
243 }
244 }
245 return 0;
246  
247 }
248  
249 sub nv_mpfr {
250  
251 unless($Math::NV::no_warn & 1) {
252 my $itsa = $_[0];
253 $itsa = _itsa($itsa); # make sure that $_[0] has POK flag set && all numeric flags unset
254 warn "Argument given to is_eq() is $_itsa{$itsa}, not a string - probably not what you want"
255 if $itsa != 4;
256 }
257  
258 my($val, $bits);
259  
260 $bits = defined($_[1]) ? $_[1] : mant_dig();
261  
262 return _double_double($_[0]) if $bits == 106; # doubledouble
263  
264 if($bits == mant_dig() ) { # 53, 64 or 113 bits
265  
266 unless($Math::NV::mpfr_strtofr_bug) {
267 $val = _subnormalize($_[0], $bits);
268 }
269 else { # ELSE1
270 $val = Rmpfr_init2($bits);
271 Rmpfr_strtofr($val, $_[0], 0, 0);
272 my $val_bits = get_relevant_prec($val); # check for subnormality.
273  
274 Rmpfr_set($val, get_subnormal($_[0], $val_bits, $bits, $val), MPFR_RNDN);
275  
276 } # ELSE1
277  
278 my $nv = Rmpfr_get_NV($val, 0);
279 my $ret = scalar(reverse(unpack("h*", pack("F<", $nv))));
280  
281 return $ret;
282 }
283  
284 if($bits == 53) {
285  
286 unless($Math::NV::mpfr_strtofr_bug) {
287 $val = _subnormalize($_[0], 53);
288 }
289 else { # ELSE1
290 $val = Rmpfr_init2($bits);
291 Rmpfr_strtofr($val, $_[0], 0, 0);
292 my $val_bits = get_relevant_prec($val); # check for subnormality.
293  
294 Rmpfr_set($val, get_subnormal($_[0], $val_bits, $bits, $val), MPFR_RNDN);
295  
296 } # ELSE1
297  
298 my $nv = Rmpfr_get_d($val, 0);
299 return scalar(reverse(unpack("h*", pack("d<", $nv))));
300 }
301  
302 if($bits == 64) {
303 my @bytes = Math::MPFR::_ld_bytes($_[0], 64);
304 return join('', @bytes);
305 }
306  
307 if($bits == 113) {
308  
309 my $t;
310 eval{$t = Math::MPFR::_have_IEEE_754_long_double();}; # needs Math-MPFR-3.33, perl-5.22.
311 if(!$@ && $t) {
312 my @bytes = Math::MPFR::_ld_bytes($_[0], 113);
313 return join('', @bytes);
314 }
315 else { # assume __float128 (though that might not be the case)
316 my @bytes = Math::MPFR::_f128_bytes($_[0], 113);
317 return join('', @bytes);
318 }
319 }
320  
321 die "Unrecognized value for bits ($bits)";
322 }
323  
324 sub set_mpfr {
325  
326 unless($Math::NV::no_warn & 1) {
327 my $itsa = $_[0];
328 $itsa = _itsa($itsa); # make sure that $_[0] has POK flag set && all numeric flags unset
329 warn "Argument given to is_eq() is $_itsa{$itsa}, not a string - probably not what you want"
330 if $itsa != 4;
331 }
332  
333 my $bits = mant_dig();
334 $bits = 2098 if $bits == 106;
335  
336 my $val;
337  
338 # my $val = Rmpfr_init2($bits);
339 # my $inex = Rmpfr_strtofr($val, $_[0], 0, 0);
340  
341 if($bits == 2098) {
342 $val = Rmpfr_init2(2098);
343 Rmpfr_strtofr($val, $_[0], 0, 0);
344 return Rmpfr_get_ld($val, 0);
345 }
346  
347 die "In set_mpfr: unrecognized nv precision of $bits bits"
348 unless($bits == 53 || $bits == 64 || $bits == 113);
349  
350 unless($Math::NV::mpfr_strtofr_bug) {
351 $val = _subnormalize($_[0], $bits);
352 }
353 else { # ELSE1
354 $val = Rmpfr_init2($bits);
355 Rmpfr_strtofr($val, $_[0], 0, 0);
356 my $val_bits = get_relevant_prec($val); # check for subnormality.
357  
358 Rmpfr_set($val, get_subnormal($_[0], $val_bits, $bits, $val), MPFR_RNDN);
359  
360 } # ELSE1
361  
362 return Rmpfr_get_NV($val, MPFR_RNDN);
363  
364 }
365  
366 sub is_inexact {
367  
368 die "is_inexact() requires at least mpfr-3.1.6"
369 if $Math::NV::mpfr_strtofr_bug;
370  
371 unless($Math::NV::no_warn & 1) {
372 my $itsa = $_[0];
373 $itsa = _itsa($itsa); # make sure that $_[0] has POK flag set && all numeric flags unset
374 warn "Argument given to is_inexact() is $_itsa{$itsa}, not a string - possibly not what you want"
375 if $itsa != 4;
376 }
377  
378 my $bits = mant_dig();
379 $bits = 2098 if $bits == 106;
380  
381 my $val = Rmpfr_init2($bits);
382  
383 my $inex = Rmpfr_strtofr($val, $_[0], 0, 0);
384 my $nv = atonv($_[0]);
385  
386 my $cmp = Rmpfr_cmp_NV($val, $nv) * -1;
387  
388 return $inex if !$cmp;
389 return $cmp;
390 }
391  
392 sub set_C {
393 unless($Math::NV::no_warn & 1) {
394 my $itsa = $_[0];
395 $itsa = _itsa($itsa); # make sure that $_[0] has POK flag set && all numeric flags unset
396 warn "Argument given to is_eq() is $_itsa{$itsa}, not a string - probably not what you want"
397 if $itsa != 4;
398 }
399 return _set_C($_[0]);
400 }
401  
402  
403 sub _double_double {
404 my $val = Rmpfr_init2(2098);
405 Rmpfr_set_str($val, shift, 0, 0);
406 my @val = _dd_obj($val);
407 return [scalar(reverse(unpack("h*", pack("d<", $val[0])))),
408 scalar(reverse(unpack("h*", pack("d<", $val[1]))))];
409 }
410  
411 sub _dd_obj {
412 my $obj = shift;
413 my $msd = Rmpfr_get_d($obj, 0);
414 if($msd == 0 || $msd != $msd || $msd / $msd != 1) {return ($msd, 0.0)} # it's inf, nan or zero.
415 $obj -= $msd;
416 return ($msd, Rmpfr_get_d($obj, 0));
417 }
418  
419 # use _subnormalize instead if MPFR_VERSION > 196869
420 sub get_subnormal {
421  
422 my($str, $prec, $bits) = (shift, shift, shift);
423  
424 my $signbit = Rmpfr_signbit($_[0]) ? -1 : 1;
425  
426 # If $prec < 0, set $val to (appropriately signed) 0.
427 if($prec < 0) {
428 return $Math::NV::DENORM_MIN{'0'} * $signbit;
429 }
430  
431 # If prec == 0, then the value is less than the
432 # minimum subnormal number.
433 if($prec == 0) {
434 return $Math::NV::DENORM_MIN{$bits} * $signbit if abs($_[0]) > $Math::NV::DENORM_MIN{"${bits}MIN"};
435 return $Math::NV::DENORM_MIN{'0'} * $signbit;
436 }
437  
438 # Can't set precision to 1 bit with
439 # older versions of the mpfr library
440 if($prec == 1) {
441 return ($Math::NV::DENORM_MIN{$bits} * $signbit * 2) if(abs($_[0]) >= $Math::NV::DENORM_MIN{"${bits}MIN"}
442 + $Math::NV::DENORM_MIN{$bits});
443 return $Math::NV::DENORM_MIN{$bits} * $signbit;
444 }
445  
446 my $val = Rmpfr_init2($prec);
447 Rmpfr_set_str($val, $str, 0, 0);
448 return $val;
449 }
450  
451 sub get_relevant_prec {
452 my $bits = Rmpfr_get_prec($_[0]);
453 die "Unrecognized precision ($bits) handed to get_relevant_prec()"
454 unless ($bits == 53 || $bits == 64 || $bits == 113 || $bits == 106 || $bits == 2098);
455  
456 my $init = $bits == 53 || $bits == 106 || $bits == 2098 ? 1074
457 : $bits == 64 ? 16445
458 : 16494;
459  
460 return $init + Rmpfr_get_exp($_[0]);
461  
462 }
463  
464 # use get_subnormal instead if MPFR_VERSION <= 196869
465 sub _subnormalize {
466 # Called as: $val = _subnormalize($string, $bits);
467 # mpfr_subnormalize(fr, inex, MPFR_RNDN);
468  
469 my $emin = Rmpfr_get_emin();
470 my $emax = Rmpfr_get_emax();
471  
472 # Default precision shouldn't matter as we're
473 # specifying precision of $val correctly.
474 # my $original_prec = Rmpfr_get_default_prec();
475  
476 my $sub_emin = $_[1] == 53 ? -1073
477 : $_[1] == 64 ? -16444
478 : -16493; # $_[1] == 113
479  
480 my $sub_emax = $_[1] == 53 ? 1024
481 : 16384;
482  
483 # Rmpfr_set_default_prec($_[1]);
484  
485 Rmpfr_set_emin($sub_emin);
486 Rmpfr_set_emax($sub_emax);
487  
488 my $val = Rmpfr_init2($_[1]);
489 my $inex = Rmpfr_strtofr($val, $_[0], 0, 0);
490  
491 Rmpfr_subnormalize($val, $inex, 0);
492  
493 Rmpfr_set_emin($emin);
494 Rmpfr_set_emax($emax);
495 # Rmpfr_set_default_prec($original_prec);
496  
497 return $val;
498 }
499  
500 1;
501  
502 __END__
↑ Summary NV.xs »