Benjamin Peterson | 101447b | 2015-03-09 10:37:50 -0400 | [diff] [blame] | 1 | import fractions |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 2 | import operator |
Benjamin Peterson | 101447b | 2015-03-09 10:37:50 -0400 | [diff] [blame] | 3 | import os |
| 4 | import random |
| 5 | import sys |
| 6 | import struct |
Benjamin Peterson | a915723 | 2015-03-06 09:08:44 -0500 | [diff] [blame] | 7 | import time |
Benjamin Peterson | 101447b | 2015-03-09 10:37:50 -0400 | [diff] [blame] | 8 | import unittest |
| 9 | |
| 10 | from test import support |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 11 | from test.test_grammar import (VALID_UNDERSCORE_LITERALS, |
| 12 | INVALID_UNDERSCORE_LITERALS) |
Benjamin Peterson | 101447b | 2015-03-09 10:37:50 -0400 | [diff] [blame] | 13 | from math import isinf, isnan, copysign, ldexp |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 14 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 15 | INF = float("inf") |
| 16 | NAN = float("nan") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 17 | |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 18 | have_getformat = hasattr(float, "__getformat__") |
| 19 | requires_getformat = unittest.skipUnless(have_getformat, |
| 20 | "requires __getformat__") |
| 21 | requires_setformat = unittest.skipUnless(hasattr(float, "__setformat__"), |
| 22 | "requires __setformat__") |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 23 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 24 | #locate file with float format test values |
| 25 | test_dir = os.path.dirname(__file__) or os.curdir |
| 26 | format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt') |
| 27 | |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 28 | class FloatSubclass(float): |
| 29 | pass |
| 30 | |
| 31 | class OtherFloatSubclass(float): |
| 32 | pass |
| 33 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 34 | class GeneralFloatCases(unittest.TestCase): |
| 35 | |
| 36 | def test_float(self): |
| 37 | self.assertEqual(float(3.14), 3.14) |
| 38 | self.assertEqual(float(314), 314.0) |
| 39 | self.assertEqual(float(" 3.14 "), 3.14) |
| 40 | self.assertRaises(ValueError, float, " 0x3.1 ") |
| 41 | self.assertRaises(ValueError, float, " -0x3.p-1 ") |
| 42 | self.assertRaises(ValueError, float, " +0x3.p-1 ") |
| 43 | self.assertRaises(ValueError, float, "++3.14") |
| 44 | self.assertRaises(ValueError, float, "+-3.14") |
| 45 | self.assertRaises(ValueError, float, "-+3.14") |
| 46 | self.assertRaises(ValueError, float, "--3.14") |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 47 | self.assertRaises(ValueError, float, ".nan") |
| 48 | self.assertRaises(ValueError, float, "+.inf") |
| 49 | self.assertRaises(ValueError, float, ".") |
| 50 | self.assertRaises(ValueError, float, "-.") |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 51 | self.assertRaises(TypeError, float, {}) |
Ezio Melotti | a5b9599 | 2013-11-07 19:18:34 +0200 | [diff] [blame] | 52 | self.assertRaisesRegex(TypeError, "not 'dict'", float, {}) |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 53 | # Lone surrogate |
Serhiy Storchaka | 9b6c60c | 2017-11-13 21:23:48 +0200 | [diff] [blame] | 54 | self.assertRaises(ValueError, float, '\uD8F0') |
Mark Dickinson | c2d8689 | 2010-02-12 21:18:34 +0000 | [diff] [blame] | 55 | # check that we don't accept alternate exponent markers |
| 56 | self.assertRaises(ValueError, float, "-1.7d29") |
| 57 | self.assertRaises(ValueError, float, "3D-14") |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 58 | self.assertEqual(float(" \u0663.\u0661\u0664 "), 3.14) |
| 59 | self.assertEqual(float("\N{EM SPACE}3.14\N{EN SPACE}"), 3.14) |
Mark Dickinson | a9023be | 2009-10-27 22:12:20 +0000 | [diff] [blame] | 60 | # extra long strings should not be a problem |
| 61 | float(b'.' + b'1'*1000) |
| 62 | float('.' + '1'*1000) |
INADA Naoki | 16dfca4 | 2018-07-14 12:06:43 +0900 | [diff] [blame] | 63 | # Invalid unicode string |
| 64 | # See bpo-34087 |
| 65 | self.assertRaises(ValueError, float, '\u3053\u3093\u306b\u3061\u306f') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 66 | |
Brett Cannon | a721aba | 2016-09-09 14:57:09 -0700 | [diff] [blame] | 67 | def test_underscores(self): |
| 68 | for lit in VALID_UNDERSCORE_LITERALS: |
| 69 | if not any(ch in lit for ch in 'jJxXoObB'): |
| 70 | self.assertEqual(float(lit), eval(lit)) |
| 71 | self.assertEqual(float(lit), float(lit.replace('_', ''))) |
| 72 | for lit in INVALID_UNDERSCORE_LITERALS: |
| 73 | if lit in ('0_7', '09_99'): # octals are not recognized here |
| 74 | continue |
| 75 | if not any(ch in lit for ch in 'jJxXoObB'): |
| 76 | self.assertRaises(ValueError, float, lit) |
| 77 | # Additional test cases; nan and inf are never valid as literals, |
| 78 | # only in the float() constructor, but we don't allow underscores |
| 79 | # in or around them. |
| 80 | self.assertRaises(ValueError, float, '_NaN') |
| 81 | self.assertRaises(ValueError, float, 'Na_N') |
| 82 | self.assertRaises(ValueError, float, 'IN_F') |
| 83 | self.assertRaises(ValueError, float, '-_INF') |
| 84 | self.assertRaises(ValueError, float, '-INF_') |
| 85 | # Check that we handle bytes values correctly. |
| 86 | self.assertRaises(ValueError, float, b'0_.\xff9') |
| 87 | |
Martin Panter | eeb896c | 2015-11-07 02:32:21 +0000 | [diff] [blame] | 88 | def test_non_numeric_input_types(self): |
| 89 | # Test possible non-numeric types for the argument x, including |
| 90 | # subclasses of the explicitly documented accepted types. |
| 91 | class CustomStr(str): pass |
| 92 | class CustomBytes(bytes): pass |
| 93 | class CustomByteArray(bytearray): pass |
| 94 | |
| 95 | factories = [ |
| 96 | bytes, |
| 97 | bytearray, |
| 98 | lambda b: CustomStr(b.decode()), |
| 99 | CustomBytes, |
| 100 | CustomByteArray, |
| 101 | memoryview, |
| 102 | ] |
| 103 | try: |
| 104 | from array import array |
| 105 | except ImportError: |
| 106 | pass |
| 107 | else: |
| 108 | factories.append(lambda b: array('B', b)) |
| 109 | |
| 110 | for f in factories: |
| 111 | x = f(b" 3.14 ") |
| 112 | with self.subTest(type(x)): |
| 113 | self.assertEqual(float(x), 3.14) |
| 114 | with self.assertRaisesRegex(ValueError, "could not convert"): |
| 115 | float(f(b'A' * 0x10)) |
| 116 | |
| 117 | def test_float_memoryview(self): |
| 118 | self.assertEqual(float(memoryview(b'12.3')[1:4]), 2.3) |
| 119 | self.assertEqual(float(memoryview(b'12.3\x00')[1:4]), 2.3) |
| 120 | self.assertEqual(float(memoryview(b'12.3 ')[1:4]), 2.3) |
| 121 | self.assertEqual(float(memoryview(b'12.3A')[1:4]), 2.3) |
| 122 | self.assertEqual(float(memoryview(b'12.34')[1:4]), 2.3) |
| 123 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 124 | def test_error_message(self): |
Serhiy Storchaka | 9e6ac83 | 2017-03-09 20:07:58 +0200 | [diff] [blame] | 125 | def check(s): |
| 126 | with self.assertRaises(ValueError, msg='float(%r)' % (s,)) as cm: |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 127 | float(s) |
Serhiy Storchaka | 9e6ac83 | 2017-03-09 20:07:58 +0200 | [diff] [blame] | 128 | self.assertEqual(str(cm.exception), |
| 129 | 'could not convert string to float: %r' % (s,)) |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 130 | |
Serhiy Storchaka | 9e6ac83 | 2017-03-09 20:07:58 +0200 | [diff] [blame] | 131 | check('\xbd') |
| 132 | check('123\xbd') |
| 133 | check(' 123 456 ') |
| 134 | check(b' 123 456 ') |
| 135 | |
| 136 | # non-ascii digits (error came from non-digit '!') |
| 137 | check('\u0663\u0661\u0664!') |
| 138 | # embedded NUL |
| 139 | check('123\x00') |
| 140 | check('123\x00 245') |
| 141 | check('123\x00245') |
| 142 | # byte string with embedded NUL |
| 143 | check(b'123\x00') |
| 144 | # non-UTF-8 byte string |
| 145 | check(b'123\xa0') |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 147 | @support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 148 | def test_float_with_comma(self): |
| 149 | # set locale to something that doesn't use '.' for the decimal point |
| 150 | # float must not accept the locale specific decimal point but |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 151 | # it still has to accept the normal python syntax |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 152 | import locale |
| 153 | if not locale.localeconv()['decimal_point'] == ',': |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 154 | self.skipTest('decimal_point is not ","') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 155 | |
| 156 | self.assertEqual(float(" 3.14 "), 3.14) |
| 157 | self.assertEqual(float("+3.14 "), 3.14) |
| 158 | self.assertEqual(float("-3.14 "), -3.14) |
| 159 | self.assertEqual(float(".14 "), .14) |
| 160 | self.assertEqual(float("3. "), 3.0) |
| 161 | self.assertEqual(float("3.e3 "), 3000.0) |
| 162 | self.assertEqual(float("3.2e3 "), 3200.0) |
| 163 | self.assertEqual(float("2.5e-1 "), 0.25) |
| 164 | self.assertEqual(float("5e-1"), 0.5) |
| 165 | self.assertRaises(ValueError, float, " 3,14 ") |
| 166 | self.assertRaises(ValueError, float, " +3,14 ") |
| 167 | self.assertRaises(ValueError, float, " -3,14 ") |
| 168 | self.assertRaises(ValueError, float, " 0x3.1 ") |
| 169 | self.assertRaises(ValueError, float, " -0x3.p-1 ") |
| 170 | self.assertRaises(ValueError, float, " +0x3.p-1 ") |
| 171 | self.assertEqual(float(" 25.e-1 "), 2.5) |
Eli Bendersky | 67ebabd | 2011-02-25 10:14:17 +0000 | [diff] [blame] | 172 | self.assertAlmostEqual(float(" .25e-1 "), .025) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 173 | |
| 174 | def test_floatconversion(self): |
| 175 | # Make sure that calls to __float__() work properly |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 176 | class Foo1(object): |
| 177 | def __float__(self): |
| 178 | return 42. |
| 179 | |
| 180 | class Foo2(float): |
| 181 | def __float__(self): |
| 182 | return 42. |
| 183 | |
| 184 | class Foo3(float): |
| 185 | def __new__(cls, value=0.): |
| 186 | return float.__new__(cls, 2*value) |
| 187 | |
| 188 | def __float__(self): |
| 189 | return self |
| 190 | |
| 191 | class Foo4(float): |
| 192 | def __float__(self): |
| 193 | return 42 |
| 194 | |
Benjamin Peterson | 2808d3c | 2009-04-15 21:34:27 +0000 | [diff] [blame] | 195 | # Issue 5759: __float__ not called on str subclasses (though it is on |
| 196 | # unicode subclasses). |
| 197 | class FooStr(str): |
| 198 | def __float__(self): |
| 199 | return float(str(self)) + 1 |
| 200 | |
Serhiy Storchaka | 16931c3 | 2016-06-03 21:42:55 +0300 | [diff] [blame] | 201 | self.assertEqual(float(Foo1()), 42.) |
| 202 | self.assertEqual(float(Foo2()), 42.) |
| 203 | with self.assertWarns(DeprecationWarning): |
| 204 | self.assertEqual(float(Foo3(21)), 42.) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 205 | self.assertRaises(TypeError, float, Foo4(42)) |
Serhiy Storchaka | 16931c3 | 2016-06-03 21:42:55 +0300 | [diff] [blame] | 206 | self.assertEqual(float(FooStr('8')), 9.) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 207 | |
Benjamin Peterson | a915723 | 2015-03-06 09:08:44 -0500 | [diff] [blame] | 208 | class Foo5: |
| 209 | def __float__(self): |
| 210 | return "" |
| 211 | self.assertRaises(TypeError, time.sleep, Foo5()) |
| 212 | |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 213 | # Issue #24731 |
| 214 | class F: |
| 215 | def __float__(self): |
| 216 | return OtherFloatSubclass(42.) |
Serhiy Storchaka | 16931c3 | 2016-06-03 21:42:55 +0300 | [diff] [blame] | 217 | with self.assertWarns(DeprecationWarning): |
| 218 | self.assertEqual(float(F()), 42.) |
| 219 | with self.assertWarns(DeprecationWarning): |
| 220 | self.assertIs(type(float(F())), float) |
| 221 | with self.assertWarns(DeprecationWarning): |
| 222 | self.assertEqual(FloatSubclass(F()), 42.) |
| 223 | with self.assertWarns(DeprecationWarning): |
| 224 | self.assertIs(type(FloatSubclass(F())), FloatSubclass) |
Serhiy Storchaka | 1509580 | 2015-11-25 15:47:01 +0200 | [diff] [blame] | 225 | |
Serhiy Storchaka | 58d23e6 | 2017-03-06 00:53:39 +0200 | [diff] [blame] | 226 | def test_keyword_args(self): |
Serhiy Storchaka | 2e56424 | 2017-03-06 17:01:06 +0200 | [diff] [blame] | 227 | with self.assertRaisesRegex(TypeError, 'keyword argument'): |
| 228 | float(x='3.14') |
Serhiy Storchaka | 58d23e6 | 2017-03-06 00:53:39 +0200 | [diff] [blame] | 229 | |
Benjamin Peterson | b3b8ba6 | 2011-10-28 19:42:48 -0400 | [diff] [blame] | 230 | def test_is_integer(self): |
| 231 | self.assertFalse((1.1).is_integer()) |
| 232 | self.assertTrue((1.).is_integer()) |
| 233 | self.assertFalse(float("nan").is_integer()) |
| 234 | self.assertFalse(float("inf").is_integer()) |
| 235 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 236 | def test_floatasratio(self): |
| 237 | for f, ratio in [ |
| 238 | (0.875, (7, 8)), |
| 239 | (-0.875, (-7, 8)), |
| 240 | (0.0, (0, 1)), |
| 241 | (11.5, (23, 2)), |
| 242 | ]: |
| 243 | self.assertEqual(f.as_integer_ratio(), ratio) |
| 244 | |
| 245 | for i in range(10000): |
| 246 | f = random.random() |
| 247 | f *= 10 ** random.randint(-100, 100) |
| 248 | n, d = f.as_integer_ratio() |
| 249 | self.assertEqual(float(n).__truediv__(d), f) |
| 250 | |
| 251 | R = fractions.Fraction |
| 252 | self.assertEqual(R(0, 1), |
| 253 | R(*float(0.0).as_integer_ratio())) |
| 254 | self.assertEqual(R(5, 2), |
| 255 | R(*float(2.5).as_integer_ratio())) |
| 256 | self.assertEqual(R(1, 2), |
| 257 | R(*float(0.5).as_integer_ratio())) |
| 258 | self.assertEqual(R(4728779608739021, 2251799813685248), |
| 259 | R(*float(2.1).as_integer_ratio())) |
| 260 | self.assertEqual(R(-4728779608739021, 2251799813685248), |
| 261 | R(*float(-2.1).as_integer_ratio())) |
| 262 | self.assertEqual(R(-2100, 1), |
| 263 | R(*float(-2100.0).as_integer_ratio())) |
| 264 | |
| 265 | self.assertRaises(OverflowError, float('inf').as_integer_ratio) |
| 266 | self.assertRaises(OverflowError, float('-inf').as_integer_ratio) |
| 267 | self.assertRaises(ValueError, float('nan').as_integer_ratio) |
| 268 | |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 269 | def test_float_containment(self): |
| 270 | floats = (INF, -INF, 0.0, 1.0, NAN) |
| 271 | for f in floats: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 272 | self.assertIn(f, [f]) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 273 | self.assertIn(f, (f,)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 274 | self.assertIn(f, {f}) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 275 | self.assertIn(f, {f: None}) |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 276 | self.assertEqual([f].count(f), 1, "[].count('%r') != 1" % f) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 277 | self.assertIn(f, floats) |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 278 | |
| 279 | for f in floats: |
| 280 | # nonidentical containers, same type, same contents |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 281 | self.assertTrue([f] == [f], "[%r] != [%r]" % (f, f)) |
| 282 | self.assertTrue((f,) == (f,), "(%r,) != (%r,)" % (f, f)) |
| 283 | self.assertTrue({f} == {f}, "{%r} != {%r}" % (f, f)) |
| 284 | self.assertTrue({f : None} == {f: None}, "{%r : None} != " |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 285 | "{%r : None}" % (f, f)) |
| 286 | |
| 287 | # identical containers |
| 288 | l, t, s, d = [f], (f,), {f}, {f: None} |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 289 | self.assertTrue(l == l, "[%r] not equal to itself" % f) |
| 290 | self.assertTrue(t == t, "(%r,) not equal to itself" % f) |
| 291 | self.assertTrue(s == s, "{%r} not equal to itself" % f) |
| 292 | self.assertTrue(d == d, "{%r : None} not equal to itself" % f) |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 293 | |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 294 | def assertEqualAndEqualSign(self, a, b): |
| 295 | # fail unless a == b and a and b have the same sign bit; |
| 296 | # the only difference from assertEqual is that this test |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 297 | # distinguishes -0.0 and 0.0. |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 298 | self.assertEqual((a, copysign(1.0, a)), (b, copysign(1.0, b))) |
| 299 | |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 300 | @support.requires_IEEE_754 |
Mark Dickinson | d2a9b20 | 2010-12-04 12:25:30 +0000 | [diff] [blame] | 301 | def test_float_mod(self): |
| 302 | # Check behaviour of % operator for IEEE 754 special cases. |
| 303 | # In particular, check signs of zeros. |
| 304 | mod = operator.mod |
| 305 | |
| 306 | self.assertEqualAndEqualSign(mod(-1.0, 1.0), 0.0) |
| 307 | self.assertEqualAndEqualSign(mod(-1e-100, 1.0), 1.0) |
| 308 | self.assertEqualAndEqualSign(mod(-0.0, 1.0), 0.0) |
| 309 | self.assertEqualAndEqualSign(mod(0.0, 1.0), 0.0) |
| 310 | self.assertEqualAndEqualSign(mod(1e-100, 1.0), 1e-100) |
| 311 | self.assertEqualAndEqualSign(mod(1.0, 1.0), 0.0) |
| 312 | |
| 313 | self.assertEqualAndEqualSign(mod(-1.0, -1.0), -0.0) |
| 314 | self.assertEqualAndEqualSign(mod(-1e-100, -1.0), -1e-100) |
| 315 | self.assertEqualAndEqualSign(mod(-0.0, -1.0), -0.0) |
| 316 | self.assertEqualAndEqualSign(mod(0.0, -1.0), -0.0) |
| 317 | self.assertEqualAndEqualSign(mod(1e-100, -1.0), -1.0) |
| 318 | self.assertEqualAndEqualSign(mod(1.0, -1.0), -0.0) |
| 319 | |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 320 | @support.requires_IEEE_754 |
Mark Dickinson | 9ab44b5 | 2009-12-30 16:22:49 +0000 | [diff] [blame] | 321 | def test_float_pow(self): |
| 322 | # test builtin pow and ** operator for IEEE 754 special cases. |
| 323 | # Special cases taken from section F.9.4.4 of the C99 specification |
| 324 | |
| 325 | for pow_op in pow, operator.pow: |
| 326 | # x**NAN is NAN for any x except 1 |
| 327 | self.assertTrue(isnan(pow_op(-INF, NAN))) |
| 328 | self.assertTrue(isnan(pow_op(-2.0, NAN))) |
| 329 | self.assertTrue(isnan(pow_op(-1.0, NAN))) |
| 330 | self.assertTrue(isnan(pow_op(-0.5, NAN))) |
| 331 | self.assertTrue(isnan(pow_op(-0.0, NAN))) |
| 332 | self.assertTrue(isnan(pow_op(0.0, NAN))) |
| 333 | self.assertTrue(isnan(pow_op(0.5, NAN))) |
| 334 | self.assertTrue(isnan(pow_op(2.0, NAN))) |
| 335 | self.assertTrue(isnan(pow_op(INF, NAN))) |
| 336 | self.assertTrue(isnan(pow_op(NAN, NAN))) |
| 337 | |
| 338 | # NAN**y is NAN for any y except +-0 |
| 339 | self.assertTrue(isnan(pow_op(NAN, -INF))) |
| 340 | self.assertTrue(isnan(pow_op(NAN, -2.0))) |
| 341 | self.assertTrue(isnan(pow_op(NAN, -1.0))) |
| 342 | self.assertTrue(isnan(pow_op(NAN, -0.5))) |
| 343 | self.assertTrue(isnan(pow_op(NAN, 0.5))) |
| 344 | self.assertTrue(isnan(pow_op(NAN, 1.0))) |
| 345 | self.assertTrue(isnan(pow_op(NAN, 2.0))) |
| 346 | self.assertTrue(isnan(pow_op(NAN, INF))) |
| 347 | |
| 348 | # (+-0)**y raises ZeroDivisionError for y a negative odd integer |
| 349 | self.assertRaises(ZeroDivisionError, pow_op, -0.0, -1.0) |
| 350 | self.assertRaises(ZeroDivisionError, pow_op, 0.0, -1.0) |
| 351 | |
| 352 | # (+-0)**y raises ZeroDivisionError for y finite and negative |
| 353 | # but not an odd integer |
| 354 | self.assertRaises(ZeroDivisionError, pow_op, -0.0, -2.0) |
| 355 | self.assertRaises(ZeroDivisionError, pow_op, -0.0, -0.5) |
| 356 | self.assertRaises(ZeroDivisionError, pow_op, 0.0, -2.0) |
| 357 | self.assertRaises(ZeroDivisionError, pow_op, 0.0, -0.5) |
| 358 | |
| 359 | # (+-0)**y is +-0 for y a positive odd integer |
| 360 | self.assertEqualAndEqualSign(pow_op(-0.0, 1.0), -0.0) |
| 361 | self.assertEqualAndEqualSign(pow_op(0.0, 1.0), 0.0) |
| 362 | |
| 363 | # (+-0)**y is 0 for y finite and positive but not an odd integer |
| 364 | self.assertEqualAndEqualSign(pow_op(-0.0, 0.5), 0.0) |
| 365 | self.assertEqualAndEqualSign(pow_op(-0.0, 2.0), 0.0) |
| 366 | self.assertEqualAndEqualSign(pow_op(0.0, 0.5), 0.0) |
| 367 | self.assertEqualAndEqualSign(pow_op(0.0, 2.0), 0.0) |
| 368 | |
| 369 | # (-1)**+-inf is 1 |
| 370 | self.assertEqualAndEqualSign(pow_op(-1.0, -INF), 1.0) |
| 371 | self.assertEqualAndEqualSign(pow_op(-1.0, INF), 1.0) |
| 372 | |
| 373 | # 1**y is 1 for any y, even if y is an infinity or nan |
| 374 | self.assertEqualAndEqualSign(pow_op(1.0, -INF), 1.0) |
| 375 | self.assertEqualAndEqualSign(pow_op(1.0, -2.0), 1.0) |
| 376 | self.assertEqualAndEqualSign(pow_op(1.0, -1.0), 1.0) |
| 377 | self.assertEqualAndEqualSign(pow_op(1.0, -0.5), 1.0) |
| 378 | self.assertEqualAndEqualSign(pow_op(1.0, -0.0), 1.0) |
| 379 | self.assertEqualAndEqualSign(pow_op(1.0, 0.0), 1.0) |
| 380 | self.assertEqualAndEqualSign(pow_op(1.0, 0.5), 1.0) |
| 381 | self.assertEqualAndEqualSign(pow_op(1.0, 1.0), 1.0) |
| 382 | self.assertEqualAndEqualSign(pow_op(1.0, 2.0), 1.0) |
| 383 | self.assertEqualAndEqualSign(pow_op(1.0, INF), 1.0) |
| 384 | self.assertEqualAndEqualSign(pow_op(1.0, NAN), 1.0) |
| 385 | |
| 386 | # x**+-0 is 1 for any x, even if x is a zero, infinity, or nan |
| 387 | self.assertEqualAndEqualSign(pow_op(-INF, 0.0), 1.0) |
| 388 | self.assertEqualAndEqualSign(pow_op(-2.0, 0.0), 1.0) |
| 389 | self.assertEqualAndEqualSign(pow_op(-1.0, 0.0), 1.0) |
| 390 | self.assertEqualAndEqualSign(pow_op(-0.5, 0.0), 1.0) |
| 391 | self.assertEqualAndEqualSign(pow_op(-0.0, 0.0), 1.0) |
| 392 | self.assertEqualAndEqualSign(pow_op(0.0, 0.0), 1.0) |
| 393 | self.assertEqualAndEqualSign(pow_op(0.5, 0.0), 1.0) |
| 394 | self.assertEqualAndEqualSign(pow_op(1.0, 0.0), 1.0) |
| 395 | self.assertEqualAndEqualSign(pow_op(2.0, 0.0), 1.0) |
| 396 | self.assertEqualAndEqualSign(pow_op(INF, 0.0), 1.0) |
| 397 | self.assertEqualAndEqualSign(pow_op(NAN, 0.0), 1.0) |
| 398 | self.assertEqualAndEqualSign(pow_op(-INF, -0.0), 1.0) |
| 399 | self.assertEqualAndEqualSign(pow_op(-2.0, -0.0), 1.0) |
| 400 | self.assertEqualAndEqualSign(pow_op(-1.0, -0.0), 1.0) |
| 401 | self.assertEqualAndEqualSign(pow_op(-0.5, -0.0), 1.0) |
| 402 | self.assertEqualAndEqualSign(pow_op(-0.0, -0.0), 1.0) |
| 403 | self.assertEqualAndEqualSign(pow_op(0.0, -0.0), 1.0) |
| 404 | self.assertEqualAndEqualSign(pow_op(0.5, -0.0), 1.0) |
| 405 | self.assertEqualAndEqualSign(pow_op(1.0, -0.0), 1.0) |
| 406 | self.assertEqualAndEqualSign(pow_op(2.0, -0.0), 1.0) |
| 407 | self.assertEqualAndEqualSign(pow_op(INF, -0.0), 1.0) |
| 408 | self.assertEqualAndEqualSign(pow_op(NAN, -0.0), 1.0) |
| 409 | |
| 410 | # x**y defers to complex pow for finite negative x and |
| 411 | # non-integral y. |
| 412 | self.assertEqual(type(pow_op(-2.0, -0.5)), complex) |
| 413 | self.assertEqual(type(pow_op(-2.0, 0.5)), complex) |
| 414 | self.assertEqual(type(pow_op(-1.0, -0.5)), complex) |
| 415 | self.assertEqual(type(pow_op(-1.0, 0.5)), complex) |
| 416 | self.assertEqual(type(pow_op(-0.5, -0.5)), complex) |
| 417 | self.assertEqual(type(pow_op(-0.5, 0.5)), complex) |
| 418 | |
| 419 | # x**-INF is INF for abs(x) < 1 |
| 420 | self.assertEqualAndEqualSign(pow_op(-0.5, -INF), INF) |
| 421 | self.assertEqualAndEqualSign(pow_op(-0.0, -INF), INF) |
| 422 | self.assertEqualAndEqualSign(pow_op(0.0, -INF), INF) |
| 423 | self.assertEqualAndEqualSign(pow_op(0.5, -INF), INF) |
| 424 | |
| 425 | # x**-INF is 0 for abs(x) > 1 |
| 426 | self.assertEqualAndEqualSign(pow_op(-INF, -INF), 0.0) |
| 427 | self.assertEqualAndEqualSign(pow_op(-2.0, -INF), 0.0) |
| 428 | self.assertEqualAndEqualSign(pow_op(2.0, -INF), 0.0) |
| 429 | self.assertEqualAndEqualSign(pow_op(INF, -INF), 0.0) |
| 430 | |
| 431 | # x**INF is 0 for abs(x) < 1 |
| 432 | self.assertEqualAndEqualSign(pow_op(-0.5, INF), 0.0) |
| 433 | self.assertEqualAndEqualSign(pow_op(-0.0, INF), 0.0) |
| 434 | self.assertEqualAndEqualSign(pow_op(0.0, INF), 0.0) |
| 435 | self.assertEqualAndEqualSign(pow_op(0.5, INF), 0.0) |
| 436 | |
| 437 | # x**INF is INF for abs(x) > 1 |
| 438 | self.assertEqualAndEqualSign(pow_op(-INF, INF), INF) |
| 439 | self.assertEqualAndEqualSign(pow_op(-2.0, INF), INF) |
| 440 | self.assertEqualAndEqualSign(pow_op(2.0, INF), INF) |
| 441 | self.assertEqualAndEqualSign(pow_op(INF, INF), INF) |
| 442 | |
| 443 | # (-INF)**y is -0.0 for y a negative odd integer |
| 444 | self.assertEqualAndEqualSign(pow_op(-INF, -1.0), -0.0) |
| 445 | |
| 446 | # (-INF)**y is 0.0 for y negative but not an odd integer |
| 447 | self.assertEqualAndEqualSign(pow_op(-INF, -0.5), 0.0) |
| 448 | self.assertEqualAndEqualSign(pow_op(-INF, -2.0), 0.0) |
| 449 | |
| 450 | # (-INF)**y is -INF for y a positive odd integer |
| 451 | self.assertEqualAndEqualSign(pow_op(-INF, 1.0), -INF) |
| 452 | |
| 453 | # (-INF)**y is INF for y positive but not an odd integer |
| 454 | self.assertEqualAndEqualSign(pow_op(-INF, 0.5), INF) |
| 455 | self.assertEqualAndEqualSign(pow_op(-INF, 2.0), INF) |
| 456 | |
| 457 | # INF**y is INF for y positive |
| 458 | self.assertEqualAndEqualSign(pow_op(INF, 0.5), INF) |
| 459 | self.assertEqualAndEqualSign(pow_op(INF, 1.0), INF) |
| 460 | self.assertEqualAndEqualSign(pow_op(INF, 2.0), INF) |
| 461 | |
| 462 | # INF**y is 0.0 for y negative |
| 463 | self.assertEqualAndEqualSign(pow_op(INF, -2.0), 0.0) |
| 464 | self.assertEqualAndEqualSign(pow_op(INF, -1.0), 0.0) |
| 465 | self.assertEqualAndEqualSign(pow_op(INF, -0.5), 0.0) |
| 466 | |
| 467 | # basic checks not covered by the special cases above |
| 468 | self.assertEqualAndEqualSign(pow_op(-2.0, -2.0), 0.25) |
| 469 | self.assertEqualAndEqualSign(pow_op(-2.0, -1.0), -0.5) |
| 470 | self.assertEqualAndEqualSign(pow_op(-2.0, -0.0), 1.0) |
| 471 | self.assertEqualAndEqualSign(pow_op(-2.0, 0.0), 1.0) |
| 472 | self.assertEqualAndEqualSign(pow_op(-2.0, 1.0), -2.0) |
| 473 | self.assertEqualAndEqualSign(pow_op(-2.0, 2.0), 4.0) |
| 474 | self.assertEqualAndEqualSign(pow_op(-1.0, -2.0), 1.0) |
| 475 | self.assertEqualAndEqualSign(pow_op(-1.0, -1.0), -1.0) |
| 476 | self.assertEqualAndEqualSign(pow_op(-1.0, -0.0), 1.0) |
| 477 | self.assertEqualAndEqualSign(pow_op(-1.0, 0.0), 1.0) |
| 478 | self.assertEqualAndEqualSign(pow_op(-1.0, 1.0), -1.0) |
| 479 | self.assertEqualAndEqualSign(pow_op(-1.0, 2.0), 1.0) |
| 480 | self.assertEqualAndEqualSign(pow_op(2.0, -2.0), 0.25) |
| 481 | self.assertEqualAndEqualSign(pow_op(2.0, -1.0), 0.5) |
| 482 | self.assertEqualAndEqualSign(pow_op(2.0, -0.0), 1.0) |
| 483 | self.assertEqualAndEqualSign(pow_op(2.0, 0.0), 1.0) |
| 484 | self.assertEqualAndEqualSign(pow_op(2.0, 1.0), 2.0) |
| 485 | self.assertEqualAndEqualSign(pow_op(2.0, 2.0), 4.0) |
| 486 | |
| 487 | # 1 ** large and -1 ** large; some libms apparently |
| 488 | # have problems with these |
| 489 | self.assertEqualAndEqualSign(pow_op(1.0, -1e100), 1.0) |
| 490 | self.assertEqualAndEqualSign(pow_op(1.0, 1e100), 1.0) |
| 491 | self.assertEqualAndEqualSign(pow_op(-1.0, -1e100), 1.0) |
| 492 | self.assertEqualAndEqualSign(pow_op(-1.0, 1e100), 1.0) |
| 493 | |
| 494 | # check sign for results that underflow to 0 |
| 495 | self.assertEqualAndEqualSign(pow_op(-2.0, -2000.0), 0.0) |
| 496 | self.assertEqual(type(pow_op(-2.0, -2000.5)), complex) |
| 497 | self.assertEqualAndEqualSign(pow_op(-2.0, -2001.0), -0.0) |
| 498 | self.assertEqualAndEqualSign(pow_op(2.0, -2000.0), 0.0) |
| 499 | self.assertEqualAndEqualSign(pow_op(2.0, -2000.5), 0.0) |
| 500 | self.assertEqualAndEqualSign(pow_op(2.0, -2001.0), 0.0) |
| 501 | self.assertEqualAndEqualSign(pow_op(-0.5, 2000.0), 0.0) |
| 502 | self.assertEqual(type(pow_op(-0.5, 2000.5)), complex) |
| 503 | self.assertEqualAndEqualSign(pow_op(-0.5, 2001.0), -0.0) |
| 504 | self.assertEqualAndEqualSign(pow_op(0.5, 2000.0), 0.0) |
| 505 | self.assertEqualAndEqualSign(pow_op(0.5, 2000.5), 0.0) |
| 506 | self.assertEqualAndEqualSign(pow_op(0.5, 2001.0), 0.0) |
| 507 | |
| 508 | # check we don't raise an exception for subnormal results, |
| 509 | # and validate signs. Tests currently disabled, since |
| 510 | # they fail on systems where a subnormal result from pow |
| 511 | # is flushed to zero (e.g. Debian/ia64.) |
| 512 | #self.assertTrue(0.0 < pow_op(0.5, 1048) < 1e-315) |
| 513 | #self.assertTrue(0.0 < pow_op(-0.5, 1048) < 1e-315) |
| 514 | #self.assertTrue(0.0 < pow_op(0.5, 1047) < 1e-315) |
| 515 | #self.assertTrue(0.0 > pow_op(-0.5, 1047) > -1e-315) |
| 516 | #self.assertTrue(0.0 < pow_op(2.0, -1048) < 1e-315) |
| 517 | #self.assertTrue(0.0 < pow_op(-2.0, -1048) < 1e-315) |
| 518 | #self.assertTrue(0.0 < pow_op(2.0, -1047) < 1e-315) |
| 519 | #self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315) |
Mark Dickinson | 4a1f593 | 2008-11-12 23:23:36 +0000 | [diff] [blame] | 520 | |
| 521 | |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 522 | @requires_setformat |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 523 | class FormatFunctionsTestCase(unittest.TestCase): |
| 524 | |
| 525 | def setUp(self): |
| 526 | self.save_formats = {'double':float.__getformat__('double'), |
| 527 | 'float':float.__getformat__('float')} |
| 528 | |
| 529 | def tearDown(self): |
| 530 | float.__setformat__('double', self.save_formats['double']) |
| 531 | float.__setformat__('float', self.save_formats['float']) |
| 532 | |
| 533 | def test_getformat(self): |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 534 | self.assertIn(float.__getformat__('double'), |
| 535 | ['unknown', 'IEEE, big-endian', 'IEEE, little-endian']) |
| 536 | self.assertIn(float.__getformat__('float'), |
| 537 | ['unknown', 'IEEE, big-endian', 'IEEE, little-endian']) |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 538 | self.assertRaises(ValueError, float.__getformat__, 'chicken') |
| 539 | self.assertRaises(TypeError, float.__getformat__, 1) |
| 540 | |
| 541 | def test_setformat(self): |
| 542 | for t in 'double', 'float': |
| 543 | float.__setformat__(t, 'unknown') |
| 544 | if self.save_formats[t] == 'IEEE, big-endian': |
| 545 | self.assertRaises(ValueError, float.__setformat__, |
| 546 | t, 'IEEE, little-endian') |
| 547 | elif self.save_formats[t] == 'IEEE, little-endian': |
| 548 | self.assertRaises(ValueError, float.__setformat__, |
| 549 | t, 'IEEE, big-endian') |
| 550 | else: |
| 551 | self.assertRaises(ValueError, float.__setformat__, |
| 552 | t, 'IEEE, big-endian') |
| 553 | self.assertRaises(ValueError, float.__setformat__, |
| 554 | t, 'IEEE, little-endian') |
| 555 | self.assertRaises(ValueError, float.__setformat__, |
| 556 | t, 'chicken') |
| 557 | self.assertRaises(ValueError, float.__setformat__, |
| 558 | 'chicken', 'unknown') |
| 559 | |
Guido van Rossum | 2be161d | 2007-05-15 20:43:51 +0000 | [diff] [blame] | 560 | BE_DOUBLE_INF = b'\x7f\xf0\x00\x00\x00\x00\x00\x00' |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 561 | LE_DOUBLE_INF = bytes(reversed(BE_DOUBLE_INF)) |
Guido van Rossum | 2be161d | 2007-05-15 20:43:51 +0000 | [diff] [blame] | 562 | BE_DOUBLE_NAN = b'\x7f\xf8\x00\x00\x00\x00\x00\x00' |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 563 | LE_DOUBLE_NAN = bytes(reversed(BE_DOUBLE_NAN)) |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 564 | |
Guido van Rossum | 2be161d | 2007-05-15 20:43:51 +0000 | [diff] [blame] | 565 | BE_FLOAT_INF = b'\x7f\x80\x00\x00' |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 566 | LE_FLOAT_INF = bytes(reversed(BE_FLOAT_INF)) |
Guido van Rossum | 2be161d | 2007-05-15 20:43:51 +0000 | [diff] [blame] | 567 | BE_FLOAT_NAN = b'\x7f\xc0\x00\x00' |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 568 | LE_FLOAT_NAN = bytes(reversed(BE_FLOAT_NAN)) |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 569 | |
| 570 | # on non-IEEE platforms, attempting to unpack a bit pattern |
| 571 | # representing an infinity or a NaN should raise an exception. |
| 572 | |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 573 | @requires_setformat |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 574 | class UnknownFormatTestCase(unittest.TestCase): |
| 575 | def setUp(self): |
| 576 | self.save_formats = {'double':float.__getformat__('double'), |
| 577 | 'float':float.__getformat__('float')} |
| 578 | float.__setformat__('double', 'unknown') |
| 579 | float.__setformat__('float', 'unknown') |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 580 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 581 | def tearDown(self): |
| 582 | float.__setformat__('double', self.save_formats['double']) |
| 583 | float.__setformat__('float', self.save_formats['float']) |
| 584 | |
| 585 | def test_double_specials_dont_unpack(self): |
| 586 | for fmt, data in [('>d', BE_DOUBLE_INF), |
| 587 | ('>d', BE_DOUBLE_NAN), |
| 588 | ('<d', LE_DOUBLE_INF), |
| 589 | ('<d', LE_DOUBLE_NAN)]: |
| 590 | self.assertRaises(ValueError, struct.unpack, fmt, data) |
| 591 | |
| 592 | def test_float_specials_dont_unpack(self): |
| 593 | for fmt, data in [('>f', BE_FLOAT_INF), |
| 594 | ('>f', BE_FLOAT_NAN), |
| 595 | ('<f', LE_FLOAT_INF), |
| 596 | ('<f', LE_FLOAT_NAN)]: |
| 597 | self.assertRaises(ValueError, struct.unpack, fmt, data) |
| 598 | |
| 599 | |
| 600 | # on an IEEE platform, all we guarantee is that bit patterns |
| 601 | # representing infinities or NaNs do not raise an exception; all else |
| 602 | # is accident (today). |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 603 | # let's also try to guarantee that -0.0 and 0.0 don't get confused. |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 604 | |
| 605 | class IEEEFormatTestCase(unittest.TestCase): |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 606 | |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 607 | @support.requires_IEEE_754 |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 608 | def test_double_specials_do_unpack(self): |
| 609 | for fmt, data in [('>d', BE_DOUBLE_INF), |
| 610 | ('>d', BE_DOUBLE_NAN), |
| 611 | ('<d', LE_DOUBLE_INF), |
| 612 | ('<d', LE_DOUBLE_NAN)]: |
| 613 | struct.unpack(fmt, data) |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 614 | |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 615 | @support.requires_IEEE_754 |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 616 | def test_float_specials_do_unpack(self): |
| 617 | for fmt, data in [('>f', BE_FLOAT_INF), |
| 618 | ('>f', BE_FLOAT_NAN), |
| 619 | ('<f', LE_FLOAT_INF), |
| 620 | ('<f', LE_FLOAT_NAN)]: |
| 621 | struct.unpack(fmt, data) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 622 | |
Benjamin Peterson | 2bb69a5 | 2017-09-10 23:50:46 -0700 | [diff] [blame] | 623 | @support.requires_IEEE_754 |
| 624 | def test_serialized_float_rounding(self): |
| 625 | from _testcapi import FLT_MAX |
| 626 | self.assertEqual(struct.pack("<f", 3.40282356e38), struct.pack("<f", FLT_MAX)) |
| 627 | self.assertEqual(struct.pack("<f", -3.40282356e38), struct.pack("<f", -FLT_MAX)) |
| 628 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 629 | class FormatTestCase(unittest.TestCase): |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 630 | |
Eric Smith | 11fe3e0 | 2007-08-31 01:33:06 +0000 | [diff] [blame] | 631 | def test_format(self): |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 632 | # these should be rewritten to use both format(x, spec) and |
| 633 | # x.__format__(spec) |
| 634 | |
| 635 | self.assertEqual(format(0.0, 'f'), '0.000000') |
| 636 | |
| 637 | # the default is 'g', except for empty format spec |
| 638 | self.assertEqual(format(0.0, ''), '0.0') |
| 639 | self.assertEqual(format(0.01, ''), '0.01') |
| 640 | self.assertEqual(format(0.01, 'g'), '0.01') |
| 641 | |
Eric Smith | 6337622 | 2009-05-05 14:04:18 +0000 | [diff] [blame] | 642 | # empty presentation type should format in the same way as str |
| 643 | # (issue 5920) |
| 644 | x = 100/7. |
| 645 | self.assertEqual(format(x, ''), str(x)) |
| 646 | self.assertEqual(format(x, '-'), str(x)) |
| 647 | self.assertEqual(format(x, '>'), str(x)) |
| 648 | self.assertEqual(format(x, '2'), str(x)) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 649 | |
| 650 | self.assertEqual(format(1.0, 'f'), '1.000000') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 651 | |
| 652 | self.assertEqual(format(-1.0, 'f'), '-1.000000') |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 653 | |
| 654 | self.assertEqual(format( 1.0, ' f'), ' 1.000000') |
| 655 | self.assertEqual(format(-1.0, ' f'), '-1.000000') |
| 656 | self.assertEqual(format( 1.0, '+f'), '+1.000000') |
| 657 | self.assertEqual(format(-1.0, '+f'), '-1.000000') |
| 658 | |
| 659 | # % formatting |
| 660 | self.assertEqual(format(-1.0, '%'), '-100.000000%') |
| 661 | |
| 662 | # conversion to string should fail |
| 663 | self.assertRaises(ValueError, format, 3.0, "s") |
| 664 | |
Eric Smith | 7b69c6c | 2008-01-27 21:07:59 +0000 | [diff] [blame] | 665 | # other format specifiers shouldn't work on floats, |
| 666 | # in particular int specifiers |
| 667 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 668 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 669 | if not format_spec in 'eEfFgGn%': |
| 670 | self.assertRaises(ValueError, format, 0.0, format_spec) |
| 671 | self.assertRaises(ValueError, format, 1.0, format_spec) |
| 672 | self.assertRaises(ValueError, format, -1.0, format_spec) |
| 673 | self.assertRaises(ValueError, format, 1e100, format_spec) |
| 674 | self.assertRaises(ValueError, format, -1e100, format_spec) |
| 675 | self.assertRaises(ValueError, format, 1e-100, format_spec) |
| 676 | self.assertRaises(ValueError, format, -1e-100, format_spec) |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 677 | |
Eric Smith | 741191f | 2009-05-06 13:08:15 +0000 | [diff] [blame] | 678 | # issue 3382 |
| 679 | self.assertEqual(format(NAN, 'f'), 'nan') |
| 680 | self.assertEqual(format(NAN, 'F'), 'NAN') |
| 681 | self.assertEqual(format(INF, 'f'), 'inf') |
| 682 | self.assertEqual(format(INF, 'F'), 'INF') |
| 683 | |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 684 | @support.requires_IEEE_754 |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 685 | def test_format_testfile(self): |
Brian Curtin | 076623b | 2010-10-31 00:03:45 +0000 | [diff] [blame] | 686 | with open(format_testfile) as testfile: |
| 687 | for line in testfile: |
| 688 | if line.startswith('--'): |
| 689 | continue |
| 690 | line = line.strip() |
| 691 | if not line: |
| 692 | continue |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 693 | |
Brian Curtin | 076623b | 2010-10-31 00:03:45 +0000 | [diff] [blame] | 694 | lhs, rhs = map(str.strip, line.split('->')) |
| 695 | fmt, arg = lhs.split() |
| 696 | self.assertEqual(fmt % float(arg), rhs) |
| 697 | self.assertEqual(fmt % -float(arg), '-' + rhs) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 698 | |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 699 | def test_issue5864(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 700 | self.assertEqual(format(123.456, '.4'), '123.5') |
| 701 | self.assertEqual(format(1234.56, '.4'), '1.235e+03') |
| 702 | self.assertEqual(format(12345.6, '.4'), '1.235e+04') |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 703 | |
Xtreak | 3f7983a | 2019-01-07 20:39:14 +0530 | [diff] [blame] | 704 | def test_issue35560(self): |
| 705 | self.assertEqual(format(123.0, '00'), '123.0') |
| 706 | self.assertEqual(format(123.34, '00f'), '123.340000') |
| 707 | self.assertEqual(format(123.34, '00e'), '1.233400e+02') |
| 708 | self.assertEqual(format(123.34, '00g'), '123.34') |
| 709 | self.assertEqual(format(123.34, '00.10f'), '123.3400000000') |
| 710 | self.assertEqual(format(123.34, '00.10e'), '1.2334000000e+02') |
| 711 | self.assertEqual(format(123.34, '00.10g'), '123.34') |
| 712 | self.assertEqual(format(123.34, '01f'), '123.340000') |
| 713 | |
| 714 | self.assertEqual(format(-123.0, '00'), '-123.0') |
| 715 | self.assertEqual(format(-123.34, '00f'), '-123.340000') |
| 716 | self.assertEqual(format(-123.34, '00e'), '-1.233400e+02') |
| 717 | self.assertEqual(format(-123.34, '00g'), '-123.34') |
| 718 | self.assertEqual(format(-123.34, '00.10f'), '-123.3400000000') |
| 719 | self.assertEqual(format(-123.34, '00.10f'), '-123.3400000000') |
| 720 | self.assertEqual(format(-123.34, '00.10e'), '-1.2334000000e+02') |
| 721 | self.assertEqual(format(-123.34, '00.10g'), '-123.34') |
| 722 | |
Mark Dickinson | 7efad9e | 2009-04-17 20:59:58 +0000 | [diff] [blame] | 723 | class ReprTestCase(unittest.TestCase): |
| 724 | def test_repr(self): |
Serhiy Storchaka | 5b10b98 | 2019-03-05 10:06:26 +0200 | [diff] [blame] | 725 | with open(os.path.join(os.path.split(__file__)[0], |
| 726 | 'floating_points.txt')) as floats_file: |
| 727 | for line in floats_file: |
| 728 | line = line.strip() |
| 729 | if not line or line.startswith('#'): |
| 730 | continue |
| 731 | v = eval(line) |
| 732 | self.assertEqual(v, eval(repr(v))) |
Mark Dickinson | 7efad9e | 2009-04-17 20:59:58 +0000 | [diff] [blame] | 733 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 734 | @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', |
| 735 | "applies only when using short float repr style") |
| 736 | def test_short_repr(self): |
| 737 | # test short float repr introduced in Python 3.1. One aspect |
| 738 | # of this repr is that we get some degree of str -> float -> |
| 739 | # str roundtripping. In particular, for any numeric string |
| 740 | # containing 15 or fewer significant digits, those exact same |
| 741 | # digits (modulo trailing zeros) should appear in the output. |
| 742 | # No more repr(0.03) -> "0.029999999999999999"! |
| 743 | |
| 744 | test_strings = [ |
| 745 | # output always includes *either* a decimal point and at |
| 746 | # least one digit after that point, or an exponent. |
| 747 | '0.0', |
| 748 | '1.0', |
| 749 | '0.01', |
| 750 | '0.02', |
| 751 | '0.03', |
| 752 | '0.04', |
| 753 | '0.05', |
| 754 | '1.23456789', |
| 755 | '10.0', |
| 756 | '100.0', |
| 757 | # values >= 1e16 get an exponent... |
| 758 | '1000000000000000.0', |
| 759 | '9999999999999990.0', |
| 760 | '1e+16', |
| 761 | '1e+17', |
| 762 | # ... and so do values < 1e-4 |
| 763 | '0.001', |
| 764 | '0.001001', |
| 765 | '0.00010000000000001', |
| 766 | '0.0001', |
| 767 | '9.999999999999e-05', |
| 768 | '1e-05', |
| 769 | # values designed to provoke failure if the FPU rounding |
| 770 | # precision isn't set correctly |
| 771 | '8.72293771110361e+25', |
| 772 | '7.47005307342313e+26', |
| 773 | '2.86438000439698e+28', |
| 774 | '8.89142905246179e+28', |
| 775 | '3.08578087079232e+35', |
| 776 | ] |
| 777 | |
| 778 | for s in test_strings: |
| 779 | negs = '-'+s |
| 780 | self.assertEqual(s, repr(float(s))) |
| 781 | self.assertEqual(negs, repr(float(negs))) |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 782 | # Since Python 3.2, repr and str are identical |
| 783 | self.assertEqual(repr(float(s)), str(float(s))) |
| 784 | self.assertEqual(repr(float(negs)), str(float(negs))) |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 785 | |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 786 | @support.requires_IEEE_754 |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 787 | class RoundTestCase(unittest.TestCase): |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 788 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 789 | def test_inf_nan(self): |
| 790 | self.assertRaises(OverflowError, round, INF) |
| 791 | self.assertRaises(OverflowError, round, -INF) |
| 792 | self.assertRaises(ValueError, round, NAN) |
Mark Dickinson | 4ca33d1 | 2009-11-24 10:59:34 +0000 | [diff] [blame] | 793 | self.assertRaises(TypeError, round, INF, 0.0) |
| 794 | self.assertRaises(TypeError, round, -INF, 1.0) |
| 795 | self.assertRaises(TypeError, round, NAN, "ceci n'est pas un integer") |
| 796 | self.assertRaises(TypeError, round, -0.0, 1j) |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 797 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 798 | def test_large_n(self): |
| 799 | for n in [324, 325, 400, 2**31-1, 2**31, 2**32, 2**100]: |
| 800 | self.assertEqual(round(123.456, n), 123.456) |
| 801 | self.assertEqual(round(-123.456, n), -123.456) |
| 802 | self.assertEqual(round(1e300, n), 1e300) |
| 803 | self.assertEqual(round(1e-320, n), 1e-320) |
| 804 | self.assertEqual(round(1e150, 300), 1e150) |
| 805 | self.assertEqual(round(1e300, 307), 1e300) |
| 806 | self.assertEqual(round(-3.1415, 308), -3.1415) |
| 807 | self.assertEqual(round(1e150, 309), 1e150) |
| 808 | self.assertEqual(round(1.4e-315, 315), 1e-315) |
| 809 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 810 | def test_small_n(self): |
| 811 | for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]: |
| 812 | self.assertEqual(round(123.456, n), 0.0) |
| 813 | self.assertEqual(round(-123.456, n), -0.0) |
| 814 | self.assertEqual(round(1e300, n), 0.0) |
| 815 | self.assertEqual(round(1e-320, n), 0.0) |
| 816 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 817 | def test_overflow(self): |
| 818 | self.assertRaises(OverflowError, round, 1.6e308, -308) |
| 819 | self.assertRaises(OverflowError, round, -1.7e308, -308) |
| 820 | |
| 821 | @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', |
| 822 | "applies only when using short float repr style") |
| 823 | def test_previous_round_bugs(self): |
| 824 | # particular cases that have occurred in bug reports |
| 825 | self.assertEqual(round(562949953421312.5, 1), |
| 826 | 562949953421312.5) |
| 827 | self.assertEqual(round(56294995342131.5, 3), |
| 828 | 56294995342131.5) |
| 829 | # round-half-even |
| 830 | self.assertEqual(round(25.0, -1), 20.0) |
| 831 | self.assertEqual(round(35.0, -1), 40.0) |
| 832 | self.assertEqual(round(45.0, -1), 40.0) |
| 833 | self.assertEqual(round(55.0, -1), 60.0) |
| 834 | self.assertEqual(round(65.0, -1), 60.0) |
| 835 | self.assertEqual(round(75.0, -1), 80.0) |
| 836 | self.assertEqual(round(85.0, -1), 80.0) |
| 837 | self.assertEqual(round(95.0, -1), 100.0) |
| 838 | |
| 839 | @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', |
| 840 | "applies only when using short float repr style") |
| 841 | def test_matches_float_format(self): |
| 842 | # round should give the same results as float formatting |
| 843 | for i in range(500): |
| 844 | x = i/1000. |
| 845 | self.assertEqual(float(format(x, '.0f')), round(x, 0)) |
| 846 | self.assertEqual(float(format(x, '.1f')), round(x, 1)) |
| 847 | self.assertEqual(float(format(x, '.2f')), round(x, 2)) |
| 848 | self.assertEqual(float(format(x, '.3f')), round(x, 3)) |
| 849 | |
| 850 | for i in range(5, 5000, 10): |
| 851 | x = i/1000. |
| 852 | self.assertEqual(float(format(x, '.0f')), round(x, 0)) |
| 853 | self.assertEqual(float(format(x, '.1f')), round(x, 1)) |
| 854 | self.assertEqual(float(format(x, '.2f')), round(x, 2)) |
| 855 | self.assertEqual(float(format(x, '.3f')), round(x, 3)) |
| 856 | |
| 857 | for i in range(500): |
| 858 | x = random.random() |
| 859 | self.assertEqual(float(format(x, '.0f')), round(x, 0)) |
| 860 | self.assertEqual(float(format(x, '.1f')), round(x, 1)) |
| 861 | self.assertEqual(float(format(x, '.2f')), round(x, 2)) |
| 862 | self.assertEqual(float(format(x, '.3f')), round(x, 3)) |
| 863 | |
Eric Smith | 8a10ecc | 2009-12-02 17:58:24 +0000 | [diff] [blame] | 864 | def test_format_specials(self): |
| 865 | # Test formatting of nans and infs. |
| 866 | |
| 867 | def test(fmt, value, expected): |
| 868 | # Test with both % and format(). |
| 869 | self.assertEqual(fmt % value, expected, fmt) |
Eric Smith | 984bb58 | 2010-11-25 16:08:06 +0000 | [diff] [blame] | 870 | fmt = fmt[1:] # strip off the % |
| 871 | self.assertEqual(format(value, fmt), expected, fmt) |
Eric Smith | 8a10ecc | 2009-12-02 17:58:24 +0000 | [diff] [blame] | 872 | |
| 873 | for fmt in ['%e', '%f', '%g', '%.0e', '%.6f', '%.20g', |
| 874 | '%#e', '%#f', '%#g', '%#.20e', '%#.15f', '%#.3g']: |
| 875 | pfmt = '%+' + fmt[1:] |
| 876 | sfmt = '% ' + fmt[1:] |
| 877 | test(fmt, INF, 'inf') |
| 878 | test(fmt, -INF, '-inf') |
| 879 | test(fmt, NAN, 'nan') |
| 880 | test(fmt, -NAN, 'nan') |
| 881 | # When asking for a sign, it's always provided. nans are |
| 882 | # always positive. |
| 883 | test(pfmt, INF, '+inf') |
| 884 | test(pfmt, -INF, '-inf') |
| 885 | test(pfmt, NAN, '+nan') |
| 886 | test(pfmt, -NAN, '+nan') |
| 887 | # When using ' ' for a sign code, only infs can be negative. |
| 888 | # Others have a space. |
| 889 | test(sfmt, INF, ' inf') |
| 890 | test(sfmt, -INF, '-inf') |
| 891 | test(sfmt, NAN, ' nan') |
| 892 | test(sfmt, -NAN, ' nan') |
| 893 | |
Steve Dower | cb39d1f | 2015-04-15 16:10:59 -0400 | [diff] [blame] | 894 | def test_None_ndigits(self): |
| 895 | for x in round(1.23), round(1.23, None), round(1.23, ndigits=None): |
| 896 | self.assertEqual(x, 1) |
| 897 | self.assertIsInstance(x, int) |
| 898 | for x in round(1.78), round(1.78, None), round(1.78, ndigits=None): |
| 899 | self.assertEqual(x, 2) |
| 900 | self.assertIsInstance(x, int) |
| 901 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 902 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 903 | # Beginning with Python 2.6 float has cross platform compatible |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 904 | # ways to create and represent inf and nan |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 905 | class InfNanTest(unittest.TestCase): |
| 906 | def test_inf_from_str(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 907 | self.assertTrue(isinf(float("inf"))) |
| 908 | self.assertTrue(isinf(float("+inf"))) |
| 909 | self.assertTrue(isinf(float("-inf"))) |
| 910 | self.assertTrue(isinf(float("infinity"))) |
| 911 | self.assertTrue(isinf(float("+infinity"))) |
| 912 | self.assertTrue(isinf(float("-infinity"))) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 913 | |
| 914 | self.assertEqual(repr(float("inf")), "inf") |
| 915 | self.assertEqual(repr(float("+inf")), "inf") |
| 916 | self.assertEqual(repr(float("-inf")), "-inf") |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 917 | self.assertEqual(repr(float("infinity")), "inf") |
| 918 | self.assertEqual(repr(float("+infinity")), "inf") |
| 919 | self.assertEqual(repr(float("-infinity")), "-inf") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 920 | |
| 921 | self.assertEqual(repr(float("INF")), "inf") |
| 922 | self.assertEqual(repr(float("+Inf")), "inf") |
| 923 | self.assertEqual(repr(float("-iNF")), "-inf") |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 924 | self.assertEqual(repr(float("Infinity")), "inf") |
| 925 | self.assertEqual(repr(float("+iNfInItY")), "inf") |
| 926 | self.assertEqual(repr(float("-INFINITY")), "-inf") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 927 | |
| 928 | self.assertEqual(str(float("inf")), "inf") |
| 929 | self.assertEqual(str(float("+inf")), "inf") |
| 930 | self.assertEqual(str(float("-inf")), "-inf") |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 931 | self.assertEqual(str(float("infinity")), "inf") |
| 932 | self.assertEqual(str(float("+infinity")), "inf") |
| 933 | self.assertEqual(str(float("-infinity")), "-inf") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 934 | |
| 935 | self.assertRaises(ValueError, float, "info") |
| 936 | self.assertRaises(ValueError, float, "+info") |
| 937 | self.assertRaises(ValueError, float, "-info") |
| 938 | self.assertRaises(ValueError, float, "in") |
| 939 | self.assertRaises(ValueError, float, "+in") |
| 940 | self.assertRaises(ValueError, float, "-in") |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 941 | self.assertRaises(ValueError, float, "infinit") |
| 942 | self.assertRaises(ValueError, float, "+Infin") |
| 943 | self.assertRaises(ValueError, float, "-INFI") |
| 944 | self.assertRaises(ValueError, float, "infinitys") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 945 | |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 946 | self.assertRaises(ValueError, float, "++Inf") |
| 947 | self.assertRaises(ValueError, float, "-+inf") |
| 948 | self.assertRaises(ValueError, float, "+-infinity") |
| 949 | self.assertRaises(ValueError, float, "--Infinity") |
| 950 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 951 | def test_inf_as_str(self): |
| 952 | self.assertEqual(repr(1e300 * 1e300), "inf") |
| 953 | self.assertEqual(repr(-1e300 * 1e300), "-inf") |
| 954 | |
| 955 | self.assertEqual(str(1e300 * 1e300), "inf") |
| 956 | self.assertEqual(str(-1e300 * 1e300), "-inf") |
| 957 | |
| 958 | def test_nan_from_str(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 959 | self.assertTrue(isnan(float("nan"))) |
| 960 | self.assertTrue(isnan(float("+nan"))) |
| 961 | self.assertTrue(isnan(float("-nan"))) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 962 | |
| 963 | self.assertEqual(repr(float("nan")), "nan") |
| 964 | self.assertEqual(repr(float("+nan")), "nan") |
| 965 | self.assertEqual(repr(float("-nan")), "nan") |
| 966 | |
| 967 | self.assertEqual(repr(float("NAN")), "nan") |
| 968 | self.assertEqual(repr(float("+NAn")), "nan") |
| 969 | self.assertEqual(repr(float("-NaN")), "nan") |
| 970 | |
| 971 | self.assertEqual(str(float("nan")), "nan") |
| 972 | self.assertEqual(str(float("+nan")), "nan") |
| 973 | self.assertEqual(str(float("-nan")), "nan") |
| 974 | |
| 975 | self.assertRaises(ValueError, float, "nana") |
| 976 | self.assertRaises(ValueError, float, "+nana") |
| 977 | self.assertRaises(ValueError, float, "-nana") |
| 978 | self.assertRaises(ValueError, float, "na") |
| 979 | self.assertRaises(ValueError, float, "+na") |
| 980 | self.assertRaises(ValueError, float, "-na") |
| 981 | |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 982 | self.assertRaises(ValueError, float, "++nan") |
| 983 | self.assertRaises(ValueError, float, "-+NAN") |
| 984 | self.assertRaises(ValueError, float, "+-NaN") |
| 985 | self.assertRaises(ValueError, float, "--nAn") |
| 986 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 987 | def test_nan_as_str(self): |
| 988 | self.assertEqual(repr(1e300 * 1e300 * 0), "nan") |
| 989 | self.assertEqual(repr(-1e300 * 1e300 * 0), "nan") |
| 990 | |
| 991 | self.assertEqual(str(1e300 * 1e300 * 0), "nan") |
| 992 | self.assertEqual(str(-1e300 * 1e300 * 0), "nan") |
Christian Heimes | 827b35c | 2007-12-10 22:19:17 +0000 | [diff] [blame] | 993 | |
Mark Dickinson | e383e82 | 2012-04-29 15:31:56 +0100 | [diff] [blame] | 994 | def test_inf_signs(self): |
| 995 | self.assertEqual(copysign(1.0, float('inf')), 1.0) |
| 996 | self.assertEqual(copysign(1.0, float('-inf')), -1.0) |
| 997 | |
| 998 | @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', |
| 999 | "applies only when using short float repr style") |
| 1000 | def test_nan_signs(self): |
| 1001 | # When using the dtoa.c code, the sign of float('nan') should |
| 1002 | # be predictable. |
| 1003 | self.assertEqual(copysign(1.0, float('nan')), 1.0) |
| 1004 | self.assertEqual(copysign(1.0, float('-nan')), -1.0) |
| 1005 | |
| 1006 | |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1007 | fromHex = float.fromhex |
| 1008 | toHex = float.hex |
| 1009 | class HexFloatTestCase(unittest.TestCase): |
| 1010 | MAX = fromHex('0x.fffffffffffff8p+1024') # max normal |
| 1011 | MIN = fromHex('0x1p-1022') # min normal |
| 1012 | TINY = fromHex('0x0.0000000000001p-1022') # min subnormal |
| 1013 | EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up |
| 1014 | |
| 1015 | def identical(self, x, y): |
| 1016 | # check that floats x and y are identical, or that both |
| 1017 | # are NaNs |
| 1018 | if isnan(x) or isnan(y): |
| 1019 | if isnan(x) == isnan(y): |
| 1020 | return |
| 1021 | elif x == y and (x != 0.0 or copysign(1.0, x) == copysign(1.0, y)): |
| 1022 | return |
| 1023 | self.fail('%r not identical to %r' % (x, y)) |
| 1024 | |
| 1025 | def test_ends(self): |
Mark Dickinson | 38bbc48 | 2008-07-16 11:32:23 +0000 | [diff] [blame] | 1026 | self.identical(self.MIN, ldexp(1.0, -1022)) |
| 1027 | self.identical(self.TINY, ldexp(1.0, -1074)) |
| 1028 | self.identical(self.EPS, ldexp(1.0, -52)) |
| 1029 | self.identical(self.MAX, 2.*(ldexp(1.0, 1023) - ldexp(1.0, 970))) |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1030 | |
| 1031 | def test_invalid_inputs(self): |
| 1032 | invalid_inputs = [ |
| 1033 | 'infi', # misspelt infinities and nans |
| 1034 | '-Infinit', |
| 1035 | '++inf', |
| 1036 | '-+Inf', |
| 1037 | '--nan', |
| 1038 | '+-NaN', |
| 1039 | 'snan', |
| 1040 | 'NaNs', |
| 1041 | 'nna', |
Mark Dickinson | d1ec8b2 | 2009-05-11 15:45:15 +0000 | [diff] [blame] | 1042 | 'an', |
| 1043 | 'nf', |
| 1044 | 'nfinity', |
| 1045 | 'inity', |
| 1046 | 'iinity', |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1047 | '0xnan', |
| 1048 | '', |
| 1049 | ' ', |
| 1050 | 'x1.0p0', |
| 1051 | '0xX1.0p0', |
| 1052 | '+ 0x1.0p0', # internal whitespace |
| 1053 | '- 0x1.0p0', |
| 1054 | '0 x1.0p0', |
| 1055 | '0x 1.0p0', |
| 1056 | '0x1 2.0p0', |
| 1057 | '+0x1 .0p0', |
| 1058 | '0x1. 0p0', |
| 1059 | '-0x1.0 1p0', |
| 1060 | '-0x1.0 p0', |
| 1061 | '+0x1.0p +0', |
| 1062 | '0x1.0p -0', |
| 1063 | '0x1.0p 0', |
| 1064 | '+0x1.0p+ 0', |
| 1065 | '-0x1.0p- 0', |
| 1066 | '++0x1.0p-0', # double signs |
| 1067 | '--0x1.0p0', |
| 1068 | '+-0x1.0p+0', |
| 1069 | '-+0x1.0p0', |
| 1070 | '0x1.0p++0', |
| 1071 | '+0x1.0p+-0', |
| 1072 | '-0x1.0p-+0', |
| 1073 | '0x1.0p--0', |
| 1074 | '0x1.0.p0', |
| 1075 | '0x.p0', # no hex digits before or after point |
| 1076 | '0x1,p0', # wrong decimal point character |
| 1077 | '0x1pa', |
| 1078 | '0x1p\uff10', # fullwidth Unicode digits |
| 1079 | '\uff10x1p0', |
| 1080 | '0x\uff11p0', |
| 1081 | '0x1.\uff10p0', |
| 1082 | '0x1p0 \n 0x2p0', |
| 1083 | '0x1p0\0 0x1p0', # embedded null byte is not end of string |
| 1084 | ] |
| 1085 | for x in invalid_inputs: |
Mark Dickinson | 589b795 | 2008-08-21 20:05:56 +0000 | [diff] [blame] | 1086 | try: |
| 1087 | result = fromHex(x) |
| 1088 | except ValueError: |
| 1089 | pass |
| 1090 | else: |
| 1091 | self.fail('Expected float.fromhex(%r) to raise ValueError; ' |
| 1092 | 'got %r instead' % (x, result)) |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1093 | |
| 1094 | |
Mark Dickinson | d1ec8b2 | 2009-05-11 15:45:15 +0000 | [diff] [blame] | 1095 | def test_whitespace(self): |
| 1096 | value_pairs = [ |
| 1097 | ('inf', INF), |
| 1098 | ('-Infinity', -INF), |
| 1099 | ('nan', NAN), |
| 1100 | ('1.0', 1.0), |
| 1101 | ('-0x.2', -0.125), |
| 1102 | ('-0.0', -0.0) |
| 1103 | ] |
| 1104 | whitespace = [ |
| 1105 | '', |
| 1106 | ' ', |
| 1107 | '\t', |
| 1108 | '\n', |
| 1109 | '\n \t', |
| 1110 | '\f', |
| 1111 | '\v', |
| 1112 | '\r' |
| 1113 | ] |
| 1114 | for inp, expected in value_pairs: |
| 1115 | for lead in whitespace: |
| 1116 | for trail in whitespace: |
| 1117 | got = fromHex(lead + inp + trail) |
| 1118 | self.identical(got, expected) |
| 1119 | |
| 1120 | |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1121 | def test_from_hex(self): |
| 1122 | MIN = self.MIN; |
| 1123 | MAX = self.MAX; |
| 1124 | TINY = self.TINY; |
| 1125 | EPS = self.EPS; |
| 1126 | |
| 1127 | # two spellings of infinity, with optional signs; case-insensitive |
| 1128 | self.identical(fromHex('inf'), INF) |
| 1129 | self.identical(fromHex('+Inf'), INF) |
| 1130 | self.identical(fromHex('-INF'), -INF) |
| 1131 | self.identical(fromHex('iNf'), INF) |
| 1132 | self.identical(fromHex('Infinity'), INF) |
| 1133 | self.identical(fromHex('+INFINITY'), INF) |
| 1134 | self.identical(fromHex('-infinity'), -INF) |
| 1135 | self.identical(fromHex('-iNFiNitY'), -INF) |
| 1136 | |
| 1137 | # nans with optional sign; case insensitive |
| 1138 | self.identical(fromHex('nan'), NAN) |
| 1139 | self.identical(fromHex('+NaN'), NAN) |
| 1140 | self.identical(fromHex('-NaN'), NAN) |
| 1141 | self.identical(fromHex('-nAN'), NAN) |
| 1142 | |
| 1143 | # variations in input format |
| 1144 | self.identical(fromHex('1'), 1.0) |
| 1145 | self.identical(fromHex('+1'), 1.0) |
| 1146 | self.identical(fromHex('1.'), 1.0) |
| 1147 | self.identical(fromHex('1.0'), 1.0) |
| 1148 | self.identical(fromHex('1.0p0'), 1.0) |
| 1149 | self.identical(fromHex('01'), 1.0) |
| 1150 | self.identical(fromHex('01.'), 1.0) |
| 1151 | self.identical(fromHex('0x1'), 1.0) |
| 1152 | self.identical(fromHex('0x1.'), 1.0) |
| 1153 | self.identical(fromHex('0x1.0'), 1.0) |
| 1154 | self.identical(fromHex('+0x1.0'), 1.0) |
| 1155 | self.identical(fromHex('0x1p0'), 1.0) |
| 1156 | self.identical(fromHex('0X1p0'), 1.0) |
| 1157 | self.identical(fromHex('0X1P0'), 1.0) |
| 1158 | self.identical(fromHex('0x1P0'), 1.0) |
| 1159 | self.identical(fromHex('0x1.p0'), 1.0) |
| 1160 | self.identical(fromHex('0x1.0p0'), 1.0) |
| 1161 | self.identical(fromHex('0x.1p4'), 1.0) |
| 1162 | self.identical(fromHex('0x.1p04'), 1.0) |
| 1163 | self.identical(fromHex('0x.1p004'), 1.0) |
| 1164 | self.identical(fromHex('0x1p+0'), 1.0) |
| 1165 | self.identical(fromHex('0x1P-0'), 1.0) |
| 1166 | self.identical(fromHex('+0x1p0'), 1.0) |
| 1167 | self.identical(fromHex('0x01p0'), 1.0) |
| 1168 | self.identical(fromHex('0x1p00'), 1.0) |
| 1169 | self.identical(fromHex(' 0x1p0 '), 1.0) |
| 1170 | self.identical(fromHex('\n 0x1p0'), 1.0) |
| 1171 | self.identical(fromHex('0x1p0 \t'), 1.0) |
| 1172 | self.identical(fromHex('0xap0'), 10.0) |
| 1173 | self.identical(fromHex('0xAp0'), 10.0) |
| 1174 | self.identical(fromHex('0xaP0'), 10.0) |
| 1175 | self.identical(fromHex('0xAP0'), 10.0) |
| 1176 | self.identical(fromHex('0xbep0'), 190.0) |
| 1177 | self.identical(fromHex('0xBep0'), 190.0) |
| 1178 | self.identical(fromHex('0xbEp0'), 190.0) |
| 1179 | self.identical(fromHex('0XBE0P-4'), 190.0) |
| 1180 | self.identical(fromHex('0xBEp0'), 190.0) |
| 1181 | self.identical(fromHex('0xB.Ep4'), 190.0) |
| 1182 | self.identical(fromHex('0x.BEp8'), 190.0) |
| 1183 | self.identical(fromHex('0x.0BEp12'), 190.0) |
| 1184 | |
| 1185 | # moving the point around |
| 1186 | pi = fromHex('0x1.921fb54442d18p1') |
| 1187 | self.identical(fromHex('0x.006487ed5110b46p11'), pi) |
| 1188 | self.identical(fromHex('0x.00c90fdaa22168cp10'), pi) |
| 1189 | self.identical(fromHex('0x.01921fb54442d18p9'), pi) |
| 1190 | self.identical(fromHex('0x.03243f6a8885a3p8'), pi) |
| 1191 | self.identical(fromHex('0x.06487ed5110b46p7'), pi) |
| 1192 | self.identical(fromHex('0x.0c90fdaa22168cp6'), pi) |
| 1193 | self.identical(fromHex('0x.1921fb54442d18p5'), pi) |
| 1194 | self.identical(fromHex('0x.3243f6a8885a3p4'), pi) |
| 1195 | self.identical(fromHex('0x.6487ed5110b46p3'), pi) |
| 1196 | self.identical(fromHex('0x.c90fdaa22168cp2'), pi) |
| 1197 | self.identical(fromHex('0x1.921fb54442d18p1'), pi) |
| 1198 | self.identical(fromHex('0x3.243f6a8885a3p0'), pi) |
| 1199 | self.identical(fromHex('0x6.487ed5110b46p-1'), pi) |
| 1200 | self.identical(fromHex('0xc.90fdaa22168cp-2'), pi) |
| 1201 | self.identical(fromHex('0x19.21fb54442d18p-3'), pi) |
| 1202 | self.identical(fromHex('0x32.43f6a8885a3p-4'), pi) |
| 1203 | self.identical(fromHex('0x64.87ed5110b46p-5'), pi) |
| 1204 | self.identical(fromHex('0xc9.0fdaa22168cp-6'), pi) |
| 1205 | self.identical(fromHex('0x192.1fb54442d18p-7'), pi) |
| 1206 | self.identical(fromHex('0x324.3f6a8885a3p-8'), pi) |
| 1207 | self.identical(fromHex('0x648.7ed5110b46p-9'), pi) |
| 1208 | self.identical(fromHex('0xc90.fdaa22168cp-10'), pi) |
| 1209 | self.identical(fromHex('0x1921.fb54442d18p-11'), pi) |
| 1210 | # ... |
| 1211 | self.identical(fromHex('0x1921fb54442d1.8p-47'), pi) |
| 1212 | self.identical(fromHex('0x3243f6a8885a3p-48'), pi) |
| 1213 | self.identical(fromHex('0x6487ed5110b46p-49'), pi) |
| 1214 | self.identical(fromHex('0xc90fdaa22168cp-50'), pi) |
| 1215 | self.identical(fromHex('0x1921fb54442d18p-51'), pi) |
| 1216 | self.identical(fromHex('0x3243f6a8885a30p-52'), pi) |
| 1217 | self.identical(fromHex('0x6487ed5110b460p-53'), pi) |
| 1218 | self.identical(fromHex('0xc90fdaa22168c0p-54'), pi) |
| 1219 | self.identical(fromHex('0x1921fb54442d180p-55'), pi) |
| 1220 | |
| 1221 | |
| 1222 | # results that should overflow... |
| 1223 | self.assertRaises(OverflowError, fromHex, '-0x1p1024') |
| 1224 | self.assertRaises(OverflowError, fromHex, '0x1p+1025') |
| 1225 | self.assertRaises(OverflowError, fromHex, '+0X1p1030') |
| 1226 | self.assertRaises(OverflowError, fromHex, '-0x1p+1100') |
| 1227 | self.assertRaises(OverflowError, fromHex, '0X1p123456789123456789') |
| 1228 | self.assertRaises(OverflowError, fromHex, '+0X.8p+1025') |
| 1229 | self.assertRaises(OverflowError, fromHex, '+0x0.8p1025') |
| 1230 | self.assertRaises(OverflowError, fromHex, '-0x0.4p1026') |
| 1231 | self.assertRaises(OverflowError, fromHex, '0X2p+1023') |
| 1232 | self.assertRaises(OverflowError, fromHex, '0x2.p1023') |
| 1233 | self.assertRaises(OverflowError, fromHex, '-0x2.0p+1023') |
| 1234 | self.assertRaises(OverflowError, fromHex, '+0X4p+1022') |
| 1235 | self.assertRaises(OverflowError, fromHex, '0x1.ffffffffffffffp+1023') |
| 1236 | self.assertRaises(OverflowError, fromHex, '-0X1.fffffffffffff9p1023') |
| 1237 | self.assertRaises(OverflowError, fromHex, '0X1.fffffffffffff8p1023') |
| 1238 | self.assertRaises(OverflowError, fromHex, '+0x3.fffffffffffffp1022') |
| 1239 | self.assertRaises(OverflowError, fromHex, '0x3fffffffffffffp+970') |
| 1240 | self.assertRaises(OverflowError, fromHex, '0x10000000000000000p960') |
| 1241 | self.assertRaises(OverflowError, fromHex, '-0Xffffffffffffffffp960') |
| 1242 | |
| 1243 | # ...and those that round to +-max float |
| 1244 | self.identical(fromHex('+0x1.fffffffffffffp+1023'), MAX) |
| 1245 | self.identical(fromHex('-0X1.fffffffffffff7p1023'), -MAX) |
| 1246 | self.identical(fromHex('0X1.fffffffffffff7fffffffffffffp1023'), MAX) |
| 1247 | |
| 1248 | # zeros |
| 1249 | self.identical(fromHex('0x0p0'), 0.0) |
| 1250 | self.identical(fromHex('0x0p1000'), 0.0) |
| 1251 | self.identical(fromHex('-0x0p1023'), -0.0) |
| 1252 | self.identical(fromHex('0X0p1024'), 0.0) |
| 1253 | self.identical(fromHex('-0x0p1025'), -0.0) |
| 1254 | self.identical(fromHex('0X0p2000'), 0.0) |
| 1255 | self.identical(fromHex('0x0p123456789123456789'), 0.0) |
| 1256 | self.identical(fromHex('-0X0p-0'), -0.0) |
| 1257 | self.identical(fromHex('-0X0p-1000'), -0.0) |
| 1258 | self.identical(fromHex('0x0p-1023'), 0.0) |
| 1259 | self.identical(fromHex('-0X0p-1024'), -0.0) |
| 1260 | self.identical(fromHex('-0x0p-1025'), -0.0) |
| 1261 | self.identical(fromHex('-0x0p-1072'), -0.0) |
| 1262 | self.identical(fromHex('0X0p-1073'), 0.0) |
| 1263 | self.identical(fromHex('-0x0p-1074'), -0.0) |
| 1264 | self.identical(fromHex('0x0p-1075'), 0.0) |
| 1265 | self.identical(fromHex('0X0p-1076'), 0.0) |
| 1266 | self.identical(fromHex('-0X0p-2000'), -0.0) |
| 1267 | self.identical(fromHex('-0x0p-123456789123456789'), -0.0) |
| 1268 | |
| 1269 | # values that should underflow to 0 |
| 1270 | self.identical(fromHex('0X1p-1075'), 0.0) |
| 1271 | self.identical(fromHex('-0X1p-1075'), -0.0) |
| 1272 | self.identical(fromHex('-0x1p-123456789123456789'), -0.0) |
| 1273 | self.identical(fromHex('0x1.00000000000000001p-1075'), TINY) |
| 1274 | self.identical(fromHex('-0x1.1p-1075'), -TINY) |
| 1275 | self.identical(fromHex('0x1.fffffffffffffffffp-1075'), TINY) |
| 1276 | |
| 1277 | # check round-half-even is working correctly near 0 ... |
| 1278 | self.identical(fromHex('0x1p-1076'), 0.0) |
| 1279 | self.identical(fromHex('0X2p-1076'), 0.0) |
| 1280 | self.identical(fromHex('0X3p-1076'), TINY) |
| 1281 | self.identical(fromHex('0x4p-1076'), TINY) |
| 1282 | self.identical(fromHex('0X5p-1076'), TINY) |
| 1283 | self.identical(fromHex('0X6p-1076'), 2*TINY) |
| 1284 | self.identical(fromHex('0x7p-1076'), 2*TINY) |
| 1285 | self.identical(fromHex('0X8p-1076'), 2*TINY) |
| 1286 | self.identical(fromHex('0X9p-1076'), 2*TINY) |
| 1287 | self.identical(fromHex('0xap-1076'), 2*TINY) |
| 1288 | self.identical(fromHex('0Xbp-1076'), 3*TINY) |
| 1289 | self.identical(fromHex('0xcp-1076'), 3*TINY) |
| 1290 | self.identical(fromHex('0Xdp-1076'), 3*TINY) |
| 1291 | self.identical(fromHex('0Xep-1076'), 4*TINY) |
| 1292 | self.identical(fromHex('0xfp-1076'), 4*TINY) |
| 1293 | self.identical(fromHex('0x10p-1076'), 4*TINY) |
| 1294 | self.identical(fromHex('-0x1p-1076'), -0.0) |
| 1295 | self.identical(fromHex('-0X2p-1076'), -0.0) |
| 1296 | self.identical(fromHex('-0x3p-1076'), -TINY) |
| 1297 | self.identical(fromHex('-0X4p-1076'), -TINY) |
| 1298 | self.identical(fromHex('-0x5p-1076'), -TINY) |
| 1299 | self.identical(fromHex('-0x6p-1076'), -2*TINY) |
| 1300 | self.identical(fromHex('-0X7p-1076'), -2*TINY) |
| 1301 | self.identical(fromHex('-0X8p-1076'), -2*TINY) |
| 1302 | self.identical(fromHex('-0X9p-1076'), -2*TINY) |
| 1303 | self.identical(fromHex('-0Xap-1076'), -2*TINY) |
| 1304 | self.identical(fromHex('-0xbp-1076'), -3*TINY) |
| 1305 | self.identical(fromHex('-0xcp-1076'), -3*TINY) |
| 1306 | self.identical(fromHex('-0Xdp-1076'), -3*TINY) |
| 1307 | self.identical(fromHex('-0xep-1076'), -4*TINY) |
| 1308 | self.identical(fromHex('-0Xfp-1076'), -4*TINY) |
| 1309 | self.identical(fromHex('-0X10p-1076'), -4*TINY) |
| 1310 | |
| 1311 | # ... and near MIN ... |
| 1312 | self.identical(fromHex('0x0.ffffffffffffd6p-1022'), MIN-3*TINY) |
| 1313 | self.identical(fromHex('0x0.ffffffffffffd8p-1022'), MIN-2*TINY) |
| 1314 | self.identical(fromHex('0x0.ffffffffffffdap-1022'), MIN-2*TINY) |
| 1315 | self.identical(fromHex('0x0.ffffffffffffdcp-1022'), MIN-2*TINY) |
| 1316 | self.identical(fromHex('0x0.ffffffffffffdep-1022'), MIN-2*TINY) |
| 1317 | self.identical(fromHex('0x0.ffffffffffffe0p-1022'), MIN-2*TINY) |
| 1318 | self.identical(fromHex('0x0.ffffffffffffe2p-1022'), MIN-2*TINY) |
| 1319 | self.identical(fromHex('0x0.ffffffffffffe4p-1022'), MIN-2*TINY) |
| 1320 | self.identical(fromHex('0x0.ffffffffffffe6p-1022'), MIN-2*TINY) |
| 1321 | self.identical(fromHex('0x0.ffffffffffffe8p-1022'), MIN-2*TINY) |
| 1322 | self.identical(fromHex('0x0.ffffffffffffeap-1022'), MIN-TINY) |
| 1323 | self.identical(fromHex('0x0.ffffffffffffecp-1022'), MIN-TINY) |
| 1324 | self.identical(fromHex('0x0.ffffffffffffeep-1022'), MIN-TINY) |
| 1325 | self.identical(fromHex('0x0.fffffffffffff0p-1022'), MIN-TINY) |
| 1326 | self.identical(fromHex('0x0.fffffffffffff2p-1022'), MIN-TINY) |
| 1327 | self.identical(fromHex('0x0.fffffffffffff4p-1022'), MIN-TINY) |
| 1328 | self.identical(fromHex('0x0.fffffffffffff6p-1022'), MIN-TINY) |
| 1329 | self.identical(fromHex('0x0.fffffffffffff8p-1022'), MIN) |
| 1330 | self.identical(fromHex('0x0.fffffffffffffap-1022'), MIN) |
| 1331 | self.identical(fromHex('0x0.fffffffffffffcp-1022'), MIN) |
| 1332 | self.identical(fromHex('0x0.fffffffffffffep-1022'), MIN) |
| 1333 | self.identical(fromHex('0x1.00000000000000p-1022'), MIN) |
| 1334 | self.identical(fromHex('0x1.00000000000002p-1022'), MIN) |
| 1335 | self.identical(fromHex('0x1.00000000000004p-1022'), MIN) |
| 1336 | self.identical(fromHex('0x1.00000000000006p-1022'), MIN) |
| 1337 | self.identical(fromHex('0x1.00000000000008p-1022'), MIN) |
| 1338 | self.identical(fromHex('0x1.0000000000000ap-1022'), MIN+TINY) |
| 1339 | self.identical(fromHex('0x1.0000000000000cp-1022'), MIN+TINY) |
| 1340 | self.identical(fromHex('0x1.0000000000000ep-1022'), MIN+TINY) |
| 1341 | self.identical(fromHex('0x1.00000000000010p-1022'), MIN+TINY) |
| 1342 | self.identical(fromHex('0x1.00000000000012p-1022'), MIN+TINY) |
| 1343 | self.identical(fromHex('0x1.00000000000014p-1022'), MIN+TINY) |
| 1344 | self.identical(fromHex('0x1.00000000000016p-1022'), MIN+TINY) |
| 1345 | self.identical(fromHex('0x1.00000000000018p-1022'), MIN+2*TINY) |
| 1346 | |
| 1347 | # ... and near 1.0. |
| 1348 | self.identical(fromHex('0x0.fffffffffffff0p0'), 1.0-EPS) |
| 1349 | self.identical(fromHex('0x0.fffffffffffff1p0'), 1.0-EPS) |
| 1350 | self.identical(fromHex('0X0.fffffffffffff2p0'), 1.0-EPS) |
| 1351 | self.identical(fromHex('0x0.fffffffffffff3p0'), 1.0-EPS) |
| 1352 | self.identical(fromHex('0X0.fffffffffffff4p0'), 1.0-EPS) |
| 1353 | self.identical(fromHex('0X0.fffffffffffff5p0'), 1.0-EPS/2) |
| 1354 | self.identical(fromHex('0X0.fffffffffffff6p0'), 1.0-EPS/2) |
| 1355 | self.identical(fromHex('0x0.fffffffffffff7p0'), 1.0-EPS/2) |
| 1356 | self.identical(fromHex('0x0.fffffffffffff8p0'), 1.0-EPS/2) |
| 1357 | self.identical(fromHex('0X0.fffffffffffff9p0'), 1.0-EPS/2) |
| 1358 | self.identical(fromHex('0X0.fffffffffffffap0'), 1.0-EPS/2) |
| 1359 | self.identical(fromHex('0x0.fffffffffffffbp0'), 1.0-EPS/2) |
| 1360 | self.identical(fromHex('0X0.fffffffffffffcp0'), 1.0) |
| 1361 | self.identical(fromHex('0x0.fffffffffffffdp0'), 1.0) |
| 1362 | self.identical(fromHex('0X0.fffffffffffffep0'), 1.0) |
| 1363 | self.identical(fromHex('0x0.ffffffffffffffp0'), 1.0) |
| 1364 | self.identical(fromHex('0X1.00000000000000p0'), 1.0) |
| 1365 | self.identical(fromHex('0X1.00000000000001p0'), 1.0) |
| 1366 | self.identical(fromHex('0x1.00000000000002p0'), 1.0) |
| 1367 | self.identical(fromHex('0X1.00000000000003p0'), 1.0) |
| 1368 | self.identical(fromHex('0x1.00000000000004p0'), 1.0) |
| 1369 | self.identical(fromHex('0X1.00000000000005p0'), 1.0) |
| 1370 | self.identical(fromHex('0X1.00000000000006p0'), 1.0) |
| 1371 | self.identical(fromHex('0X1.00000000000007p0'), 1.0) |
| 1372 | self.identical(fromHex('0x1.00000000000007ffffffffffffffffffffp0'), |
| 1373 | 1.0) |
| 1374 | self.identical(fromHex('0x1.00000000000008p0'), 1.0) |
| 1375 | self.identical(fromHex('0x1.00000000000008000000000000000001p0'), |
| 1376 | 1+EPS) |
| 1377 | self.identical(fromHex('0X1.00000000000009p0'), 1.0+EPS) |
| 1378 | self.identical(fromHex('0x1.0000000000000ap0'), 1.0+EPS) |
| 1379 | self.identical(fromHex('0x1.0000000000000bp0'), 1.0+EPS) |
| 1380 | self.identical(fromHex('0X1.0000000000000cp0'), 1.0+EPS) |
| 1381 | self.identical(fromHex('0x1.0000000000000dp0'), 1.0+EPS) |
| 1382 | self.identical(fromHex('0x1.0000000000000ep0'), 1.0+EPS) |
| 1383 | self.identical(fromHex('0X1.0000000000000fp0'), 1.0+EPS) |
| 1384 | self.identical(fromHex('0x1.00000000000010p0'), 1.0+EPS) |
| 1385 | self.identical(fromHex('0X1.00000000000011p0'), 1.0+EPS) |
| 1386 | self.identical(fromHex('0x1.00000000000012p0'), 1.0+EPS) |
| 1387 | self.identical(fromHex('0X1.00000000000013p0'), 1.0+EPS) |
| 1388 | self.identical(fromHex('0X1.00000000000014p0'), 1.0+EPS) |
| 1389 | self.identical(fromHex('0x1.00000000000015p0'), 1.0+EPS) |
| 1390 | self.identical(fromHex('0x1.00000000000016p0'), 1.0+EPS) |
| 1391 | self.identical(fromHex('0X1.00000000000017p0'), 1.0+EPS) |
| 1392 | self.identical(fromHex('0x1.00000000000017ffffffffffffffffffffp0'), |
| 1393 | 1.0+EPS) |
| 1394 | self.identical(fromHex('0x1.00000000000018p0'), 1.0+2*EPS) |
| 1395 | self.identical(fromHex('0X1.00000000000018000000000000000001p0'), |
| 1396 | 1.0+2*EPS) |
| 1397 | self.identical(fromHex('0x1.00000000000019p0'), 1.0+2*EPS) |
| 1398 | self.identical(fromHex('0X1.0000000000001ap0'), 1.0+2*EPS) |
| 1399 | self.identical(fromHex('0X1.0000000000001bp0'), 1.0+2*EPS) |
| 1400 | self.identical(fromHex('0x1.0000000000001cp0'), 1.0+2*EPS) |
| 1401 | self.identical(fromHex('0x1.0000000000001dp0'), 1.0+2*EPS) |
| 1402 | self.identical(fromHex('0x1.0000000000001ep0'), 1.0+2*EPS) |
| 1403 | self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS) |
| 1404 | self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS) |
| 1405 | |
| 1406 | def test_roundtrip(self): |
| 1407 | def roundtrip(x): |
| 1408 | return fromHex(toHex(x)) |
| 1409 | |
| 1410 | for x in [NAN, INF, self.MAX, self.MIN, self.MIN-self.TINY, self.TINY, 0.0]: |
| 1411 | self.identical(x, roundtrip(x)) |
| 1412 | self.identical(-x, roundtrip(-x)) |
| 1413 | |
| 1414 | # fromHex(toHex(x)) should exactly recover x, for any non-NaN float x. |
| 1415 | import random |
| 1416 | for i in range(10000): |
| 1417 | e = random.randrange(-1200, 1200) |
| 1418 | m = random.random() |
| 1419 | s = random.choice([1.0, -1.0]) |
| 1420 | try: |
| 1421 | x = s*ldexp(m, e) |
| 1422 | except OverflowError: |
| 1423 | pass |
| 1424 | else: |
| 1425 | self.identical(x, fromHex(toHex(x))) |
| 1426 | |
Serhiy Storchaka | ea36c94 | 2016-05-12 10:37:58 +0300 | [diff] [blame] | 1427 | def test_subclass(self): |
| 1428 | class F(float): |
| 1429 | def __new__(cls, value): |
| 1430 | return float.__new__(cls, value + 1) |
| 1431 | |
| 1432 | f = F.fromhex((1.5).hex()) |
| 1433 | self.assertIs(type(f), F) |
| 1434 | self.assertEqual(f, 2.5) |
| 1435 | |
| 1436 | class F2(float): |
| 1437 | def __init__(self, value): |
| 1438 | self.foo = 'bar' |
| 1439 | |
| 1440 | f = F2.fromhex((1.5).hex()) |
| 1441 | self.assertIs(type(f), F2) |
| 1442 | self.assertEqual(f, 1.5) |
| 1443 | self.assertEqual(getattr(f, 'foo', 'none'), 'bar') |
| 1444 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1445 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1446 | if __name__ == '__main__': |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 1447 | unittest.main() |