#OTHER
authorStefan Vogel <sv@exept.de>
Wed, 27 Jan 2016 23:21:53 +0100
changeset 19125 21ab11424b93
parent 19124 e77443fb171d
child 19126 98adb7dc1950
#OTHER Integer::ModuloNumber >> #modulusOf: take care for negative numbers
Integer.st
--- a/Integer.st	Wed Jan 27 22:25:24 2016 +0100
+++ b/Integer.st	Wed Jan 27 23:21:53 2016 +0100
@@ -5145,21 +5145,30 @@
 
 !Integer::ModuloNumber methodsFor:'arithmetic'!
 
-modulusOf:aNumber
+modulusOf:dividend
     "compute the aNumber modulo myself.
      The shortcut works only, if aNumber is < modulo * modulo
      (When doing arithmethic modulo something).
      Otherwise do it the long way"
 
-    |e t cnt|
+    |e t cnt abs isNegative|
+
+    isNegative := dividend negative.
+    isNegative ifTrue:[
+        abs := dividend negated.
+    ] ifFalse:[
+        abs := dividend.
+    ].
+
+"/    self assert:aNumber < (modulus * modulus)
 
     "throw off low nbits(modulus)"
 
-    e := aNumber bitShift:shift.
+    e := abs bitShift:shift.
     e := e * reciprocal.
     e := e bitShift:shift.
     e := e * modulus.
-    e := aNumber - e.
+    e := abs - e.
 
     "this subtract is done max 2 times"
     cnt := 2.
@@ -5171,6 +5180,9 @@
         ].
         cnt := cnt - 1.
     ].
+    isNegative ifTrue:[
+        ^ modulus - e.
+    ].
     ^ e.
 
     "