Integer.st
changeset 3188 301eaf46b9e4
parent 3162 c7c8af356ff2
child 3298 bf837fee7a15
--- a/Integer.st	Mon Jan 19 16:33:41 1998 +0100
+++ b/Integer.st	Tue Jan 20 19:02:02 1998 +0100
@@ -351,13 +351,6 @@
     "Modified: 5.11.1996 / 14:06:26 / cg"
 !
 
-bitClear:anInteger
-    "return the bitwise-and of the receiver and the complement of the argument, anInteger,
-     returning the receiver with bits of the argument cleared."
-
-    ^self bitAnd:(anInteger bitInvert)
-!
-
 bitAt:index
     "return the value of the index's bit (index starts at 1).
      Notice: the result of bitShift: on negative receivers is not 
@@ -395,6 +388,13 @@
     "
 !
 
+bitClear:anInteger
+    "return the bitwise-and of the receiver and the complement of the argument, anInteger,
+     returning the receiver with bits of the argument cleared."
+
+    ^self bitAnd:(anInteger bitInvert)
+!
+
 bitOr:anInteger
     "return the bitwise-or of the receiver and the argument, anInteger.
      This is a general and slow implementation, walking over the bytes of
@@ -985,28 +985,6 @@
     "
 !
 
-recursiveFib
-    "compute the fibionacci number for the receiver.
-     WARNING:
-         Dont use this method if you need fibionacci numbers -
-         this method is for benchmarking purposes only.
-         (use #fib instead and dont ever try 60 recursiveFib ...)"
-
-    (self > 1) ifTrue:[
-	^ (self - 1) recursiveFib + (self - 2) recursiveFib
-    ].
-    ^ 1
-
-    "
-     30 fib
-     30 recursiveFib
-     Transcript showCR:(Time millisecondsToRun:[30 recursiveFib])
-     Transcript showCR:(Time millisecondsToRun:[30 fib])
-    "
-
-    "Modified: 18.7.1996 / 12:47:19 / cg"
-!
-
 gcd:anInteger 
     "return the greatest common divisor of the receiver and anInteger.
      Euclids & Knuths algorithm."
@@ -1043,6 +1021,28 @@
      65 lcm:15
      3 lcm:15
     "
+!
+
+recursiveFib
+    "compute the fibionacci number for the receiver.
+     WARNING:
+         Dont use this method if you need fibionacci numbers -
+         this method is for benchmarking purposes only.
+         (use #fib instead and dont ever try 60 recursiveFib ...)"
+
+    (self > 1) ifTrue:[
+	^ (self - 1) recursiveFib + (self - 2) recursiveFib
+    ].
+    ^ 1
+
+    "
+     30 fib
+     30 recursiveFib
+     Transcript showCR:(Time millisecondsToRun:[30 recursiveFib])
+     Transcript showCR:(Time millisecondsToRun:[30 fib])
+    "
+
+    "Modified: 18.7.1996 / 12:47:19 / cg"
 ! !
 
 !Integer methodsFor:'printing & storing'!
@@ -1094,48 +1094,28 @@
 printOn:aStream
     "append a printed description of the receiver to aStream"
 
-    aStream nextPutAll:(self printStringRadix:10)
+    self printOn:aStream base:10
+
+    "Modified: / 20.1.1998 / 14:10:45 / stefan"
 !
 
 printOn:aStream base:base
-    "append a printed description of the receiver to aStream. 
-     The receiver is printed in radix base (instead of the default, 10)."
-
-    aStream nextPutAll:(self printStringRadix:base)
-
-    "Created: 20.5.1996 / 11:53:38 / cg"
-!
-
-printOn:aStream radix:base
-    "append a printed description of the receiver to aStream.
-     The receiver is printed in radix base (instead of the default, 10).
-     This method is obsoleted by #printOn:base:, which is ST-80 compatible."
-
-    aStream nextPutAll:(self printStringRadix:base)
-
-    "Modified: 20.5.1996 / 11:54:10 / cg"
-!
-
-printString
-    "return a string representation of the receiver"
-
-    ^ self printStringRadix:10
-!
-
-printStringRadix:aRadix
     "return a string representation of the receiver in the specified
      radix (without the initial XXr)"
 
     |num s divMod mod r r2 r4 nD numN|
 
-    (aRadix between:2 and:36) ifFalse:[
-	self error:'invalid radix'.
-	^ self printStringRadix:10
+    (base between:2 and:36) ifFalse:[
+        self error:'invalid base'.
+        ^ self printOn:aStream base:10
     ].
 
-    (self = 0) ifTrue:[^ '0'].
+    (self = 0) ifTrue:[aStream nextPut:$0. ^ self].
     (self < 0) ifTrue:[
-	^ '-' , (self negated printStringRadix:aRadix)
+        aStream nextPut:$- . 
+        num := self negated.
+    ] ifFalse:[
+        num := self.
     ].
 
     "
@@ -1143,16 +1123,13 @@
      (it used to trigger stack-overflow exceptions when printing
       3000 factorial ...)
     "
-"/    leftPart := self // aRadix.
+"/    leftPart := num // base.
 "/    (leftPart ~= 0) ifTrue:[
-"/        ^ (leftPart printStringRadix:aRadix) copyWith:(Character digitValue:(self \\ aRadix))
+"/        leftPart printOn:aStream base:base.
+"/        aStream nextPut:(Character digitValue:(num \\ base).
+"/        ^ self
 "/    ].
-"/    ^ (Character digitValue:self) asString
-
-    num := self.
-    num = 0 ifTrue:[^ '0'].
-
-    s := WriteStream on:''.
+"/    aStream nextPut:(Character digitValue:num).
 
     "/ instead of computing the quotient and remainder
     "/ against radix, do it in junks of 5 or 6 digits.
@@ -1160,42 +1137,81 @@
     "/ by that factor (turning them into smallInt divisions
     "/ within that junk) and speeds up the conversions noticably.
 
-    r2 := aRadix*aRadix.   "/ radix^2
+    r2 := base*base.   "/ radix^2
     r4 := r2*r2.             "/ radix^4
-    aRadix <= 10 ifTrue:[
-	r := r4*r2.        "/ radix^6
-	nD := 6.
+    base <= 10 ifTrue:[
+        r := r4*r2.        "/ radix^6
+        nD := 6.
     ] ifFalse:[
-	r := r4*aRadix.    "/ radix^5
-	nD := 5.
+        r := r4*base.    "/ radix^5
+        nD := 5.
     ].
 
+    "get a Stream with space for the digits we are going to print.
+     We need ((num log:base) ceiling) digits, which is equivalent
+     to ((num log:2)/(base log:2) ceiling)
+    "
+    s := WriteStream on:(String new:((num highBit // base highBit - 1) + 1)).
+
     [num >= r] whileTrue:[
-	"/
-	"/ chop off nD digits.
-	"/
-	divMod := num divMod:r.
-	num := divMod at:1.
-	numN := divMod at:2.
+        "/
+        "/ chop off nD digits.
+        "/
+        divMod := num divMod:r.
+        num := divMod at:1.
+        numN := divMod at:2.
 
-	"/ process them
-	nD timesRepeat:[
-	    divMod := numN divMod:aRadix.
-	    numN := divMod at:1.
-	    mod := divMod at:2.
-	    s nextPut: (Character digitValue:mod).
-	].
+        "/ process them
+        nD timesRepeat:[
+            divMod := numN divMod:base.
+            numN := divMod at:1.
+            mod := divMod at:2.
+            s nextPut:(Character digitValue:mod).
+        ].
     ].
 
     [num ~= 0] whileTrue:[
-	divMod := num divMod:aRadix.
-	num := divMod at:1.
-	mod := divMod at:2.
-	s nextPut: (Character digitValue:mod).
+        divMod := num divMod:base.
+        num := divMod at:1.
+        mod := divMod at:2.
+        s nextPut:(Character digitValue:mod).
     ].
-    ^ s contents reverse
+
+    aStream nextPutAll:(s contents reverse).
+
+    "
+        3000 factorial printOn:Transcript base:10
+        10 printOn:Transcript base:3
+        31 printOn:Transcript base:3
+        -20  printOn:Transcript base:16
+        -20  printOn:Transcript base:10
+    "
 
     "Modified: / 17.11.1997 / 16:27:45 / cg"
+    "Modified: / 20.1.1998 / 18:05:02 / stefan"
+!
+
+printOn:aStream radix:base
+    "append a printed description of the receiver to aStream.
+     The receiver is printed in radix base (instead of the default, 10).
+     This method is obsoleted by #printOn:base:, which is ST-80 compatible."
+
+    self printOn:aStream base:base
+
+    "Modified: / 20.5.1996 / 11:54:10 / cg"
+    "Modified: / 20.1.1998 / 14:10:45 / stefan"
+!
+
+printStringRadix:base
+
+    |s|
+
+    s := WriteStream on:(String new:20).
+    self printOn:s base:base.
+    ^ s contents
+
+    "Created: / 19.1.1998 / 17:20:58 / stefan"
+    "Modified: / 20.1.1998 / 14:10:54 / stefan"
 !
 
 printStringRadix:aRadix size:sz fill:fillCharacter
@@ -1219,17 +1235,26 @@
     "
 !
 
-radixPrintStringRadix:aRadix
+radixPrintStringRadix:base
     "return a string representation of the receiver in the specified
-     radix; prepend XXr to the string"
+     base; prepend XXr to the string"
+
+    |s|
 
-    ^ (aRadix printString) , 'r', (self printStringRadix:aRadix)
+    s := WriteStream on:(String new:20).
+    base printOn:s.
+    s nextPut:$r.
+    self printOn:s base:base.
+    ^ s contents
 
     "
      31 radixPrintStringRadix:2
      31 radixPrintStringRadix:3
      31 radixPrintStringRadix:36
     "
+
+    "Created: / 19.1.1998 / 17:38:00 / stefan"
+    "Modified: / 20.1.1998 / 14:11:03 / stefan"
 ! !
 
 !Integer methodsFor:'queries'!
@@ -1460,5 +1485,5 @@
 !Integer class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.76 1998-01-12 13:19:43 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.77 1998-01-20 18:02:02 stefan Exp $'
 ! !