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): |
| 543 | for line in open(format_testfile): |
| 544 | if line.startswith('--'): |
| 545 | continue |
| 546 | line = line.strip() |
| 547 | if not line: |
| 548 | continue |
| 549 | |
| 550 | lhs, rhs = map(str.strip, line.split('->')) |
| 551 | fmt, arg = lhs.split() |
| 552 | self.assertEqual(fmt % float(arg), rhs) |
| 553 | self.assertEqual(fmt % -float(arg), '-' + rhs) |
| 554 | |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 555 | def test_issue5864(self): |
| 556 | self.assertEquals(format(123.456, '.4'), '123.5') |
| 557 | self.assertEquals(format(1234.56, '.4'), '1.235e+03') |
| 558 | self.assertEquals(format(12345.6, '.4'), '1.235e+04') |
| 559 | |
Mark Dickinson | 7efad9e | 2009-04-17 20:59:58 +0000 | [diff] [blame] | 560 | class ReprTestCase(unittest.TestCase): |
| 561 | def test_repr(self): |
| 562 | floats_file = open(os.path.join(os.path.split(__file__)[0], |
| 563 | 'floating_points.txt')) |
| 564 | for line in floats_file: |
| 565 | line = line.strip() |
| 566 | if not line or line.startswith('#'): |
| 567 | continue |
| 568 | v = eval(line) |
| 569 | self.assertEqual(v, eval(repr(v))) |
| 570 | floats_file.close() |
| 571 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 572 | @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', |
| 573 | "applies only when using short float repr style") |
| 574 | def test_short_repr(self): |
| 575 | # test short float repr introduced in Python 3.1. One aspect |
| 576 | # of this repr is that we get some degree of str -> float -> |
| 577 | # str roundtripping. In particular, for any numeric string |
| 578 | # containing 15 or fewer significant digits, those exact same |
| 579 | # digits (modulo trailing zeros) should appear in the output. |
| 580 | # No more repr(0.03) -> "0.029999999999999999"! |
| 581 | |
| 582 | test_strings = [ |
| 583 | # output always includes *either* a decimal point and at |
| 584 | # least one digit after that point, or an exponent. |
| 585 | '0.0', |
| 586 | '1.0', |
| 587 | '0.01', |
| 588 | '0.02', |
| 589 | '0.03', |
| 590 | '0.04', |
| 591 | '0.05', |
| 592 | '1.23456789', |
| 593 | '10.0', |
| 594 | '100.0', |
| 595 | # values >= 1e16 get an exponent... |
| 596 | '1000000000000000.0', |
| 597 | '9999999999999990.0', |
| 598 | '1e+16', |
| 599 | '1e+17', |
| 600 | # ... and so do values < 1e-4 |
| 601 | '0.001', |
| 602 | '0.001001', |
| 603 | '0.00010000000000001', |
| 604 | '0.0001', |
| 605 | '9.999999999999e-05', |
| 606 | '1e-05', |
| 607 | # values designed to provoke failure if the FPU rounding |
| 608 | # precision isn't set correctly |
| 609 | '8.72293771110361e+25', |
| 610 | '7.47005307342313e+26', |
| 611 | '2.86438000439698e+28', |
| 612 | '8.89142905246179e+28', |
| 613 | '3.08578087079232e+35', |
| 614 | ] |
| 615 | |
| 616 | for s in test_strings: |
| 617 | negs = '-'+s |
| 618 | self.assertEqual(s, repr(float(s))) |
| 619 | self.assertEqual(negs, repr(float(negs))) |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 620 | # Since Python 3.2, repr and str are identical |
| 621 | self.assertEqual(repr(float(s)), str(float(s))) |
| 622 | self.assertEqual(repr(float(negs)), str(float(negs))) |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 623 | |
| 624 | @requires_IEEE_754 |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 625 | class RoundTestCase(unittest.TestCase): |
Benjamin Peterson | e401c68 | 2010-07-02 23:25:44 +0000 | [diff] [blame] | 626 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 627 | def test_inf_nan(self): |
| 628 | self.assertRaises(OverflowError, round, INF) |
| 629 | self.assertRaises(OverflowError, round, -INF) |
| 630 | self.assertRaises(ValueError, round, NAN) |
Mark Dickinson | 4ca33d1 | 2009-11-24 10:59:34 +0000 | [diff] [blame] | 631 | self.assertRaises(TypeError, round, INF, 0.0) |
| 632 | self.assertRaises(TypeError, round, -INF, 1.0) |
| 633 | self.assertRaises(TypeError, round, NAN, "ceci n'est pas un integer") |
| 634 | self.assertRaises(TypeError, round, -0.0, 1j) |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 635 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 636 | def test_large_n(self): |
| 637 | for n in [324, 325, 400, 2**31-1, 2**31, 2**32, 2**100]: |
| 638 | self.assertEqual(round(123.456, n), 123.456) |
| 639 | self.assertEqual(round(-123.456, n), -123.456) |
| 640 | self.assertEqual(round(1e300, n), 1e300) |
| 641 | self.assertEqual(round(1e-320, n), 1e-320) |
| 642 | self.assertEqual(round(1e150, 300), 1e150) |
| 643 | self.assertEqual(round(1e300, 307), 1e300) |
| 644 | self.assertEqual(round(-3.1415, 308), -3.1415) |
| 645 | self.assertEqual(round(1e150, 309), 1e150) |
| 646 | self.assertEqual(round(1.4e-315, 315), 1e-315) |
| 647 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 648 | def test_small_n(self): |
| 649 | for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]: |
| 650 | self.assertEqual(round(123.456, n), 0.0) |
| 651 | self.assertEqual(round(-123.456, n), -0.0) |
| 652 | self.assertEqual(round(1e300, n), 0.0) |
| 653 | self.assertEqual(round(1e-320, n), 0.0) |
| 654 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 655 | def test_overflow(self): |
| 656 | self.assertRaises(OverflowError, round, 1.6e308, -308) |
| 657 | self.assertRaises(OverflowError, round, -1.7e308, -308) |
| 658 | |
| 659 | @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', |
| 660 | "applies only when using short float repr style") |
| 661 | def test_previous_round_bugs(self): |
| 662 | # particular cases that have occurred in bug reports |
| 663 | self.assertEqual(round(562949953421312.5, 1), |
| 664 | 562949953421312.5) |
| 665 | self.assertEqual(round(56294995342131.5, 3), |
| 666 | 56294995342131.5) |
| 667 | # round-half-even |
| 668 | self.assertEqual(round(25.0, -1), 20.0) |
| 669 | self.assertEqual(round(35.0, -1), 40.0) |
| 670 | self.assertEqual(round(45.0, -1), 40.0) |
| 671 | self.assertEqual(round(55.0, -1), 60.0) |
| 672 | self.assertEqual(round(65.0, -1), 60.0) |
| 673 | self.assertEqual(round(75.0, -1), 80.0) |
| 674 | self.assertEqual(round(85.0, -1), 80.0) |
| 675 | self.assertEqual(round(95.0, -1), 100.0) |
| 676 | |
| 677 | @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', |
| 678 | "applies only when using short float repr style") |
| 679 | def test_matches_float_format(self): |
| 680 | # round should give the same results as float formatting |
| 681 | for i in range(500): |
| 682 | x = i/1000. |
| 683 | self.assertEqual(float(format(x, '.0f')), round(x, 0)) |
| 684 | self.assertEqual(float(format(x, '.1f')), round(x, 1)) |
| 685 | self.assertEqual(float(format(x, '.2f')), round(x, 2)) |
| 686 | self.assertEqual(float(format(x, '.3f')), round(x, 3)) |
| 687 | |
| 688 | for i in range(5, 5000, 10): |
| 689 | x = i/1000. |
| 690 | self.assertEqual(float(format(x, '.0f')), round(x, 0)) |
| 691 | self.assertEqual(float(format(x, '.1f')), round(x, 1)) |
| 692 | self.assertEqual(float(format(x, '.2f')), round(x, 2)) |
| 693 | self.assertEqual(float(format(x, '.3f')), round(x, 3)) |
| 694 | |
| 695 | for i in range(500): |
| 696 | x = random.random() |
| 697 | self.assertEqual(float(format(x, '.0f')), round(x, 0)) |
| 698 | self.assertEqual(float(format(x, '.1f')), round(x, 1)) |
| 699 | self.assertEqual(float(format(x, '.2f')), round(x, 2)) |
| 700 | self.assertEqual(float(format(x, '.3f')), round(x, 3)) |
| 701 | |
Eric Smith | 8a10ecc | 2009-12-02 17:58:24 +0000 | [diff] [blame] | 702 | def test_format_specials(self): |
| 703 | # Test formatting of nans and infs. |
| 704 | |
| 705 | def test(fmt, value, expected): |
| 706 | # Test with both % and format(). |
| 707 | self.assertEqual(fmt % value, expected, fmt) |
| 708 | if not '#' in fmt: |
| 709 | # Until issue 7094 is implemented, format() for floats doesn't |
| 710 | # support '#' formatting |
| 711 | fmt = fmt[1:] # strip off the % |
| 712 | self.assertEqual(format(value, fmt), expected, fmt) |
| 713 | |
| 714 | for fmt in ['%e', '%f', '%g', '%.0e', '%.6f', '%.20g', |
| 715 | '%#e', '%#f', '%#g', '%#.20e', '%#.15f', '%#.3g']: |
| 716 | pfmt = '%+' + fmt[1:] |
| 717 | sfmt = '% ' + fmt[1:] |
| 718 | test(fmt, INF, 'inf') |
| 719 | test(fmt, -INF, '-inf') |
| 720 | test(fmt, NAN, 'nan') |
| 721 | test(fmt, -NAN, 'nan') |
| 722 | # When asking for a sign, it's always provided. nans are |
| 723 | # always positive. |
| 724 | test(pfmt, INF, '+inf') |
| 725 | test(pfmt, -INF, '-inf') |
| 726 | test(pfmt, NAN, '+nan') |
| 727 | test(pfmt, -NAN, '+nan') |
| 728 | # When using ' ' for a sign code, only infs can be negative. |
| 729 | # Others have a space. |
| 730 | test(sfmt, INF, ' inf') |
| 731 | test(sfmt, -INF, '-inf') |
| 732 | test(sfmt, NAN, ' nan') |
| 733 | test(sfmt, -NAN, ' nan') |
| 734 | |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 735 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 736 | # Beginning with Python 2.6 float has cross platform compatible |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 737 | # ways to create and represent inf and nan |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 738 | class InfNanTest(unittest.TestCase): |
| 739 | def test_inf_from_str(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 740 | self.assertTrue(isinf(float("inf"))) |
| 741 | self.assertTrue(isinf(float("+inf"))) |
| 742 | self.assertTrue(isinf(float("-inf"))) |
| 743 | self.assertTrue(isinf(float("infinity"))) |
| 744 | self.assertTrue(isinf(float("+infinity"))) |
| 745 | self.assertTrue(isinf(float("-infinity"))) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 746 | |
| 747 | self.assertEqual(repr(float("inf")), "inf") |
| 748 | self.assertEqual(repr(float("+inf")), "inf") |
| 749 | self.assertEqual(repr(float("-inf")), "-inf") |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 750 | self.assertEqual(repr(float("infinity")), "inf") |
| 751 | self.assertEqual(repr(float("+infinity")), "inf") |
| 752 | self.assertEqual(repr(float("-infinity")), "-inf") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 753 | |
| 754 | self.assertEqual(repr(float("INF")), "inf") |
| 755 | self.assertEqual(repr(float("+Inf")), "inf") |
| 756 | self.assertEqual(repr(float("-iNF")), "-inf") |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 757 | self.assertEqual(repr(float("Infinity")), "inf") |
| 758 | self.assertEqual(repr(float("+iNfInItY")), "inf") |
| 759 | self.assertEqual(repr(float("-INFINITY")), "-inf") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 760 | |
| 761 | self.assertEqual(str(float("inf")), "inf") |
| 762 | self.assertEqual(str(float("+inf")), "inf") |
| 763 | self.assertEqual(str(float("-inf")), "-inf") |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 764 | self.assertEqual(str(float("infinity")), "inf") |
| 765 | self.assertEqual(str(float("+infinity")), "inf") |
| 766 | self.assertEqual(str(float("-infinity")), "-inf") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 767 | |
| 768 | self.assertRaises(ValueError, float, "info") |
| 769 | self.assertRaises(ValueError, float, "+info") |
| 770 | self.assertRaises(ValueError, float, "-info") |
| 771 | self.assertRaises(ValueError, float, "in") |
| 772 | self.assertRaises(ValueError, float, "+in") |
| 773 | self.assertRaises(ValueError, float, "-in") |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 774 | self.assertRaises(ValueError, float, "infinit") |
| 775 | self.assertRaises(ValueError, float, "+Infin") |
| 776 | self.assertRaises(ValueError, float, "-INFI") |
| 777 | self.assertRaises(ValueError, float, "infinitys") |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 778 | |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 779 | self.assertRaises(ValueError, float, "++Inf") |
| 780 | self.assertRaises(ValueError, float, "-+inf") |
| 781 | self.assertRaises(ValueError, float, "+-infinity") |
| 782 | self.assertRaises(ValueError, float, "--Infinity") |
| 783 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 784 | def test_inf_as_str(self): |
| 785 | self.assertEqual(repr(1e300 * 1e300), "inf") |
| 786 | self.assertEqual(repr(-1e300 * 1e300), "-inf") |
| 787 | |
| 788 | self.assertEqual(str(1e300 * 1e300), "inf") |
| 789 | self.assertEqual(str(-1e300 * 1e300), "-inf") |
| 790 | |
| 791 | def test_nan_from_str(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 792 | self.assertTrue(isnan(float("nan"))) |
| 793 | self.assertTrue(isnan(float("+nan"))) |
| 794 | self.assertTrue(isnan(float("-nan"))) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 795 | |
| 796 | self.assertEqual(repr(float("nan")), "nan") |
| 797 | self.assertEqual(repr(float("+nan")), "nan") |
| 798 | self.assertEqual(repr(float("-nan")), "nan") |
| 799 | |
| 800 | self.assertEqual(repr(float("NAN")), "nan") |
| 801 | self.assertEqual(repr(float("+NAn")), "nan") |
| 802 | self.assertEqual(repr(float("-NaN")), "nan") |
| 803 | |
| 804 | self.assertEqual(str(float("nan")), "nan") |
| 805 | self.assertEqual(str(float("+nan")), "nan") |
| 806 | self.assertEqual(str(float("-nan")), "nan") |
| 807 | |
| 808 | self.assertRaises(ValueError, float, "nana") |
| 809 | self.assertRaises(ValueError, float, "+nana") |
| 810 | self.assertRaises(ValueError, float, "-nana") |
| 811 | self.assertRaises(ValueError, float, "na") |
| 812 | self.assertRaises(ValueError, float, "+na") |
| 813 | self.assertRaises(ValueError, float, "-na") |
| 814 | |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 815 | self.assertRaises(ValueError, float, "++nan") |
| 816 | self.assertRaises(ValueError, float, "-+NAN") |
| 817 | self.assertRaises(ValueError, float, "+-NaN") |
| 818 | self.assertRaises(ValueError, float, "--nAn") |
| 819 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 820 | def test_nan_as_str(self): |
| 821 | self.assertEqual(repr(1e300 * 1e300 * 0), "nan") |
| 822 | self.assertEqual(repr(-1e300 * 1e300 * 0), "nan") |
| 823 | |
| 824 | self.assertEqual(str(1e300 * 1e300 * 0), "nan") |
| 825 | self.assertEqual(str(-1e300 * 1e300 * 0), "nan") |
Christian Heimes | 827b35c | 2007-12-10 22:19:17 +0000 | [diff] [blame] | 826 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 827 | def notest_float_nan(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 828 | self.assertTrue(NAN.is_nan()) |
| 829 | self.assertFalse(INF.is_nan()) |
| 830 | self.assertFalse((0.).is_nan()) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 831 | |
| 832 | def notest_float_inf(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 833 | self.assertTrue(INF.is_inf()) |
| 834 | self.assertFalse(NAN.is_inf()) |
| 835 | self.assertFalse((0.).is_inf()) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 836 | |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 837 | fromHex = float.fromhex |
| 838 | toHex = float.hex |
| 839 | class HexFloatTestCase(unittest.TestCase): |
| 840 | MAX = fromHex('0x.fffffffffffff8p+1024') # max normal |
| 841 | MIN = fromHex('0x1p-1022') # min normal |
| 842 | TINY = fromHex('0x0.0000000000001p-1022') # min subnormal |
| 843 | EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up |
| 844 | |
| 845 | def identical(self, x, y): |
| 846 | # check that floats x and y are identical, or that both |
| 847 | # are NaNs |
| 848 | if isnan(x) or isnan(y): |
| 849 | if isnan(x) == isnan(y): |
| 850 | return |
| 851 | elif x == y and (x != 0.0 or copysign(1.0, x) == copysign(1.0, y)): |
| 852 | return |
| 853 | self.fail('%r not identical to %r' % (x, y)) |
| 854 | |
| 855 | def test_ends(self): |
Mark Dickinson | 38bbc48 | 2008-07-16 11:32:23 +0000 | [diff] [blame] | 856 | self.identical(self.MIN, ldexp(1.0, -1022)) |
| 857 | self.identical(self.TINY, ldexp(1.0, -1074)) |
| 858 | self.identical(self.EPS, ldexp(1.0, -52)) |
| 859 | 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] | 860 | |
| 861 | def test_invalid_inputs(self): |
| 862 | invalid_inputs = [ |
| 863 | 'infi', # misspelt infinities and nans |
| 864 | '-Infinit', |
| 865 | '++inf', |
| 866 | '-+Inf', |
| 867 | '--nan', |
| 868 | '+-NaN', |
| 869 | 'snan', |
| 870 | 'NaNs', |
| 871 | 'nna', |
Mark Dickinson | d1ec8b2 | 2009-05-11 15:45:15 +0000 | [diff] [blame] | 872 | 'an', |
| 873 | 'nf', |
| 874 | 'nfinity', |
| 875 | 'inity', |
| 876 | 'iinity', |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 877 | '0xnan', |
| 878 | '', |
| 879 | ' ', |
| 880 | 'x1.0p0', |
| 881 | '0xX1.0p0', |
| 882 | '+ 0x1.0p0', # internal whitespace |
| 883 | '- 0x1.0p0', |
| 884 | '0 x1.0p0', |
| 885 | '0x 1.0p0', |
| 886 | '0x1 2.0p0', |
| 887 | '+0x1 .0p0', |
| 888 | '0x1. 0p0', |
| 889 | '-0x1.0 1p0', |
| 890 | '-0x1.0 p0', |
| 891 | '+0x1.0p +0', |
| 892 | '0x1.0p -0', |
| 893 | '0x1.0p 0', |
| 894 | '+0x1.0p+ 0', |
| 895 | '-0x1.0p- 0', |
| 896 | '++0x1.0p-0', # double signs |
| 897 | '--0x1.0p0', |
| 898 | '+-0x1.0p+0', |
| 899 | '-+0x1.0p0', |
| 900 | '0x1.0p++0', |
| 901 | '+0x1.0p+-0', |
| 902 | '-0x1.0p-+0', |
| 903 | '0x1.0p--0', |
| 904 | '0x1.0.p0', |
| 905 | '0x.p0', # no hex digits before or after point |
| 906 | '0x1,p0', # wrong decimal point character |
| 907 | '0x1pa', |
| 908 | '0x1p\uff10', # fullwidth Unicode digits |
| 909 | '\uff10x1p0', |
| 910 | '0x\uff11p0', |
| 911 | '0x1.\uff10p0', |
| 912 | '0x1p0 \n 0x2p0', |
| 913 | '0x1p0\0 0x1p0', # embedded null byte is not end of string |
| 914 | ] |
| 915 | for x in invalid_inputs: |
Mark Dickinson | 589b795 | 2008-08-21 20:05:56 +0000 | [diff] [blame] | 916 | try: |
| 917 | result = fromHex(x) |
| 918 | except ValueError: |
| 919 | pass |
| 920 | else: |
| 921 | self.fail('Expected float.fromhex(%r) to raise ValueError; ' |
| 922 | 'got %r instead' % (x, result)) |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 923 | |
| 924 | |
Mark Dickinson | d1ec8b2 | 2009-05-11 15:45:15 +0000 | [diff] [blame] | 925 | def test_whitespace(self): |
| 926 | value_pairs = [ |
| 927 | ('inf', INF), |
| 928 | ('-Infinity', -INF), |
| 929 | ('nan', NAN), |
| 930 | ('1.0', 1.0), |
| 931 | ('-0x.2', -0.125), |
| 932 | ('-0.0', -0.0) |
| 933 | ] |
| 934 | whitespace = [ |
| 935 | '', |
| 936 | ' ', |
| 937 | '\t', |
| 938 | '\n', |
| 939 | '\n \t', |
| 940 | '\f', |
| 941 | '\v', |
| 942 | '\r' |
| 943 | ] |
| 944 | for inp, expected in value_pairs: |
| 945 | for lead in whitespace: |
| 946 | for trail in whitespace: |
| 947 | got = fromHex(lead + inp + trail) |
| 948 | self.identical(got, expected) |
| 949 | |
| 950 | |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 951 | def test_from_hex(self): |
| 952 | MIN = self.MIN; |
| 953 | MAX = self.MAX; |
| 954 | TINY = self.TINY; |
| 955 | EPS = self.EPS; |
| 956 | |
| 957 | # two spellings of infinity, with optional signs; case-insensitive |
| 958 | self.identical(fromHex('inf'), INF) |
| 959 | self.identical(fromHex('+Inf'), INF) |
| 960 | self.identical(fromHex('-INF'), -INF) |
| 961 | self.identical(fromHex('iNf'), INF) |
| 962 | self.identical(fromHex('Infinity'), INF) |
| 963 | self.identical(fromHex('+INFINITY'), INF) |
| 964 | self.identical(fromHex('-infinity'), -INF) |
| 965 | self.identical(fromHex('-iNFiNitY'), -INF) |
| 966 | |
| 967 | # nans with optional sign; case insensitive |
| 968 | self.identical(fromHex('nan'), NAN) |
| 969 | self.identical(fromHex('+NaN'), NAN) |
| 970 | self.identical(fromHex('-NaN'), NAN) |
| 971 | self.identical(fromHex('-nAN'), NAN) |
| 972 | |
| 973 | # variations in input format |
| 974 | self.identical(fromHex('1'), 1.0) |
| 975 | self.identical(fromHex('+1'), 1.0) |
| 976 | self.identical(fromHex('1.'), 1.0) |
| 977 | self.identical(fromHex('1.0'), 1.0) |
| 978 | self.identical(fromHex('1.0p0'), 1.0) |
| 979 | self.identical(fromHex('01'), 1.0) |
| 980 | self.identical(fromHex('01.'), 1.0) |
| 981 | self.identical(fromHex('0x1'), 1.0) |
| 982 | self.identical(fromHex('0x1.'), 1.0) |
| 983 | self.identical(fromHex('0x1.0'), 1.0) |
| 984 | self.identical(fromHex('+0x1.0'), 1.0) |
| 985 | self.identical(fromHex('0x1p0'), 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('0x1.p0'), 1.0) |
| 990 | self.identical(fromHex('0x1.0p0'), 1.0) |
| 991 | self.identical(fromHex('0x.1p4'), 1.0) |
| 992 | self.identical(fromHex('0x.1p04'), 1.0) |
| 993 | self.identical(fromHex('0x.1p004'), 1.0) |
| 994 | self.identical(fromHex('0x1p+0'), 1.0) |
| 995 | self.identical(fromHex('0x1P-0'), 1.0) |
| 996 | self.identical(fromHex('+0x1p0'), 1.0) |
| 997 | self.identical(fromHex('0x01p0'), 1.0) |
| 998 | self.identical(fromHex('0x1p00'), 1.0) |
| 999 | self.identical(fromHex(' 0x1p0 '), 1.0) |
| 1000 | self.identical(fromHex('\n 0x1p0'), 1.0) |
| 1001 | self.identical(fromHex('0x1p0 \t'), 1.0) |
| 1002 | self.identical(fromHex('0xap0'), 10.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('0xbep0'), 190.0) |
| 1007 | self.identical(fromHex('0xBep0'), 190.0) |
| 1008 | self.identical(fromHex('0xbEp0'), 190.0) |
| 1009 | self.identical(fromHex('0XBE0P-4'), 190.0) |
| 1010 | self.identical(fromHex('0xBEp0'), 190.0) |
| 1011 | self.identical(fromHex('0xB.Ep4'), 190.0) |
| 1012 | self.identical(fromHex('0x.BEp8'), 190.0) |
| 1013 | self.identical(fromHex('0x.0BEp12'), 190.0) |
| 1014 | |
| 1015 | # moving the point around |
| 1016 | pi = fromHex('0x1.921fb54442d18p1') |
| 1017 | self.identical(fromHex('0x.006487ed5110b46p11'), pi) |
| 1018 | self.identical(fromHex('0x.00c90fdaa22168cp10'), pi) |
| 1019 | self.identical(fromHex('0x.01921fb54442d18p9'), pi) |
| 1020 | self.identical(fromHex('0x.03243f6a8885a3p8'), pi) |
| 1021 | self.identical(fromHex('0x.06487ed5110b46p7'), pi) |
| 1022 | self.identical(fromHex('0x.0c90fdaa22168cp6'), pi) |
| 1023 | self.identical(fromHex('0x.1921fb54442d18p5'), pi) |
| 1024 | self.identical(fromHex('0x.3243f6a8885a3p4'), pi) |
| 1025 | self.identical(fromHex('0x.6487ed5110b46p3'), pi) |
| 1026 | self.identical(fromHex('0x.c90fdaa22168cp2'), pi) |
| 1027 | self.identical(fromHex('0x1.921fb54442d18p1'), pi) |
| 1028 | self.identical(fromHex('0x3.243f6a8885a3p0'), pi) |
| 1029 | self.identical(fromHex('0x6.487ed5110b46p-1'), pi) |
| 1030 | self.identical(fromHex('0xc.90fdaa22168cp-2'), pi) |
| 1031 | self.identical(fromHex('0x19.21fb54442d18p-3'), pi) |
| 1032 | self.identical(fromHex('0x32.43f6a8885a3p-4'), pi) |
| 1033 | self.identical(fromHex('0x64.87ed5110b46p-5'), pi) |
| 1034 | self.identical(fromHex('0xc9.0fdaa22168cp-6'), pi) |
| 1035 | self.identical(fromHex('0x192.1fb54442d18p-7'), pi) |
| 1036 | self.identical(fromHex('0x324.3f6a8885a3p-8'), pi) |
| 1037 | self.identical(fromHex('0x648.7ed5110b46p-9'), pi) |
| 1038 | self.identical(fromHex('0xc90.fdaa22168cp-10'), pi) |
| 1039 | self.identical(fromHex('0x1921.fb54442d18p-11'), pi) |
| 1040 | # ... |
| 1041 | self.identical(fromHex('0x1921fb54442d1.8p-47'), pi) |
| 1042 | self.identical(fromHex('0x3243f6a8885a3p-48'), pi) |
| 1043 | self.identical(fromHex('0x6487ed5110b46p-49'), pi) |
| 1044 | self.identical(fromHex('0xc90fdaa22168cp-50'), pi) |
| 1045 | self.identical(fromHex('0x1921fb54442d18p-51'), pi) |
| 1046 | self.identical(fromHex('0x3243f6a8885a30p-52'), pi) |
| 1047 | self.identical(fromHex('0x6487ed5110b460p-53'), pi) |
| 1048 | self.identical(fromHex('0xc90fdaa22168c0p-54'), pi) |
| 1049 | self.identical(fromHex('0x1921fb54442d180p-55'), pi) |
| 1050 | |
| 1051 | |
| 1052 | # results that should overflow... |
| 1053 | self.assertRaises(OverflowError, fromHex, '-0x1p1024') |
| 1054 | self.assertRaises(OverflowError, fromHex, '0x1p+1025') |
| 1055 | self.assertRaises(OverflowError, fromHex, '+0X1p1030') |
| 1056 | self.assertRaises(OverflowError, fromHex, '-0x1p+1100') |
| 1057 | self.assertRaises(OverflowError, fromHex, '0X1p123456789123456789') |
| 1058 | self.assertRaises(OverflowError, fromHex, '+0X.8p+1025') |
| 1059 | self.assertRaises(OverflowError, fromHex, '+0x0.8p1025') |
| 1060 | self.assertRaises(OverflowError, fromHex, '-0x0.4p1026') |
| 1061 | self.assertRaises(OverflowError, fromHex, '0X2p+1023') |
| 1062 | self.assertRaises(OverflowError, fromHex, '0x2.p1023') |
| 1063 | self.assertRaises(OverflowError, fromHex, '-0x2.0p+1023') |
| 1064 | self.assertRaises(OverflowError, fromHex, '+0X4p+1022') |
| 1065 | self.assertRaises(OverflowError, fromHex, '0x1.ffffffffffffffp+1023') |
| 1066 | self.assertRaises(OverflowError, fromHex, '-0X1.fffffffffffff9p1023') |
| 1067 | self.assertRaises(OverflowError, fromHex, '0X1.fffffffffffff8p1023') |
| 1068 | self.assertRaises(OverflowError, fromHex, '+0x3.fffffffffffffp1022') |
| 1069 | self.assertRaises(OverflowError, fromHex, '0x3fffffffffffffp+970') |
| 1070 | self.assertRaises(OverflowError, fromHex, '0x10000000000000000p960') |
| 1071 | self.assertRaises(OverflowError, fromHex, '-0Xffffffffffffffffp960') |
| 1072 | |
| 1073 | # ...and those that round to +-max float |
| 1074 | self.identical(fromHex('+0x1.fffffffffffffp+1023'), MAX) |
| 1075 | self.identical(fromHex('-0X1.fffffffffffff7p1023'), -MAX) |
| 1076 | self.identical(fromHex('0X1.fffffffffffff7fffffffffffffp1023'), MAX) |
| 1077 | |
| 1078 | # zeros |
| 1079 | self.identical(fromHex('0x0p0'), 0.0) |
| 1080 | self.identical(fromHex('0x0p1000'), 0.0) |
| 1081 | self.identical(fromHex('-0x0p1023'), -0.0) |
| 1082 | self.identical(fromHex('0X0p1024'), 0.0) |
| 1083 | self.identical(fromHex('-0x0p1025'), -0.0) |
| 1084 | self.identical(fromHex('0X0p2000'), 0.0) |
| 1085 | self.identical(fromHex('0x0p123456789123456789'), 0.0) |
| 1086 | self.identical(fromHex('-0X0p-0'), -0.0) |
| 1087 | self.identical(fromHex('-0X0p-1000'), -0.0) |
| 1088 | self.identical(fromHex('0x0p-1023'), 0.0) |
| 1089 | self.identical(fromHex('-0X0p-1024'), -0.0) |
| 1090 | self.identical(fromHex('-0x0p-1025'), -0.0) |
| 1091 | self.identical(fromHex('-0x0p-1072'), -0.0) |
| 1092 | self.identical(fromHex('0X0p-1073'), 0.0) |
| 1093 | self.identical(fromHex('-0x0p-1074'), -0.0) |
| 1094 | self.identical(fromHex('0x0p-1075'), 0.0) |
| 1095 | self.identical(fromHex('0X0p-1076'), 0.0) |
| 1096 | self.identical(fromHex('-0X0p-2000'), -0.0) |
| 1097 | self.identical(fromHex('-0x0p-123456789123456789'), -0.0) |
| 1098 | |
| 1099 | # values that should underflow to 0 |
| 1100 | self.identical(fromHex('0X1p-1075'), 0.0) |
| 1101 | self.identical(fromHex('-0X1p-1075'), -0.0) |
| 1102 | self.identical(fromHex('-0x1p-123456789123456789'), -0.0) |
| 1103 | self.identical(fromHex('0x1.00000000000000001p-1075'), TINY) |
| 1104 | self.identical(fromHex('-0x1.1p-1075'), -TINY) |
| 1105 | self.identical(fromHex('0x1.fffffffffffffffffp-1075'), TINY) |
| 1106 | |
| 1107 | # check round-half-even is working correctly near 0 ... |
| 1108 | self.identical(fromHex('0x1p-1076'), 0.0) |
| 1109 | self.identical(fromHex('0X2p-1076'), 0.0) |
| 1110 | self.identical(fromHex('0X3p-1076'), TINY) |
| 1111 | self.identical(fromHex('0x4p-1076'), TINY) |
| 1112 | self.identical(fromHex('0X5p-1076'), TINY) |
| 1113 | self.identical(fromHex('0X6p-1076'), 2*TINY) |
| 1114 | self.identical(fromHex('0x7p-1076'), 2*TINY) |
| 1115 | self.identical(fromHex('0X8p-1076'), 2*TINY) |
| 1116 | self.identical(fromHex('0X9p-1076'), 2*TINY) |
| 1117 | self.identical(fromHex('0xap-1076'), 2*TINY) |
| 1118 | self.identical(fromHex('0Xbp-1076'), 3*TINY) |
| 1119 | self.identical(fromHex('0xcp-1076'), 3*TINY) |
| 1120 | self.identical(fromHex('0Xdp-1076'), 3*TINY) |
| 1121 | self.identical(fromHex('0Xep-1076'), 4*TINY) |
| 1122 | self.identical(fromHex('0xfp-1076'), 4*TINY) |
| 1123 | self.identical(fromHex('0x10p-1076'), 4*TINY) |
| 1124 | self.identical(fromHex('-0x1p-1076'), -0.0) |
| 1125 | self.identical(fromHex('-0X2p-1076'), -0.0) |
| 1126 | self.identical(fromHex('-0x3p-1076'), -TINY) |
| 1127 | self.identical(fromHex('-0X4p-1076'), -TINY) |
| 1128 | self.identical(fromHex('-0x5p-1076'), -TINY) |
| 1129 | self.identical(fromHex('-0x6p-1076'), -2*TINY) |
| 1130 | self.identical(fromHex('-0X7p-1076'), -2*TINY) |
| 1131 | self.identical(fromHex('-0X8p-1076'), -2*TINY) |
| 1132 | self.identical(fromHex('-0X9p-1076'), -2*TINY) |
| 1133 | self.identical(fromHex('-0Xap-1076'), -2*TINY) |
| 1134 | self.identical(fromHex('-0xbp-1076'), -3*TINY) |
| 1135 | self.identical(fromHex('-0xcp-1076'), -3*TINY) |
| 1136 | self.identical(fromHex('-0Xdp-1076'), -3*TINY) |
| 1137 | self.identical(fromHex('-0xep-1076'), -4*TINY) |
| 1138 | self.identical(fromHex('-0Xfp-1076'), -4*TINY) |
| 1139 | self.identical(fromHex('-0X10p-1076'), -4*TINY) |
| 1140 | |
| 1141 | # ... and near MIN ... |
| 1142 | self.identical(fromHex('0x0.ffffffffffffd6p-1022'), MIN-3*TINY) |
| 1143 | self.identical(fromHex('0x0.ffffffffffffd8p-1022'), MIN-2*TINY) |
| 1144 | self.identical(fromHex('0x0.ffffffffffffdap-1022'), MIN-2*TINY) |
| 1145 | self.identical(fromHex('0x0.ffffffffffffdcp-1022'), MIN-2*TINY) |
| 1146 | self.identical(fromHex('0x0.ffffffffffffdep-1022'), MIN-2*TINY) |
| 1147 | self.identical(fromHex('0x0.ffffffffffffe0p-1022'), MIN-2*TINY) |
| 1148 | self.identical(fromHex('0x0.ffffffffffffe2p-1022'), MIN-2*TINY) |
| 1149 | self.identical(fromHex('0x0.ffffffffffffe4p-1022'), MIN-2*TINY) |
| 1150 | self.identical(fromHex('0x0.ffffffffffffe6p-1022'), MIN-2*TINY) |
| 1151 | self.identical(fromHex('0x0.ffffffffffffe8p-1022'), MIN-2*TINY) |
| 1152 | self.identical(fromHex('0x0.ffffffffffffeap-1022'), MIN-TINY) |
| 1153 | self.identical(fromHex('0x0.ffffffffffffecp-1022'), MIN-TINY) |
| 1154 | self.identical(fromHex('0x0.ffffffffffffeep-1022'), MIN-TINY) |
| 1155 | self.identical(fromHex('0x0.fffffffffffff0p-1022'), MIN-TINY) |
| 1156 | self.identical(fromHex('0x0.fffffffffffff2p-1022'), MIN-TINY) |
| 1157 | self.identical(fromHex('0x0.fffffffffffff4p-1022'), MIN-TINY) |
| 1158 | self.identical(fromHex('0x0.fffffffffffff6p-1022'), MIN-TINY) |
| 1159 | self.identical(fromHex('0x0.fffffffffffff8p-1022'), MIN) |
| 1160 | self.identical(fromHex('0x0.fffffffffffffap-1022'), MIN) |
| 1161 | self.identical(fromHex('0x0.fffffffffffffcp-1022'), MIN) |
| 1162 | self.identical(fromHex('0x0.fffffffffffffep-1022'), MIN) |
| 1163 | self.identical(fromHex('0x1.00000000000000p-1022'), MIN) |
| 1164 | self.identical(fromHex('0x1.00000000000002p-1022'), MIN) |
| 1165 | self.identical(fromHex('0x1.00000000000004p-1022'), MIN) |
| 1166 | self.identical(fromHex('0x1.00000000000006p-1022'), MIN) |
| 1167 | self.identical(fromHex('0x1.00000000000008p-1022'), MIN) |
| 1168 | self.identical(fromHex('0x1.0000000000000ap-1022'), MIN+TINY) |
| 1169 | self.identical(fromHex('0x1.0000000000000cp-1022'), MIN+TINY) |
| 1170 | self.identical(fromHex('0x1.0000000000000ep-1022'), MIN+TINY) |
| 1171 | self.identical(fromHex('0x1.00000000000010p-1022'), MIN+TINY) |
| 1172 | self.identical(fromHex('0x1.00000000000012p-1022'), MIN+TINY) |
| 1173 | self.identical(fromHex('0x1.00000000000014p-1022'), MIN+TINY) |
| 1174 | self.identical(fromHex('0x1.00000000000016p-1022'), MIN+TINY) |
| 1175 | self.identical(fromHex('0x1.00000000000018p-1022'), MIN+2*TINY) |
| 1176 | |
| 1177 | # ... and near 1.0. |
| 1178 | self.identical(fromHex('0x0.fffffffffffff0p0'), 1.0-EPS) |
| 1179 | self.identical(fromHex('0x0.fffffffffffff1p0'), 1.0-EPS) |
| 1180 | self.identical(fromHex('0X0.fffffffffffff2p0'), 1.0-EPS) |
| 1181 | self.identical(fromHex('0x0.fffffffffffff3p0'), 1.0-EPS) |
| 1182 | self.identical(fromHex('0X0.fffffffffffff4p0'), 1.0-EPS) |
| 1183 | self.identical(fromHex('0X0.fffffffffffff5p0'), 1.0-EPS/2) |
| 1184 | self.identical(fromHex('0X0.fffffffffffff6p0'), 1.0-EPS/2) |
| 1185 | self.identical(fromHex('0x0.fffffffffffff7p0'), 1.0-EPS/2) |
| 1186 | self.identical(fromHex('0x0.fffffffffffff8p0'), 1.0-EPS/2) |
| 1187 | self.identical(fromHex('0X0.fffffffffffff9p0'), 1.0-EPS/2) |
| 1188 | self.identical(fromHex('0X0.fffffffffffffap0'), 1.0-EPS/2) |
| 1189 | self.identical(fromHex('0x0.fffffffffffffbp0'), 1.0-EPS/2) |
| 1190 | self.identical(fromHex('0X0.fffffffffffffcp0'), 1.0) |
| 1191 | self.identical(fromHex('0x0.fffffffffffffdp0'), 1.0) |
| 1192 | self.identical(fromHex('0X0.fffffffffffffep0'), 1.0) |
| 1193 | self.identical(fromHex('0x0.ffffffffffffffp0'), 1.0) |
| 1194 | self.identical(fromHex('0X1.00000000000000p0'), 1.0) |
| 1195 | self.identical(fromHex('0X1.00000000000001p0'), 1.0) |
| 1196 | self.identical(fromHex('0x1.00000000000002p0'), 1.0) |
| 1197 | self.identical(fromHex('0X1.00000000000003p0'), 1.0) |
| 1198 | self.identical(fromHex('0x1.00000000000004p0'), 1.0) |
| 1199 | self.identical(fromHex('0X1.00000000000005p0'), 1.0) |
| 1200 | self.identical(fromHex('0X1.00000000000006p0'), 1.0) |
| 1201 | self.identical(fromHex('0X1.00000000000007p0'), 1.0) |
| 1202 | self.identical(fromHex('0x1.00000000000007ffffffffffffffffffffp0'), |
| 1203 | 1.0) |
| 1204 | self.identical(fromHex('0x1.00000000000008p0'), 1.0) |
| 1205 | self.identical(fromHex('0x1.00000000000008000000000000000001p0'), |
| 1206 | 1+EPS) |
| 1207 | self.identical(fromHex('0X1.00000000000009p0'), 1.0+EPS) |
| 1208 | self.identical(fromHex('0x1.0000000000000ap0'), 1.0+EPS) |
| 1209 | self.identical(fromHex('0x1.0000000000000bp0'), 1.0+EPS) |
| 1210 | self.identical(fromHex('0X1.0000000000000cp0'), 1.0+EPS) |
| 1211 | self.identical(fromHex('0x1.0000000000000dp0'), 1.0+EPS) |
| 1212 | self.identical(fromHex('0x1.0000000000000ep0'), 1.0+EPS) |
| 1213 | self.identical(fromHex('0X1.0000000000000fp0'), 1.0+EPS) |
| 1214 | self.identical(fromHex('0x1.00000000000010p0'), 1.0+EPS) |
| 1215 | self.identical(fromHex('0X1.00000000000011p0'), 1.0+EPS) |
| 1216 | self.identical(fromHex('0x1.00000000000012p0'), 1.0+EPS) |
| 1217 | self.identical(fromHex('0X1.00000000000013p0'), 1.0+EPS) |
| 1218 | self.identical(fromHex('0X1.00000000000014p0'), 1.0+EPS) |
| 1219 | self.identical(fromHex('0x1.00000000000015p0'), 1.0+EPS) |
| 1220 | self.identical(fromHex('0x1.00000000000016p0'), 1.0+EPS) |
| 1221 | self.identical(fromHex('0X1.00000000000017p0'), 1.0+EPS) |
| 1222 | self.identical(fromHex('0x1.00000000000017ffffffffffffffffffffp0'), |
| 1223 | 1.0+EPS) |
| 1224 | self.identical(fromHex('0x1.00000000000018p0'), 1.0+2*EPS) |
| 1225 | self.identical(fromHex('0X1.00000000000018000000000000000001p0'), |
| 1226 | 1.0+2*EPS) |
| 1227 | self.identical(fromHex('0x1.00000000000019p0'), 1.0+2*EPS) |
| 1228 | self.identical(fromHex('0X1.0000000000001ap0'), 1.0+2*EPS) |
| 1229 | self.identical(fromHex('0X1.0000000000001bp0'), 1.0+2*EPS) |
| 1230 | self.identical(fromHex('0x1.0000000000001cp0'), 1.0+2*EPS) |
| 1231 | self.identical(fromHex('0x1.0000000000001dp0'), 1.0+2*EPS) |
| 1232 | self.identical(fromHex('0x1.0000000000001ep0'), 1.0+2*EPS) |
| 1233 | self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS) |
| 1234 | self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS) |
| 1235 | |
| 1236 | def test_roundtrip(self): |
| 1237 | def roundtrip(x): |
| 1238 | return fromHex(toHex(x)) |
| 1239 | |
| 1240 | for x in [NAN, INF, self.MAX, self.MIN, self.MIN-self.TINY, self.TINY, 0.0]: |
| 1241 | self.identical(x, roundtrip(x)) |
| 1242 | self.identical(-x, roundtrip(-x)) |
| 1243 | |
| 1244 | # fromHex(toHex(x)) should exactly recover x, for any non-NaN float x. |
| 1245 | import random |
| 1246 | for i in range(10000): |
| 1247 | e = random.randrange(-1200, 1200) |
| 1248 | m = random.random() |
| 1249 | s = random.choice([1.0, -1.0]) |
| 1250 | try: |
| 1251 | x = s*ldexp(m, e) |
| 1252 | except OverflowError: |
| 1253 | pass |
| 1254 | else: |
| 1255 | self.identical(x, fromHex(toHex(x))) |
| 1256 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1257 | |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1258 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1259 | support.run_unittest( |
Amaury Forgeot d'Arc | 7e958d1 | 2008-09-06 21:03:22 +0000 | [diff] [blame] | 1260 | GeneralFloatCases, |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1261 | FormatFunctionsTestCase, |
| 1262 | UnknownFormatTestCase, |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 1263 | IEEEFormatTestCase, |
Christian Heimes | 827b35c | 2007-12-10 22:19:17 +0000 | [diff] [blame] | 1264 | FormatTestCase, |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1265 | ReprTestCase, |
Mark Dickinson | e6a076d | 2009-04-18 11:48:33 +0000 | [diff] [blame] | 1266 | RoundTestCase, |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1267 | InfNanTest, |
Mark Dickinson | 65fe25e | 2008-07-16 11:30:51 +0000 | [diff] [blame] | 1268 | HexFloatTestCase, |
Christian Heimes | b76922a | 2007-12-11 01:06:40 +0000 | [diff] [blame] | 1269 | ) |
Michael W. Hudson | ba283e2 | 2005-05-27 15:23:20 +0000 | [diff] [blame] | 1270 | |
| 1271 | if __name__ == '__main__': |
| 1272 | test_main() |