Add better error reporting for MemoryErrors caused by str->float conversions.
[python.git] / Lib / test / test_strtod.py
blob189254260cd1aabee05434cb83e425d510b9df8d
1 # Tests for the correctly-rounded string -> float conversions
2 # introduced in Python 2.7 and 3.1.
4 import random
5 import struct
6 import unittest
7 import re
8 import sys
9 from test import test_support
11 # Correctly rounded str -> float in pure Python, for comparison.
13 strtod_parser = re.compile(r""" # A numeric string consists of:
14 (?P<sign>[-+])? # an optional sign, followed by
15 (?=\d|\.\d) # a number with at least one digit
16 (?P<int>\d*) # having a (possibly empty) integer part
17 (?:\.(?P<frac>\d*))? # followed by an optional fractional part
18 (?:E(?P<exp>[-+]?\d+))? # and an optional exponent
20 """, re.VERBOSE | re.IGNORECASE).match
22 def strtod(s, mant_dig=53, min_exp = -1021, max_exp = 1024):
23 """Convert a finite decimal string to a hex string representing an
24 IEEE 754 binary64 float. Return 'inf' or '-inf' on overflow.
25 This function makes no use of floating-point arithmetic at any
26 stage."""
28 # parse string into a pair of integers 'a' and 'b' such that
29 # abs(decimal value) = a/b, along with a boolean 'negative'.
30 m = strtod_parser(s)
31 if m is None:
32 raise ValueError('invalid numeric string')
33 fraction = m.group('frac') or ''
34 intpart = int(m.group('int') + fraction)
35 exp = int(m.group('exp') or '0') - len(fraction)
36 negative = m.group('sign') == '-'
37 a, b = intpart*10**max(exp, 0), 10**max(0, -exp)
39 # quick return for zeros
40 if not a:
41 return '-0x0.0p+0' if negative else '0x0.0p+0'
43 # compute exponent e for result; may be one too small in the case
44 # that the rounded value of a/b lies in a different binade from a/b
45 d = a.bit_length() - b.bit_length()
46 d += (a >> d if d >= 0 else a << -d) >= b
47 e = max(d, min_exp) - mant_dig
49 # approximate a/b by number of the form q * 2**e; adjust e if necessary
50 a, b = a << max(-e, 0), b << max(e, 0)
51 q, r = divmod(a, b)
52 if 2*r > b or 2*r == b and q & 1:
53 q += 1
54 if q.bit_length() == mant_dig+1:
55 q //= 2
56 e += 1
58 # double check that (q, e) has the right form
59 assert q.bit_length() <= mant_dig and e >= min_exp - mant_dig
60 assert q.bit_length() == mant_dig or e == min_exp - mant_dig
62 # check for overflow and underflow
63 if e + q.bit_length() > max_exp:
64 return '-inf' if negative else 'inf'
65 if not q:
66 return '-0x0.0p+0' if negative else '0x0.0p+0'
68 # for hex representation, shift so # bits after point is a multiple of 4
69 hexdigs = 1 + (mant_dig-2)//4
70 shift = 3 - (mant_dig-2)%4
71 q, e = q << shift, e - shift
72 return '{}0x{:x}.{:0{}x}p{:+d}'.format(
73 '-' if negative else '',
74 q // 16**hexdigs,
75 q % 16**hexdigs,
76 hexdigs,
77 e + 4*hexdigs)
79 TEST_SIZE = 10
81 @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
82 "applies only when using short float repr style")
83 class StrtodTests(unittest.TestCase):
84 def check_strtod(self, s):
85 """Compare the result of Python's builtin correctly rounded
86 string->float conversion (using float) to a pure Python
87 correctly rounded string->float implementation. Fail if the
88 two methods give different results."""
90 try:
91 fs = float(s)
92 except OverflowError:
93 got = '-inf' if s[0] == '-' else 'inf'
94 except MemoryError:
95 got = 'memory error'
96 else:
97 got = fs.hex()
98 expected = strtod(s)
99 self.assertEqual(expected, got,
100 "Incorrectly rounded str->float conversion for {}: "
101 "expected {}, got {}".format(s, expected, got))
103 def test_halfway_cases(self):
104 # test halfway cases for the round-half-to-even rule
105 for i in xrange(1000):
106 for j in xrange(TEST_SIZE):
107 # bit pattern for a random finite positive (or +0.0) float
108 bits = random.randrange(2047*2**52)
110 # convert bit pattern to a number of the form m * 2**e
111 e, m = divmod(bits, 2**52)
112 if e:
113 m, e = m + 2**52, e - 1
114 e -= 1074
116 # add 0.5 ulps
117 m, e = 2*m + 1, e - 1
119 # convert to a decimal string
120 if e >= 0:
121 digits = m << e
122 exponent = 0
123 else:
124 # m * 2**e = (m * 5**-e) * 10**e
125 digits = m * 5**-e
126 exponent = e
127 s = '{}e{}'.format(digits, exponent)
128 self.check_strtod(s)
130 # get expected answer via struct, to triple check
131 #fs = struct.unpack('<d', struct.pack('<Q', bits + (bits&1)))[0]
132 #self.assertEqual(fs, float(s))
134 def test_boundaries(self):
135 # boundaries expressed as triples (n, e, u), where
136 # n*10**e is an approximation to the boundary value and
137 # u*10**e is 1ulp
138 boundaries = [
139 (10000000000000000000, -19, 1110), # a power of 2 boundary (1.0)
140 (17976931348623159077, 289, 1995), # overflow boundary (2.**1024)
141 (22250738585072013831, -327, 4941), # normal/subnormal (2.**-1022)
142 (0, -327, 4941), # zero
144 for n, e, u in boundaries:
145 for j in xrange(1000):
146 for i in xrange(TEST_SIZE):
147 digits = n + random.randrange(-3*u, 3*u)
148 exponent = e
149 s = '{}e{}'.format(digits, exponent)
150 self.check_strtod(s)
151 n *= 10
152 u *= 10
153 e -= 1
155 def test_underflow_boundary(self):
156 # test values close to 2**-1075, the underflow boundary; similar
157 # to boundary_tests, except that the random error doesn't scale
158 # with n
159 for exponent in xrange(-400, -320):
160 base = 10**-exponent // 2**1075
161 for j in xrange(TEST_SIZE):
162 digits = base + random.randrange(-1000, 1000)
163 s = '{}e{}'.format(digits, exponent)
164 self.check_strtod(s)
166 def test_bigcomp(self):
167 DIG10 = 10**50
168 for i in xrange(1000):
169 for j in xrange(TEST_SIZE):
170 digits = random.randrange(DIG10)
171 exponent = random.randrange(-400, 400)
172 s = '{}e{}'.format(digits, exponent)
173 self.check_strtod(s)
175 def test_parsing(self):
176 # make '0' more likely to be chosen than other digits
177 digits = '000000123456789'
178 signs = ('+', '-', '')
180 # put together random short valid strings
181 # \d*[.\d*]?e
182 for i in xrange(1000):
183 for j in xrange(TEST_SIZE):
184 s = random.choice(signs)
185 intpart_len = random.randrange(5)
186 s += ''.join(random.choice(digits) for _ in xrange(intpart_len))
187 if random.choice([True, False]):
188 s += '.'
189 fracpart_len = random.randrange(5)
190 s += ''.join(random.choice(digits)
191 for _ in xrange(fracpart_len))
192 else:
193 fracpart_len = 0
194 if random.choice([True, False]):
195 s += random.choice(['e', 'E'])
196 s += random.choice(signs)
197 exponent_len = random.randrange(1, 4)
198 s += ''.join(random.choice(digits)
199 for _ in xrange(exponent_len))
201 if intpart_len + fracpart_len:
202 self.check_strtod(s)
203 else:
204 try:
205 float(s)
206 except ValueError:
207 pass
208 else:
209 assert False, "expected ValueError"
211 def test_particular(self):
212 # inputs that produced crashes or incorrectly rounded results with
213 # previous versions of dtoa.c, for various reasons
214 test_strings = [
215 # issue 7632 bug 1, originally reported failing case
216 '2183167012312112312312.23538020374420446192e-370',
217 # 5 instances of issue 7632 bug 2
218 '12579816049008305546974391768996369464963024663104e-357',
219 '17489628565202117263145367596028389348922981857013e-357',
220 '18487398785991994634182916638542680759613590482273e-357',
221 '32002864200581033134358724675198044527469366773928e-358',
222 '94393431193180696942841837085033647913224148539854e-358',
223 # failing case for bug introduced by METD in r77451 (attempted
224 # fix for issue 7632, bug 2), and fixed in r77482.
225 '28639097178261763178489759107321392745108491825303e-311',
226 # two numbers demonstrating a flaw in the bigcomp 'dig == 0'
227 # correction block (issue 7632, bug 3)
228 '1.00000000000000001e44',
229 '1.0000000000000000100000000000000000000001e44',
230 # dtoa.c bug for numbers just smaller than a power of 2 (issue
231 # 7632, bug 4)
232 '99999999999999994487665465554760717039532578546e-47',
233 # failing case for off-by-one error introduced by METD in
234 # r77483 (dtoa.c cleanup), fixed in r77490
235 '965437176333654931799035513671997118345570045914469' #...
236 '6213413350821416312194420007991306908470147322020121018368e0',
237 # incorrect lsb detection for round-half-to-even when
238 # bc->scale != 0 (issue 7632, bug 6).
239 '104308485241983990666713401708072175773165034278685' #...
240 '682646111762292409330928739751702404658197872319129' #...
241 '036519947435319418387839758990478549477777586673075' #...
242 '945844895981012024387992135617064532141489278815239' #...
243 '849108105951619997829153633535314849999674266169258' #...
244 '928940692239684771590065027025835804863585454872499' #...
245 '320500023126142553932654370362024104462255244034053' #...
246 '203998964360882487378334860197725139151265590832887' #...
247 '433736189468858614521708567646743455601905935595381' #...
248 '852723723645799866672558576993978025033590728687206' #...
249 '296379801363024094048327273913079612469982585674824' #...
250 '156000783167963081616214710691759864332339239688734' #...
251 '656548790656486646106983450809073750535624894296242' #...
252 '072010195710276073042036425579852459556183541199012' #...
253 '652571123898996574563824424330960027873516082763671875e-1075',
254 # demonstration that original fix for issue 7632 bug 1 was
255 # buggy; the exit condition was too strong
256 '247032822920623295e-341',
257 # issue 7632 bug 5: the following 2 strings convert differently
258 '1000000000000000000000000000000000000000e-16',
259 '10000000000000000000000000000000000000000e-17',
260 # issue 7632 bug 8: the following produced 10.0
261 '10.900000000000000012345678912345678912345',
263 for s in test_strings:
264 self.check_strtod(s)
266 def test_main():
267 test_support.run_unittest(StrtodTests)
269 if __name__ == "__main__":
270 test_main()