"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "mpmath/libmp/libmpf.py" between
mpmath-0.19.tar.gz and mpmath-1.0.0.tar.gz

About: mpmath is a Python library for arbitrary-precision floating-point arithmetic.

libmpf.py  (mpmath-0.19):libmpf.py  (mpmath-1.0.0)
skipping to change at line 349 skipping to change at line 349
if (not man) and exp: if (not man) and exp:
raise ValueError("mantissa and exponent are undefined for %s" % man) raise ValueError("mantissa and exponent are undefined for %s" % man)
return man, exp return man, exp
def to_int(s, rnd=None): def to_int(s, rnd=None):
"""Convert a raw mpf to the nearest int. Rounding is done down by """Convert a raw mpf to the nearest int. Rounding is done down by
default (same as int(float) in Python), but can be changed. If the default (same as int(float) in Python), but can be changed. If the
input is inf/nan, an exception is raised.""" input is inf/nan, an exception is raised."""
sign, man, exp, bc = s sign, man, exp, bc = s
if (not man) and exp: if (not man) and exp:
raise ValueError("cannot convert %s to int" % man) raise ValueError("cannot convert inf or nan to int")
if exp >= 0: if exp >= 0:
if sign: if sign:
return (-man) << exp return (-man) << exp
return man << exp return man << exp
# Make default rounding fast # Make default rounding fast
if not rnd: if not rnd:
if sign: if sign:
return -(man >> (-exp)) return -(man >> (-exp))
else: else:
return man >> (-exp) return man >> (-exp)
skipping to change at line 427 skipping to change at line 427
try: try:
m, e = math.frexp(x) m, e = math.frexp(x)
except: except:
if x == math_float_inf: return finf if x == math_float_inf: return finf
if x == -math_float_inf: return fninf if x == -math_float_inf: return fninf
return fnan return fnan
if x == math_float_inf: return finf if x == math_float_inf: return finf
if x == -math_float_inf: return fninf if x == -math_float_inf: return fninf
return from_man_exp(int(m*(1<<53)), e-53, prec, rnd) return from_man_exp(int(m*(1<<53)), e-53, prec, rnd)
def to_float(s, strict=False): def to_float(s, strict=False, rnd=round_fast):
""" """
Convert a raw mpf to a Python float. The result is exact if the Convert a raw mpf to a Python float. The result is exact if the
bitcount of s is <= 53 and no underflow/overflow occurs. bitcount of s is <= 53 and no underflow/overflow occurs.
If the number is too large or too small to represent as a regular If the number is too large or too small to represent as a regular
float, it will be converted to inf or 0.0. Setting strict=True float, it will be converted to inf or 0.0. Setting strict=True
forces an OverflowError to be raised instead. forces an OverflowError to be raised instead.
Warning: with a directed rounding mode, the correct nearest representable
floating-point number in the specified direction might not be computed
in case of overflow or (gradual) underflow.
""" """
sign, man, exp, bc = s sign, man, exp, bc = s
if not man: if not man:
if s == fzero: return 0.0 if s == fzero: return 0.0
if s == finf: return math_float_inf if s == finf: return math_float_inf
if s == fninf: return -math_float_inf if s == fninf: return -math_float_inf
return math_float_inf/math_float_inf return math_float_inf/math_float_inf
if bc > 53:
sign, man, exp, bc = normalize1(sign, man, exp, bc, 53, rnd)
if sign: if sign:
man = -man man = -man
try: try:
if bc < 100: return math.ldexp(man, exp)
return math.ldexp(man, exp)
# Try resizing the mantissa. Overflow may still happen here.
n = bc - 53
m = man >> n
return math.ldexp(m, exp + n)
except OverflowError: except OverflowError:
if strict: if strict:
raise raise
# Overflow to infinity # Overflow to infinity
if exp + bc > 0: if exp + bc > 0:
if sign: if sign:
return -math_float_inf return -math_float_inf
else: else:
return math_float_inf return math_float_inf
# Underflow to zero # Underflow to zero
 End of changes. 5 change blocks. 
8 lines changed or deleted 9 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)