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