about summary refs log tree commit diff stats
path: root/miasm2/expression/modint.py
diff options
context:
space:
mode:
authorserpilliere <serpilliere@users.noreply.github.com>2017-01-14 12:27:22 +0100
committerGitHub <noreply@github.com>2017-01-14 12:27:22 +0100
commit35941c723827ab1d0e8b1fe86894cc188a869dcd (patch)
treeea8293629a6d05b13a4f429019afd5da2a616234 /miasm2/expression/modint.py
parent97d25c6e584e48b4ff85e529da290db34847868e (diff)
parent50f6ca146c0b810982ad5b42eea84ee0b6d26f8a (diff)
downloadmiasm-35941c723827ab1d0e8b1fe86894cc188a869dcd.tar.gz
miasm-35941c723827ab1d0e8b1fe86894cc188a869dcd.zip
Merge pull request #477 from commial/fix/division
Fix/division
Diffstat (limited to 'miasm2/expression/modint.py')
-rw-r--r--miasm2/expression/modint.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/miasm2/expression/modint.py b/miasm2/expression/modint.py
index 90dabfac..b6a0e4ee 100644
--- a/miasm2/expression/modint.py
+++ b/miasm2/expression/modint.py
@@ -41,11 +41,15 @@ class moduint(object):
             return self.__class__(self.arg & y)
 
     def __div__(self, y):
+        # Python: 8 / -7 == -2 (C-like: -1)
+        # int(float) trick cannot be used, due to information loss
+        den = int(y)
+        num = int(self)
+        result_sign = 1 if (den * num) >= 0 else -1
+        cls = self.__class__
         if isinstance(y, moduint):
             cls = self.maxcast(y)
-            return cls(int(float(self.arg) / y.arg))
-        else:
-            return self.__class__(int(float(self.arg) / y))
+        return ((abs(num) / abs(den)) * result_sign)
 
     def __int__(self):
         return int(self.arg)
@@ -64,11 +68,11 @@ class moduint(object):
             return self.__class__(self.arg << y)
 
     def __mod__(self, y):
+        # See __div__ for implementation choice
+        cls = self.__class__
         if isinstance(y, moduint):
             cls = self.maxcast(y)
-            return cls(self.arg - (y.arg * int(float(self.arg)/y.arg)))
-        else:
-            return self.__class__(self.arg - (y * int(float(self.arg)/y)))
+        return cls(self.arg - (y * (self / y)))
 
     def __mul__(self, y):
         if isinstance(y, moduint):