1# Tests for the correctly-rounded string -> float conversions 2# introduced in Python 2.7 and 3.1. 3 4import random 5import struct 6import unittest 7import re 8import sys 9from test import test_support 10 11if getattr(sys, 'float_repr_style', '') != 'short': 12 raise unittest.SkipTest('correctly-rounded string->float conversions ' 13 'not available on this system') 14 15# Correctly rounded str -> float in pure Python, for comparison. 16 17strtod_parser = re.compile(r""" # A numeric string consists of: 18 (?P<sign>[-+])? # an optional sign, followed by 19 (?=\d|\.\d) # a number with at least one digit 20 (?P<int>\d*) # having a (possibly empty) integer part 21 (?:\.(?P<frac>\d*))? # followed by an optional fractional part 22 (?:E(?P<exp>[-+]?\d+))? # and an optional exponent 23 \Z 24""", re.VERBOSE | re.IGNORECASE).match 25 26# Pure Python version of correctly rounded string->float conversion. 27# Avoids any use of floating-point by returning the result as a hex string. 28def strtod(s, mant_dig=53, min_exp = -1021, max_exp = 1024): 29 """Convert a finite decimal string to a hex string representing an 30 IEEE 754 binary64 float. Return 'inf' or '-inf' on overflow. 31 This function makes no use of floating-point arithmetic at any 32 stage.""" 33 34 # parse string into a pair of integers 'a' and 'b' such that 35 # abs(decimal value) = a/b, along with a boolean 'negative'. 36 m = strtod_parser(s) 37 if m is None: 38 raise ValueError('invalid numeric string') 39 fraction = m.group('frac') or '' 40 intpart = int(m.group('int') + fraction) 41 exp = int(m.group('exp') or '0') - len(fraction) 42 negative = m.group('sign') == '-' 43 a, b = intpart*10**max(exp, 0), 10**max(0, -exp) 44 45 # quick return for zeros 46 if not a: 47 return '-0x0.0p+0' if negative else '0x0.0p+0' 48 49 # compute exponent e for result; may be one too small in the case 50 # that the rounded value of a/b lies in a different binade from a/b 51 d = a.bit_length() - b.bit_length() 52 d += (a >> d if d >= 0 else a << -d) >= b 53 e = max(d, min_exp) - mant_dig 54 55 # approximate a/b by number of the form q * 2**e; adjust e if necessary 56 a, b = a << max(-e, 0), b << max(e, 0) 57 q, r = divmod(a, b) 58 if 2*r > b or 2*r == b and q & 1: 59 q += 1 60 if q.bit_length() == mant_dig+1: 61 q //= 2 62 e += 1 63 64 # double check that (q, e) has the right form 65 assert q.bit_length() <= mant_dig and e >= min_exp - mant_dig 66 assert q.bit_length() == mant_dig or e == min_exp - mant_dig 67 68 # check for overflow and underflow 69 if e + q.bit_length() > max_exp: 70 return '-inf' if negative else 'inf' 71 if not q: 72 return '-0x0.0p+0' if negative else '0x0.0p+0' 73 74 # for hex representation, shift so # bits after point is a multiple of 4 75 hexdigs = 1 + (mant_dig-2)//4 76 shift = 3 - (mant_dig-2)%4 77 q, e = q << shift, e - shift 78 return '{}0x{:x}.{:0{}x}p{:+d}'.format( 79 '-' if negative else '', 80 q // 16**hexdigs, 81 q % 16**hexdigs, 82 hexdigs, 83 e + 4*hexdigs) 84 85TEST_SIZE = 10 86 87class StrtodTests(unittest.TestCase): 88 def check_strtod(self, s): 89 """Compare the result of Python's builtin correctly rounded 90 string->float conversion (using float) to a pure Python 91 correctly rounded string->float implementation. Fail if the 92 two methods give different results.""" 93 94 try: 95 fs = float(s) 96 except OverflowError: 97 got = '-inf' if s[0] == '-' else 'inf' 98 except MemoryError: 99 got = 'memory error' 100 else: 101 got = fs.hex() 102 expected = strtod(s) 103 self.assertEqual(expected, got, 104 "Incorrectly rounded str->float conversion for {}: " 105 "expected {}, got {}".format(s, expected, got)) 106 107 def test_short_halfway_cases(self): 108 # exact halfway cases with a small number of significant digits 109 for k in 0, 5, 10, 15, 20: 110 # upper = smallest integer >= 2**54/5**k 111 upper = -(-2**54//5**k) 112 # lower = smallest odd number >= 2**53/5**k 113 lower = -(-2**53//5**k) 114 if lower % 2 == 0: 115 lower += 1 116 for i in xrange(TEST_SIZE): 117 # Select a random odd n in [2**53/5**k, 118 # 2**54/5**k). Then n * 10**k gives a halfway case 119 # with small number of significant digits. 120 n, e = random.randrange(lower, upper, 2), k 121 122 # Remove any additional powers of 5. 123 while n % 5 == 0: 124 n, e = n // 5, e + 1 125 assert n % 10 in (1, 3, 7, 9) 126 127 # Try numbers of the form n * 2**p2 * 10**e, p2 >= 0, 128 # until n * 2**p2 has more than 20 significant digits. 129 digits, exponent = n, e 130 while digits < 10**20: 131 s = '{}e{}'.format(digits, exponent) 132 self.check_strtod(s) 133 # Same again, but with extra trailing zeros. 134 s = '{}e{}'.format(digits * 10**40, exponent - 40) 135 self.check_strtod(s) 136 digits *= 2 137 138 # Try numbers of the form n * 5**p2 * 10**(e - p5), p5 139 # >= 0, with n * 5**p5 < 10**20. 140 digits, exponent = n, e 141 while digits < 10**20: 142 s = '{}e{}'.format(digits, exponent) 143 self.check_strtod(s) 144 # Same again, but with extra trailing zeros. 145 s = '{}e{}'.format(digits * 10**40, exponent - 40) 146 self.check_strtod(s) 147 digits *= 5 148 exponent -= 1 149 150 def test_halfway_cases(self): 151 # test halfway cases for the round-half-to-even rule 152 for i in xrange(100 * TEST_SIZE): 153 # bit pattern for a random finite positive (or +0.0) float 154 bits = random.randrange(2047*2**52) 155 156 # convert bit pattern to a number of the form m * 2**e 157 e, m = divmod(bits, 2**52) 158 if e: 159 m, e = m + 2**52, e - 1 160 e -= 1074 161 162 # add 0.5 ulps 163 m, e = 2*m + 1, e - 1 164 165 # convert to a decimal string 166 if e >= 0: 167 digits = m << e 168 exponent = 0 169 else: 170 # m * 2**e = (m * 5**-e) * 10**e 171 digits = m * 5**-e 172 exponent = e 173 s = '{}e{}'.format(digits, exponent) 174 self.check_strtod(s) 175 176 def test_boundaries(self): 177 # boundaries expressed as triples (n, e, u), where 178 # n*10**e is an approximation to the boundary value and 179 # u*10**e is 1ulp 180 boundaries = [ 181 (10000000000000000000, -19, 1110), # a power of 2 boundary (1.0) 182 (17976931348623159077, 289, 1995), # overflow boundary (2.**1024) 183 (22250738585072013831, -327, 4941), # normal/subnormal (2.**-1022) 184 (0, -327, 4941), # zero 185 ] 186 for n, e, u in boundaries: 187 for j in xrange(1000): 188 digits = n + random.randrange(-3*u, 3*u) 189 exponent = e 190 s = '{}e{}'.format(digits, exponent) 191 self.check_strtod(s) 192 n *= 10 193 u *= 10 194 e -= 1 195 196 def test_underflow_boundary(self): 197 # test values close to 2**-1075, the underflow boundary; similar 198 # to boundary_tests, except that the random error doesn't scale 199 # with n 200 for exponent in xrange(-400, -320): 201 base = 10**-exponent // 2**1075 202 for j in xrange(TEST_SIZE): 203 digits = base + random.randrange(-1000, 1000) 204 s = '{}e{}'.format(digits, exponent) 205 self.check_strtod(s) 206 207 def test_bigcomp(self): 208 for ndigs in 5, 10, 14, 15, 16, 17, 18, 19, 20, 40, 41, 50: 209 dig10 = 10**ndigs 210 for i in xrange(10 * TEST_SIZE): 211 digits = random.randrange(dig10) 212 exponent = random.randrange(-400, 400) 213 s = '{}e{}'.format(digits, exponent) 214 self.check_strtod(s) 215 216 def test_parsing(self): 217 # make '0' more likely to be chosen than other digits 218 digits = '000000123456789' 219 signs = ('+', '-', '') 220 221 # put together random short valid strings 222 # \d*[.\d*]?e 223 for i in xrange(1000): 224 for j in xrange(TEST_SIZE): 225 s = random.choice(signs) 226 intpart_len = random.randrange(5) 227 s += ''.join(random.choice(digits) for _ in xrange(intpart_len)) 228 if random.choice([True, False]): 229 s += '.' 230 fracpart_len = random.randrange(5) 231 s += ''.join(random.choice(digits) 232 for _ in xrange(fracpart_len)) 233 else: 234 fracpart_len = 0 235 if random.choice([True, False]): 236 s += random.choice(['e', 'E']) 237 s += random.choice(signs) 238 exponent_len = random.randrange(1, 4) 239 s += ''.join(random.choice(digits) 240 for _ in xrange(exponent_len)) 241 242 if intpart_len + fracpart_len: 243 self.check_strtod(s) 244 else: 245 try: 246 float(s) 247 except ValueError: 248 pass 249 else: 250 assert False, "expected ValueError" 251 252 @test_support.precisionbigmemtest(size=test_support._2G, memuse=3, 253 dry_run=False) 254 def test_oversized_digit_strings(self, maxsize): 255 # Input string whose length doesn't fit in an INT. 256 s = "1." + "1" * int(2.2e9) 257 with self.assertRaises(ValueError): 258 float(s) 259 del s 260 261 s = "0." + "0" * int(2.2e9) + "1" 262 with self.assertRaises(ValueError): 263 float(s) 264 del s 265 266 def test_large_exponents(self): 267 # Verify that the clipping of the exponent in strtod doesn't affect the 268 # output values. 269 def positive_exp(n): 270 """ Long string with value 1.0 and exponent n""" 271 return '0.{}1e+{}'.format('0'*(n-1), n) 272 273 def negative_exp(n): 274 """ Long string with value 1.0 and exponent -n""" 275 return '1{}e-{}'.format('0'*n, n) 276 277 self.assertEqual(float(positive_exp(10000)), 1.0) 278 self.assertEqual(float(positive_exp(20000)), 1.0) 279 self.assertEqual(float(positive_exp(30000)), 1.0) 280 self.assertEqual(float(negative_exp(10000)), 1.0) 281 self.assertEqual(float(negative_exp(20000)), 1.0) 282 self.assertEqual(float(negative_exp(30000)), 1.0) 283 284 def test_particular(self): 285 # inputs that produced crashes or incorrectly rounded results with 286 # previous versions of dtoa.c, for various reasons 287 test_strings = [ 288 # issue 7632 bug 1, originally reported failing case 289 '2183167012312112312312.23538020374420446192e-370', 290 # 5 instances of issue 7632 bug 2 291 '12579816049008305546974391768996369464963024663104e-357', 292 '17489628565202117263145367596028389348922981857013e-357', 293 '18487398785991994634182916638542680759613590482273e-357', 294 '32002864200581033134358724675198044527469366773928e-358', 295 '94393431193180696942841837085033647913224148539854e-358', 296 '73608278998966969345824653500136787876436005957953e-358', 297 '64774478836417299491718435234611299336288082136054e-358', 298 '13704940134126574534878641876947980878824688451169e-357', 299 '46697445774047060960624497964425416610480524760471e-358', 300 # failing case for bug introduced by METD in r77451 (attempted 301 # fix for issue 7632, bug 2), and fixed in r77482. 302 '28639097178261763178489759107321392745108491825303e-311', 303 # two numbers demonstrating a flaw in the bigcomp 'dig == 0' 304 # correction block (issue 7632, bug 3) 305 '1.00000000000000001e44', 306 '1.0000000000000000100000000000000000000001e44', 307 # dtoa.c bug for numbers just smaller than a power of 2 (issue 308 # 7632, bug 4) 309 '99999999999999994487665465554760717039532578546e-47', 310 # failing case for off-by-one error introduced by METD in 311 # r77483 (dtoa.c cleanup), fixed in r77490 312 '965437176333654931799035513671997118345570045914469' #... 313 '6213413350821416312194420007991306908470147322020121018368e0', 314 # incorrect lsb detection for round-half-to-even when 315 # bc->scale != 0 (issue 7632, bug 6). 316 '104308485241983990666713401708072175773165034278685' #... 317 '682646111762292409330928739751702404658197872319129' #... 318 '036519947435319418387839758990478549477777586673075' #... 319 '945844895981012024387992135617064532141489278815239' #... 320 '849108105951619997829153633535314849999674266169258' #... 321 '928940692239684771590065027025835804863585454872499' #... 322 '320500023126142553932654370362024104462255244034053' #... 323 '203998964360882487378334860197725139151265590832887' #... 324 '433736189468858614521708567646743455601905935595381' #... 325 '852723723645799866672558576993978025033590728687206' #... 326 '296379801363024094048327273913079612469982585674824' #... 327 '156000783167963081616214710691759864332339239688734' #... 328 '656548790656486646106983450809073750535624894296242' #... 329 '072010195710276073042036425579852459556183541199012' #... 330 '652571123898996574563824424330960027873516082763671875e-1075', 331 # demonstration that original fix for issue 7632 bug 1 was 332 # buggy; the exit condition was too strong 333 '247032822920623295e-341', 334 # demonstrate similar problem to issue 7632 bug1: crash 335 # with 'oversized quotient in quorem' message. 336 '99037485700245683102805043437346965248029601286431e-373', 337 '99617639833743863161109961162881027406769510558457e-373', 338 '98852915025769345295749278351563179840130565591462e-372', 339 '99059944827693569659153042769690930905148015876788e-373', 340 '98914979205069368270421829889078356254059760327101e-372', 341 # issue 7632 bug 5: the following 2 strings convert differently 342 '1000000000000000000000000000000000000000e-16', 343 '10000000000000000000000000000000000000000e-17', 344 # issue 7632 bug 7 345 '991633793189150720000000000000000000000000000000000000000e-33', 346 # And another, similar, failing halfway case 347 '4106250198039490000000000000000000000000000000000000000e-38', 348 # issue 7632 bug 8: the following produced 10.0 349 '10.900000000000000012345678912345678912345', 350 351 # two humongous values from issue 7743 352 '116512874940594195638617907092569881519034793229385' #... 353 '228569165191541890846564669771714896916084883987920' #... 354 '473321268100296857636200926065340769682863349205363' #... 355 '349247637660671783209907949273683040397979984107806' #... 356 '461822693332712828397617946036239581632976585100633' #... 357 '520260770761060725403904123144384571612073732754774' #... 358 '588211944406465572591022081973828448927338602556287' #... 359 '851831745419397433012491884869454462440536895047499' #... 360 '436551974649731917170099387762871020403582994193439' #... 361 '761933412166821484015883631622539314203799034497982' #... 362 '130038741741727907429575673302461380386596501187482' #... 363 '006257527709842179336488381672818798450229339123527' #... 364 '858844448336815912020452294624916993546388956561522' #... 365 '161875352572590420823607478788399460162228308693742' #... 366 '05287663441403533948204085390898399055004119873046875e-1075', 367 368 '525440653352955266109661060358202819561258984964913' #... 369 '892256527849758956045218257059713765874251436193619' #... 370 '443248205998870001633865657517447355992225852945912' #... 371 '016668660000210283807209850662224417504752264995360' #... 372 '631512007753855801075373057632157738752800840302596' #... 373 '237050247910530538250008682272783660778181628040733' #... 374 '653121492436408812668023478001208529190359254322340' #... 375 '397575185248844788515410722958784640926528544043090' #... 376 '115352513640884988017342469275006999104519620946430' #... 377 '818767147966495485406577703972687838176778993472989' #... 378 '561959000047036638938396333146685137903018376496408' #... 379 '319705333868476925297317136513970189073693314710318' #... 380 '991252811050501448326875232850600451776091303043715' #... 381 '157191292827614046876950225714743118291034780466325' #... 382 '085141343734564915193426994587206432697337118211527' #... 383 '278968731294639353354774788602467795167875117481660' #... 384 '4738791256853675690543663283782215866825e-1180', 385 386 # exercise exit conditions in bigcomp comparison loop 387 '2602129298404963083833853479113577253105939995688e2', 388 '260212929840496308383385347911357725310593999568896e0', 389 '26021292984049630838338534791135772531059399956889601e-2', 390 '260212929840496308383385347911357725310593999568895e0', 391 '260212929840496308383385347911357725310593999568897e0', 392 '260212929840496308383385347911357725310593999568996e0', 393 '260212929840496308383385347911357725310593999568866e0', 394 # 2**53 395 '9007199254740992.00', 396 # 2**1024 - 2**970: exact overflow boundary. All values 397 # smaller than this should round to something finite; any value 398 # greater than or equal to this one overflows. 399 '179769313486231580793728971405303415079934132710037' #... 400 '826936173778980444968292764750946649017977587207096' #... 401 '330286416692887910946555547851940402630657488671505' #... 402 '820681908902000708383676273854845817711531764475730' #... 403 '270069855571366959622842914819860834936475292719074' #... 404 '168444365510704342711559699508093042880177904174497792', 405 # 2**1024 - 2**970 - tiny 406 '179769313486231580793728971405303415079934132710037' #... 407 '826936173778980444968292764750946649017977587207096' #... 408 '330286416692887910946555547851940402630657488671505' #... 409 '820681908902000708383676273854845817711531764475730' #... 410 '270069855571366959622842914819860834936475292719074' #... 411 '168444365510704342711559699508093042880177904174497791.999', 412 # 2**1024 - 2**970 + tiny 413 '179769313486231580793728971405303415079934132710037' #... 414 '826936173778980444968292764750946649017977587207096' #... 415 '330286416692887910946555547851940402630657488671505' #... 416 '820681908902000708383676273854845817711531764475730' #... 417 '270069855571366959622842914819860834936475292719074' #... 418 '168444365510704342711559699508093042880177904174497792.001', 419 # 1 - 2**-54, +-tiny 420 '999999999999999944488848768742172978818416595458984375e-54', 421 '9999999999999999444888487687421729788184165954589843749999999e-54', 422 '9999999999999999444888487687421729788184165954589843750000001e-54', 423 ] 424 for s in test_strings: 425 self.check_strtod(s) 426 427def test_main(): 428 test_support.run_unittest(StrtodTests) 429 430if __name__ == "__main__": 431 test_main() 432