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