def test_nan(self): # Test that nan is 'far' from small, tiny, inf, max and min for dt in [np.float32, np.float64]: if dt == np.float32: maxulp = 1e6 else: maxulp = 1e12 inf = np.array([np.inf]).astype(dt) nan = np.array([np.nan]).astype(dt) big = np.array([np.finfo(dt).max]) tiny = np.array([np.finfo(dt).tiny]) zero = np.array([np.PZERO]).astype(dt) nzero = np.array([np.NZERO]).astype(dt) assert_raises( AssertionError, lambda: assert_array_max_ulp(nan, inf, maxulp=maxulp)) assert_raises( AssertionError, lambda: assert_array_max_ulp(nan, big, maxulp=maxulp)) assert_raises( AssertionError, lambda: assert_array_max_ulp(nan, tiny, maxulp=maxulp)) assert_raises( AssertionError, lambda: assert_array_max_ulp(nan, zero, maxulp=maxulp)) assert_raises( AssertionError, lambda: assert_array_max_ulp(nan, nzero, maxulp=maxulp))
def test_complex64_fail(self): nulp = 5 x = np.linspace(-20, 20, 50, dtype=np.float32) x = 10**x x = np.r_[-x, x] xi = x + x * 1j eps = np.finfo(x.dtype).eps y = x + x * eps * nulp * 2. assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, x + y * 1j, nulp) assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, y + x * 1j, nulp) y = x + x * eps * nulp assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, y + y * 1j, nulp) epsneg = np.finfo(x.dtype).epsneg y = x - x * epsneg * nulp * 2. assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, x + y * 1j, nulp) assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, y + x * 1j, nulp) y = x - x * epsneg * nulp assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, y + y * 1j, nulp)
def test_complex128_fail(self): nulp = 5 x = np.linspace(-20, 20, 50, dtype=np.float64) x = 10**x x = np.r_[-x, x] xi = x + x * 1j eps = np.finfo(x.dtype).eps y = x + x * eps * nulp * 2. assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, x + y * 1j, nulp) assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, y + x * 1j, nulp) # The test condition needs to be at least a factor of sqrt(2) smaller # because the real and imaginary parts both change y = x + x * eps * nulp assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, y + y * 1j, nulp) epsneg = np.finfo(x.dtype).epsneg y = x - x * epsneg * nulp * 2. assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, x + y * 1j, nulp) assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, y + x * 1j, nulp) y = x - x * epsneg * nulp assert_raises(AssertionError, assert_array_almost_equal_nulp, xi, y + y * 1j, nulp)
def test_float16_pass(self): nulp = 5 x = np.linspace(-4, 4, 10, dtype=np.float16) x = 10**x x = np.r_[-x, x] eps = np.finfo(x.dtype).eps y = x + x * eps * nulp / 2. assert_array_almost_equal_nulp(x, y, nulp) epsneg = np.finfo(x.dtype).epsneg y = x - x * epsneg * nulp / 2. assert_array_almost_equal_nulp(x, y, nulp)
def test_float16_fail(self): nulp = 5 x = np.linspace(-4, 4, 10, dtype=np.float16) x = 10**x x = np.r_[-x, x] eps = np.finfo(x.dtype).eps y = x + x * eps * nulp * 2. assert_raises(AssertionError, assert_array_almost_equal_nulp, x, y, nulp) epsneg = np.finfo(x.dtype).epsneg y = x - x * epsneg * nulp * 2. assert_raises(AssertionError, assert_array_almost_equal_nulp, x, y, nulp)
def test_plausible_finfo(): # Assert that finfo returns reasonable results for all types for ftype in np.sctypes['float'] + np.sctypes['complex']: info = np.finfo(ftype) assert_(info.nmant > 1) assert_(info.minexp < -1) assert_(info.maxexp > 1)
def test_int_from_huge_longdouble(self): # Produce a longdouble that would overflow a double, # use exponent that avoids bug in Darwin pow function. exp = np.finfo(np.double).maxexp - 1 huge_ld = 2 * 1234 * np.longdouble(2)**exp huge_i = 2 * 1234 * 2**exp assert_(huge_ld != np.inf) assert_equal(int(huge_ld), huge_i)
def test_float64_pass(self): # The number of units of least precision # In this case, use a few places above the lowest level (ie nulp=1) nulp = 5 x = np.linspace(-20, 20, 50, dtype=np.float64) x = 10**x x = np.r_[-x, x] # Addition eps = np.finfo(x.dtype).eps y = x + x * eps * nulp / 2. assert_array_almost_equal_nulp(x, y, nulp) # Subtraction epsneg = np.finfo(x.dtype).epsneg y = x - x * epsneg * nulp / 2. assert_array_almost_equal_nulp(x, y, nulp)
def _test_abs_func(self, absfunc): for tp in floating_types + complex_floating_types: x = tp(-1.5) assert_equal(absfunc(x), 1.5) x = tp(0.0) res = absfunc(x) # assert_equal() checks zero signedness assert_equal(res, 0.0) x = tp(-0.0) res = absfunc(x) assert_equal(res, 0.0) x = tp(np.finfo(tp).max) assert_equal(absfunc(x), x.real) x = tp(np.finfo(tp).tiny) assert_equal(absfunc(x), x.real) x = tp(np.finfo(tp).min) assert_equal(absfunc(x), -x.real)
def test_complex64_pass(self): nulp = 5 x = np.linspace(-20, 20, 50, dtype=np.float32) x = 10**x x = np.r_[-x, x] xi = x + x * 1j eps = np.finfo(x.dtype).eps y = x + x * eps * nulp / 2. assert_array_almost_equal_nulp(xi, x + y * 1j, nulp) assert_array_almost_equal_nulp(xi, y + x * 1j, nulp) y = x + x * eps * nulp / 4. assert_array_almost_equal_nulp(xi, y + y * 1j, nulp) epsneg = np.finfo(x.dtype).epsneg y = x - x * epsneg * nulp / 2. assert_array_almost_equal_nulp(xi, x + y * 1j, nulp) assert_array_almost_equal_nulp(xi, y + x * 1j, nulp) y = x - x * epsneg * nulp / 4. assert_array_almost_equal_nulp(xi, y + y * 1j, nulp)
def do_precision_upper_bound(self, float_small, float_large): eps = np.finfo(float_large).eps arr = np.array([1.0], float_small) range = np.array([0.0, 1.0 - eps], float_large) # test is looking for behavior when the bounds change between dtypes if range.astype(float_small)[-1] != 1: return # previously crashed count, x_loc = np.histogram(arr, bins=1, range=range) assert_equal(count, [1]) # gh-10322 means that the type comes from arr - this may change assert_equal(x_loc.dtype, float_small)
def _test_type_repr(self, t): finfo = np.finfo(t) last_fraction_bit_idx = finfo.nexp + finfo.nmant last_exponent_bit_idx = finfo.nexp storage_bytes = np.dtype(t).itemsize * 8 # could add some more types to the list below for which in ['small denorm', 'small norm']: # Values from http://en.wikipedia.org/wiki/IEEE_754 constr = np.array([0x00] * storage_bytes, dtype=np.uint8) if which == 'small denorm': byte = last_fraction_bit_idx // 8 bytebit = 7 - (last_fraction_bit_idx % 8) constr[byte] = 1 << bytebit elif which == 'small norm': byte = last_exponent_bit_idx // 8 bytebit = 7 - (last_exponent_bit_idx % 8) constr[byte] = 1 << bytebit else: raise ValueError('hmm') val = constr.view(t)[0] val_repr = repr(val) val2 = t(eval(val_repr)) if not (val2 == 0 and val < 1e-100): assert_equal(val, val2)
def test_dragon4(self): # these tests are adapted from Ryan Juckett's dragon4 implementation, # see dragon4.c for details. fpos32 = lambda x, **k: np.format_float_positional(np.float32(x), **k) fsci32 = lambda x, **k: np.format_float_scientific(np.float32(x), **k) fpos64 = lambda x, **k: np.format_float_positional(np.float64(x), **k) fsci64 = lambda x, **k: np.format_float_scientific(np.float64(x), **k) preckwd = lambda prec: {'unique': False, 'precision': prec} assert_equal(fpos32('1.0'), "1.") assert_equal(fsci32('1.0'), "1.e+00") assert_equal(fpos32('10.234'), "10.234") assert_equal(fpos32('-10.234'), "-10.234") assert_equal(fsci32('10.234'), "1.0234e+01") assert_equal(fsci32('-10.234'), "-1.0234e+01") assert_equal(fpos32('1000.0'), "1000.") assert_equal(fpos32('1.0', precision=0), "1.") assert_equal(fsci32('1.0', precision=0), "1.e+00") assert_equal(fpos32('10.234', precision=0), "10.") assert_equal(fpos32('-10.234', precision=0), "-10.") assert_equal(fsci32('10.234', precision=0), "1.e+01") assert_equal(fsci32('-10.234', precision=0), "-1.e+01") assert_equal(fpos32('10.234', precision=2), "10.23") assert_equal(fsci32('-10.234', precision=2), "-1.02e+01") assert_equal(fsci64('9.9999999999999995e-08', **preckwd(16)), '9.9999999999999995e-08') assert_equal(fsci64('9.8813129168249309e-324', **preckwd(16)), '9.8813129168249309e-324') assert_equal(fsci64('9.9999999999999694e-311', **preckwd(16)), '9.9999999999999694e-311') # test rounding # 3.1415927410 is closest float32 to np.pi assert_equal(fpos32('3.14159265358979323846', **preckwd(10)), "3.1415927410") assert_equal(fsci32('3.14159265358979323846', **preckwd(10)), "3.1415927410e+00") assert_equal(fpos64('3.14159265358979323846', **preckwd(10)), "3.1415926536") assert_equal(fsci64('3.14159265358979323846', **preckwd(10)), "3.1415926536e+00") # 299792448 is closest float32 to 299792458 assert_equal(fpos32('299792458.0', **preckwd(5)), "299792448.00000") assert_equal(fsci32('299792458.0', **preckwd(5)), "2.99792e+08") assert_equal(fpos64('299792458.0', **preckwd(5)), "299792458.00000") assert_equal(fsci64('299792458.0', **preckwd(5)), "2.99792e+08") assert_equal(fpos32('3.14159265358979323846', **preckwd(25)), "3.1415927410125732421875000") assert_equal(fpos64('3.14159265358979323846', **preckwd(50)), "3.14159265358979311599796346854418516159057617187500") assert_equal(fpos64('3.14159265358979323846'), "3.141592653589793") # smallest numbers assert_equal( fpos32(0.5**(126 + 23), unique=False, precision=149), "0.00000000000000000000000000000000000000000000140129846432" "4817070923729583289916131280261941876515771757068283889791" "08268586060148663818836212158203125") assert_equal( fpos64(0.5**(1022 + 52), unique=False, precision=1074), "0.00000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000000000000000000000000000" "0000000000000000000000000000000000049406564584124654417656" "8792868221372365059802614324764425585682500675507270208751" "8652998363616359923797965646954457177309266567103559397963" "9877479601078187812630071319031140452784581716784898210368" "8718636056998730723050006387409153564984387312473397273169" "6151400317153853980741262385655911710266585566867681870395" "6031062493194527159149245532930545654440112748012970999954" "1931989409080416563324524757147869014726780159355238611550" "1348035264934720193790268107107491703332226844753335720832" "4319360923828934583680601060115061698097530783422773183292" "4790498252473077637592724787465608477820373446969953364701" "7972677717585125660551199131504891101451037862738167250955" "8373897335989936648099411642057026370902792427675445652290" "87538682506419718265533447265625") # largest numbers assert_equal(fpos32(np.finfo(np.float32).max, **preckwd(0)), "340282346638528859811704183484516925440.") assert_equal( fpos64(np.finfo(np.float64).max, **preckwd(0)), "1797693134862315708145274237317043567980705675258449965989" "1747680315726078002853876058955863276687817154045895351438" "2464234321326889464182768467546703537516986049910576551282" "0762454900903893289440758685084551339423045832369032229481" "6580855933212334827479782620414472316873817718091929988125" "0404026184124858368.") # Warning: In unique mode only the integer digits necessary for # uniqueness are computed, the rest are 0. Should we change this? assert_equal(fpos32(np.finfo(np.float32).max, precision=0), "340282350000000000000000000000000000000.") # test trailing zeros assert_equal(fpos32('1.0', unique=False, precision=3), "1.000") assert_equal(fpos64('1.0', unique=False, precision=3), "1.000") assert_equal(fsci32('1.0', unique=False, precision=3), "1.000e+00") assert_equal(fsci64('1.0', unique=False, precision=3), "1.000e+00") assert_equal(fpos32('1.5', unique=False, precision=3), "1.500") assert_equal(fpos64('1.5', unique=False, precision=3), "1.500") assert_equal(fsci32('1.5', unique=False, precision=3), "1.500e+00") assert_equal(fsci64('1.5', unique=False, precision=3), "1.500e+00") # gh-10713 assert_equal( fpos64('324', unique=False, precision=5, fractional=False), "324.00")
def test_double(self): # Generate 1 + small deviation, check that adding eps gives a few UNL x = np.ones(10).astype(np.float64) x += 0.01 * np.random.randn(10).astype(np.float64) eps = np.finfo(np.float64).eps assert_array_max_ulp(x, x + eps, maxulp=200)
def test_finfo_repr(self): expected = "finfo(resolution=1e-06, min=-3.4028235e+38," + \ " max=3.4028235e+38, dtype=float32)" assert_equal(repr(np.finfo(np.float32)), expected)
class TestConversion(object): def test_int_from_long(self): l = [1e6, 1e12, 1e18, -1e6, -1e12, -1e18] li = [10**6, 10**12, 10**18, -10**6, -10**12, -10**18] for T in [None, np.float64, np.int64]: a = np.array(l, dtype=T) assert_equal([int(_m) for _m in a], li) a = np.array(l[:3], dtype=np.uint64) assert_equal([int(_m) for _m in a], li[:3]) def test_iinfo_long_values(self): for code in 'bBhH': res = np.array(np.iinfo(code).max + 1, dtype=code) tgt = np.iinfo(code).min assert_(res == tgt) for code in np.typecodes['AllInteger']: res = np.array(np.iinfo(code).max, dtype=code) tgt = np.iinfo(code).max assert_(res == tgt) for code in np.typecodes['AllInteger']: res = np.typeDict[code](np.iinfo(code).max) tgt = np.iinfo(code).max assert_(res == tgt) def test_int_raise_behaviour(self): def overflow_error_func(dtype): np.typeDict[dtype](np.iinfo(dtype).max + 1) for code in 'lLqQ': assert_raises(OverflowError, overflow_error_func, code) def test_int_from_infinite_longdouble(self): # gh-627 x = np.longdouble(np.inf) assert_raises(OverflowError, int, x) with suppress_warnings() as sup: sup.record(np.ComplexWarning) x = np.clongdouble(np.inf) assert_raises(OverflowError, int, x) assert_equal(len(sup.log), 1) @pytest.mark.skipif(not IS_PYPY, reason="Test is PyPy only (gh-9972)") def test_int_from_infinite_longdouble___int__(self): x = np.longdouble(np.inf) assert_raises(OverflowError, x.__int__) with suppress_warnings() as sup: sup.record(np.ComplexWarning) x = np.clongdouble(np.inf) assert_raises(OverflowError, x.__int__) assert_equal(len(sup.log), 1) @pytest.mark.skipif(np.finfo(np.double) == np.finfo(np.longdouble), reason="long double is same as double") @pytest.mark.skipif(platform.machine().startswith("ppc64"), reason="IBM double double") def test_int_from_huge_longdouble(self): # Produce a longdouble that would overflow a double, # use exponent that avoids bug in Darwin pow function. exp = np.finfo(np.double).maxexp - 1 huge_ld = 2 * 1234 * np.longdouble(2)**exp huge_i = 2 * 1234 * 2**exp assert_(huge_ld != np.inf) assert_equal(int(huge_ld), huge_i) def test_int_from_longdouble(self): x = np.longdouble(1.5) assert_equal(int(x), 1) x = np.longdouble(-10.5) assert_equal(int(x), -10) def test_numpy_scalar_relational_operators(self): # All integer for dt1 in np.typecodes['AllInteger']: assert_(1 > np.array(0, dtype=dt1)[()], "type %s failed" % (dt1, )) assert_(not 1 < np.array(0, dtype=dt1)[()], "type %s failed" % (dt1, )) for dt2 in np.typecodes['AllInteger']: assert_( np.array(1, dtype=dt1)[()] > np.array(0, dtype=dt2)[()], "type %s and %s failed" % (dt1, dt2)) assert_( not np.array(1, dtype=dt1)[()] < np.array(0, dtype=dt2)[()], "type %s and %s failed" % (dt1, dt2)) #Unsigned integers for dt1 in 'BHILQP': assert_(-1 < np.array(1, dtype=dt1)[()], "type %s failed" % (dt1, )) assert_(not -1 > np.array(1, dtype=dt1)[()], "type %s failed" % (dt1, )) assert_(-1 != np.array(1, dtype=dt1)[()], "type %s failed" % (dt1, )) #unsigned vs signed for dt2 in 'bhilqp': assert_( np.array(1, dtype=dt1)[()] > np.array(-1, dtype=dt2)[()], "type %s and %s failed" % (dt1, dt2)) assert_( not np.array(1, dtype=dt1)[()] < np.array(-1, dtype=dt2)[()], "type %s and %s failed" % (dt1, dt2)) assert_( np.array(1, dtype=dt1)[()] != np.array(-1, dtype=dt2)[()], "type %s and %s failed" % (dt1, dt2)) #Signed integers and floats for dt1 in 'bhlqp' + np.typecodes['Float']: assert_(1 > np.array(-1, dtype=dt1)[()], "type %s failed" % (dt1, )) assert_(not 1 < np.array(-1, dtype=dt1)[()], "type %s failed" % (dt1, )) assert_(-1 == np.array(-1, dtype=dt1)[()], "type %s failed" % (dt1, )) for dt2 in 'bhlqp' + np.typecodes['Float']: assert_( np.array(1, dtype=dt1)[()] > np.array(-1, dtype=dt2)[()], "type %s and %s failed" % (dt1, dt2)) assert_( not np.array(1, dtype=dt1)[()] < np.array(-1, dtype=dt2)[()], "type %s and %s failed" % (dt1, dt2)) assert_( np.array(-1, dtype=dt1)[()] == np.array(-1, dtype=dt2)[()], "type %s and %s failed" % (dt1, dt2)) def test_scalar_comparison_to_none(self): # Scalars should just return False and not give a warnings. # The comparisons are flagged by pep8, ignore that. with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', FutureWarning) assert_(not np.float32(1) == None) assert_(not np.str_('test') == None) # This is dubious (see below): assert_(not np.datetime64('NaT') == None) assert_(np.float32(1) != None) assert_(np.str_('test') != None) # This is dubious (see below): assert_(np.datetime64('NaT') != None) assert_(len(w) == 0) # For documentation purposes, this is why the datetime is dubious. # At the time of deprecation this was no behaviour change, but # it has to be considered when the deprecations are done. assert_(np.equal(np.datetime64('NaT'), None))
from __future__ import division, absolute_import, print_function import pytest import numpy1 as np from numpy1.testing import ( assert_, assert_equal, assert_raises, assert_array_equal, temppath, ) from numpy1.core.tests._locales import CommaDecimalPointLocale LD_INFO = np.finfo(np.longdouble) longdouble_longer_than_double = (LD_INFO.eps < np.finfo(np.double).eps) _o = 1 + LD_INFO.eps string_to_longdouble_inaccurate = (_o != np.longdouble(repr(_o))) del _o def test_scalar_extraction(): """Confirm that extracting a value doesn't convert to python float""" o = 1 + LD_INFO.eps a = np.array([o, o, o]) assert_equal(a[1], o) # Conversions string -> long double
def test_inf(self): for dt in [np.float32, np.float64]: inf = np.array([np.inf]).astype(dt) big = np.array([np.finfo(dt).max]) assert_array_max_ulp(inf, big, maxulp=200)