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