Ticket #13033: 13033_pari_to_mpfr.patch
File 13033_pari_to_mpfr.patch, 12.6 KB (added by , 10 years ago) |
---|
-
sage/rings/real_mpfi.pyx
# HG changeset patch # User Jeroen Demeyer <jdemeyer@cage.ugent.be> # Date 1338169914 25200 # Node ID dfd3b8044a4d904b60bba6db3a89111290cbc657 # Parent 8709b976e906b35b6174d82ea0c3776d2f36e24a Increase exponent range of real numbers to the maximal possible range diff --git a/sage/rings/real_mpfi.pyx b/sage/rings/real_mpfi.pyx
a b 1 1 """ 2 Field ofArbitrary Precision Real Intervals2 Arbitrary Precision Real Intervals 3 3 4 4 AUTHORS: 5 5 … … 2122 2122 sage: a.fp_rank_diameter() 2123 2123 30524 2124 2124 sage: (RIF(sqrt(2)) - RIF(sqrt(2))).fp_rank_diameter() 2125 9671406088542672151117826 2126 2125 9671406088542672151117826 # 32-bit 2126 41538374868278620559869609387229186 # 64-bit 2127 2127 2128 Just because we have the best possible interval, doesn't mean the 2128 2129 interval is actually small:: 2129 2130 sage: a = RIF(pi)^1234567890; a 2131 [2.0985787164673874e323228496 .. +infinity] 2130 2131 sage: a = RIF(pi)^12345678901234567890; a 2132 [2.0985787164673874e323228496 .. +infinity] # 32-bit 2133 [5.8756537891115869e1388255822130839282 .. +infinity] # 64-bit 2132 2134 sage: a.fp_rank_diameter() 2133 2135 1 2134 2136 """ 2135 2137 return self.lower().fp_rank_delta(self.upper()) 2136 2138 2137 2139 def is_exact(self): 2138 return mpfr_equal_p(&self.value.left, &self.value.right) 2140 return mpfr_equal_p(&self.value.left, &self.value.right) 2139 2141 2140 2142 def magnitude(self): 2141 2143 """ -
sage/rings/real_mpfr.pyx
diff --git a/sage/rings/real_mpfr.pyx b/sage/rings/real_mpfr.pyx
a b 1 1 r""" 2 Field ofArbitrary Precision Real Numbers2 Arbitrary Precision Real Numbers 3 3 4 4 AUTHORS: 5 5 … … 24 24 25 25 - Robert Bradshaw (2009-09): decimal literals, optimizations 26 26 27 - Jeroen Demeyer (2012-05-27): set the MPFR exponent range to the 28 maximal possible value (:trac:`13033`) 29 27 30 This is a binding for the MPFR arbitrary-precision floating point 28 31 library. 29 32 … … 35 38 In Sage (as in MPFR), floating-point numbers of precision 36 39 `p` are of the form `s m 2^{e-p}`, where 37 40 `s \in \{-1, 1\}`, `2^{p-1} \leq m < 2^p`, and 38 `-2^ {30} + 1 \leq e \leq 2^{30} - 1`; plus the special39 values ``+0``, ``-0``, 40 ``+infinity``, ``-infinity``, and 41 `` NaN`` (which stands for Not-a-Number).41 `-2^B + 1 \leq e \leq 2^B - 1` where `B = 30` on 32-bit systems 42 and `B = 62` on 64-bit systems; 43 additionally, there are the special values ``+0``, ``-0``, 44 ``+infinity``, ``-infinity`` and ``NaN`` (which stands for Not-a-Number). 42 45 43 46 Operations in this module which are direct wrappers of MPFR 44 47 functions are "correctly rounded"; we briefly describe what this … … 180 183 181 184 def mpfr_prec_min(): 182 185 """ 183 Return the mpfr variable MPFR_PREC_MIN. 186 Return the mpfr variable ``MPFR_PREC_MIN``. 187 188 EXAMPLES:: 189 190 sage: from sage.rings.real_mpfr import mpfr_prec_min 191 sage: mpfr_prec_min() 192 2 193 sage: R = RealField(2) 194 sage: R(2) + R(1) 195 3.0 196 sage: R(4) + R(1) 197 4.0 198 sage: R = RealField(1) 199 Traceback (most recent call last): 200 ... 201 ValueError: prec (=1) must be >= 2 and <= 2147483391 184 202 """ 185 203 return MPFR_PREC_MIN 186 204 … … 199 217 sage: R = RealField(2^31-256) 200 218 Traceback (most recent call last): 201 219 ... 202 ValueError: prec (=2147483392) must be >= 2 and <= 2147483391 .220 ValueError: prec (=2147483392) must be >= 2 and <= 2147483391 203 221 """ 204 222 global MY_MPFR_PREC_MAX 205 223 return MY_MPFR_PREC_MAX 206 224 225 def mpfr_get_exp_min(): 226 """ 227 Return the current minimal exponent for MPFR numbers. 228 229 EXAMPLES:: 230 231 sage: from sage.rings.real_mpfr import mpfr_get_exp_min 232 sage: mpfr_get_exp_min() 233 -1073741823 # 32-bit 234 -4611686018427387903 # 64-bit 235 sage: 0.5 >> (-mpfr_get_exp_min()) 236 2.38256490488795e-323228497 # 32-bit 237 8.50969131174084e-1388255822130839284 # 64-bit 238 sage: 0.5 >> (-mpfr_get_exp_min()+1) 239 0.000000000000000 240 """ 241 return mpfr_get_emin() 242 243 def mpfr_get_exp_max(): 244 """ 245 Return the current maximal exponent for MPFR numbers. 246 247 EXAMPLES:: 248 249 sage: from sage.rings.real_mpfr import mpfr_get_exp_max 250 sage: mpfr_get_exp_max() 251 1073741823 # 32-bit 252 4611686018427387903 # 64-bit 253 sage: 0.5 << mpfr_get_exp_max() 254 1.04928935823369e323228496 # 32-bit 255 2.93782689455579e1388255822130839282 # 64-bit 256 sage: 0.5 << (mpfr_get_exp_max()+1) 257 +infinity 258 """ 259 return mpfr_get_emax() 260 261 def mpfr_set_exp_min(mp_exp_t e): 262 """ 263 Set the minimal exponent for MPFR numbers. 264 265 EXAMPLES:: 266 267 sage: from sage.rings.real_mpfr import mpfr_get_exp_min, mpfr_set_exp_min 268 sage: old = mpfr_get_exp_min() 269 sage: mpfr_set_exp_min(-1000) 270 sage: 0.5 >> 1000 271 4.66631809251609e-302 272 sage: 0.5 >> 1001 273 0.000000000000000 274 sage: mpfr_set_exp_min(old) 275 sage: 0.5 >> 1001 276 2.33315904625805e-302 277 """ 278 if mpfr_set_emin(e) != 0: 279 raise OverflowError("bad value for mpfr_set_exp_min()") 280 281 def mpfr_set_exp_max(mp_exp_t e): 282 """ 283 Set the maximal exponent for MPFR numbers. 284 285 EXAMPLES:: 286 287 sage: from sage.rings.real_mpfr import mpfr_get_exp_max, mpfr_set_exp_max 288 sage: old = mpfr_get_exp_max() 289 sage: mpfr_set_exp_max(1000) 290 sage: 0.5 << 1000 291 5.35754303593134e300 292 sage: 0.5 << 1001 293 +infinity 294 sage: mpfr_set_exp_max(old) 295 sage: 0.5 << 1001 296 1.07150860718627e301 297 """ 298 if mpfr_set_emax(e) != 0: 299 raise OverflowError("bad value for mpfr_set_exp_max()") 300 301 def mpfr_get_exp_min_min(): 302 """ 303 Get the minimal value allowed for :func:`mpfr_set_exp_min`. 304 305 EXAMPLES:: 306 307 sage: from sage.rings.real_mpfr import mpfr_get_exp_min_min, mpfr_set_exp_min 308 sage: mpfr_get_exp_min_min() 309 -1073741823 # 32-bit 310 -4611686018427387903 # 64-bit 311 312 This is really the minimal value allowed:: 313 314 sage: mpfr_set_exp_min(mpfr_get_exp_min_min() - 1) 315 Traceback (most recent call last): 316 ... 317 OverflowError: bad value for mpfr_set_exp_min() 318 """ 319 return mpfr_get_emin_min() 320 321 def mpfr_get_exp_max_max(): 322 """ 323 Get the maximal value allowed for :func:`mpfr_set_exp_max`. 324 325 EXAMPLES:: 326 327 sage: from sage.rings.real_mpfr import mpfr_get_exp_max_max, mpfr_set_exp_max 328 sage: mpfr_get_exp_max_max() 329 1073741823 # 32-bit 330 4611686018427387903 # 64-bit 331 332 This is really the maximal value allowed:: 333 334 sage: mpfr_set_exp_max(mpfr_get_exp_max_max() + 1) 335 Traceback (most recent call last): 336 ... 337 OverflowError: bad value for mpfr_set_exp_max() 338 """ 339 return mpfr_get_emax_max() 340 341 # On Sage startup, set the exponent range to the maximum allowed 342 mpfr_set_exp_min(mpfr_get_emin_min()) 343 mpfr_set_exp_max(mpfr_get_emax_max()) 344 207 345 #***************************************************************************** 208 346 # 209 347 # Real Field 210 348 # 211 349 #***************************************************************************** 212 # The real field is in Pyrex, so mpfr elements will have access to350 # The real field is in Cython, so mpfr elements will have access to 213 351 # their parent via direct C calls, which will be faster. 214 352 215 353 _rounding_modes = ['RNDN', 'RNDZ', 'RNDU', 'RNDD'] … … 295 433 global MY_MPFR_PREC_MAX 296 434 cdef RealNumber rn 297 435 if prec < MPFR_PREC_MIN or prec > MY_MPFR_PREC_MAX: 298 raise ValueError, "prec (=%s) must be >= %s and <= %s ."%(436 raise ValueError, "prec (=%s) must be >= %s and <= %s"%( 299 437 prec, MPFR_PREC_MIN, MY_MPFR_PREC_MAX) 300 438 self.__prec = prec 301 439 if not isinstance(rnd, str): … … 1218 1356 1219 1357 mpz_import(mantissa, lg(g) - 2, 1, wordsize/8, 0, 0, &g[2]) 1220 1358 1221 cdef int exponent1359 cdef mp_exp_t exponent 1222 1360 exponent = expo(g) 1223 1361 1224 1362 # Round to nearest for best results when setting a low-precision … … 1751 1889 sage: RR(0).nextbelow().nextbelow().fp_rank() 1752 1890 -2 1753 1891 sage: RR(1).fp_rank() 1754 4835703278458516698824705 1892 4835703278458516698824705 # 32-bit 1893 20769187434139310514121985316880385 # 64-bit 1755 1894 sage: RR(-1).fp_rank() 1756 -4835703278458516698824705 1895 -4835703278458516698824705 # 32-bit 1896 -20769187434139310514121985316880385 # 64-bit 1757 1897 sage: RR(1).fp_rank() - RR(1).nextbelow().fp_rank() 1758 1898 1 1759 1899 sage: RR(-infinity).fp_rank() 1760 -9671406552413433770278913 1900 -9671406552413433770278913 # 32-bit 1901 -41538374868278621023740371006390273 # 64-bit 1761 1902 sage: RR(-infinity).fp_rank() - RR(-infinity).nextabove().fp_rank() 1762 1903 -1 1763 1904 """ … … 1766 1907 1767 1908 cdef Integer z = PY_NEW(Integer) 1768 1909 1769 cdef mp_exp_t EXP_MIN = -(1<<30) + 11770 cdef mp_exp_t EXP_MAX = (1<<30) - 11910 cdef mp_exp_t EXP_MIN = mpfr_get_exp_min() 1911 cdef mp_exp_t EXP_MAX = mpfr_get_exp_max() 1771 1912 # fp_rank(0.0) = 0 1772 1913 # fp_rank(m*2^e-p) = (m-2^{p-1})+(e-EXP_MIN)*2^{p-1}+1 1773 1914 # = m+(e-EXP_MIN-1)*2^{p-1}+1 … … 1837 1978 There are lots of floating-point numbers around 0:: 1838 1979 1839 1980 sage: R2(-1).fp_rank_delta(R2(1)) 1840 4294967298 1841 """1842 1981 4294967298 # 32-bit 1982 18446744073709551618 # 64-bit 1983 """ 1843 1984 # We create the API for forward compatibility, because it can have 1844 1985 # a (somewhat) more efficient implementation than this; but for now, 1845 1986 # we just go with the stupid implementation. … … 2348 2489 sage: (1.0).nexttoward(RR('-infinity')).str(truncate=False) 2349 2490 '0.99999999999999989' 2350 2491 sage: RR(infinity).nexttoward(0) 2351 2.09857871646739e323228496 2492 2.09857871646739e323228496 # 32-bit 2493 5.87565378911159e1388255822130839282 # 64-bit 2352 2494 sage: RR(pi).str(truncate=False) 2353 2495 '3.1415926535897931' 2354 2496 sage: RR(pi).nexttoward(22/7).str(truncate=False) … … 2376 2518 EXAMPLES:: 2377 2519 2378 2520 sage: RR('-infinity').nextabove() 2379 -2.09857871646739e323228496 2521 -2.09857871646739e323228496 # 32-bit 2522 -5.87565378911159e1388255822130839282 # 64-bit 2380 2523 sage: RR(0).nextabove() 2381 2.38256490488795e-323228497 2524 2.38256490488795e-323228497 # 32-bit 2525 8.50969131174084e-1388255822130839284 # 64-bit 2382 2526 sage: RR('+infinity').nextabove() 2383 2527 +infinity 2384 2528 sage: RR(-sqrt(2)).str(truncate=False) … … 2402 2546 sage: RR('-infinity').nextbelow() 2403 2547 -infinity 2404 2548 sage: RR(0).nextbelow() 2405 -2.38256490488795e-323228497 2549 -2.38256490488795e-323228497 # 32-bit 2550 -8.50969131174084e-1388255822130839284 # 64-bit 2406 2551 sage: RR('+infinity').nextbelow() 2407 2.09857871646739e323228496 2552 2.09857871646739e323228496 # 32-bit 2553 5.87565378911159e1388255822130839282 # 64-bit 2408 2554 sage: RR(-sqrt(2)).str(truncate=False) 2409 2555 '-1.4142135623730951' 2410 2556 sage: RR(-sqrt(2)).nextbelow().str(truncate=False) … … 2578 2724 2 2579 2725 sage: RealField(100)(0.0)._pari_().sizeword() 2580 2726 2 2727 2728 Check that the largest and smallest exponents representable by 2729 PARI convert correctly:: 2730 2731 sage: a = pari(0.5) << (sys.maxint+1)/4 2732 sage: RR(a) >> (sys.maxint+1)/4 2733 0.500000000000000 2734 sage: a = pari(0.5) >> (sys.maxint-3)/4 2735 sage: RR(a) << (sys.maxint-3)/4 2736 0.500000000000000 2581 2737 """ 2582 2738 # This uses interfaces of MPFR and PARI which are documented 2583 2739 # (and not marked subject-to-change). It could be faster … … 2598 2754 cdef int rounded_prec 2599 2755 rounded_prec = (self.prec() + wordsize - 1) & ~(wordsize - 1) 2600 2756 2601 # Yes, assigning to self works fine, even in Pyrex.2757 # Yes, assigning to self works fine, even in Cython. 2602 2758 if rounded_prec > prec: 2603 2759 self = RealField(rounded_prec)(self) 2604 2760