ยปCore Development>Code coverage>Lib/test/test_float.py

Python code coverage for Lib/test/test_float.py

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