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