Integer.st
branchjv
changeset 20143 c64b16f62536
parent 20134 cab81d17f2d9
parent 20140 bba1c52b0978
child 20342 219a5a47e8b1
equal deleted inserted replaced
20136:8e3509beb85d 20143:c64b16f62536
  4124     "print the receiver as a hex number on the standard output stream"
  4124     "print the receiver as a hex number on the standard output stream"
  4125 
  4125 
  4126      (self printStringRadix:16) print
  4126      (self printStringRadix:16) print
  4127 !
  4127 !
  4128 
  4128 
  4129 printOn:aStream base:b showRadix:showRadix
  4129 printOn:aStream base:base showRadix:showRadix
  4130     "append a string representation of the receiver in the specified numberBase to aStream
  4130     "append a string representation of the receiver in the specified numberBase to aStream
  4131      (if showRadix is true, with initial XXr)
  4131      (if showRadix is true, with initial XXr)
  4132      The radix argument should be between 2 and 36."
  4132      The base argument should be between 2 and 36.
  4133 
  4133      If it is negative, digits > 9 are printed as lowecase a-z."
  4134     |base num s divMod mod r r2 r4 nD numN|
  4134 
  4135 
  4135     |absBase num s divMod mod r r2 r4 nD numN string|
  4136     base := b.
  4136 
  4137     (base isInteger and:[ base between:2 and:36 ]) ifFalse:[
  4137     (base isInteger and:[absBase := base abs. absBase between:2 and:36]) ifFalse:[
  4138         ConversionError raiseRequestWith:self errorString:' - invalid base: ', base printString.
  4138         ConversionError raiseRequestWith:self errorString:' - invalid base: ', absBase printString.
  4139         base := 10.
  4139         absBase := 10.
  4140     ].
  4140     ].
  4141 
  4141 
  4142     showRadix ifTrue:[
  4142     showRadix ifTrue:[
  4143         base printOn:aStream.
  4143         absBase printOn:aStream.
  4144         aStream nextPut:$r.
  4144         aStream nextPut:$r.
  4145     ].
  4145     ].
  4146 
  4146 
  4147     (self = 0) ifTrue:[aStream nextPut:$0. ^ self].
  4147     (self = 0) ifTrue:[aStream nextPut:$0. ^ self].
  4148     (self negative) ifTrue:[
  4148     self negative ifTrue:[
  4149         aStream nextPut:$- .
  4149         aStream nextPut:$- .
  4150         num := self negated.
  4150         num := self negated.
  4151     ] ifFalse:[
  4151     ] ifFalse:[
  4152         num := self.
  4152         num := self.
  4153     ].
  4153     ].
  4168     "/ against radix, do it in junks of 5 or 6 digits.
  4168     "/ against radix, do it in junks of 5 or 6 digits.
  4169     "/ This reduces the number of LargeInt-divisions
  4169     "/ This reduces the number of LargeInt-divisions
  4170     "/ by that factor (turning them into smallInt divisions
  4170     "/ by that factor (turning them into smallInt divisions
  4171     "/ within that junk) and speeds up the conversions noticably.
  4171     "/ within that junk) and speeds up the conversions noticably.
  4172 
  4172 
  4173     r2 := base*base.   "/ radix^2
  4173     r2 := absBase*absBase.   "/ radix^2
  4174     r4 := r2*r2.        "/ radix^4
  4174     r4 := r2*r2.        "/ radix^4
  4175     base <= 10 ifTrue:[
  4175     absBase <= 10 ifTrue:[
  4176         r := r4*r2.     "/ radix^6 (chunks of 6 digits)
  4176         r := r4*r2.     "/ radix^6 (chunks of 6 digits)
  4177         nD := 6.
  4177         nD := 6.
  4178     ] ifFalse:[
  4178     ] ifFalse:[
  4179         r := r4*base.    "/ radix^5 (chunks of 5 digits)
  4179         r := r4*absBase.    "/ radix^5 (chunks of 5 digits)
  4180         nD := 5.
  4180         nD := 5.
  4181     ].
  4181     ].
  4182     SmallInteger maxBits >= 63 ifTrue:[
  4182     SmallInteger maxBits >= 63 ifTrue:[
  4183         r := r*r2.    "/ radix^7 (chunks of 6 digits)
  4183         r := r*r2.    "/ radix^7 (chunks of 6 digits)
  4184         nD := nD + 2.
  4184         nD := nD + 2.
  4198         num := divMod at:1.
  4198         num := divMod at:1.
  4199         numN := divMod at:2.
  4199         numN := divMod at:2.
  4200 
  4200 
  4201         "/ process them
  4201         "/ process them
  4202         nD timesRepeat:[
  4202         nD timesRepeat:[
  4203             divMod := numN divMod:base.
  4203             divMod := numN divMod:absBase.
  4204             numN := divMod at:1.
  4204             numN := divMod at:1.
  4205             mod := divMod at:2.
  4205             mod := divMod at:2.
  4206             s nextPut:(Character digitValue:mod).
  4206             s nextPut:(Character digitValue:mod).
  4207         ].
  4207         ].
  4208     ].
  4208     ].
  4209 
  4209 
  4210     [num ~~ 0] whileTrue:[
  4210     [num ~~ 0] whileTrue:[
  4211         divMod := num divMod:base.
  4211         divMod := num divMod:absBase.
  4212         num := divMod at:1.
  4212         num := divMod at:1.
  4213         mod := divMod at:2.
  4213         mod := divMod at:2.
  4214         s nextPut:(Character digitValue:mod).
  4214         s nextPut:(Character digitValue:mod).
  4215     ].
  4215     ].
  4216 
  4216     string := s contents reverse.
  4217     aStream nextPutAll:(s contents reverse).
  4217     base < 0 ifTrue:[
       
  4218         string := string asLowercase.
       
  4219     ].
       
  4220     aStream nextPutAll:string.
  4218 
  4221 
  4219     "
  4222     "
  4220         3000 factorial printOn:Transcript base:10
  4223         3000 factorial printOn:Transcript base:10
  4221         10 printOn:Transcript base:3
  4224         10 printOn:Transcript base:3
  4222         31 printOn:Transcript base:3
  4225         31 printOn:Transcript base:3
  4223         10 printOn:Transcript base:2
  4226         10 printOn:Transcript base:2
  4224         31 printOn:Transcript base:2
  4227         31 printOn:Transcript base:2
  4225         -20  printOn:Transcript base:16
  4228         -28  printOn:Transcript base:16
       
  4229         -28  printOn:Transcript base:-16
  4226         -20  printOn:Transcript base:10
  4230         -20  printOn:Transcript base:10
  4227         Time millisecondsToRun:[10000 factorial printString]
  4231         Time millisecondsToRun:[10000 factorial printString]
  4228         '%012d' printf:{  (2 raisedTo:20) }
  4232         '%012d' printf:{  (2 raisedTo:20) }
  4229     "
  4233     "
  4230 
  4234 
  4251 printOn:aStream base:baseInteger size:sz fill:fillCharacter
  4255 printOn:aStream base:baseInteger size:sz fill:fillCharacter
  4252     "print a string representation of the receiver in the specified
  4256     "print a string representation of the receiver in the specified
  4253      base. The string is padded on the left with fillCharacter to make
  4257      base. The string is padded on the left with fillCharacter to make
  4254      its size as specified in sz."
  4258      its size as specified in sz."
  4255 
  4259 
  4256     |stream string actualSize|
  4260     |string actualSize|
  4257 
  4261 
  4258     stream := WriteStream on:(String new:sz).
  4262     string := self printStringRadix:baseInteger.
  4259     self printOn:stream base:baseInteger showRadix:false.
       
  4260     string := stream contents.
       
  4261     actualSize := string size.
  4263     actualSize := string size.
  4262     actualSize < sz ifTrue:[
  4264     actualSize < sz ifTrue:[
  4263         aStream next:sz-actualSize put:fillCharacter.
  4265         aStream next:sz-actualSize put:fillCharacter.
  4264     ].
  4266     ].
  4265     aStream nextPutAll:string.
  4267     aStream nextPutAll:string.
  4385         (Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt].
  4387         (Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt].
  4386      ]
  4388      ]
  4387     "
  4389     "
  4388 !
  4390 !
  4389 
  4391 
  4390 printStringRadix:base
       
  4391     "return a string representation of the receiver in the specified
       
  4392      base; does NOT prepend XXr to the string.
       
  4393      See also: radixPrintStringRadix:
       
  4394                printOn:base:showRadix:"
       
  4395 
       
  4396     ^ self printStringRadix:base showRadix:false
       
  4397 
       
  4398     "
       
  4399      10 printStringRadix:16
       
  4400     "
       
  4401 
       
  4402     "Created: / 19-01-1998 / 17:20:58 / stefan"
       
  4403     "Modified: / 20-01-1998 / 14:10:54 / stefan"
       
  4404     "Modified: / 23-09-2011 / 13:59:36 / cg"
       
  4405     "Modified (comment): / 26-07-2013 / 12:55:18 / cg"
       
  4406 !
       
  4407 
       
  4408 printStringRadix:aRadix size:sz fill:fillCharacter
  4392 printStringRadix:aRadix size:sz fill:fillCharacter
  4409     "return a string representation of the receiver in the specified
  4393     "return a string representation of the receiver in the specified
  4410      radix. The string is padded on the left with fillCharacter to make
  4394      radix. The string is padded on the left with fillCharacter to make
  4411      its size as specified in sz."
  4395      its size as specified in sz."
  4412 
  4396