Skip to content

Commit

Permalink
Add floor division support
Browse files Browse the repository at this point in the history
  • Loading branch information
arthursoprana committed Oct 7, 2019
1 parent bd775d1 commit 9494b47
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/barril/units/_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,18 @@ def __getitem__(self, index):
def __iter__(self):
return iter(self.values)

def _calculate_floor_division(self, other, div_or_rdiv_function):
from numpy import floor

result = div_or_rdiv_function(other)
result._value = floor(result._value)
return result

def __truediv__(self, other):
return self._DoOperation(self, other, "Divide")

__floordiv__ = __truediv__
def __floordiv__(self, other):
return self._calculate_floor_division(other, self.__truediv__)

def __mul__(self, other):
return self._DoOperation(self, other, "Multiply")
Expand All @@ -261,7 +269,8 @@ def __sub__(self, other):
def __rtruediv__(self, other):
return self._DoOperation(other, self, "Divide")

__rfloordiv__ = __rtruediv__
def __rfloordiv__(self, other):
return self._calculate_floor_division(other, self.__rtruediv__)

def __rdiv__(self, other):
return self._DoOperation(other, self, "Divide")
Expand Down
13 changes: 11 additions & 2 deletions src/barril/units/_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,19 @@ def __lt__(self, other):
v2 = other.GetValue(self.unit)
return v1 < v2

def _calculate_floor_division(self, other, div_or_rdiv_function):
from math import floor

result = div_or_rdiv_function(other)
result._value = floor(result._value)
return result

# right ----------------------------------------------------------------------------------------
def __rtruediv__(self, other):
return self._DoOperation(other, self, "Divide", lambda a, b: a / b)

__rfloordiv__ = __rtruediv__
def __rfloordiv__(self, other):
return self._calculate_floor_division(other, self.__rtruediv__)

def __rmul__(self, other):
return self._DoOperation(other, self, "Multiply", lambda a, b: a * b)
Expand All @@ -284,7 +292,8 @@ def __radd__(self, other):
def __truediv__(self, other):
return self._DoOperation(self, other, "Divide", lambda a, b: a / b)

__floordiv__ = __truediv__
def __floordiv__(self, other):
return self._calculate_floor_division(other, self.__truediv__)

def __mul__(self, other):
return self._DoOperation(self, other, "Multiply", lambda a, b: a * b)
Expand Down
13 changes: 11 additions & 2 deletions src/barril/units/_tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,21 @@ def testDivision(unit_database_len_time):
assert calculated1 == s1 / s2


def testFloorDivision():
a = Array([3.5, 4.2], "m")
b = Array([100.0, 100.0], "cm")
assert approx((a // b).GetValues("")) == [3.0, 4.0]
assert approx((350 // b).GetValues("1/cm")) == [3.0, 3.0]
assert approx((a // 1.0).GetValues("m")) == [3.0, 4.0]


def testNumberOverArray():
a = Array([2.0, 2.0], "m")
b = Array([3.0, 3.0], "m")
c = 1.0 / a
assert c.GetValues("1/m") == [0.5, 0.5]
assert (3.0 / a).GetValues("1/m") == [1.5, 1.5]

assert approx(c.GetValues("1/m")) == [0.5, 0.5]
assert approx((3.0 / a).GetValues("1/m")) == [1.5, 1.5]
assert b / a == b * 1 / a == b * (1 / a)


Expand Down
8 changes: 8 additions & 0 deletions src/barril/units/_tests/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ def testDivision(unit_database_len_time):
assert calculated1 == s1 / s2


def testFloorDivision():
a = Scalar(3.5, "m")
b = Scalar(100.0, "cm")
assert (a // b).GetValueAndUnit() == (3.0, "")
assert (350 // b).GetValueAndUnit() == (3.0, "1/cm")
assert (a // 1.0).GetValueAndUnit() == (3.0, "m")


def testNumberOverScalar():
a = Scalar(2.0, "m")
b = Scalar(3.0, "m")
Expand Down

0 comments on commit 9494b47

Please sign in to comment.