author | Claus Gittinger <cg@exept.de> |
Sat, 09 Dec 1995 22:34:58 +0100 | |
changeset 726 | 997e30ef8423 |
parent 701 | a309e3ef7faf |
child 927 | 8d8edf9df0ae |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Number subclass:#Integer |
|
701 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'DefaultDisplayRadix' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
88 | 20 |
!Integer class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
abstract superclass for all integer numbers. |
|
39 |
See details in concrete subclasses LargeInteger and SmallInteger. |
|
212 | 40 |
|
41 |
Class variables: |
|
42 |
||
43 |
DefaultDisplayRadix the radix in which integers present their |
|
44 |
displayString (which is used in inspectors) |
|
45 |
If you are to look at many hex numbers, bitmasks |
|
46 |
etc. you may set this to 2 or 16. |
|
47 |
(avoids typing printStringRadix:.. all the time |
|
48 |
- I know - I am lazy ;-). Default is 10. |
|
49 |
||
88 | 50 |
" |
51 |
! ! |
|
1 | 52 |
|
92 | 53 |
!Integer class methodsFor:'instance creation'! |
54 |
||
55 |
new:numberOfBytes neg:negative |
|
56 |
"for ST-80 compatibility: |
|
57 |
Return an empty Integer (uninitialized value) with space for |
|
58 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 59 |
negative specifies if the result should be a negative number. |
60 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 61 |
|
132 | 62 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
159 | 63 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
701 | 64 |
! |
65 |
||
66 |
readFrom:aStringOrStream |
|
67 |
"return the next unsigned Integer from the (character-)stream aStream |
|
68 |
as decimal number. |
|
69 |
||
70 |
NOTICE: |
|
71 |
This behaves different from the default readFrom:, in returning |
|
72 |
0 (instead of raising an error) in case no number can be read. |
|
73 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
74 |
- depending on the upcoming ANSI standard, this may change." |
|
75 |
||
76 |
^ self readFrom:aStringOrStream onError:0 |
|
77 |
||
78 |
" |
|
79 |
Integer readFrom:(ReadStream on:'foobar') |
|
80 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
|
81 |
" |
|
82 |
! |
|
83 |
||
84 |
readFrom:aStringOrStream onError:exceptionBlock |
|
85 |
"return the next Integer from the (character-)stream aStream, |
|
86 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
87 |
Also, all initial whitespace is skipped. |
|
88 |
If the string does not represent a valid integer number, |
|
89 |
return the value of exceptionBlock." |
|
90 |
||
91 |
|str nextChar value negative| |
|
92 |
||
93 |
str := aStringOrStream readStream. |
|
94 |
||
95 |
nextChar := str skipSeparators. |
|
96 |
(nextChar == $-) ifTrue:[ |
|
97 |
negative := true. |
|
98 |
nextChar := str nextPeek |
|
99 |
] ifFalse:[ |
|
100 |
negative := false |
|
101 |
]. |
|
102 |
(nextChar isNil or:[nextChar isDigit not]) ifTrue:[ |
|
103 |
" |
|
104 |
the string does not represent an integer |
|
105 |
" |
|
106 |
^ exceptionBlock value |
|
107 |
]. |
|
108 |
value := self readFrom:str radix:10 onError:[^ exceptionBlock value]. |
|
109 |
nextChar := str peek. |
|
110 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
|
111 |
"-xxr<number> is invalid; should be xxr-<val>" |
|
112 |
||
113 |
negative ifTrue:[ |
|
114 |
'invalid (negative) radix ignored' errorPrintNL. |
|
115 |
negative := false |
|
116 |
]. |
|
117 |
str next. |
|
118 |
value := self readFrom:str radix:value onError:[^ exceptionBlock value] |
|
119 |
]. |
|
120 |
negative ifTrue:[ |
|
121 |
^ value negated |
|
122 |
]. |
|
123 |
^ value |
|
124 |
||
125 |
" |
|
126 |
Integer readFrom:'12345' |
|
127 |
Integer readFrom:'-12345' |
|
128 |
Integer readFrom:'+12345' |
|
129 |
Integer readFrom:'16rFFFF' |
|
130 |
Integer readFrom:'12345.1234' |
|
131 |
Integer readFrom:'foo' |
|
132 |
Integer readFrom:'foo' onError:[0] |
|
133 |
" |
|
134 |
||
135 |
"Created: 16.11.1995 / 22:48:59 / cg" |
|
136 |
! |
|
137 |
||
138 |
readFrom:aStringOrStream radix:radix |
|
139 |
"return the next unsigned Integer from the (character-)stream aStream |
|
140 |
in radix; (assumes that the initial XXr has already been read). |
|
141 |
No whitespace-skipping is done. |
|
142 |
Returns 0 if no number available. |
|
143 |
||
144 |
NOTICE: |
|
145 |
This behaves different from the default readFrom:, in returning |
|
146 |
0 (instead of raising an error) in case no number can be read. |
|
147 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
148 |
- depending on the upcoming ANSI standard, this may change." |
|
149 |
||
150 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
151 |
! |
|
152 |
||
153 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
154 |
"return the next unsigned Integer from the (character-)stream aStream |
|
155 |
in radix; (assumes that the initial XXr has already been read). |
|
156 |
No whitespace-skipping is done. |
|
157 |
Returns the value of exceptionBlock, if no number is available." |
|
158 |
||
159 |
|str nextChar value| |
|
160 |
||
161 |
str := aStringOrStream readStream. |
|
162 |
||
163 |
nextChar := str peek. |
|
164 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifFalse:[ |
|
165 |
^ exceptionBlock value |
|
166 |
]. |
|
167 |
||
168 |
value := nextChar digitValue. |
|
169 |
nextChar := str nextPeek. |
|
170 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
|
171 |
value := value * radix + nextChar digitValue. |
|
172 |
nextChar := str nextPeek |
|
173 |
]. |
|
174 |
^ value |
|
175 |
||
176 |
" |
|
177 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
|
178 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
179 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
180 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
181 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
182 |
" |
|
183 |
! ! |
|
184 |
||
185 |
!Integer class methodsFor:'constants'! |
|
186 |
||
187 |
unity |
|
188 |
"return the neutral element for multiplication" |
|
189 |
||
190 |
^ 1 |
|
191 |
! |
|
192 |
||
193 |
zero |
|
194 |
"return the neutral element for addition" |
|
195 |
||
196 |
^ 0 |
|
92 | 197 |
! ! |
198 |
||
49 | 199 |
!Integer class methodsFor:'misc'! |
200 |
||
201 |
displayRadix:aNumber |
|
54 | 202 |
"being tired of always sending #printStringRadix: in the inspectors, |
49 | 203 |
this allows you to change the default print radix for the displayString |
204 |
method." |
|
205 |
||
206 |
DefaultDisplayRadix := aNumber |
|
132 | 207 |
|
208 |
" |
|
209 |
Integer displayRadix:16. 123456 inspect |
|
210 |
Integer displayRadix:10. 123456 inspect |
|
211 |
" |
|
49 | 212 |
! ! |
213 |
||
1 | 214 |
!Integer methodsFor:'arithmetic'! |
215 |
||
216 |
quo:aNumber |
|
217 |
"Return the integer quotient of dividing the receiver by aNumber with |
|
54 | 218 |
truncation towards zero. For Integers this is same as //" |
1 | 219 |
|
220 |
^ self // aNumber |
|
221 |
||
222 |
! ! |
|
223 |
||
69 | 224 |
!Integer methodsFor:'bit operators'! |
225 |
||
226 |
bitAnd:anInteger |
|
357 | 227 |
"return the bitwise-and of the receiver and the argument, anInteger. |
228 |
This is a general and slow implementation, walking over the bytes of |
|
229 |
the receiver and the argument." |
|
69 | 230 |
|
92 | 231 |
|result n byte| |
69 | 232 |
|
92 | 233 |
n := (anInteger digitLength) min:(self digitLength). |
234 |
result := self class basicNew numberOfDigits:n. |
|
69 | 235 |
|
236 |
1 to:n do:[:index | |
|
159 | 237 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
238 |
result digitAt:index put:byte. |
|
69 | 239 |
]. |
357 | 240 |
(byte == 0 or:[n < 5]) ifTrue:[ |
159 | 241 |
^ result normalize |
92 | 242 |
]. |
243 |
^ result |
|
357 | 244 |
|
245 |
" |
|
246 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
|
247 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
248 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
249 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
250 |
" |
|
69 | 251 |
! |
252 |
||
85 | 253 |
bitAt:index |
92 | 254 |
"return the value of the index's bit (index starts at 1). |
255 |
Notice: the result of bitShift: on negative receivers is not |
|
159 | 256 |
defined in the language standard (since the implementation |
257 |
is free to choose any internal representation for integers)" |
|
85 | 258 |
|
259 |
|i "{Class: SmallInteger}"| |
|
260 |
||
261 |
i := index - 1. |
|
262 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
|
92 | 263 |
|
264 |
" |
|
265 |
1 bitAt:1 |
|
266 |
(1 bitShift:30) bitAt:30 |
|
267 |
(1 bitShift:30) bitAt:31 |
|
268 |
(1 bitShift:30) bitAt:32 |
|
269 |
(1 bitShift:31) bitAt:31 |
|
270 |
(1 bitShift:31) bitAt:32 |
|
271 |
(1 bitShift:31) bitAt:33 |
|
272 |
(1 bitShift:32) bitAt:32 |
|
273 |
(1 bitShift:32) bitAt:33 |
|
274 |
(1 bitShift:32) bitAt:34 |
|
275 |
(1 bitShift:64) bitAt:64 |
|
276 |
(1 bitShift:64) bitAt:65 |
|
277 |
(1 bitShift:64) bitAt:66 |
|
278 |
(1 bitShift:1000) bitAt:1000 |
|
279 |
(1 bitShift:1000) bitAt:1001 |
|
280 |
(1 bitShift:1000) bitAt:1002 |
|
281 |
(1 bitShift:1000) bitAt:2000 |
|
282 |
" |
|
85 | 283 |
! |
284 |
||
701 | 285 |
bitOr:anInteger |
286 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
287 |
This is a general and slow implementation, walking over the bytes of |
|
288 |
the receiver and the argument." |
|
289 |
||
290 |
|result n byte| |
|
291 |
||
292 |
n := (anInteger digitLength) max:(self digitLength). |
|
293 |
result := self class basicNew numberOfDigits:n. |
|
294 |
||
295 |
1 to:n do:[:index | |
|
296 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
|
297 |
result digitAt:index put:byte. |
|
298 |
]. |
|
299 |
"/ no need to normalize - if the operands were correct |
|
300 |
"/ byte == 0 ifTrue:[ |
|
301 |
"/ ^ result normalize |
|
302 |
"/ ]. |
|
303 |
^ result |
|
304 |
! |
|
305 |
||
85 | 306 |
bitShift:shiftCount |
307 |
"return the value of the receiver shifted by shiftCount bits; |
|
308 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 309 |
|
310 |
Notice: the result of bitShift: on negative receivers is not |
|
159 | 311 |
defined in the language standard (since the implementation |
312 |
is free to choose any internal representation for integers)" |
|
85 | 313 |
|
92 | 314 |
|result |
315 |
prev "{ Class: SmallInteger }" |
|
316 |
next "{ Class: SmallInteger }" |
|
317 |
byte "{ Class: SmallInteger }" |
|
359 | 318 |
byte2 "{ Class: SmallInteger }" |
92 | 319 |
bitShift "{ Class: SmallInteger }" |
320 |
revShift "{ Class: SmallInteger }" |
|
321 |
digitShift "{ Class: SmallInteger }" |
|
322 |
n "{ Class: SmallInteger }" |
|
323 |
nn "{ Class: SmallInteger }"| |
|
85 | 324 |
|
325 |
shiftCount > 0 ifTrue:[ |
|
159 | 326 |
"left shift" |
85 | 327 |
|
159 | 328 |
digitShift := shiftCount // 8. |
329 |
bitShift := shiftCount \\ 8. |
|
330 |
n := self digitLength. |
|
92 | 331 |
|
159 | 332 |
" |
333 |
modulu 8 shifts can be done faster ... |
|
334 |
" |
|
335 |
bitShift == 0 ifTrue:[ |
|
336 |
result := self class basicNew numberOfDigits:n + digitShift. |
|
337 |
result sign:self sign. |
|
338 |
result digits replaceFrom:(digitShift + 1) with:self digits. |
|
339 |
" |
|
340 |
no normalize needed, since receiver was already normalized |
|
341 |
" |
|
342 |
^ result |
|
343 |
]. |
|
92 | 344 |
|
159 | 345 |
" |
346 |
less-than-8 shifts can be done faster ... |
|
347 |
" |
|
348 |
digitShift == 0 ifTrue:[ |
|
349 |
n := n + 1. |
|
350 |
result := self class basicNew numberOfDigits:n. |
|
351 |
result sign:self sign. |
|
352 |
prev := 0. |
|
353 |
1 to:n-1 do:[:index | |
|
354 |
byte := self digitAt:index. |
|
355 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
356 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
357 |
prev := byte bitShift:-8. |
|
358 |
]. |
|
359 |
result digitAt:n put:prev. |
|
360 |
" |
|
361 |
might have stored a 0-byte ... |
|
362 |
" |
|
363 |
prev == 0 ifTrue:[ |
|
364 |
^ result normalize |
|
365 |
]. |
|
366 |
^ result. |
|
367 |
]. |
|
92 | 368 |
|
159 | 369 |
" |
370 |
slow case ... |
|
371 |
" |
|
372 |
n := n + digitShift + 1. |
|
373 |
result := self class basicNew numberOfDigits:n. |
|
374 |
result sign:self sign. |
|
359 | 375 |
byte := self digitAt:1. |
376 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
|
159 | 377 |
result digitAt:(digitShift + 1) put:byte. |
378 |
revShift := -8 + bitShift. |
|
379 |
2 to:(self digitLength) do:[:index | |
|
359 | 380 |
byte := self digitAt:index. |
381 |
byte2 := self digitAt:index-1. |
|
382 |
byte := byte bitShift:bitShift. |
|
383 |
byte2 := byte2 bitShift:revShift. |
|
384 |
byte := byte bitOr:byte2. |
|
159 | 385 |
byte := byte bitAnd:16rFF. |
359 | 386 |
result digitAt:(index + digitShift) put:byte. |
159 | 387 |
]. |
359 | 388 |
byte2 := self digitAt:self digitLength. |
389 |
byte2 := byte2 bitShift:revShift. |
|
390 |
byte2 := byte2 bitAnd:16rFF. |
|
391 |
result digitAt:(self digitLength + digitShift + 1) put:byte2. |
|
159 | 392 |
" |
393 |
might have stored a 0-byte ... |
|
394 |
" |
|
359 | 395 |
byte2 == 0 ifTrue:[ |
159 | 396 |
^ result normalize |
397 |
]. |
|
398 |
^ result |
|
92 | 399 |
]. |
400 |
||
401 |
shiftCount < 0 ifTrue:[ |
|
159 | 402 |
"right shift" |
85 | 403 |
|
159 | 404 |
digitShift := shiftCount negated // 8. |
405 |
bitShift := shiftCount negated \\ 8. |
|
406 |
n := self digitLength. |
|
92 | 407 |
|
159 | 408 |
digitShift >= n ifTrue:[ |
409 |
^ 0 |
|
410 |
]. |
|
92 | 411 |
|
159 | 412 |
" |
413 |
modulu 8 shifts can be done faster ... |
|
414 |
" |
|
415 |
bitShift == 0 ifTrue:[ |
|
416 |
n := n-digitShift. |
|
417 |
result := self class basicNew numberOfDigits:n. |
|
418 |
result sign:self sign. |
|
419 |
result digits replaceFrom:1 with:self digits startingAt:(digitShift + 1) . |
|
420 |
n <= 4 ifTrue:[ |
|
421 |
^ result normalize |
|
422 |
]. |
|
423 |
^ result |
|
424 |
]. |
|
85 | 425 |
|
159 | 426 |
" |
427 |
less-than-8 shifts can be done faster ... |
|
428 |
" |
|
429 |
digitShift == 0 ifTrue:[ |
|
430 |
result := self class basicNew numberOfDigits:n. |
|
431 |
result sign:self sign. |
|
432 |
prev := 0. |
|
433 |
bitShift := bitShift negated. |
|
434 |
revShift := 8 + bitShift. |
|
435 |
n to:1 by:-1 do:[:index | |
|
436 |
byte := self digitAt:index. |
|
437 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
438 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
439 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
440 |
prev := next. |
|
441 |
]. |
|
442 |
(n <= 5) ifTrue:[ |
|
443 |
^ result normalize |
|
444 |
]. |
|
445 |
^ result |
|
446 |
]. |
|
92 | 447 |
|
159 | 448 |
" |
449 |
slow case ... |
|
450 |
" |
|
451 |
nn := n-digitShift. |
|
452 |
result := self class basicNew numberOfDigits:nn. |
|
453 |
result sign:self sign. |
|
85 | 454 |
|
159 | 455 |
prev := 0. |
456 |
bitShift := bitShift negated. |
|
457 |
revShift := 8 + bitShift. |
|
458 |
n to:(1 + digitShift) by:-1 do:[:index | |
|
459 |
byte := self digitAt:index. |
|
460 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
461 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
462 |
result digitAt:(index - digitShift) put:byte. |
|
463 |
prev := next. |
|
464 |
]. |
|
465 |
"the last stored byte ..." |
|
466 |
^ result normalize |
|
85 | 467 |
]. |
92 | 468 |
|
469 |
^ self "no shift" |
|
701 | 470 |
! |
1 | 471 |
|
701 | 472 |
bitXor:anInteger |
473 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
474 |
This is a general and slow implementation, walking over the bytes of |
|
475 |
the receiver and the argument." |
|
476 |
||
477 |
|result n byte| |
|
478 |
||
479 |
n := (anInteger digitLength) max:(self digitLength). |
|
480 |
result := self class basicNew numberOfDigits:n. |
|
1 | 481 |
|
701 | 482 |
1 to:n do:[:index | |
483 |
byte := (anInteger digitAt:index) bitXor:(self digitAt:index). |
|
484 |
result digitAt:index put:byte. |
|
485 |
]. |
|
486 |
byte == 0 ifTrue:[ |
|
487 |
^ result normalize |
|
488 |
]. |
|
489 |
^ result |
|
1 | 490 |
|
701 | 491 |
" |
492 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
|
493 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
|
494 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
495 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
|
496 |
" |
|
1 | 497 |
! |
498 |
||
701 | 499 |
highBit |
500 |
"return the bitIndex of the highest bit set. The returned bitIndex |
|
501 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
502 |
||
503 |
|byteNr highByte| |
|
504 |
||
505 |
byteNr := self digitLength. |
|
506 |
highByte := self digitAt:byteNr. |
|
507 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 508 |
|
701 | 509 |
" |
510 |
1 highBit |
|
511 |
(1 bitShift:1) highBit |
|
512 |
(1 bitShift:30) highBit |
|
513 |
(1 bitShift:31) highBit |
|
514 |
(1 bitShift:32) highBit |
|
515 |
(1 bitShift:33) highBit |
|
516 |
(1 bitShift:64) highBit |
|
517 |
(1 bitShift:1000) highBit |
|
518 |
((1 bitShift:64)-1) highBit |
|
519 |
" |
|
520 |
! ! |
|
521 |
||
522 |
!Integer methodsFor:'coercing and converting'! |
|
523 |
||
524 |
asFraction |
|
525 |
"return a Fraction with same value as receiver" |
|
526 |
||
527 |
^ Fraction numerator:self denominator:1 |
|
1 | 528 |
! |
529 |
||
701 | 530 |
asInteger |
1 | 531 |
"return the receiver truncated towards zero - |
532 |
for integers this is self" |
|
533 |
||
534 |
^ self |
|
535 |
! ! |
|
536 |
||
701 | 537 |
!Integer methodsFor:'double dispatching'! |
538 |
||
539 |
differenceFromFraction:aFraction |
|
540 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 541 |
|
701 | 542 |
|d| |
1 | 543 |
|
701 | 544 |
d := aFraction denominator. |
545 |
^ (Fraction numerator:(aFraction numerator - (self * d)) |
|
546 |
denominator:d) reduced |
|
1 | 547 |
! |
548 |
||
701 | 549 |
productFromFraction:aFraction |
550 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 551 |
|
701 | 552 |
^ (Fraction numerator:(self * aFraction numerator) |
553 |
denominator:aFraction denominator) reduced |
|
1 | 554 |
! |
555 |
||
701 | 556 |
quotientFromFraction:aFraction |
557 |
"sent when a fraction does not know how to divide the receiver, an integer" |
|
558 |
||
559 |
^ (Fraction numerator:aFraction numerator |
|
560 |
denominator:(self * aFraction denominator)) reduced |
|
561 |
! |
|
1 | 562 |
|
701 | 563 |
sumFromFraction:aFraction |
564 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
565 |
||
566 |
|d| |
|
567 |
||
568 |
d := aFraction denominator. |
|
569 |
^ (Fraction numerator:(aFraction numerator + (self * d)) |
|
570 |
denominator:d) reduced |
|
1 | 571 |
! ! |
572 |
||
573 |
!Integer methodsFor:'misc math'! |
|
574 |
||
701 | 575 |
acker:n |
576 |
"return the value of acker(self, n)" |
|
577 |
||
578 |
(self == 0) ifTrue:[^ n + 1]. |
|
579 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
580 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
581 |
||
582 |
" |
|
583 |
3 acker:2 |
|
584 |
" |
|
585 |
! |
|
586 |
||
1 | 587 |
factorial |
588 |
"return 1*2*3...*self" |
|
589 |
||
356 | 590 |
(self >= 2) ifTrue:[ |
159 | 591 |
^ self * (self - 1) factorial |
1 | 592 |
]. |
356 | 593 |
^ 1 |
212 | 594 |
|
595 |
" |
|
596 |
10 factorial |
|
597 |
1000 factorial |
|
598 |
" |
|
1 | 599 |
! |
600 |
||
701 | 601 |
fastFib |
602 |
"this method just to show how a changed algorithm can |
|
603 |
change things much more drastic than tuning ... |
|
604 |
(compare 30 fib with 30 fastFib / dont even try 60 fib)" |
|
605 |
||
606 |
^ self fibUsingCache:(OrderedCollection new) |
|
607 |
||
608 |
" |
|
609 |
Transcript showCr:(Time millisecondsToRun:[30 fib]) |
|
610 |
Transcript showCr:(Time millisecondsToRun:[30 fastFib]) |
|
611 |
Transcript showCr:(Time millisecondsToRun:[60 fastFib]) |
|
612 |
Transcript showCr:(Time millisecondsToRun:[200 fastFib]) |
|
613 |
" |
|
614 |
! |
|
615 |
||
616 |
fib |
|
617 |
"dont use this method if you need fibionacci numbers - |
|
618 |
this method is for benchmarking purposes only. |
|
619 |
(use fastFib instead and dont ever try 60 fib ...)" |
|
620 |
||
621 |
(self > 1) ifTrue:[ |
|
622 |
^ (self - 1) fib + (self - 2) fib |
|
623 |
]. |
|
624 |
^ 1 |
|
625 |
||
626 |
" |
|
627 |
10 fib |
|
628 |
20 fib |
|
629 |
Transcript showCr:(Time millisecondsToRun:[30 fib]) |
|
630 |
" |
|
631 |
||
632 |
" |
|
633 |
ds3100 486/50 Indy(no cache) |
|
634 |
20804 4800 2145 |
|
635 |
" |
|
636 |
! |
|
637 |
||
638 |
fibUsingCache:fibCache |
|
639 |
"actual fastfib worker; keep track of already computed fib |
|
640 |
numbers in fibCache, which is passed around. Take value from |
|
641 |
the cache if already present. Doing so, the double recursive |
|
642 |
fib actually becomes a single recursive algorithm." |
|
643 |
||
644 |
|fib| |
|
645 |
||
646 |
self <= 1 ifTrue:[^ 1]. |
|
647 |
||
648 |
fibCache size >= self ifTrue:[ |
|
649 |
^ fibCache at:self |
|
650 |
]. |
|
651 |
fib := ((self - 2) fibUsingCache:fibCache) + ((self - 1) fibUsingCache:fibCache). |
|
652 |
||
653 |
fibCache grow:self. |
|
654 |
fibCache at:self put:fib. |
|
655 |
^ fib |
|
656 |
! |
|
657 |
||
1 | 658 |
gcd:anInteger |
659 |
"return the greatest common divisor (Euclid's algorithm)" |
|
660 |
||
661 |
|ttt selfInteger temp| |
|
662 |
||
663 |
ttt := anInteger. |
|
664 |
selfInteger := self. |
|
665 |
[ttt ~~ 0] whileTrue:[ |
|
159 | 666 |
temp := selfInteger \\ ttt. |
667 |
selfInteger := ttt. |
|
668 |
ttt := temp |
|
1 | 669 |
]. |
670 |
^ selfInteger |
|
52 | 671 |
|
92 | 672 |
" |
673 |
65 gcd:15 |
|
674 |
3 gcd:15 |
|
675 |
132 gcd:55 |
|
676 |
" |
|
1 | 677 |
! |
678 |
||
679 |
lcm:anInteger |
|
52 | 680 |
"return the least common multiple (using gcd:)" |
681 |
||
682 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
683 |
||
92 | 684 |
" |
685 |
65 lcm:15 |
|
686 |
3 lcm:15 |
|
687 |
" |
|
1 | 688 |
! ! |
689 |
||
690 |
!Integer methodsFor:'printing & storing'! |
|
691 |
||
49 | 692 |
displayString |
693 |
"return a string for displaying in a view (as in inspector). |
|
694 |
The output radix is usually 10, but can be changed by setting |
|
695 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
696 |
||
54 | 697 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
159 | 698 |
^ self printString |
54 | 699 |
]. |
49 | 700 |
^ self radixPrintStringRadix:DefaultDisplayRadix |
701 |
||
92 | 702 |
" |
703 |
Integer displayRadix:16. 12345 |
|
704 |
Integer displayRadix:2. 12345 |
|
705 |
Integer displayRadix:10. 12345 |
|
706 |
" |
|
49 | 707 |
! |
708 |
||
701 | 709 |
errorPrintHex |
710 |
"print the receiver as a hex number on the standard error stream" |
|
711 |
||
712 |
(self printStringRadix:16) errorPrint |
|
713 |
! |
|
1 | 714 |
|
701 | 715 |
hexPrintString |
716 |
"return a hex string representation of the receiver" |
|
717 |
||
718 |
^ self printStringRadix:16 |
|
719 |
! |
|
720 |
||
721 |
printHex |
|
722 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 723 |
|
701 | 724 |
(self printStringRadix:16) print |
725 |
! |
|
726 |
||
727 |
printOn:aStream |
|
728 |
"append a printed description of the receiver to aStream" |
|
729 |
||
730 |
aStream nextPutAll:(self printStringRadix:10) |
|
731 |
! |
|
732 |
||
733 |
printOn:aStream radix:base |
|
734 |
"append a printed description of the receiver to aStream" |
|
735 |
||
736 |
aStream nextPutAll:(self printStringRadix:base) |
|
737 |
! |
|
738 |
||
739 |
printString |
|
740 |
"return a string representation of the receiver" |
|
741 |
||
742 |
^ self printStringRadix:10 |
|
1 | 743 |
! |
744 |
||
745 |
printStringRadix:aRadix |
|
746 |
"return a string representation of the receiver in the specified |
|
747 |
radix (without the initial XXr)" |
|
748 |
||
92 | 749 |
|num s "rx leftPart"| |
1 | 750 |
|
54 | 751 |
(aRadix between:2 and:36) ifFalse:[ |
159 | 752 |
self error:'invalid radix'. |
753 |
^ self printStringRadix:10 |
|
54 | 754 |
]. |
755 |
||
1 | 756 |
(self = 0) ifTrue:[^ '0']. |
757 |
(self < 0) ifTrue:[ |
|
159 | 758 |
^ '-' , (self negated printStringRadix:aRadix) |
1 | 759 |
]. |
92 | 760 |
|
761 |
" |
|
762 |
claus: changed it from a recursive algorithm; |
|
763 |
(it used to trigger stack-overflow exceptions when printing |
|
764 |
3000 factorial ...) |
|
765 |
" |
|
766 |
"/ leftPart := self // aRadix. |
|
767 |
"/ (leftPart ~= 0) ifTrue:[ |
|
768 |
"/ ^ (leftPart printStringRadix:aRadix) copyWith:(Character digitValue:(self \\ aRadix)) |
|
769 |
"/ ]. |
|
770 |
"/ ^ (Character digitValue:self) asString |
|
771 |
||
772 |
num := self. |
|
773 |
s := (Character digitValue:(num \\ aRadix)) asString. |
|
774 |
num := num // aRadix. |
|
775 |
[num ~= 0] whileTrue:[ |
|
159 | 776 |
s := (Character digitValue:(num \\ aRadix)) asString , s. |
777 |
num := num // aRadix. |
|
1 | 778 |
]. |
92 | 779 |
^ s |
1 | 780 |
! |
781 |
||
782 |
printStringRadix:aRadix size:sz fill:fillCharacter |
|
783 |
"return a string representation of the receiver in the specified |
|
784 |
radix. The string is padded on the left with fillCharacter to make |
|
785 |
its size as specified in sz." |
|
786 |
||
92 | 787 |
|s| |
1 | 788 |
|
789 |
s := self printStringRadix:aRadix. |
|
790 |
s size < sz ifTrue:[ |
|
159 | 791 |
s := ((String new:(sz - s size)) atAllPut:fillCharacter) , s |
1 | 792 |
]. |
793 |
^ s |
|
794 |
||
92 | 795 |
" |
796 |
1024 printStringRadix:16 size:4 fill:$0 |
|
797 |
1024 printStringRadix:2 size:16 fill:$. |
|
798 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
799 |
" |
|
216 | 800 |
! |
801 |
||
701 | 802 |
radixPrintStringRadix:aRadix |
803 |
"return a string representation of the receiver in the specified |
|
804 |
radix; prepend XXr to the string" |
|
805 |
||
806 |
^ (aRadix printString) , 'r', (self printStringRadix:aRadix) |
|
216 | 807 |
|
701 | 808 |
" |
809 |
31 radixPrintStringRadix:2 |
|
810 |
31 radixPrintStringRadix:3 |
|
811 |
31 radixPrintStringRadix:36 |
|
812 |
" |
|
1 | 813 |
! ! |
814 |
||
701 | 815 |
!Integer methodsFor:'queries'! |
202 | 816 |
|
701 | 817 |
digitAt:n |
818 |
"return the n-th byte of the binary representation. |
|
819 |
This is a very stupid implementation, and should be redefined in |
|
820 |
concrete subclasses." |
|
202 | 821 |
|
701 | 822 |
|num count| |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
823 |
|
701 | 824 |
num := self. |
825 |
count := n. |
|
826 |
[count > 1] whileTrue:[ |
|
827 |
num := num // 256. |
|
828 |
count := count - 1 |
|
202 | 829 |
]. |
701 | 830 |
^ num \\ 256 |
159 | 831 |
|
832 |
" |
|
701 | 833 |
16r44332211 digitAt:1 |
834 |
16r44332211 digitAt:2 |
|
835 |
16r44332211 digitAt:3 |
|
836 |
16r44332211 digitAt:4 |
|
837 |
16r44332211 digitAt:5 |
|
838 |
16r00332211 digitAt:4 |
|
839 |
16r00332211 digitAt:5 |
|
159 | 840 |
" |
1 | 841 |
! |
842 |
||
701 | 843 |
digitLength |
844 |
"return the number of bytes needed for the binary representation |
|
845 |
of the receiver" |
|
846 |
||
847 |
^ (self log:256) ceiling asInteger |
|
848 |
! |
|
849 |
||
850 |
isInteger |
|
851 |
"return true, if the receiver is some kind of integer number" |
|
852 |
||
853 |
^ true |
|
854 |
! ! |
|
202 | 855 |
|
701 | 856 |
!Integer methodsFor:'truncation & rounding'! |
857 |
||
858 |
ceiling |
|
859 |
"I am my ceiling" |
|
345 | 860 |
|
701 | 861 |
^ self |
862 |
! |
|
345 | 863 |
|
701 | 864 |
compressed |
865 |
"this is ST-80's name for ST/X's normalize. |
|
866 |
ST/X may be changed for full compatibility." |
|
867 |
||
868 |
^ self normalize |
|
202 | 869 |
! |
870 |
||
701 | 871 |
floor |
872 |
"I am my floor" |
|
873 |
||
874 |
^ self |
|
875 |
! |
|
202 | 876 |
|
701 | 877 |
normalize |
878 |
"this is ST/X's name for ST-80's compressed. |
|
879 |
ST/X may be changed for full compatibility." |
|
1 | 880 |
|
701 | 881 |
^ self |
882 |
! |
|
1 | 883 |
|
701 | 884 |
rounded |
885 |
"return the receiver rounded toward the next Integer - |
|
886 |
for integers this is self" |
|
887 |
||
888 |
^ self |
|
889 |
! |
|
69 | 890 |
|
701 | 891 |
truncated |
892 |
"return the receiver truncated towards zero - |
|
893 |
for integers this is self" |
|
159 | 894 |
|
701 | 895 |
^ self |
896 |
! ! |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
897 |
|
701 | 898 |
!Integer class methodsFor:'documentation'! |
899 |
||
900 |
version |
|
901 |
^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.35 1995-12-07 21:35:05 cg Exp $' |
|
1 | 902 |
! ! |