def test_floordiv_array(self):
with testing.NumpyError(divide='ignore'):
self.check_array_array_op(operator.floordiv, no_complex=True)
python类floordiv()的实例源码
def test_broadcasted_floordiv(self):
with testing.NumpyError(divide='ignore'):
self.check_array_broadcasted_op(operator.floordiv, no_complex=True)
def test_doubly_broadcasted_floordiv(self):
with testing.NumpyError(divide='ignore'):
self.check_array_doubly_broadcasted_op(operator.floordiv,
no_complex=True)
def test_floordiv(self):
self.assertRaises(TypeError, operator.floordiv, 5)
self.assertRaises(TypeError, operator.floordiv, None, None)
self.assertTrue(operator.floordiv(5, 2) == 2)
def test_complex(self):
self.assertFalse(issubclass(complex, Real))
self.assertTrue(issubclass(complex, Complex))
c1, c2 = complex(3, 2), complex(4,1)
# XXX: This is not ideal, but see the comment in math_trunc().
self.assertRaises(TypeError, math.trunc, c1)
self.assertRaises(TypeError, operator.mod, c1, c2)
self.assertRaises(TypeError, divmod, c1, c2)
self.assertRaises(TypeError, operator.floordiv, c1, c2)
self.assertRaises(TypeError, float, c1)
self.assertRaises(TypeError, int, c1)
def __floordiv__(self, y):
return NonStandardInteger(operator.floordiv(self.val, y))
def __rfloordiv__(self, y):
return NonStandardInteger(operator.floordiv(y, self.val))
def test_float_modulus_exact(self):
# test that float results are exact for small integers. This also
# holds for the same integers scaled by powers of two.
nlst = list(range(-127, 0))
plst = list(range(1, 128))
dividend = nlst + [0] + plst
divisor = nlst + plst
arg = list(itertools.product(dividend, divisor))
tgt = list(divmod(*t) for t in arg)
a, b = np.array(arg, dtype=int).T
# convert exact integer results from Python to float so that
# signed zero can be used, it is checked.
tgtdiv, tgtrem = np.array(tgt, dtype=float).T
tgtdiv = np.where((tgtdiv == 0.0) & ((b < 0) ^ (a < 0)), -0.0, tgtdiv)
tgtrem = np.where((tgtrem == 0.0) & (b < 0), -0.0, tgtrem)
for dt in np.typecodes['Float']:
msg = 'dtype: %s' % (dt,)
fa = a.astype(dt)
fb = b.astype(dt)
# use list comprehension so a_ and b_ are scalars
div = [self.floordiv(a_, b_) for a_, b_ in zip(fa, fb)]
rem = [self.mod(a_, b_) for a_, b_ in zip(fa, fb)]
assert_equal(div, tgtdiv, err_msg=msg)
assert_equal(rem, tgtrem, err_msg=msg)
def check_floordiv(Poly):
c1 = list(random((4,)) + .5)
c2 = list(random((3,)) + .5)
c3 = list(random((2,)) + .5)
p1 = Poly(c1)
p2 = Poly(c2)
p3 = Poly(c3)
p4 = p1 * p2 + p3
c4 = list(p4.coef)
assert_poly_almost_equal(p4 // p2, p1)
assert_poly_almost_equal(p4 // c2, p1)
assert_poly_almost_equal(c4 // p2, p1)
assert_poly_almost_equal(p4 // tuple(c2), p1)
assert_poly_almost_equal(tuple(c4) // p2, p1)
assert_poly_almost_equal(p4 // np.array(c2), p1)
assert_poly_almost_equal(np.array(c4) // p2, p1)
assert_poly_almost_equal(2 // p2, Poly([0]))
assert_poly_almost_equal(p2 // 2, 0.5*p2)
assert_raises(
TypeError, op.floordiv, p1, Poly([0], domain=Poly.domain + 1))
assert_raises(
TypeError, op.floordiv, p1, Poly([0], window=Poly.window + 1))
if Poly is Polynomial:
assert_raises(TypeError, op.floordiv, p1, Chebyshev([0]))
else:
assert_raises(TypeError, op.floordiv, p1, Polynomial([0]))
def __floordiv__(self, other):
assert type(other) in scalar_types
return Vector2(operator.floordiv(self.x, other),
operator.floordiv(self.y, other))
def __rfloordiv__(self, other):
assert type(other) in scalar_types
return Vector2(operator.floordiv(other, self.x),
operator.floordiv(other, self.y))
def __floordiv__(self, other):
assert type(other) in scalar_types
return Vector3(operator.floordiv(self.x, other),
operator.floordiv(self.y, other),
operator.floordiv(self.z, other))
def __rfloordiv__(self, other):
assert type(other) in scalar_types
return Vector3(operator.floordiv(other, self.x),
operator.floordiv(other, self.y),
operator.floordiv(other, self.z))
def apply_floordiv(left: Column, right):
if type(right) == Column:
result, index = apply_fast_floordiv(left.values, right.values,
left.index, right.index)
return Column(result, index)
else:
return Column(operator.floordiv(left.values, right), left.index)
def test_div(self):
self.assertRaises(TypeError, operator.div, 5)
self.assertRaises(TypeError, operator.div, None, None)
self.assertTrue(operator.floordiv(5, 2) == 2)
def test_floordiv(self):
self.assertRaises(TypeError, operator.floordiv, 5)
self.assertRaises(TypeError, operator.floordiv, None, None)
self.assertTrue(operator.floordiv(5, 2) == 2)
def test_div(self):
self.assertRaises(TypeError, operator.div, 5)
self.assertRaises(TypeError, operator.div, None, None)
self.assertTrue(operator.floordiv(5, 2) == 2)
def test_floordiv(self):
self.assertRaises(TypeError, operator.floordiv, 5)
self.assertRaises(TypeError, operator.floordiv, None, None)
self.assertTrue(operator.floordiv(5, 2) == 2)
def test_rfloordiv_scalar(self):
with testing.NumpyError(divide='ignore'):
self.check_array_scalar_op(operator.floordiv, swap=True)
def test_floordiv_array(self):
with testing.NumpyError(divide='ignore'):
self.check_array_array_op(operator.floordiv)