Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: floordiv, mod, divmod #943 #946

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions pint/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,8 @@ def __int__(self):
def __float__(self):
if self.dimensionless:
return float(self._convert_magnitude_not_inplace(UnitsContainer()))
# else: # ( for math.floor !? )
# return float(self._magnitude)
raise DimensionalityError(self._units, "dimensionless")

def __complex__(self):
Expand Down Expand Up @@ -1092,14 +1094,24 @@ def __rtruediv__(self, other):
__div__ = __truediv__
__rdiv__ = __rtruediv__
__idiv__ = __itruediv__

def __ceil__(self):
return self.__floordiv__(1) + self.__class__(1, self._units)

def __floor__(self):
return self.__floordiv__(1)

def __ifloordiv__(self, other):
if self._check(other):
self._magnitude //= other.to(self._units)._magnitude
elif self.dimensionless:
self._magnitude = self.to("")._magnitude // other
else:
raise DimensionalityError(self._units, "dimensionless")
if not hasattr(other, "dimensionless"):
magnitude = self._magnitude // other
return self.__class__(magnitude, self._units)
else:
raise DimensionalityError(self._units, "dimensionless")
self._units = UnitsContainer({})
return self

Expand All @@ -1110,7 +1122,11 @@ def __floordiv__(self, other):
elif self.dimensionless:
magnitude = self.to("")._magnitude // other
else:
raise DimensionalityError(self._units, "dimensionless")
if not hasattr(other, "dimensionless"):
magnitude = self._magnitude // other
return self.__class__(magnitude, self._units)
else:
raise DimensionalityError(self._units, "dimensionless")
return self.__class__(magnitude, UnitsContainer({}))

@check_implemented
Expand All @@ -1131,9 +1147,11 @@ def __imod__(self, other):

@check_implemented
def __mod__(self, other):
if not self._check(other):
other = self.__class__(other, UnitsContainer({}))
magnitude = self._magnitude % other.to(self._units)._magnitude
if self._check(other):
magnitude = self._magnitude % other.to(self._units)._magnitude
else:
# other = self.__class__(other, UnitsContainer({}))
magnitude = self._magnitude % other
return self.__class__(magnitude, self._units)

@check_implemented
Expand All @@ -1149,9 +1167,15 @@ def __rmod__(self, other):

@check_implemented
def __divmod__(self, other):
if not self._check(other):
other = self.__class__(other, UnitsContainer({}))
q, r = divmod(self._magnitude, other.to(self._units)._magnitude)
if self._check(other):
q, r = divmod(self._magnitude, other.to(self._units)._magnitude)
else:
if not hasattr(other, "dimensionless"):
q, r = divmod(self._magnitude, other)
return (self.__class__(q, self._units), self.__class__(r, self._units))
else:
other = self.__class__(other, UnitsContainer({}))
q, r = divmod(self._magnitude, other.to(self._units)._magnitude)
return (self.__class__(q, UnitsContainer({})), self.__class__(r, self._units))

@check_implemented
Expand Down
10 changes: 5 additions & 5 deletions pint/testsuite/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,23 +717,23 @@ def _test_quantity_floordiv(self, unit, func):
b = self.Q_("3*second")
self.assertRaises(DimensionalityError, op.floordiv, a, b)
self.assertRaises(DimensionalityError, op.floordiv, 3, b)
self.assertRaises(DimensionalityError, op.floordiv, a, 3)
self.assertRaises(DimensionalityError, op.ifloordiv, a, b)
self.assertRaises(DimensionalityError, op.ifloordiv, 3, b)
self.assertRaises(DimensionalityError, op.ifloordiv, a, 3)
func(op.floordiv, unit * 10.0, "4.2*meter/meter", 2, unit)
func(op.floordiv, "10*meter", "4.2*inch", 93, unit)
func(op.floordiv, unit * 10.0, 3, 3 * unit, unit)
func(op.ifloordiv, unit * 10, 3, 3 * unit, unit)

def _test_quantity_mod(self, unit, func):
a = self.Q_("10*meter")
b = self.Q_("3*second")
self.assertRaises(DimensionalityError, op.mod, a, b)
self.assertRaises(DimensionalityError, op.mod, 3, b)
self.assertRaises(DimensionalityError, op.mod, a, 3)
self.assertRaises(DimensionalityError, op.imod, a, b)
self.assertRaises(DimensionalityError, op.imod, 3, b)
self.assertRaises(DimensionalityError, op.imod, a, 3)
func(op.mod, unit * 10.0, "4.2*meter/meter", 1.6, unit)
func(op.mod, unit * 10.0, 3, 1 * unit, unit)
func(op.imod, unit * 10.0, 3, 1 * unit, unit)

def _test_quantity_ifloordiv(self, unit, func):
func(op.ifloordiv, 10.0, "4.2*meter/meter", 2, unit)
Expand Down Expand Up @@ -782,7 +782,7 @@ def _test_quantity_divmod(self):
b = self.Q_("3*second")
self.assertRaises(DimensionalityError, divmod, a, b)
self.assertRaises(DimensionalityError, divmod, 3, b)
self.assertRaises(DimensionalityError, divmod, a, 3)
self._test_quantity_divmod_one("10*meter", 3)

def _test_numeric(self, unit, ifunc):
self._test_quantity_add_sub(unit, self._test_not_inplace)
Expand Down