Back to index

python3.2  3.2.2
Classes | Functions | Variables
test.test_strtod Namespace Reference

Classes

class  StrtodTests

Functions

def strtod
def test_main

Variables

tuple strtod_parser = re.compile(r""" # A numeric string consists of: (?P<sign>[-+])? # an optional sign, followed by (?=\d|\.\d) # a number with at least one digit (?P<int>\d*) # having a (possibly empty) integer part (?:\.(?P<frac>\d*))? # followed by an optional fractional part (?:E(?P<exp>[-+]?\d+))? # and an optional exponent \Z""", re.VERBOSE | re.IGNORECASE)
int TEST_SIZE = 10

Function Documentation

def test.test_strtod.strtod (   s,
  mant_dig = 53,
  min_exp = -1021,
  max_exp = 1024 
)
Convert a finite decimal string to a hex string representing an
IEEE 754 binary64 float.  Return 'inf' or '-inf' on overflow.
This function makes no use of floating-point arithmetic at any
stage.

Definition at line 27 of file test_strtod.py.

00027 
00028 def strtod(s, mant_dig=53, min_exp = -1021, max_exp = 1024):
00029     """Convert a finite decimal string to a hex string representing an
00030     IEEE 754 binary64 float.  Return 'inf' or '-inf' on overflow.
00031     This function makes no use of floating-point arithmetic at any
00032     stage."""
00033 
00034     # parse string into a pair of integers 'a' and 'b' such that
00035     # abs(decimal value) = a/b, along with a boolean 'negative'.
00036     m = strtod_parser(s)
00037     if m is None:
00038         raise ValueError('invalid numeric string')
00039     fraction = m.group('frac') or ''
00040     intpart = int(m.group('int') + fraction)
00041     exp = int(m.group('exp') or '0') - len(fraction)
00042     negative = m.group('sign') == '-'
00043     a, b = intpart*10**max(exp, 0), 10**max(0, -exp)
00044 
00045     # quick return for zeros
00046     if not a:
00047         return '-0x0.0p+0' if negative else '0x0.0p+0'
00048 
00049     # compute exponent e for result; may be one too small in the case
00050     # that the rounded value of a/b lies in a different binade from a/b
00051     d = a.bit_length() - b.bit_length()
00052     d += (a >> d if d >= 0 else a << -d) >= b
00053     e = max(d, min_exp) - mant_dig
00054 
00055     # approximate a/b by number of the form q * 2**e; adjust e if necessary
00056     a, b = a << max(-e, 0), b << max(e, 0)
00057     q, r = divmod(a, b)
00058     if 2*r > b or 2*r == b and q & 1:
00059         q += 1
00060         if q.bit_length() == mant_dig+1:
00061             q //= 2
00062             e += 1
00063 
00064     # double check that (q, e) has the right form
00065     assert q.bit_length() <= mant_dig and e >= min_exp - mant_dig
00066     assert q.bit_length() == mant_dig or e == min_exp - mant_dig
00067 
00068     # check for overflow and underflow
00069     if e + q.bit_length() > max_exp:
00070         return '-inf' if negative else 'inf'
00071     if not q:
00072         return '-0x0.0p+0' if negative else '0x0.0p+0'
00073 
00074     # for hex representation, shift so # bits after point is a multiple of 4
00075     hexdigs = 1 + (mant_dig-2)//4
00076     shift = 3 - (mant_dig-2)%4
00077     q, e = q << shift, e - shift
00078     return '{}0x{:x}.{:0{}x}p{:+d}'.format(
00079         '-' if negative else '',
00080         q // 16**hexdigs,
00081         q % 16**hexdigs,
00082         hexdigs,
00083         e + 4*hexdigs)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 401 of file test_strtod.py.

00401 
00402 def test_main():
00403     test.support.run_unittest(StrtodTests)

Here is the call graph for this function:


Variable Documentation

tuple test.test_strtod.strtod_parser = re.compile(r""" # A numeric string consists of: (?P<sign>[-+])? # an optional sign, followed by (?=\d|\.\d) # a number with at least one digit (?P<int>\d*) # having a (possibly empty) integer part (?:\.(?P<frac>\d*))? # followed by an optional fractional part (?:E(?P<exp>[-+]?\d+))? # and an optional exponent \Z""", re.VERBOSE | re.IGNORECASE)

Definition at line 16 of file test_strtod.py.

Definition at line 84 of file test_strtod.py.