SmallInteger.st
changeset 21939 8c7600a5fc60
parent 21917 00931f45bfff
child 21949 ce997689ab94
equal deleted inserted replaced
21938:95ff2af6355f 21939:8c7600a5fc60
  4155     ^ self gcd:anInteger
  4155     ^ self gcd:anInteger
  4156 
  4156 
  4157     "Created: 1.3.1997 / 16:58:01 / cg"
  4157     "Created: 1.3.1997 / 16:58:01 / cg"
  4158 !
  4158 !
  4159 
  4159 
       
  4160 integerLog10
       
  4161     "return the truncation of log10 of the receiver.
       
  4162      The same as (self log:10) floor.
       
  4163      Stupid implementation, which is used to find out the number of digits needed
       
  4164      to print a number/and for conversion to a LargeInteger.
       
  4165      Implemented that way, to allow for tiny systems (PDAs) without a Float class
       
  4166      (i.e. without log)."
       
  4167 
       
  4168     self > 0 ifTrue:[
       
  4169         self < 10000 ifTrue:[
       
  4170             self < 10 ifTrue:[^ 0].
       
  4171             self < 100 ifTrue:[^ 1].
       
  4172             self < 1000 ifTrue:[^ 2].
       
  4173             ^ 3
       
  4174         ].
       
  4175         self < 100000000 ifTrue:[
       
  4176             self < 100000 ifTrue:[^ 4].
       
  4177             self < 1000000 ifTrue:[^ 5].
       
  4178             self < 10000000 ifTrue:[^ 6].
       
  4179             ^ 7
       
  4180         ].
       
  4181         self < 1000000000 ifTrue:[^ 8].
       
  4182         SmallInteger maxBytes == 4 ifTrue:[
       
  4183             "/ on a 32 bit machine, SmallInt cannot be larger
       
  4184             ^ 9
       
  4185         ].
       
  4186 
       
  4187         "/ 64 bit machine
       
  4188         self < 100000000000000 ifTrue:[
       
  4189             self < 10000000000 ifTrue:[^ 9].
       
  4190             self < 100000000000 ifTrue:[^ 10].
       
  4191             self < 1000000000000 ifTrue:[^ 11].
       
  4192             self < 10000000000000 ifTrue:[^ 12].
       
  4193             ^ 13
       
  4194         ].
       
  4195         self < 1000000000000000 ifTrue:[^ 14].
       
  4196         self < 10000000000000000 ifTrue:[^ 15].
       
  4197         self < 100000000000000000 ifTrue:[^ 16].
       
  4198         self < 1000000000000000000 ifTrue:[^ 17].
       
  4199         ^ 18.
       
  4200     ].
       
  4201 
       
  4202     ^ self class
       
  4203         raise:#domainErrorSignal
       
  4204         receiver:self
       
  4205         selector:#intlog10
       
  4206         arguments:#()
       
  4207         errorString:'logarithm of negative integer'
       
  4208 
       
  4209     "
       
  4210       99 integerLog10
       
  4211       100 integerLog10
       
  4212       101 integerLog10
       
  4213       (101 log:10) floor
       
  4214       120 integerLog10
       
  4215       -1 integerLog10
       
  4216     "
       
  4217 
       
  4218     "Created: / 02-07-2017 / 01:19:09 / cg"
       
  4219 !
       
  4220 
  4160 intlog10
  4221 intlog10
  4161     "return the truncation of log10 of the receiver.
  4222     "return the truncation of log10 of the receiver.
  4162      The same as (self log:10) floor.
  4223      The same as (self log:10) floor.
  4163      Stupid implementation, which is used to find out the number of digits needed
  4224      Stupid implementation, which is used to find out the number of digits needed
  4164      to print a number/and for conversion to a LargeInteger.
  4225      to print a number/and for conversion to a LargeInteger.
  4165      Implemented that way, to allow for tiny systems (PDAs) without a Float class
  4226      Implemented that way, to allow for tiny systems (PDAs) without a Float class
  4166      (i.e. without log)."
  4227      (i.e. without log)."
  4167 
  4228 
  4168     self > 0 ifTrue:[
  4229     ^ self integerLog10
  4169 	self < 10000 ifTrue:[
       
  4170 	    self < 10 ifTrue:[^ 0].
       
  4171 	    self < 100 ifTrue:[^ 1].
       
  4172 	    self < 1000 ifTrue:[^ 2].
       
  4173 	    ^ 3
       
  4174 	].
       
  4175 	self < 100000000 ifTrue:[
       
  4176 	    self < 100000 ifTrue:[^ 4].
       
  4177 	    self < 1000000 ifTrue:[^ 5].
       
  4178 	    self < 10000000 ifTrue:[^ 6].
       
  4179 	    ^ 7
       
  4180 	].
       
  4181 	self < 1000000000 ifTrue:[^ 8].
       
  4182 	SmallInteger maxBytes == 4 ifTrue:[
       
  4183 	    "/ on a 32 bit machine, SmallInt cannot be larger
       
  4184 	    ^ 9
       
  4185 	].
       
  4186 
       
  4187 	"/ 64 bit machine
       
  4188 	self < 100000000000000 ifTrue:[
       
  4189 	    self < 10000000000 ifTrue:[^ 9].
       
  4190 	    self < 100000000000 ifTrue:[^ 10].
       
  4191 	    self < 1000000000000 ifTrue:[^ 11].
       
  4192 	    self < 10000000000000 ifTrue:[^ 12].
       
  4193 	    ^ 13
       
  4194 	].
       
  4195 	self < 1000000000000000 ifTrue:[^ 14].
       
  4196 	self < 10000000000000000 ifTrue:[^ 15].
       
  4197 	self < 100000000000000000 ifTrue:[^ 16].
       
  4198 	self < 1000000000000000000 ifTrue:[^ 17].
       
  4199 	^ 18.
       
  4200     ].
       
  4201 
       
  4202     ^ self class
       
  4203 	raise:#domainErrorSignal
       
  4204 	receiver:self
       
  4205 	selector:#intlog10
       
  4206 	arguments:#()
       
  4207 	errorString:'logarithm of negative integer'
       
  4208 
  4230 
  4209     "
  4231     "
  4210       99 intlog10
  4232       99 intlog10
  4211       100 intlog10
  4233       100 intlog10
  4212       101 intlog10
  4234       101 intlog10
  4213       (101 log:10) floor
  4235       (101 log:10) floor
  4214       120 intlog10
  4236       120 intlog10
  4215       -1 intlog10
  4237       -1 intlog10
  4216     "
  4238     "
       
  4239 
       
  4240     "Modified: / 02-07-2017 / 01:19:05 / cg"
  4217 ! !
  4241 ! !
  4218 
  4242 
  4219 !SmallInteger methodsFor:'printing & storing'!
  4243 !SmallInteger methodsFor:'printing & storing'!
  4220 
  4244 
  4221 asBCD
  4245 asBCD