Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 1 | # Tests for the correctly-rounded string -> float conversions |
| 2 | # introduced in Python 2.7 and 3.1. |
| 3 | |
| 4 | import random |
| 5 | import struct |
| 6 | import unittest |
| 7 | import re |
| 8 | import sys |
| 9 | import test.support |
| 10 | |
| 11 | # Correctly rounded str -> float in pure Python, for comparison. |
| 12 | |
| 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 |
| 19 | \Z |
| 20 | """, re.VERBOSE | re.IGNORECASE).match |
| 21 | |
| 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.""" |
| 27 | |
| 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) |
| 38 | |
| 39 | # quick return for zeros |
| 40 | if not a: |
| 41 | return '-0x0.0p+0' if negative else '0x0.0p+0' |
| 42 | |
| 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 |
| 48 | |
| 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 |
| 57 | |
| 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 |
| 61 | |
| 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' |
| 67 | |
| 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) |
| 78 | |
Mark Dickinson | 6328f41 | 2010-02-21 14:51:02 +0000 | [diff] [blame] | 79 | TEST_SIZE = 10 |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 80 | |
| 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.""" |
| 89 | |
| 90 | try: |
| 91 | fs = float(s) |
| 92 | except OverflowError: |
| 93 | got = '-inf' if s[0] == '-' else 'inf' |
Mark Dickinson | 863d61d | 2010-01-16 20:35:50 +0000 | [diff] [blame] | 94 | except MemoryError: |
| 95 | got = 'memory error' |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 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)) |
| 102 | |
Mark Dickinson | e42ffae | 2010-01-21 19:57:43 +0000 | [diff] [blame] | 103 | def test_short_halfway_cases(self): |
| 104 | # exact halfway cases with a small number of significant digits |
| 105 | for k in 0, 5, 10, 15, 20: |
| 106 | # upper = smallest integer >= 2**54/5**k |
| 107 | upper = -(-2**54//5**k) |
| 108 | # lower = smallest odd number >= 2**53/5**k |
| 109 | lower = -(-2**53//5**k) |
| 110 | if lower % 2 == 0: |
| 111 | lower += 1 |
Mark Dickinson | 6328f41 | 2010-02-21 14:51:02 +0000 | [diff] [blame] | 112 | for i in range(TEST_SIZE): |
Mark Dickinson | e42ffae | 2010-01-21 19:57:43 +0000 | [diff] [blame] | 113 | # Select a random odd n in [2**53/5**k, |
| 114 | # 2**54/5**k). Then n * 10**k gives a halfway case |
| 115 | # with small number of significant digits. |
| 116 | n, e = random.randrange(lower, upper, 2), k |
| 117 | |
| 118 | # Remove any additional powers of 5. |
| 119 | while n % 5 == 0: |
| 120 | n, e = n // 5, e + 1 |
| 121 | assert n % 10 in (1, 3, 7, 9) |
| 122 | |
| 123 | # Try numbers of the form n * 2**p2 * 10**e, p2 >= 0, |
| 124 | # until n * 2**p2 has more than 20 significant digits. |
| 125 | digits, exponent = n, e |
| 126 | while digits < 10**20: |
| 127 | s = '{}e{}'.format(digits, exponent) |
| 128 | self.check_strtod(s) |
| 129 | # Same again, but with extra trailing zeros. |
| 130 | s = '{}e{}'.format(digits * 10**40, exponent - 40) |
| 131 | self.check_strtod(s) |
| 132 | digits *= 2 |
| 133 | |
| 134 | # Try numbers of the form n * 5**p2 * 10**(e - p5), p5 |
| 135 | # >= 0, with n * 5**p5 < 10**20. |
| 136 | digits, exponent = n, e |
| 137 | while digits < 10**20: |
| 138 | s = '{}e{}'.format(digits, exponent) |
| 139 | self.check_strtod(s) |
| 140 | # Same again, but with extra trailing zeros. |
| 141 | s = '{}e{}'.format(digits * 10**40, exponent - 40) |
| 142 | self.check_strtod(s) |
| 143 | digits *= 5 |
| 144 | exponent -= 1 |
| 145 | |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 146 | def test_halfway_cases(self): |
| 147 | # test halfway cases for the round-half-to-even rule |
Mark Dickinson | 6328f41 | 2010-02-21 14:51:02 +0000 | [diff] [blame] | 148 | for i in range(100 * TEST_SIZE): |
| 149 | # bit pattern for a random finite positive (or +0.0) float |
| 150 | bits = random.randrange(2047*2**52) |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 151 | |
Mark Dickinson | 6328f41 | 2010-02-21 14:51:02 +0000 | [diff] [blame] | 152 | # convert bit pattern to a number of the form m * 2**e |
| 153 | e, m = divmod(bits, 2**52) |
| 154 | if e: |
| 155 | m, e = m + 2**52, e - 1 |
| 156 | e -= 1074 |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 157 | |
Mark Dickinson | 6328f41 | 2010-02-21 14:51:02 +0000 | [diff] [blame] | 158 | # add 0.5 ulps |
| 159 | m, e = 2*m + 1, e - 1 |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 160 | |
Mark Dickinson | 6328f41 | 2010-02-21 14:51:02 +0000 | [diff] [blame] | 161 | # convert to a decimal string |
| 162 | if e >= 0: |
| 163 | digits = m << e |
| 164 | exponent = 0 |
| 165 | else: |
| 166 | # m * 2**e = (m * 5**-e) * 10**e |
| 167 | digits = m * 5**-e |
| 168 | exponent = e |
| 169 | s = '{}e{}'.format(digits, exponent) |
| 170 | self.check_strtod(s) |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 171 | |
| 172 | def test_boundaries(self): |
| 173 | # boundaries expressed as triples (n, e, u), where |
| 174 | # n*10**e is an approximation to the boundary value and |
| 175 | # u*10**e is 1ulp |
| 176 | boundaries = [ |
| 177 | (10000000000000000000, -19, 1110), # a power of 2 boundary (1.0) |
| 178 | (17976931348623159077, 289, 1995), # overflow boundary (2.**1024) |
| 179 | (22250738585072013831, -327, 4941), # normal/subnormal (2.**-1022) |
| 180 | (0, -327, 4941), # zero |
| 181 | ] |
| 182 | for n, e, u in boundaries: |
| 183 | for j in range(1000): |
Mark Dickinson | 6328f41 | 2010-02-21 14:51:02 +0000 | [diff] [blame] | 184 | digits = n + random.randrange(-3*u, 3*u) |
| 185 | exponent = e |
| 186 | s = '{}e{}'.format(digits, exponent) |
| 187 | self.check_strtod(s) |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 188 | n *= 10 |
| 189 | u *= 10 |
| 190 | e -= 1 |
| 191 | |
| 192 | def test_underflow_boundary(self): |
| 193 | # test values close to 2**-1075, the underflow boundary; similar |
| 194 | # to boundary_tests, except that the random error doesn't scale |
| 195 | # with n |
| 196 | for exponent in range(-400, -320): |
| 197 | base = 10**-exponent // 2**1075 |
| 198 | for j in range(TEST_SIZE): |
| 199 | digits = base + random.randrange(-1000, 1000) |
| 200 | s = '{}e{}'.format(digits, exponent) |
| 201 | self.check_strtod(s) |
| 202 | |
| 203 | def test_bigcomp(self): |
Mark Dickinson | e42ffae | 2010-01-21 19:57:43 +0000 | [diff] [blame] | 204 | for ndigs in 5, 10, 14, 15, 16, 17, 18, 19, 20, 40, 41, 50: |
| 205 | dig10 = 10**ndigs |
Mark Dickinson | 6328f41 | 2010-02-21 14:51:02 +0000 | [diff] [blame] | 206 | for i in range(10 * TEST_SIZE): |
Mark Dickinson | e42ffae | 2010-01-21 19:57:43 +0000 | [diff] [blame] | 207 | digits = random.randrange(dig10) |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 208 | exponent = random.randrange(-400, 400) |
| 209 | s = '{}e{}'.format(digits, exponent) |
| 210 | self.check_strtod(s) |
| 211 | |
| 212 | def test_parsing(self): |
Mark Dickinson | 11f6578 | 2010-01-16 18:12:46 +0000 | [diff] [blame] | 213 | # make '0' more likely to be chosen than other digits |
| 214 | digits = '000000123456789' |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 215 | signs = ('+', '-', '') |
| 216 | |
| 217 | # put together random short valid strings |
| 218 | # \d*[.\d*]?e |
| 219 | for i in range(1000): |
| 220 | for j in range(TEST_SIZE): |
| 221 | s = random.choice(signs) |
| 222 | intpart_len = random.randrange(5) |
| 223 | s += ''.join(random.choice(digits) for _ in range(intpart_len)) |
| 224 | if random.choice([True, False]): |
| 225 | s += '.' |
| 226 | fracpart_len = random.randrange(5) |
| 227 | s += ''.join(random.choice(digits) |
| 228 | for _ in range(fracpart_len)) |
| 229 | else: |
| 230 | fracpart_len = 0 |
| 231 | if random.choice([True, False]): |
| 232 | s += random.choice(['e', 'E']) |
| 233 | s += random.choice(signs) |
| 234 | exponent_len = random.randrange(1, 4) |
| 235 | s += ''.join(random.choice(digits) |
| 236 | for _ in range(exponent_len)) |
| 237 | |
| 238 | if intpart_len + fracpart_len: |
| 239 | self.check_strtod(s) |
| 240 | else: |
| 241 | try: |
| 242 | float(s) |
| 243 | except ValueError: |
| 244 | pass |
| 245 | else: |
| 246 | assert False, "expected ValueError" |
| 247 | |
| 248 | def test_particular(self): |
| 249 | # inputs that produced crashes or incorrectly rounded results with |
| 250 | # previous versions of dtoa.c, for various reasons |
| 251 | test_strings = [ |
| 252 | # issue 7632 bug 1, originally reported failing case |
| 253 | '2183167012312112312312.23538020374420446192e-370', |
| 254 | # 5 instances of issue 7632 bug 2 |
| 255 | '12579816049008305546974391768996369464963024663104e-357', |
| 256 | '17489628565202117263145367596028389348922981857013e-357', |
| 257 | '18487398785991994634182916638542680759613590482273e-357', |
| 258 | '32002864200581033134358724675198044527469366773928e-358', |
| 259 | '94393431193180696942841837085033647913224148539854e-358', |
| 260 | # failing case for bug introduced by METD in r77451 (attempted |
| 261 | # fix for issue 7632, bug 2), and fixed in r77482. |
| 262 | '28639097178261763178489759107321392745108491825303e-311', |
| 263 | # two numbers demonstrating a flaw in the bigcomp 'dig == 0' |
| 264 | # correction block (issue 7632, bug 3) |
| 265 | '1.00000000000000001e44', |
| 266 | '1.0000000000000000100000000000000000000001e44', |
| 267 | # dtoa.c bug for numbers just smaller than a power of 2 (issue |
| 268 | # 7632, bug 4) |
| 269 | '99999999999999994487665465554760717039532578546e-47', |
| 270 | # failing case for off-by-one error introduced by METD in |
| 271 | # r77483 (dtoa.c cleanup), fixed in r77490 |
| 272 | '965437176333654931799035513671997118345570045914469' #... |
| 273 | '6213413350821416312194420007991306908470147322020121018368e0', |
| 274 | # incorrect lsb detection for round-half-to-even when |
| 275 | # bc->scale != 0 (issue 7632, bug 6). |
| 276 | '104308485241983990666713401708072175773165034278685' #... |
| 277 | '682646111762292409330928739751702404658197872319129' #... |
| 278 | '036519947435319418387839758990478549477777586673075' #... |
| 279 | '945844895981012024387992135617064532141489278815239' #... |
| 280 | '849108105951619997829153633535314849999674266169258' #... |
| 281 | '928940692239684771590065027025835804863585454872499' #... |
| 282 | '320500023126142553932654370362024104462255244034053' #... |
| 283 | '203998964360882487378334860197725139151265590832887' #... |
| 284 | '433736189468858614521708567646743455601905935595381' #... |
| 285 | '852723723645799866672558576993978025033590728687206' #... |
| 286 | '296379801363024094048327273913079612469982585674824' #... |
| 287 | '156000783167963081616214710691759864332339239688734' #... |
| 288 | '656548790656486646106983450809073750535624894296242' #... |
| 289 | '072010195710276073042036425579852459556183541199012' #... |
| 290 | '652571123898996574563824424330960027873516082763671875e-1075', |
| 291 | # demonstration that original fix for issue 7632 bug 1 was |
| 292 | # buggy; the exit condition was too strong |
| 293 | '247032822920623295e-341', |
Mark Dickinson | e42ffae | 2010-01-21 19:57:43 +0000 | [diff] [blame] | 294 | # demonstrate similar problem to issue 7632 bug1: crash |
| 295 | # with 'oversized quotient in quorem' message. |
| 296 | '99037485700245683102805043437346965248029601286431e-373', |
| 297 | '99617639833743863161109961162881027406769510558457e-373', |
| 298 | '98852915025769345295749278351563179840130565591462e-372', |
| 299 | '99059944827693569659153042769690930905148015876788e-373', |
| 300 | '98914979205069368270421829889078356254059760327101e-372', |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 301 | # issue 7632 bug 5: the following 2 strings convert differently |
| 302 | '1000000000000000000000000000000000000000e-16', |
Mark Dickinson | 11f6578 | 2010-01-16 18:12:46 +0000 | [diff] [blame] | 303 | '10000000000000000000000000000000000000000e-17', |
Mark Dickinson | e42ffae | 2010-01-21 19:57:43 +0000 | [diff] [blame] | 304 | # issue 7632 bug 7 |
| 305 | '991633793189150720000000000000000000000000000000000000000e-33', |
| 306 | # And another, similar, failing halfway case |
| 307 | '4106250198039490000000000000000000000000000000000000000e-38', |
Mark Dickinson | 11f6578 | 2010-01-16 18:12:46 +0000 | [diff] [blame] | 308 | # issue 7632 bug 8: the following produced 10.0 |
| 309 | '10.900000000000000012345678912345678912345', |
Benjamin Peterson | 23b9ef7 | 2010-02-03 02:43:37 +0000 | [diff] [blame] | 310 | |
| 311 | # two humongous values from issue 7743 |
| 312 | '116512874940594195638617907092569881519034793229385' #... |
| 313 | '228569165191541890846564669771714896916084883987920' #... |
| 314 | '473321268100296857636200926065340769682863349205363' #... |
| 315 | '349247637660671783209907949273683040397979984107806' #... |
| 316 | '461822693332712828397617946036239581632976585100633' #... |
| 317 | '520260770761060725403904123144384571612073732754774' #... |
| 318 | '588211944406465572591022081973828448927338602556287' #... |
| 319 | '851831745419397433012491884869454462440536895047499' #... |
| 320 | '436551974649731917170099387762871020403582994193439' #... |
| 321 | '761933412166821484015883631622539314203799034497982' #... |
| 322 | '130038741741727907429575673302461380386596501187482' #... |
| 323 | '006257527709842179336488381672818798450229339123527' #... |
| 324 | '858844448336815912020452294624916993546388956561522' #... |
| 325 | '161875352572590420823607478788399460162228308693742' #... |
| 326 | '05287663441403533948204085390898399055004119873046875e-1075', |
| 327 | |
| 328 | '525440653352955266109661060358202819561258984964913' #... |
| 329 | '892256527849758956045218257059713765874251436193619' #... |
| 330 | '443248205998870001633865657517447355992225852945912' #... |
| 331 | '016668660000210283807209850662224417504752264995360' #... |
| 332 | '631512007753855801075373057632157738752800840302596' #... |
| 333 | '237050247910530538250008682272783660778181628040733' #... |
| 334 | '653121492436408812668023478001208529190359254322340' #... |
| 335 | '397575185248844788515410722958784640926528544043090' #... |
| 336 | '115352513640884988017342469275006999104519620946430' #... |
| 337 | '818767147966495485406577703972687838176778993472989' #... |
| 338 | '561959000047036638938396333146685137903018376496408' #... |
| 339 | '319705333868476925297317136513970189073693314710318' #... |
| 340 | '991252811050501448326875232850600451776091303043715' #... |
| 341 | '157191292827614046876950225714743118291034780466325' #... |
| 342 | '085141343734564915193426994587206432697337118211527' #... |
| 343 | '278968731294639353354774788602467795167875117481660' #... |
| 344 | '4738791256853675690543663283782215866825e-1180', |
| 345 | |
Mark Dickinson | e42ffae | 2010-01-21 19:57:43 +0000 | [diff] [blame] | 346 | # exercise exit conditions in bigcomp comparison loop |
| 347 | '2602129298404963083833853479113577253105939995688e2', |
| 348 | '260212929840496308383385347911357725310593999568896e0', |
| 349 | '26021292984049630838338534791135772531059399956889601e-2', |
| 350 | '260212929840496308383385347911357725310593999568895e0', |
| 351 | '260212929840496308383385347911357725310593999568897e0', |
| 352 | '260212929840496308383385347911357725310593999568996e0', |
| 353 | '260212929840496308383385347911357725310593999568866e0', |
| 354 | # 2**53 |
| 355 | '9007199254740992.00', |
| 356 | # 2**1024 - 2**970: exact overflow boundary. All values |
| 357 | # smaller than this should round to something finite; any value |
| 358 | # greater than or equal to this one overflows. |
| 359 | '179769313486231580793728971405303415079934132710037' #... |
| 360 | '826936173778980444968292764750946649017977587207096' #... |
| 361 | '330286416692887910946555547851940402630657488671505' #... |
| 362 | '820681908902000708383676273854845817711531764475730' #... |
| 363 | '270069855571366959622842914819860834936475292719074' #... |
| 364 | '168444365510704342711559699508093042880177904174497792', |
| 365 | # 2**1024 - 2**970 - tiny |
| 366 | '179769313486231580793728971405303415079934132710037' #... |
| 367 | '826936173778980444968292764750946649017977587207096' #... |
| 368 | '330286416692887910946555547851940402630657488671505' #... |
| 369 | '820681908902000708383676273854845817711531764475730' #... |
| 370 | '270069855571366959622842914819860834936475292719074' #... |
| 371 | '168444365510704342711559699508093042880177904174497791.999', |
| 372 | # 2**1024 - 2**970 + tiny |
| 373 | '179769313486231580793728971405303415079934132710037' #... |
| 374 | '826936173778980444968292764750946649017977587207096' #... |
| 375 | '330286416692887910946555547851940402630657488671505' #... |
| 376 | '820681908902000708383676273854845817711531764475730' #... |
| 377 | '270069855571366959622842914819860834936475292719074' #... |
| 378 | '168444365510704342711559699508093042880177904174497792.001', |
| 379 | # 1 - 2**-54, +-tiny |
| 380 | '999999999999999944488848768742172978818416595458984375e-54', |
| 381 | '9999999999999999444888487687421729788184165954589843749999999e-54', |
| 382 | '9999999999999999444888487687421729788184165954589843750000001e-54', |
Mark Dickinson | 9000c16 | 2010-01-14 15:43:57 +0000 | [diff] [blame] | 383 | ] |
| 384 | for s in test_strings: |
| 385 | self.check_strtod(s) |
| 386 | |
| 387 | def test_main(): |
| 388 | test.support.run_unittest(StrtodTests) |
| 389 | |
| 390 | if __name__ == "__main__": |
| 391 | test_main() |