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