| 1 |
|
| 2 |
import pytest |
| 3 |
import sys |
| 4 |
|
| 5 |
import py |
| 6 |
|
| 7 |
from pypy.objspace.std.floatobject import W_FloatObject, _remove_underscores |
| 8 |
from pypy.objspace.std.intobject import W_IntObject |
| 9 |
|
| 10 |
|
| 11 |
class TestW_FloatObject: |
| 12 |
def test_pow_fff(self): |
| 13 |
x = 10.0 |
| 14 |
y = 2.0 |
| 15 |
z = 13.0 |
| 16 |
f1 = W_FloatObject(x) |
| 17 |
f2 = W_FloatObject(y) |
| 18 |
f3 = W_FloatObject(z) |
| 19 |
self.space.raises_w(self.space.w_TypeError, |
| 20 |
f1.descr_pow, |
| 21 |
self.space, f2, f3) |
| 22 |
|
| 23 |
def test_pow_ffn(self): |
| 24 |
x = 10.0 |
| 25 |
y = 2.0 |
| 26 |
f1 = W_FloatObject(x) |
| 27 |
f2 = W_FloatObject(y) |
| 28 |
v = f1.descr_pow(self.space, f2, self.space.w_None) |
| 29 |
assert v.floatval == x ** y |
| 30 |
f1 = W_FloatObject(-1.23) |
| 31 |
f2 = W_FloatObject(-4.56) |
| 32 |
v = f1.descr_pow(self.space, f2, self.space.w_None) |
| 33 |
assert self.space.isinstance_w(v, self.space.w_complex) |
| 34 |
x = -10 |
| 35 |
y = 2.0 |
| 36 |
f1 = W_FloatObject(x) |
| 37 |
f2 = W_FloatObject(y) |
| 38 |
v = f1.descr_pow(self.space, f2, self.space.w_None) |
| 39 |
assert v.floatval == x**y |
| 40 |
|
| 41 |
def test_dont_use_long_impl(self): |
| 42 |
from pypy.objspace.std.longobject import W_LongObject |
| 43 |
space = self.space |
| 44 |
saved = W_LongObject.__dict__['fromfloat'] |
| 45 |
W_LongObject.fromfloat = lambda space, x: disabled |
| 46 |
try: |
| 47 |
w_i = space.wrap(12) |
| 48 |
w_f = space.wrap(12.3) |
| 49 |
assert space.unwrap(space.eq(w_f, w_i)) is False |
| 50 |
assert space.unwrap(space.eq(w_i, w_f)) is False |
| 51 |
assert space.unwrap(space.ne(w_f, w_i)) is True |
| 52 |
assert space.unwrap(space.ne(w_i, w_f)) is True |
| 53 |
assert space.unwrap(space.lt(w_f, w_i)) is False |
| 54 |
assert space.unwrap(space.lt(w_i, w_f)) is True |
| 55 |
assert space.unwrap(space.le(w_f, w_i)) is False |
| 56 |
assert space.unwrap(space.le(w_i, w_f)) is True |
| 57 |
assert space.unwrap(space.gt(w_f, w_i)) is True |
| 58 |
assert space.unwrap(space.gt(w_i, w_f)) is False |
| 59 |
assert space.unwrap(space.ge(w_f, w_i)) is True |
| 60 |
assert space.unwrap(space.ge(w_i, w_f)) is False |
| 61 |
finally: |
| 62 |
W_LongObject.fromfloat = saved |
| 63 |
|
| 64 |
def test_remove_undercores(self): |
| 65 |
valid = [ |
| 66 |
'0_0_0', |
| 67 |
'4_2', |
| 68 |
'1_0000_0000', |
| 69 |
'0b1001_0100', |
| 70 |
'0o5_7_7', |
| 71 |
'1_00_00.5', |
| 72 |
'1_00_00.5e5', |
| 73 |
'1_00_00e5_1', |
| 74 |
'1e1_0', |
| 75 |
'.1_4', |
| 76 |
'.1_4e1', |
| 77 |
'1_00_00j', |
| 78 |
'1_00_00.5j', |
| 79 |
'1_00_00e5_1j', |
| 80 |
'.1_4j', |
| 81 |
'(1_2.5+3_3j)', |
| 82 |
'(.5_6j)', |
| 83 |
] |
| 84 |
for s in valid: |
| 85 |
assert _remove_underscores(s) == s.replace("_", "") |
| 86 |
|
| 87 |
invalid = [ |
| 88 |
|
| 89 |
'0_', |
| 90 |
'42_', |
| 91 |
'1.4j_', |
| 92 |
|
| 93 |
'4_______2', |
| 94 |
'0.1__4', |
| 95 |
'0.1__4j', |
| 96 |
'0b1001__0100', |
| 97 |
'0xffff__ffff', |
| 98 |
'0x___', |
| 99 |
'0o5__77', |
| 100 |
'1e1__0', |
| 101 |
'1e1__0j', |
| 102 |
|
| 103 |
'1_.4', |
| 104 |
'1_.4j', |
| 105 |
|
| 106 |
'1._4', |
| 107 |
'1._4j', |
| 108 |
'._5', |
| 109 |
'._5j', |
| 110 |
|
| 111 |
'1.0e+_1', |
| 112 |
'1.0e+_1j', |
| 113 |
|
| 114 |
'1.4_j', |
| 115 |
'1.4e5_j', |
| 116 |
|
| 117 |
'1_e1', |
| 118 |
'1.4_e1', |
| 119 |
'1.4_e1j', |
| 120 |
|
| 121 |
'1e_1', |
| 122 |
'1.4e_1', |
| 123 |
'1.4e_1j', |
| 124 |
|
| 125 |
'(1+1.5_j_)', |
| 126 |
'(1+1.5_j)', |
| 127 |
] |
| 128 |
for s in invalid: |
| 129 |
pytest.raises(ValueError, _remove_underscores, s) |
| 130 |
|
| 131 |
def test_avoid_bigints(space): |
| 132 |
w_f = space.newfloat(123.456) |
| 133 |
assert isinstance(w_f.descr_trunc(space), W_IntObject) |
| 134 |
assert isinstance(w_f.descr___round__(space), W_IntObject) |
| 135 |
|
| 136 |
|
| 137 |
class AppTestAppFloatTest: |
| 138 |
spaceconfig = dict(usemodules=['binascii', 'time']) |
| 139 |
|
| 140 |
def setup_class(cls): |
| 141 |
cls.w_py26 = cls.space.wrap(sys.version_info >= (2, 6)) |
| 142 |
|
| 143 |
def test_isinteger(self): |
| 144 |
assert (1.).is_integer() |
| 145 |
assert not (1.1).is_integer() |
| 146 |
assert not float("inf").is_integer() |
| 147 |
assert not float("nan").is_integer() |
| 148 |
|
| 149 |
def test_conjugate(self): |
| 150 |
assert (1.).conjugate() == 1. |
| 151 |
assert (-1.).conjugate() == -1. |
| 152 |
|
| 153 |
class F(float): |
| 154 |
pass |
| 155 |
assert F(1.).conjugate() == 1. |
| 156 |
|
| 157 |
class F(float): |
| 158 |
def __pos__(self): |
| 159 |
return 42. |
| 160 |
assert F(1.).conjugate() == 1. |
| 161 |
|
| 162 |
def test_negatives(self): |
| 163 |
assert -1.1 < 0 |
| 164 |
assert -0.1 < 0 |
| 165 |
|
| 166 |
def test_float_callable(self): |
| 167 |
assert 0.125 == float(0.125) |
| 168 |
|
| 169 |
def test_float_int(self): |
| 170 |
assert 42.0 == float(42) |
| 171 |
|
| 172 |
def test_float_hash(self): |
| 173 |
import math |
| 174 |
import sys |
| 175 |
|
| 176 |
assert hash(-1.0) == -2 |
| 177 |
assert hash(-2.0) == -2 |
| 178 |
assert hash(-3.0) == -3 |
| 179 |
assert hash(42.0) == 42 |
| 180 |
if sys.maxsize > 2 ** 31 - 1: |
| 181 |
assert hash(42.125) == 288230376151711786 |
| 182 |
assert hash(math.ldexp(0.125, 1000)) == 2097152 |
| 183 |
assert hash(3.141593) == 326491229203594243 |
| 184 |
assert hash(2.5) == 1152921504606846978 |
| 185 |
else: |
| 186 |
assert hash(42.125) == 268435498 |
| 187 |
assert hash(math.ldexp(0.125, 1000)) == 32 |
| 188 |
assert hash(3.141593) == 671854639 |
| 189 |
assert hash(2.5) == 1073741826 |
| 190 |
inf = float('inf') |
| 191 |
assert hash(inf) == 314159 |
| 192 |
assert hash(-inf) == -314159 |
| 193 |
|
| 194 |
def test_float_hash_nan(self): |
| 195 |
nan = float('nan') |
| 196 |
assert hash(nan) == object.__hash__(nan) |
| 197 |
class F(float): |
| 198 |
pass |
| 199 |
nanf1 = F('nan') |
| 200 |
nanf2 = F('nan') |
| 201 |
assert hash(nanf1) != 0 |
| 202 |
assert hash(nanf1) != 0 |
| 203 |
assert hash(nanf1) != hash(nanf2) |
| 204 |
|
| 205 |
def test_int_float(self): |
| 206 |
assert int(42.1234) == 42 |
| 207 |
assert int(4e10) == 40000000000 |
| 208 |
|
| 209 |
raises(OverflowError, int, float('inf')) |
| 210 |
raises(ValueError, int, float('nan')) |
| 211 |
|
| 212 |
def test_float_string(self): |
| 213 |
assert 42 == float("42") |
| 214 |
assert 42.25 == float("42.25") |
| 215 |
inf = 1e200*1e200 |
| 216 |
assert float("inf") == inf |
| 217 |
assert float("+inf") == inf |
| 218 |
assert float("-INf") == -inf |
| 219 |
assert str(inf) == "inf" |
| 220 |
assert str(-inf) == "-inf" |
| 221 |
assert str(float("infinity")) == 'inf' |
| 222 |
assert str(float("+infinity")) == 'inf' |
| 223 |
assert str(float("-infinity")) == '-inf' |
| 224 |
assert str(float("nan")) == "nan" |
| 225 |
assert str(float("-nAn")) == "nan" |
| 226 |
assert repr(inf) == "inf" |
| 227 |
assert repr(-inf) == "-inf" |
| 228 |
assert repr(float("nan")) == "nan" |
| 229 |
assert repr(float("+nan")) == "nan" |
| 230 |
assert repr(float("-nAn")) == "nan" |
| 231 |
|
| 232 |
assert float(memoryview(b"inf")) == inf |
| 233 |
assert float(bytearray(b"inf")) == inf |
| 234 |
|
| 235 |
raises(UnicodeEncodeError, float, u"\ud800") |
| 236 |
|
| 237 |
def test_float_string_underscores(self): |
| 238 |
valid = [ |
| 239 |
'0_0_0', |
| 240 |
'4_2', |
| 241 |
'1_0000_0000', |
| 242 |
'1_00_00.5', |
| 243 |
'1_00_00.5e5', |
| 244 |
'1_00_00e5_1', |
| 245 |
'1e1_0', |
| 246 |
'.1_4', |
| 247 |
'.1_4e1', |
| 248 |
] |
| 249 |
for s in valid: |
| 250 |
assert float(s) == float(s.replace("_", "")) |
| 251 |
assert eval(s) == eval(s.replace("_", "")) |
| 252 |
|
| 253 |
invalid = [ |
| 254 |
|
| 255 |
'0_', |
| 256 |
'42_', |
| 257 |
|
| 258 |
'4_______2', |
| 259 |
'0.1__4', |
| 260 |
'0.1__4j', |
| 261 |
'0b1001__0100', |
| 262 |
'0xffff__ffff', |
| 263 |
'0x___', |
| 264 |
'0o5__77', |
| 265 |
'1e1__0', |
| 266 |
|
| 267 |
'1_.4', |
| 268 |
|
| 269 |
'1._4', |
| 270 |
'._5', |
| 271 |
|
| 272 |
'1.0e+_1', |
| 273 |
|
| 274 |
'1_e1', |
| 275 |
'1.4_e1', |
| 276 |
|
| 277 |
'1e_1', |
| 278 |
'1.4e_1', |
| 279 |
] |
| 280 |
for s in invalid: |
| 281 |
raises(ValueError, float, s) |
| 282 |
raises(SyntaxError, eval, s) |
| 283 |
|
| 284 |
def test_float_unicode(self): |
| 285 |
|
| 286 |
assert 42.75 == float(chr(0x00A0)+str("42.75")+chr(0x2000)) |
| 287 |
class FloatStr(str): |
| 288 |
def __float__(self): |
| 289 |
return float(str(self)) + 1 |
| 290 |
assert float(FloatStr("8")) == 9.0 |
| 291 |
|
| 292 |
def test_float_long(self): |
| 293 |
assert 42.0 == float(42) |
| 294 |
assert 10000000000.0 == float(10000000000) |
| 295 |
raises(OverflowError, float, 10**400) |
| 296 |
|
| 297 |
def test_as_integer_ratio(self): |
| 298 |
for f, ratio in [ |
| 299 |
(0.875, (7, 8)), |
| 300 |
(-0.875, (-7, 8)), |
| 301 |
(0.0, (0, 1)), |
| 302 |
(11.5, (23, 2)), |
| 303 |
]: |
| 304 |
assert f.as_integer_ratio() == ratio |
| 305 |
|
| 306 |
raises(OverflowError, float('inf').as_integer_ratio) |
| 307 |
raises(OverflowError, float('-inf').as_integer_ratio) |
| 308 |
raises(ValueError, float('nan').as_integer_ratio) |
| 309 |
|
| 310 |
def test_float_conversion(self): |
| 311 |
class X(float): |
| 312 |
def __float__(self): |
| 313 |
return 42. |
| 314 |
assert float(X()) == 42. |
| 315 |
|
| 316 |
def test_float_conversion_deprecated_warning(self): |
| 317 |
import warnings |
| 318 |
|
| 319 |
class X(float): |
| 320 |
def __float__(self): |
| 321 |
return self |
| 322 |
x = X(42) |
| 323 |
|
| 324 |
with warnings.catch_warnings(record=True) as log: |
| 325 |
warnings.simplefilter("always", DeprecationWarning) |
| 326 |
converted_x = float(x) |
| 327 |
|
| 328 |
assert converted_x == 42. |
| 329 |
assert len(log) == 1 |
| 330 |
|
| 331 |
def test_round(self): |
| 332 |
import math |
| 333 |
assert 1.0 == round(1.0) |
| 334 |
assert 1.0 == round(1.1) |
| 335 |
assert 2.0 == round(1.9) |
| 336 |
assert 2.0 == round(1.5) |
| 337 |
assert -2.0 == round(-1.5) |
| 338 |
assert -2.0 == round(-1.5) |
| 339 |
assert -2.0 == round(-1.5, 0) |
| 340 |
assert -2.0 == round(-1.5, 0) |
| 341 |
assert -2.0 == round(-1.5, None) |
| 342 |
assert 22.2 == round(22.222222, 1) |
| 343 |
assert 20.0 == round(22.22222, -1) |
| 344 |
assert 0.0 == round(22.22222, -2) |
| 345 |
|
| 346 |
assert round(123.456, -308) == 0.0 |
| 347 |
assert round(123.456, -700) == 0.0 |
| 348 |
assert round(123.456, -2**100) == 0.0 |
| 349 |
assert math.copysign(1., round(-123.456, -700)) == -1. |
| 350 |
assert round(2.5, 0) == 2.0 |
| 351 |
|
| 352 |
def test_round_special_method(self): |
| 353 |
assert 2.0 == 1.9 .__round__() |
| 354 |
assert -2.0 == -1.5 .__round__(None) |
| 355 |
assert 20.0 == 22.22222 .__round__(-1) |
| 356 |
|
| 357 |
def test_special_float_method(self): |
| 358 |
class a(object): |
| 359 |
def __float__(self): |
| 360 |
self.ar = True |
| 361 |
return None |
| 362 |
inst = a() |
| 363 |
raises(TypeError, float, inst) |
| 364 |
assert inst.ar |
| 365 |
|
| 366 |
class b(object): |
| 367 |
pass |
| 368 |
raises((AttributeError, TypeError), float, b()) |
| 369 |
|
| 370 |
def test_getnewargs(self): |
| 371 |
assert 0.0 .__getnewargs__() == (0.0,) |
| 372 |
|
| 373 |
def test_pow(self): |
| 374 |
import math |
| 375 |
|
| 376 |
def pw(x, y): |
| 377 |
return x ** y |
| 378 |
def espeq(x, y): |
| 379 |
return not abs(x-y) > 1e05 |
| 380 |
raises(ZeroDivisionError, pw, 0.0, -1) |
| 381 |
assert pw(0, 0.5) == 0.0 |
| 382 |
assert espeq(pw(4.0, 0.5), 2.0) |
| 383 |
assert pw(4.0, 0) == 1.0 |
| 384 |
assert pw(-4.0, 0) == 1.0 |
| 385 |
assert type(pw(-1.0, 0.5)) == complex |
| 386 |
assert pw(-1.0, 2.0) == 1.0 |
| 387 |
assert pw(-1.0, 3.0) == -1.0 |
| 388 |
assert pw(-1.0, 1e200) == 1.0 |
| 389 |
if self.py26: |
| 390 |
assert pw(0.0, float("-inf")) == float("inf") |
| 391 |
assert math.isnan(pw(-3, float("nan"))) |
| 392 |
assert math.isnan(pw(-3., float("nan"))) |
| 393 |
assert pw(-1.0, -float('inf')) == 1.0 |
| 394 |
assert pw(-1.0, float('inf')) == 1.0 |
| 395 |
assert pw(float('inf'), 0) == 1.0 |
| 396 |
assert pw(float('nan'), 0) == 1.0 |
| 397 |
|
| 398 |
assert math.isinf(pw(-0.5, float('-inf'))) |
| 399 |
assert math.isinf(pw(+0.5, float('-inf'))) |
| 400 |
assert pw(-1.5, float('-inf')) == 0.0 |
| 401 |
assert pw(+1.5, float('-inf')) == 0.0 |
| 402 |
|
| 403 |
assert str(pw(float('-inf'), -0.5)) == '0.0' |
| 404 |
assert str(pw(float('-inf'), -2.0)) == '0.0' |
| 405 |
assert str(pw(float('-inf'), -1.0)) == '-0.0' |
| 406 |
assert str(pw(float('-inf'), 1.0)) == '-inf' |
| 407 |
assert str(pw(float('-inf'), 2.0)) == 'inf' |
| 408 |
|
| 409 |
def test_builtin_pow(self): |
| 410 |
result = pow(-1, 0.5) |
| 411 |
def assertAlmostEqual(x, y): assert round(abs(y - x), 7) == 0 |
| 412 |
assertAlmostEqual(result, 1j) |
| 413 |
|
| 414 |
def test_pow_neg_base(self): |
| 415 |
import math |
| 416 |
def pw(x, y): |
| 417 |
return x ** y |
| 418 |
assert pw(-2.0, 2.0) == 4 |
| 419 |
res = pw(-2.0, -2001.0) |
| 420 |
assert res == -0.0 |
| 421 |
assert math.copysign(1., res) == -1. |
| 422 |
assert pw(-1.0, -1e15) == 1.0 |
| 423 |
|
| 424 |
def test_float_cmp(self): |
| 425 |
assert 12.5 == 12.5 |
| 426 |
assert 12.5 != -3.2 |
| 427 |
assert 12.5 < 123.4 |
| 428 |
assert .25 <= .25 |
| 429 |
assert -5744.23 <= -51.2 |
| 430 |
assert 4.3 > 2.3 |
| 431 |
assert 0.01 >= -0.01 |
| 432 |
|
| 433 |
verylonglong = 10**400 |
| 434 |
infinite = 1e200*1e200 |
| 435 |
assert 12.0 == 12 |
| 436 |
assert 1e300 == (1e300) |
| 437 |
assert 12.1 != 12 |
| 438 |
assert infinite != 123456789 |
| 439 |
assert 12.9 < 13 |
| 440 |
assert -infinite < -13 |
| 441 |
assert 12.9 <= 13 |
| 442 |
assert 13.0 <= 13 |
| 443 |
assert 13.01 > 13 |
| 444 |
assert 13.0 >= 13 |
| 445 |
assert 13.01 >= 13 |
| 446 |
assert 12.0 == 12 |
| 447 |
assert 12.1 != 12 |
| 448 |
assert infinite != 123456789 |
| 449 |
assert 12.9 < 13 |
| 450 |
assert -infinite < -13 |
| 451 |
assert 12.9 <= 13 |
| 452 |
assert 13.0 <= 13 |
| 453 |
assert 13.01 > 13 |
| 454 |
assert 13.0 >= 13 |
| 455 |
assert 13.01 >= 13 |
| 456 |
assert infinite > verylonglong |
| 457 |
assert infinite >= verylonglong |
| 458 |
assert 1234.56 < verylonglong |
| 459 |
assert 1234.56 <= verylonglong |
| 460 |
|
| 461 |
assert 12 == 12.0 |
| 462 |
assert int(1e300) == 1e300 |
| 463 |
assert 12 != 12.1 |
| 464 |
assert 123456789 != infinite |
| 465 |
assert 13 > 12.9 |
| 466 |
assert -13 > -infinite |
| 467 |
assert 13 >= 12.9 |
| 468 |
assert 13 >= 13.0 |
| 469 |
assert 13 < 13.01 |
| 470 |
assert 13 <= 13.0 |
| 471 |
assert 13 <= 13.01 |
| 472 |
assert verylonglong < infinite |
| 473 |
assert verylonglong <= infinite |
| 474 |
assert verylonglong > 1234.56 |
| 475 |
assert verylonglong >= 1234.56 |
| 476 |
assert 13 >= 12.9 |
| 477 |
assert 13 >= 13.0 |
| 478 |
assert 13 < 13.01 |
| 479 |
assert 13 <= 13.0 |
| 480 |
assert 13 <= 13.01 |
| 481 |
|
| 482 |
def test_comparison_more(self): |
| 483 |
import sys |
| 484 |
is_pypy = '__pypy__' in sys.builtin_module_names |
| 485 |
infinity = 1e200*1e200 |
| 486 |
nan = infinity/infinity |
| 487 |
for x in (123, 1 << 30, |
| 488 |
(1 << 33) - 1, 1 << 33, (1 << 33) + 1, |
| 489 |
1 << 62, 1 << 70): |
| 490 |
|
| 491 |
assert (x == float(x)) |
| 492 |
assert (x >= float(x)) |
| 493 |
assert (x <= float(x)) |
| 494 |
assert not (x != float(x)) |
| 495 |
assert not (x > float(x)) |
| 496 |
assert not (x < float(x)) |
| 497 |
|
| 498 |
assert not ((x - 1) == float(x)) |
| 499 |
assert not ((x - 1) >= float(x)) |
| 500 |
assert ((x - 1) <= float(x)) |
| 501 |
assert ((x - 1) != float(x)) |
| 502 |
assert not ((x - 1) > float(x)) |
| 503 |
assert ((x - 1) < float(x)) |
| 504 |
|
| 505 |
assert not ((x + 1) == float(x)) |
| 506 |
assert ((x + 1) >= float(x)) |
| 507 |
assert not ((x + 1) <= float(x)) |
| 508 |
assert ((x + 1) != float(x)) |
| 509 |
assert ((x + 1) > float(x)) |
| 510 |
assert not ((x + 1) < float(x)) |
| 511 |
|
| 512 |
assert not (x == infinity) |
| 513 |
assert not (x >= infinity) |
| 514 |
assert (x <= infinity) |
| 515 |
assert (x != infinity) |
| 516 |
assert not (x > infinity) |
| 517 |
assert (x < infinity) |
| 518 |
|
| 519 |
assert not (x == -infinity) |
| 520 |
assert (x >= -infinity) |
| 521 |
assert not (x <= -infinity) |
| 522 |
assert (x != -infinity) |
| 523 |
assert (x > -infinity) |
| 524 |
assert not (x < -infinity) |
| 525 |
|
| 526 |
if is_pypy: |
| 527 |
assert not (x == nan) |
| 528 |
assert not (x >= nan) |
| 529 |
assert not (x <= nan) |
| 530 |
assert (x != nan) |
| 531 |
assert not (x > nan) |
| 532 |
assert not (x < nan) |
| 533 |
|
| 534 |
assert (float(x) == x) |
| 535 |
assert (float(x) <= x) |
| 536 |
assert (float(x) >= x) |
| 537 |
assert not (float(x) != x) |
| 538 |
assert not (float(x) < x) |
| 539 |
assert not (float(x) > x) |
| 540 |
|
| 541 |
assert not (float(x) == (x - 1)) |
| 542 |
assert not (float(x) <= (x - 1)) |
| 543 |
assert (float(x) >= (x - 1)) |
| 544 |
assert (float(x) != (x - 1)) |
| 545 |
assert not (float(x) < (x - 1)) |
| 546 |
assert (float(x) > (x - 1)) |
| 547 |
|
| 548 |
assert not (float(x) == (x + 1)) |
| 549 |
assert (float(x) <= (x + 1)) |
| 550 |
assert not (float(x) >= (x + 1)) |
| 551 |
assert (float(x) != (x + 1)) |
| 552 |
assert (float(x) < (x + 1)) |
| 553 |
assert not (float(x) > (x + 1)) |
| 554 |
|
| 555 |
assert not (infinity == x) |
| 556 |
assert (infinity >= x) |
| 557 |
assert not (infinity <= x) |
| 558 |
assert (infinity != x) |
| 559 |
assert (infinity > x) |
| 560 |
assert not (infinity < x) |
| 561 |
|
| 562 |
assert not (-infinity == x) |
| 563 |
assert not (-infinity >= x) |
| 564 |
assert (-infinity <= x) |
| 565 |
assert (-infinity != x) |
| 566 |
assert not (-infinity > x) |
| 567 |
assert (-infinity < x) |
| 568 |
|
| 569 |
if is_pypy: |
| 570 |
assert not (nan == x) |
| 571 |
assert not (nan <= x) |
| 572 |
assert not (nan >= x) |
| 573 |
assert (nan != x) |
| 574 |
assert not (nan < x) |
| 575 |
assert not (nan > x) |
| 576 |
|
| 577 |
def test___getformat__(self): |
| 578 |
assert float.__getformat__("float") != "unknown" |
| 579 |
assert float.__getformat__("double") != "unknown" |
| 580 |
raises(ValueError, float.__getformat__, "random") |
| 581 |
|
| 582 |
def test_trunc(self): |
| 583 |
import math |
| 584 |
assert math.trunc(1.5) == 1 |
| 585 |
assert math.trunc(-1.5) == -1 |
| 586 |
assert math.trunc(1.999999) == 1 |
| 587 |
assert math.trunc(-1.999999) == -1 |
| 588 |
assert math.trunc(-0.999999) == -0 |
| 589 |
assert math.trunc(-100.999) == -100 |
| 590 |
raises(OverflowError, math.trunc, float("inf")) |
| 591 |
|
| 592 |
def test_floor_ceil(self): |
| 593 |
import math |
| 594 |
assert math.floor(1.5) == float(1.5).__floor__() |
| 595 |
assert math.ceil(1.5) == float(1.5).__ceil__() |
| 596 |
|
| 597 |
def test_call_special(self): |
| 598 |
assert 5 .__add__(3.14) is NotImplemented |
| 599 |
assert 3.25 .__add__(5) == 8.25 |
| 600 |
|
| 601 |
assert 5 .__eq__(3.14) is NotImplemented |
| 602 |
assert 3.14 .__eq__(5) is False |
| 603 |
|
| 604 |
def test_from_string(self): |
| 605 |
raises(ValueError, float, "\0") |
| 606 |
raises(ValueError, float, '\uD8F0') |
| 607 |
|
| 608 |
def test_format(self): |
| 609 |
f = 1.1234e200 |
| 610 |
assert f.__format__("G") == "1.1234E+200" |
| 611 |
assert 123.456.__format__('.4') == '123.5' |
| 612 |
assert 1234.56.__format__('.4') == '1.235e+03' |
| 613 |
assert 12345.6.__format__('.4') == '1.235e+04' |
| 614 |
|
| 615 |
def test_format_thousands(self): |
| 616 |
f = float('inf') |
| 617 |
assert format(1000, '20,f') == ' 1,000.000000' |
| 618 |
|
| 619 |
def test_format_bug_inf_float(self): |
| 620 |
f = float('inf') |
| 621 |
assert format(f, '7,f') == ' inf' |
| 622 |
f = float('-inf') |
| 623 |
assert format(f, '7,f') == ' -inf' |
| 624 |
f = float('nan') |
| 625 |
assert format(f, '7,f') == ' nan' |
| 626 |
|
| 627 |
def test_float_real(self): |
| 628 |
class A(float): pass |
| 629 |
b = A(5).real |
| 630 |
assert type(b) is float |
| 631 |
|
| 632 |
def test_invalid_literal_message(self): |
| 633 |
try: |
| 634 |
float('abcdef') |
| 635 |
except ValueError as e: |
| 636 |
assert 'abcdef' in str(e) |
| 637 |
else: |
| 638 |
assert False, 'did not raise' |
| 639 |
|
| 640 |
@py.test.mark.skipif("not config.option.runappdirect and sys.maxunicode == 0xffff") |
| 641 |
def test_float_from_unicode(self): |
| 642 |
s = '\U0001D7CF\U0001D7CE.4' 𝟎.4 |
| 643 |
assert float(s) == 10.4 |
| 644 |
|
| 645 |
def test_hash_minus_one(self): |
| 646 |
assert hash(-1.0) == -2 |
| 647 |
assert (-1.0).__hash__() == -2 |
| 648 |
|
| 649 |
def test_float_from_dict(self): |
| 650 |
try: |
| 651 |
float({}) |
| 652 |
except TypeError as e: |
| 653 |
assert "not 'dict'" in str(e) |
| 654 |
else: |
| 655 |
assert False, 'did not raise' |
| 656 |
|
| 657 |
def test_non_numeric_input_types(self): |
| 658 |
|
| 659 |
|
| 660 |
class CustomStr(str): pass |
| 661 |
class CustomBytes(bytes): pass |
| 662 |
class CustomByteArray(bytearray): pass |
| 663 |
|
| 664 |
factories = [ |
| 665 |
bytes, |
| 666 |
bytearray, |
| 667 |
lambda b: CustomStr(b.decode()), |
| 668 |
CustomBytes, |
| 669 |
CustomByteArray, |
| 670 |
memoryview, |
| 671 |
] |
| 672 |
try: |
| 673 |
from array import array |
| 674 |
except ImportError: |
| 675 |
pass |
| 676 |
else: |
| 677 |
factories.append(lambda b: array('B', b)) |
| 678 |
|
| 679 |
for f in factories: |
| 680 |
x = f(b" 3.14 ") |
| 681 |
assert float(x) == 3.14 |
| 682 |
try: |
| 683 |
float(f(b'A' * 0x10)) |
| 684 |
except ValueError as e: |
| 685 |
assert "could not convert" in str(e) |
| 686 |
else: |
| 687 |
assert False, 'did not raise' |
| 688 |
|
| 689 |
def test_new_pos_only(self): |
| 690 |
with raises(TypeError) as info: |
| 691 |
float(x=1) |
| 692 |
assert "float() takes no keyword arguments" in str(info.value) |
| 693 |
|
| 694 |
def test_float_constructor_calls_index(self): |
| 695 |
class A: |
| 696 |
def __index__(self): |
| 697 |
return 25 |
| 698 |
assert float(A()) == 25.0 |
| 699 |
|
| 700 |
reallybig = 1 << 1000 |
| 701 |
class A: |
| 702 |
def __index__(self): |
| 703 |
return reallybig |
| 704 |
assert float(A()) == float(reallybig) |
| 705 |
|
| 706 |
def test_subclass_kwarg(self): |
| 707 |
class bare_subclass(float): |
| 708 |
pass |
| 709 |
with raises(TypeError): |
| 710 |
bare_subclass(1.0, newarg=3) |
| 711 |
|
| 712 |
class subclass_with_new(float): |
| 713 |
def __new__(cls, arg, newarg=None): |
| 714 |
self = super().__new__(cls, arg) |
| 715 |
self.newarg = newarg |
| 716 |
return self |
| 717 |
u = subclass_with_new(1.0, newarg=3) |
| 718 |
assert u.newarg == 3 |
| 719 |
|
| 720 |
class subclass_with_init(float): |
| 721 |
def __init__(self, arg, newarg=None): |
| 722 |
self.newarg = newarg |
| 723 |
u = subclass_with_init(1.0, newarg=3) |
| 724 |
assert u.newarg == 3 |
| 725 |
|
| 726 |
|
| 727 |
class AppTestFloatHex: |
| 728 |
spaceconfig = { |
| 729 |
'usemodules': ['binascii', 'time', 'struct'], |
| 730 |
} |
| 731 |
|
| 732 |
def w_identical(self, x, y): |
| 733 |
import math |
| 734 |
|
| 735 |
|
| 736 |
if math.isnan(x) or math.isnan(y): |
| 737 |
if math.isnan(x) == math.isnan(y): |
| 738 |
return |
| 739 |
assert (x == y and (x != 0.0 or |
| 740 |
math.copysign(1.0, x) == math.copysign(1.0, y))) |
| 741 |
|
| 742 |
def test_from_hex(self): |
| 743 |
fromHex = float.fromhex |
| 744 |
import sys |
| 745 |
INF = float("inf") |
| 746 |
NAN = float("nan") |
| 747 |
MIN = sys.float_info.min |
| 748 |
MAX = sys.float_info.max |
| 749 |
TINY = fromHex('0x0.0000000000001p-1022') |
| 750 |
EPS = sys.float_info.epsilon |
| 751 |
|
| 752 |
|
| 753 |
self.identical(fromHex('inf'), INF) |
| 754 |
self.identical(fromHex('+Inf'), INF) |
| 755 |
self.identical(fromHex('-INF'), -INF) |
| 756 |
self.identical(fromHex('iNf'), INF) |
| 757 |
self.identical(fromHex('Infinity'), INF) |
| 758 |
self.identical(fromHex('+INFINITY'), INF) |
| 759 |
self.identical(fromHex('-infinity'), -INF) |
| 760 |
self.identical(fromHex('-iNFiNitY'), -INF) |
| 761 |
|
| 762 |
|
| 763 |
self.identical(fromHex('nan'), NAN) |
| 764 |
self.identical(fromHex('+NaN'), NAN) |
| 765 |
self.identical(fromHex('-NaN'), NAN) |
| 766 |
self.identical(fromHex('-nAN'), NAN) |
| 767 |
|
| 768 |
|
| 769 |
self.identical(fromHex('1'), 1.0) |
| 770 |
self.identical(fromHex('+1'), 1.0) |
| 771 |
self.identical(fromHex('1.'), 1.0) |
| 772 |
self.identical(fromHex('1.0'), 1.0) |
| 773 |
self.identical(fromHex('1.0p0'), 1.0) |
| 774 |
self.identical(fromHex('01'), 1.0) |
| 775 |
self.identical(fromHex('01.'), 1.0) |
| 776 |
self.identical(fromHex('0x1'), 1.0) |
| 777 |
self.identical(fromHex('0x1.'), 1.0) |
| 778 |
self.identical(fromHex('0x1.0'), 1.0) |
| 779 |
self.identical(fromHex('+0x1.0'), 1.0) |
| 780 |
self.identical(fromHex('0x1p0'), 1.0) |
| 781 |
self.identical(fromHex('0X1p0'), 1.0) |
| 782 |
self.identical(fromHex('0X1P0'), 1.0) |
| 783 |
self.identical(fromHex('0x1P0'), 1.0) |
| 784 |
self.identical(fromHex('0x1.p0'), 1.0) |
| 785 |
self.identical(fromHex('0x1.0p0'), 1.0) |
| 786 |
self.identical(fromHex('0x.1p4'), 1.0) |
| 787 |
self.identical(fromHex('0x.1p04'), 1.0) |
| 788 |
self.identical(fromHex('0x.1p004'), 1.0) |
| 789 |
self.identical(fromHex('0x1p+0'), 1.0) |
| 790 |
self.identical(fromHex('0x1P-0'), 1.0) |
| 791 |
self.identical(fromHex('+0x1p0'), 1.0) |
| 792 |
self.identical(fromHex('0x01p0'), 1.0) |
| 793 |
self.identical(fromHex('0x1p00'), 1.0) |
| 794 |
self.identical(fromHex('0x1p0'), 1.0) |
| 795 |
self.identical(fromHex(' 0x1p0 '), 1.0) |
| 796 |
self.identical(fromHex('\n 0x1p0'), 1.0) |
| 797 |
self.identical(fromHex('0x1p0 \t'), 1.0) |
| 798 |
self.identical(fromHex('0xap0'), 10.0) |
| 799 |
self.identical(fromHex('0xAp0'), 10.0) |
| 800 |
self.identical(fromHex('0xaP0'), 10.0) |
| 801 |
self.identical(fromHex('0xAP0'), 10.0) |
| 802 |
self.identical(fromHex('0xbep0'), 190.0) |
| 803 |
self.identical(fromHex('0xBep0'), 190.0) |
| 804 |
self.identical(fromHex('0xbEp0'), 190.0) |
| 805 |
self.identical(fromHex('0XBE0P-4'), 190.0) |
| 806 |
self.identical(fromHex('0xBEp0'), 190.0) |
| 807 |
self.identical(fromHex('0xB.Ep4'), 190.0) |
| 808 |
self.identical(fromHex('0x.BEp8'), 190.0) |
| 809 |
self.identical(fromHex('0x.0BEp12'), 190.0) |
| 810 |
|
| 811 |
|
| 812 |
pi = fromHex('0x1.921fb54442d18p1') |
| 813 |
self.identical(fromHex('0x.006487ed5110b46p11'), pi) |
| 814 |
self.identical(fromHex('0x.00c90fdaa22168cp10'), pi) |
| 815 |
self.identical(fromHex('0x.01921fb54442d18p9'), pi) |
| 816 |
self.identical(fromHex('0x.03243f6a8885a3p8'), pi) |
| 817 |
self.identical(fromHex('0x.06487ed5110b46p7'), pi) |
| 818 |
self.identical(fromHex('0x.0c90fdaa22168cp6'), pi) |
| 819 |
self.identical(fromHex('0x.1921fb54442d18p5'), pi) |
| 820 |
self.identical(fromHex('0x.3243f6a8885a3p4'), pi) |
| 821 |
self.identical(fromHex('0x.6487ed5110b46p3'), pi) |
| 822 |
self.identical(fromHex('0x.c90fdaa22168cp2'), pi) |
| 823 |
self.identical(fromHex('0x1.921fb54442d18p1'), pi) |
| 824 |
self.identical(fromHex('0x3.243f6a8885a3p0'), pi) |
| 825 |
self.identical(fromHex('0x6.487ed5110b46p-1'), pi) |
| 826 |
self.identical(fromHex('0xc.90fdaa22168cp-2'), pi) |
| 827 |
self.identical(fromHex('0x19.21fb54442d18p-3'), pi) |
| 828 |
self.identical(fromHex('0x32.43f6a8885a3p-4'), pi) |
| 829 |
self.identical(fromHex('0x64.87ed5110b46p-5'), pi) |
| 830 |
self.identical(fromHex('0xc9.0fdaa22168cp-6'), pi) |
| 831 |
self.identical(fromHex('0x192.1fb54442d18p-7'), pi) |
| 832 |
self.identical(fromHex('0x324.3f6a8885a3p-8'), pi) |
| 833 |
self.identical(fromHex('0x648.7ed5110b46p-9'), pi) |
| 834 |
self.identical(fromHex('0xc90.fdaa22168cp-10'), pi) |
| 835 |
self.identical(fromHex('0x1921.fb54442d18p-11'), pi) |
| 836 |
|
| 837 |
self.identical(fromHex('0x1921fb54442d1.8p-47'), pi) |
| 838 |
self.identical(fromHex('0x3243f6a8885a3p-48'), pi) |
| 839 |
self.identical(fromHex('0x6487ed5110b46p-49'), pi) |
| 840 |
self.identical(fromHex('0xc90fdaa22168cp-50'), pi) |
| 841 |
self.identical(fromHex('0x1921fb54442d18p-51'), pi) |
| 842 |
self.identical(fromHex('0x3243f6a8885a30p-52'), pi) |
| 843 |
self.identical(fromHex('0x6487ed5110b460p-53'), pi) |
| 844 |
self.identical(fromHex('0xc90fdaa22168c0p-54'), pi) |
| 845 |
self.identical(fromHex('0x1921fb54442d180p-55'), pi) |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
raises(OverflowError, fromHex, '-0x1p1024') |
| 850 |
raises(OverflowError, fromHex, '0x1p+1025') |
| 851 |
raises(OverflowError, fromHex, '+0X1p1030') |
| 852 |
raises(OverflowError, fromHex, '-0x1p+1100') |
| 853 |
raises(OverflowError, fromHex, '0X1p123456789123456789') |
| 854 |
raises(OverflowError, fromHex, '+0X.8p+1025') |
| 855 |
raises(OverflowError, fromHex, '+0x0.8p1025') |
| 856 |
raises(OverflowError, fromHex, '-0x0.4p1026') |
| 857 |
raises(OverflowError, fromHex, '0X2p+1023') |
| 858 |
raises(OverflowError, fromHex, '0x2.p1023') |
| 859 |
raises(OverflowError, fromHex, '-0x2.0p+1023') |
| 860 |
raises(OverflowError, fromHex, '+0X4p+1022') |
| 861 |
raises(OverflowError, fromHex, '0x1.ffffffffffffffp+1023') |
| 862 |
raises(OverflowError, fromHex, '-0X1.fffffffffffff9p1023') |
| 863 |
raises(OverflowError, fromHex, '0X1.fffffffffffff8p1023') |
| 864 |
raises(OverflowError, fromHex, '+0x3.fffffffffffffp1022') |
| 865 |
raises(OverflowError, fromHex, '0x3fffffffffffffp+970') |
| 866 |
raises(OverflowError, fromHex, '0x10000000000000000p960') |
| 867 |
raises(OverflowError, fromHex, '-0Xffffffffffffffffp960') |
| 868 |
|
| 869 |
|
| 870 |
self.identical(fromHex('+0x1.fffffffffffffp+1023'), MAX) |
| 871 |
self.identical(fromHex('-0X1.fffffffffffff7p1023'), -MAX) |
| 872 |
self.identical(fromHex('0X1.fffffffffffff7fffffffffffffp1023'), MAX) |
| 873 |
|
| 874 |
|
| 875 |
self.identical(fromHex('0x0p0'), 0.0) |
| 876 |
self.identical(fromHex('0x0p1000'), 0.0) |
| 877 |
self.identical(fromHex('-0x0p1023'), -0.0) |
| 878 |
self.identical(fromHex('0X0p1024'), 0.0) |
| 879 |
self.identical(fromHex('-0x0p1025'), -0.0) |
| 880 |
self.identical(fromHex('0X0p2000'), 0.0) |
| 881 |
self.identical(fromHex('0x0p123456789123456789'), 0.0) |
| 882 |
self.identical(fromHex('-0X0p-0'), -0.0) |
| 883 |
self.identical(fromHex('-0X0p-1000'), -0.0) |
| 884 |
self.identical(fromHex('0x0p-1023'), 0.0) |
| 885 |
self.identical(fromHex('-0X0p-1024'), -0.0) |
| 886 |
self.identical(fromHex('-0x0p-1025'), -0.0) |
| 887 |
self.identical(fromHex('-0x0p-1072'), -0.0) |
| 888 |
self.identical(fromHex('0X0p-1073'), 0.0) |
| 889 |
self.identical(fromHex('-0x0p-1074'), -0.0) |
| 890 |
self.identical(fromHex('0x0p-1075'), 0.0) |
| 891 |
self.identical(fromHex('0X0p-1076'), 0.0) |
| 892 |
self.identical(fromHex('-0X0p-2000'), -0.0) |
| 893 |
self.identical(fromHex('-0x0p-123456789123456789'), -0.0) |
| 894 |
self.identical(fromHex('0x1.0p00000000000000000000000000000003'), 8.0) |
| 895 |
self.identical(fromHex('0x1.0p+0000000000000000000000000000003'), 8.0) |
| 896 |
self.identical(fromHex('0x1.0p-000000000000000000000000000003'), 0.125) |
| 897 |
|
| 898 |
|
| 899 |
self.identical(fromHex('0X1p-1075'), 0.0) |
| 900 |
self.identical(fromHex('-0X1p-1075'), -0.0) |
| 901 |
self.identical(fromHex('-0x1p-123456789123456789'), -0.0) |
| 902 |
self.identical(fromHex('0x1.00000000000000001p-1075'), TINY) |
| 903 |
self.identical(fromHex('-0x1.1p-1075'), -TINY) |
| 904 |
self.identical(fromHex('0x1.fffffffffffffffffp-1075'), TINY) |
| 905 |
|
| 906 |
|
| 907 |
self.identical(fromHex('0x1p-1076'), 0.0) |
| 908 |
self.identical(fromHex('0X2p-1076'), 0.0) |
| 909 |
self.identical(fromHex('0X3p-1076'), TINY) |
| 910 |
self.identical(fromHex('0x4p-1076'), TINY) |
| 911 |
self.identical(fromHex('0X5p-1076'), TINY) |
| 912 |
self.identical(fromHex('0X6p-1076'), 2*TINY) |
| 913 |
self.identical(fromHex('0x7p-1076'), 2*TINY) |
| 914 |
self.identical(fromHex('0X8p-1076'), 2*TINY) |
| 915 |
self.identical(fromHex('0X9p-1076'), 2*TINY) |
| 916 |
self.identical(fromHex('0xap-1076'), 2*TINY) |
| 917 |
self.identical(fromHex('0Xbp-1076'), 3*TINY) |
| 918 |
self.identical(fromHex('0xcp-1076'), 3*TINY) |
| 919 |
self.identical(fromHex('0Xdp-1076'), 3*TINY) |
| 920 |
self.identical(fromHex('0Xep-1076'), 4*TINY) |
| 921 |
self.identical(fromHex('0xfp-1076'), 4*TINY) |
| 922 |
self.identical(fromHex('0x10p-1076'), 4*TINY) |
| 923 |
self.identical(fromHex('-0x1p-1076'), -0.0) |
| 924 |
self.identical(fromHex('-0X2p-1076'), -0.0) |
| 925 |
self.identical(fromHex('-0x3p-1076'), -TINY) |
| 926 |
self.identical(fromHex('-0X4p-1076'), -TINY) |
| 927 |
self.identical(fromHex('-0x5p-1076'), -TINY) |
| 928 |
self.identical(fromHex('-0x6p-1076'), -2*TINY) |
| 929 |
self.identical(fromHex('-0X7p-1076'), -2*TINY) |
| 930 |
self.identical(fromHex('-0X8p-1076'), -2*TINY) |
| 931 |
self.identical(fromHex('-0X9p-1076'), -2*TINY) |
| 932 |
self.identical(fromHex('-0Xap-1076'), -2*TINY) |
| 933 |
self.identical(fromHex('-0xbp-1076'), -3*TINY) |
| 934 |
self.identical(fromHex('-0xcp-1076'), -3*TINY) |
| 935 |
self.identical(fromHex('-0Xdp-1076'), -3*TINY) |
| 936 |
self.identical(fromHex('-0xep-1076'), -4*TINY) |
| 937 |
self.identical(fromHex('-0Xfp-1076'), -4*TINY) |
| 938 |
self.identical(fromHex('-0X10p-1076'), -4*TINY) |
| 939 |
|
| 940 |
|
| 941 |
self.identical(fromHex('0x0.ffffffffffffd6p-1022'), MIN-3*TINY) |
| 942 |
self.identical(fromHex('0x0.ffffffffffffd8p-1022'), MIN-2*TINY) |
| 943 |
self.identical(fromHex('0x0.ffffffffffffdap-1022'), MIN-2*TINY) |
| 944 |
self.identical(fromHex('0x0.ffffffffffffdcp-1022'), MIN-2*TINY) |
| 945 |
self.identical(fromHex('0x0.ffffffffffffdep-1022'), MIN-2*TINY) |
| 946 |
self.identical(fromHex('0x0.ffffffffffffe0p-1022'), MIN-2*TINY) |
| 947 |
self.identical(fromHex('0x0.ffffffffffffe2p-1022'), MIN-2*TINY) |
| 948 |
self.identical(fromHex('0x0.ffffffffffffe4p-1022'), MIN-2*TINY) |
| 949 |
self.identical(fromHex('0x0.ffffffffffffe6p-1022'), MIN-2*TINY) |
| 950 |
self.identical(fromHex('0x0.ffffffffffffe8p-1022'), MIN-2*TINY) |
| 951 |
self.identical(fromHex('0x0.ffffffffffffeap-1022'), MIN-TINY) |
| 952 |
self.identical(fromHex('0x0.ffffffffffffecp-1022'), MIN-TINY) |
| 953 |
self.identical(fromHex('0x0.ffffffffffffeep-1022'), MIN-TINY) |
| 954 |
self.identical(fromHex('0x0.fffffffffffff0p-1022'), MIN-TINY) |
| 955 |
self.identical(fromHex('0x0.fffffffffffff2p-1022'), MIN-TINY) |
| 956 |
self.identical(fromHex('0x0.fffffffffffff4p-1022'), MIN-TINY) |
| 957 |
self.identical(fromHex('0x0.fffffffffffff6p-1022'), MIN-TINY) |
| 958 |
self.identical(fromHex('0x0.fffffffffffff8p-1022'), MIN) |
| 959 |
self.identical(fromHex('0x0.fffffffffffffap-1022'), MIN) |
| 960 |
self.identical(fromHex('0x0.fffffffffffffcp-1022'), MIN) |
| 961 |
self.identical(fromHex('0x0.fffffffffffffep-1022'), MIN) |
| 962 |
self.identical(fromHex('0x1.00000000000000p-1022'), MIN) |
| 963 |
self.identical(fromHex('0x1.00000000000002p-1022'), MIN) |
| 964 |
self.identical(fromHex('0x1.00000000000004p-1022'), MIN) |
| 965 |
self.identical(fromHex('0x1.00000000000006p-1022'), MIN) |
| 966 |
self.identical(fromHex('0x1.00000000000008p-1022'), MIN) |
| 967 |
self.identical(fromHex('0x1.0000000000000ap-1022'), MIN+TINY) |
| 968 |
self.identical(fromHex('0x1.0000000000000cp-1022'), MIN+TINY) |
| 969 |
self.identical(fromHex('0x1.0000000000000ep-1022'), MIN+TINY) |
| 970 |
self.identical(fromHex('0x1.00000000000010p-1022'), MIN+TINY) |
| 971 |
self.identical(fromHex('0x1.00000000000012p-1022'), MIN+TINY) |
| 972 |
self.identical(fromHex('0x1.00000000000014p-1022'), MIN+TINY) |
| 973 |
self.identical(fromHex('0x1.00000000000016p-1022'), MIN+TINY) |
| 974 |
self.identical(fromHex('0x1.00000000000018p-1022'), MIN+2*TINY) |
| 975 |
|
| 976 |
|
| 977 |
self.identical(fromHex('0x0.fffffffffffff0p0'), 1.0-EPS) |
| 978 |
self.identical(fromHex('0x0.fffffffffffff1p0'), 1.0-EPS) |
| 979 |
self.identical(fromHex('0X0.fffffffffffff2p0'), 1.0-EPS) |
| 980 |
self.identical(fromHex('0x0.fffffffffffff3p0'), 1.0-EPS) |
| 981 |
self.identical(fromHex('0X0.fffffffffffff4p0'), 1.0-EPS) |
| 982 |
self.identical(fromHex('0X0.fffffffffffff5p0'), 1.0-EPS/2) |
| 983 |
self.identical(fromHex('0X0.fffffffffffff6p0'), 1.0-EPS/2) |
| 984 |
self.identical(fromHex('0x0.fffffffffffff7p0'), 1.0-EPS/2) |
| 985 |
self.identical(fromHex('0x0.fffffffffffff8p0'), 1.0-EPS/2) |
| 986 |
self.identical(fromHex('0X0.fffffffffffff9p0'), 1.0-EPS/2) |
| 987 |
self.identical(fromHex('0X0.fffffffffffffap0'), 1.0-EPS/2) |
| 988 |
self.identical(fromHex('0x0.fffffffffffffbp0'), 1.0-EPS/2) |
| 989 |
self.identical(fromHex('0X0.fffffffffffffcp0'), 1.0) |
| 990 |
self.identical(fromHex('0x0.fffffffffffffdp0'), 1.0) |
| 991 |
self.identical(fromHex('0X0.fffffffffffffep0'), 1.0) |
| 992 |
self.identical(fromHex('0x0.ffffffffffffffp0'), 1.0) |
| 993 |
self.identical(fromHex('0X1.00000000000000p0'), 1.0) |
| 994 |
self.identical(fromHex('0X1.00000000000001p0'), 1.0) |
| 995 |
self.identical(fromHex('0x1.00000000000002p0'), 1.0) |
| 996 |
self.identical(fromHex('0X1.00000000000003p0'), 1.0) |
| 997 |
self.identical(fromHex('0x1.00000000000004p0'), 1.0) |
| 998 |
self.identical(fromHex('0X1.00000000000005p0'), 1.0) |
| 999 |
self.identical(fromHex('0X1.00000000000006p0'), 1.0) |
| 1000 |
self.identical(fromHex('0X1.00000000000007p0'), 1.0) |
| 1001 |
self.identical(fromHex('0x1.00000000000007ffffffffffffffffffffp0'), |
| 1002 |
1.0) |
| 1003 |
self.identical(fromHex('0x1.00000000000008p0'), 1.0) |
| 1004 |
self.identical(fromHex('0x1.00000000000008000000000000000001p0'), |
| 1005 |
1+EPS) |
| 1006 |
self.identical(fromHex('0X1.00000000000009p0'), 1.0+EPS) |
| 1007 |
self.identical(fromHex('0x1.0000000000000ap0'), 1.0+EPS) |
| 1008 |
self.identical(fromHex('0x1.0000000000000bp0'), 1.0+EPS) |
| 1009 |
self.identical(fromHex('0X1.0000000000000cp0'), 1.0+EPS) |
| 1010 |
self.identical(fromHex('0x1.0000000000000dp0'), 1.0+EPS) |
| 1011 |
self.identical(fromHex('0x1.0000000000000ep0'), 1.0+EPS) |
| 1012 |
self.identical(fromHex('0X1.0000000000000fp0'), 1.0+EPS) |
| 1013 |
self.identical(fromHex('0x1.00000000000010p0'), 1.0+EPS) |
| 1014 |
self.identical(fromHex('0X1.00000000000011p0'), 1.0+EPS) |
| 1015 |
self.identical(fromHex('0x1.00000000000012p0'), 1.0+EPS) |
| 1016 |
self.identical(fromHex('0X1.00000000000013p0'), 1.0+EPS) |
| 1017 |
self.identical(fromHex('0X1.00000000000014p0'), 1.0+EPS) |
| 1018 |
self.identical(fromHex('0x1.00000000000015p0'), 1.0+EPS) |
| 1019 |
self.identical(fromHex('0x1.00000000000016p0'), 1.0+EPS) |
| 1020 |
self.identical(fromHex('0X1.00000000000017p0'), 1.0+EPS) |
| 1021 |
self.identical(fromHex('0x1.00000000000017ffffffffffffffffffffp0'), |
| 1022 |
1.0+EPS) |
| 1023 |
self.identical(fromHex('0x1.00000000000018p0'), 1.0+2*EPS) |
| 1024 |
self.identical(fromHex('0X1.00000000000018000000000000000001p0'), |
| 1025 |
1.0+2*EPS) |
| 1026 |
self.identical(fromHex('0x1.00000000000019p0'), 1.0+2*EPS) |
| 1027 |
self.identical(fromHex('0X1.0000000000001ap0'), 1.0+2*EPS) |
| 1028 |
self.identical(fromHex('0X1.0000000000001bp0'), 1.0+2*EPS) |
| 1029 |
self.identical(fromHex('0x1.0000000000001cp0'), 1.0+2*EPS) |
| 1030 |
self.identical(fromHex('0x1.0000000000001dp0'), 1.0+2*EPS) |
| 1031 |
self.identical(fromHex('0x1.0000000000001ep0'), 1.0+2*EPS) |
| 1032 |
self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS) |
| 1033 |
self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS) |
| 1034 |
|
| 1035 |
|
| 1036 |
self.identical(fromHex('0x.8p-1074'), 0.0) |
| 1037 |
self.identical(fromHex('0x.80p-1074'), 0.0) |
| 1038 |
self.identical(fromHex('0x.81p-1074'), TINY) |
| 1039 |
self.identical(fromHex('0x8p-1078'), 0.0) |
| 1040 |
self.identical(fromHex('0x8.0p-1078'), 0.0) |
| 1041 |
self.identical(fromHex('0x8.1p-1078'), TINY) |
| 1042 |
self.identical(fromHex('0x80p-1082'), 0.0) |
| 1043 |
self.identical(fromHex('0x81p-1082'), TINY) |
| 1044 |
self.identical(fromHex('.8p-1074'), 0.0) |
| 1045 |
self.identical(fromHex('8p-1078'), 0.0) |
| 1046 |
self.identical(fromHex('-.8p-1074'), -0.0) |
| 1047 |
self.identical(fromHex('+8p-1078'), 0.0) |
| 1048 |
|
| 1049 |
def test_roundtrip(self): |
| 1050 |
def roundtrip(x): |
| 1051 |
return float.fromhex(x.hex()) |
| 1052 |
import sys |
| 1053 |
import math |
| 1054 |
TINY = float.fromhex('0x0.0000000000001p-1022') |
| 1055 |
|
| 1056 |
for x in [float("nan"), float("inf"), sys.float_info.max, |
| 1057 |
sys.float_info.min, sys.float_info.min-TINY, TINY, 0.0]: |
| 1058 |
self.identical(x, roundtrip(x)) |
| 1059 |
self.identical(-x, roundtrip(-x)) |
| 1060 |
|
| 1061 |
|
| 1062 |
import random |
| 1063 |
for i in range(500): |
| 1064 |
e = random.randrange(-1200, 1200) |
| 1065 |
m = random.random() |
| 1066 |
s = random.choice([1.0, -1.0]) |
| 1067 |
try: |
| 1068 |
x = s*math.ldexp(m, e) |
| 1069 |
except OverflowError: |
| 1070 |
pass |
| 1071 |
else: |
| 1072 |
self.identical(x, float.fromhex(x.hex())) |
| 1073 |
|
| 1074 |
def test_invalid(self): |
| 1075 |
raises(ValueError, float.fromhex, "0P") |
| 1076 |
|
| 1077 |
def test_division_edgecases(self): |
| 1078 |
import math |
| 1079 |
|
| 1080 |
|
| 1081 |
inf = float("inf") |
| 1082 |
assert math.isnan(inf % 3) |
| 1083 |
assert math.isnan(inf // 3) |
| 1084 |
x, y = divmod(inf, 3) |
| 1085 |
assert math.isnan(x) |
| 1086 |
assert math.isnan(y) |
| 1087 |
x, y = divmod(3, inf) |
| 1088 |
z = 3 % inf |
| 1089 |
assert x == 0 |
| 1090 |
assert y == 3 |
| 1091 |
assert z == 3 |
| 1092 |
|
| 1093 |
|
| 1094 |
raises(ZeroDivisionError, lambda: inf % 0) |
| 1095 |
raises(ZeroDivisionError, lambda: inf // 0) |
| 1096 |
raises(ZeroDivisionError, divmod, inf, 0) |
| 1097 |
|
| 1098 |
def test_modulo_edgecases(self): |
| 1099 |
|
| 1100 |
|
| 1101 |
mod = float.__mod__ |
| 1102 |
import math |
| 1103 |
|
| 1104 |
def check(a, b): |
| 1105 |
assert (a, math.copysign(1.0, a)) == (b, math.copysign(1.0, b)) |
| 1106 |
|
| 1107 |
check(mod(-1.0, 1.0), 0.0) |
| 1108 |
check(mod(-1e-100, 1.0), 1.0) |
| 1109 |
check(mod(-0.0, 1.0), 0.0) |
| 1110 |
check(mod(0.0, 1.0), 0.0) |
| 1111 |
check(mod(1e-100, 1.0), 1e-100) |
| 1112 |
check(mod(1.0, 1.0), 0.0) |
| 1113 |
|
| 1114 |
check(mod(-1.0, -1.0), -0.0) |
| 1115 |
check(mod(-1e-100, -1.0), -1e-100) |
| 1116 |
check(mod(-0.0, -1.0), -0.0) |
| 1117 |
check(mod(0.0, -1.0), -0.0) |
| 1118 |
check(mod(1e-100, -1.0), -1.0) |
| 1119 |
check(mod(1.0, -1.0), -0.0) |
| 1120 |
|
| 1121 |
def test_equality_rounding(self): |
| 1122 |
i = int(2 ** 63 - 1) |
| 1123 |
f = float(i) |
| 1124 |
assert f == 2.0 ** 63 |
| 1125 |
assert i != f |
| 1126 |
assert f != i |
| 1127 |
assert int(i) != f |
| 1128 |
assert f != int(i) |
| 1129 |
|
| 1130 |
def test_repr_str_eq(self): |
| 1131 |
assert repr(19 * 0.1) == str(19 * 0.1) |
| 1132 |
|
| 1133 |
def test_str_is_from_object(self): |
| 1134 |
assert float.__str__ is object.__str__ |