author | Claus Gittinger <cg@exept.de> |
Mon, 12 Jan 1998 14:23:21 +0100 | |
changeset 3165 | b6bde90005a8 |
parent 3162 | c7c8af356ff2 |
child 3188 | 301eaf46b9e4 |
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 |
|
1210 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'DefaultDisplayRadix' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
20 |
!Integer class methodsFor:'documentation'! |
88 | 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 |
|
1892 | 41 |
Mixed mode arithmetic: |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
42 |
int op int -> int |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
43 |
int op fix -> fix; scale is fix's scale |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
44 |
int op fraction -> fraction |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
45 |
int op float -> float |
1892 | 46 |
|
1295 | 47 |
[Class variables:] |
212 | 48 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
49 |
DefaultDisplayRadix the radix in which integers present their |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
50 |
displayString (which is used in inspectors) |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
51 |
If you are to look at many hex numbers, bitmasks |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
52 |
etc. you may set this to 2 or 16. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
53 |
(avoids typing printStringRadix:.. all the time |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
54 |
- I know - I am lazy ;-). Default is 10. |
212 | 55 |
|
1295 | 56 |
|
57 |
[author:] |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
58 |
Claus Gittinger |
1556 | 59 |
|
60 |
[see also:] |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
61 |
Number |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
62 |
LargeInteger SmallInteger |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
63 |
Float ShortFloat Fraction FixedPoint |
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
67 |
!Integer class methodsFor:'instance creation'! |
92 | 68 |
|
3044 | 69 |
byte1:b1 byte2:b2 byte3:b3 byte4:b4 |
70 |
"Squeak compatibility: |
|
71 |
Return an Integer given four value bytes. |
|
72 |
The returned integer is either a Small- or a LargeInteger" |
|
73 |
||
74 |
|t| |
|
75 |
||
76 |
t := b4. |
|
77 |
t := (t bitShift:8) + b3. |
|
78 |
t := (t bitShift:8) + b2. |
|
79 |
^ (t bitShift:8) + b1. |
|
80 |
||
81 |
" |
|
82 |
(Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString |
|
83 |
(Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString |
|
84 |
" |
|
85 |
||
86 |
"Created: 19.10.1997 / 18:08:52 / cg" |
|
87 |
"Modified: 19.10.1997 / 18:09:04 / cg" |
|
88 |
! |
|
89 |
||
92 | 90 |
new:numberOfBytes neg:negative |
91 |
"for ST-80 compatibility: |
|
92 |
Return an empty Integer (uninitialized value) with space for |
|
93 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 94 |
negative specifies if the result should be a negative number. |
95 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 96 |
|
132 | 97 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
159 | 98 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
701 | 99 |
! |
100 |
||
101 |
readFrom:aStringOrStream |
|
102 |
"return the next unsigned Integer from the (character-)stream aStream |
|
103 |
as decimal number. |
|
104 |
||
105 |
NOTICE: |
|
106 |
This behaves different from the default readFrom:, in returning |
|
107 |
0 (instead of raising an error) in case no number can be read. |
|
108 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
109 |
- depending on the upcoming ANSI standard, this may change." |
|
110 |
||
111 |
^ self readFrom:aStringOrStream onError:0 |
|
112 |
||
113 |
" |
|
114 |
Integer readFrom:(ReadStream on:'foobar') |
|
115 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
|
116 |
" |
|
117 |
! |
|
118 |
||
119 |
readFrom:aStringOrStream onError:exceptionBlock |
|
120 |
"return the next Integer from the (character-)stream aStream, |
|
121 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
122 |
Also, all initial whitespace is skipped. |
|
123 |
If the string does not represent a valid integer number, |
|
124 |
return the value of exceptionBlock." |
|
125 |
||
1700 | 126 |
ErrorSignal handle:[:ex | |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
127 |
^ exceptionBlock value |
1700 | 128 |
] do:[ |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
129 |
|str nextChar value negative| |
701 | 130 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
131 |
str := aStringOrStream readStream. |
701 | 132 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
133 |
nextChar := str skipSeparators. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
134 |
(nextChar == $-) ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
135 |
negative := true. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
136 |
nextChar := str nextPeek |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
137 |
] ifFalse:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
138 |
negative := false |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
139 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
140 |
(nextChar isNil or:[nextChar isDigit not]) ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
141 |
" |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
142 |
the string does not represent an integer |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
143 |
" |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
144 |
^ exceptionBlock value |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
145 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
146 |
value := self readFrom:str radix:10 onError:[^ exceptionBlock value]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
147 |
nextChar := str peek. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
148 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
149 |
"-xxr<number> is invalid; should be xxr-<val>" |
1700 | 150 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
151 |
negative ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
152 |
'Integer [warning]: invalid (negative) radix ignored' errorPrintCR. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
153 |
negative := false |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
154 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
155 |
str next. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
156 |
value := self readFrom:str radix:value onError:[^ exceptionBlock value] |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
157 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
158 |
negative ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
159 |
^ value negated |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
160 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
161 |
^ value |
701 | 162 |
]. |
163 |
||
164 |
" |
|
165 |
Integer readFrom:'12345' |
|
166 |
Integer readFrom:'-12345' |
|
167 |
Integer readFrom:'+12345' |
|
168 |
Integer readFrom:'16rFFFF' |
|
169 |
Integer readFrom:'12345.1234' |
|
170 |
Integer readFrom:'foo' |
|
171 |
Integer readFrom:'foo' onError:[0] |
|
172 |
" |
|
173 |
||
174 |
"Created: 16.11.1995 / 22:48:59 / cg" |
|
2133
f9b02b0172f6
newStyle info & error messages
Claus Gittinger <cg@exept.de>
parents:
1976
diff
changeset
|
175 |
"Modified: 10.1.1997 / 17:54:30 / cg" |
701 | 176 |
! |
177 |
||
178 |
readFrom:aStringOrStream radix:radix |
|
179 |
"return the next unsigned Integer from the (character-)stream aStream |
|
180 |
in radix; (assumes that the initial XXr has already been read). |
|
181 |
No whitespace-skipping is done. |
|
182 |
Returns 0 if no number available. |
|
183 |
||
184 |
NOTICE: |
|
185 |
This behaves different from the default readFrom:, in returning |
|
186 |
0 (instead of raising an error) in case no number can be read. |
|
187 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
188 |
- depending on the upcoming ANSI standard, this may change." |
|
189 |
||
190 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
191 |
! |
|
192 |
||
193 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
194 |
"return the next unsigned Integer from the (character-)stream aStream |
|
195 |
in radix; (assumes that the initial XXr has already been read). |
|
196 |
No whitespace-skipping is done. |
|
197 |
Returns the value of exceptionBlock, if no number is available." |
|
198 |
||
199 |
|str nextChar value| |
|
200 |
||
201 |
str := aStringOrStream readStream. |
|
202 |
||
203 |
nextChar := str peek. |
|
204 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifFalse:[ |
|
205 |
^ exceptionBlock value |
|
206 |
]. |
|
207 |
||
208 |
value := nextChar digitValue. |
|
209 |
nextChar := str nextPeek. |
|
210 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
|
211 |
value := value * radix + nextChar digitValue. |
|
212 |
nextChar := str nextPeek |
|
213 |
]. |
|
214 |
^ value |
|
215 |
||
216 |
" |
|
217 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
|
218 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
219 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
220 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
221 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
222 |
" |
|
223 |
! ! |
|
224 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
225 |
!Integer class methodsFor:'constants'! |
701 | 226 |
|
227 |
unity |
|
1555 | 228 |
"return the neutral element for multiplication (1)" |
701 | 229 |
|
230 |
^ 1 |
|
1555 | 231 |
|
232 |
"Modified: 18.7.1996 / 12:26:43 / cg" |
|
701 | 233 |
! |
234 |
||
235 |
zero |
|
1555 | 236 |
"return the neutral element for addition (0)" |
701 | 237 |
|
238 |
^ 0 |
|
1555 | 239 |
|
240 |
"Modified: 18.7.1996 / 12:26:38 / cg" |
|
92 | 241 |
! ! |
242 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
243 |
!Integer class methodsFor:'misc'! |
49 | 244 |
|
245 |
displayRadix:aNumber |
|
54 | 246 |
"being tired of always sending #printStringRadix: in the inspectors, |
49 | 247 |
this allows you to change the default print radix for the displayString |
248 |
method." |
|
249 |
||
250 |
DefaultDisplayRadix := aNumber |
|
132 | 251 |
|
252 |
" |
|
253 |
Integer displayRadix:16. 123456 inspect |
|
254 |
Integer displayRadix:10. 123456 inspect |
|
255 |
" |
|
49 | 256 |
! ! |
257 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
258 |
!Integer methodsFor:'VisualAge compatibility'! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
259 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
260 |
<< aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
261 |
"left shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
262 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
263 |
^ self bitShift:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
264 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
265 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
266 |
1 << 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
267 |
64 << -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
268 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
269 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
270 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
271 |
>> aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
272 |
"right shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
273 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
274 |
^ self bitShift:(aNumber negated) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
275 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
276 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
277 |
1 >> -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
278 |
64 >> 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
279 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
280 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
281 |
|
1210 | 282 |
clearBit:index |
283 |
"return a new number where the specified bit is off. |
|
284 |
Bits are counted from 1 starting with the least significant." |
|
285 |
||
286 |
^ self bitAnd:(1 bitShift:index-1) bitInvert |
|
287 |
||
288 |
" |
|
289 |
5 clearBit:3 => 1 |
|
290 |
16rFFFF clearBit:16 => 32767 (16r7FFF) |
|
291 |
16rFFFF clearBit:1 => 65534 (16rFFFE) |
|
292 |
" |
|
293 |
! |
|
294 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
295 |
isBitSet:index |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
296 |
"return true if the index' bit is set; false otherwise. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
297 |
Bits are counted from 1 starting with the least significant." |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
298 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
299 |
^ (self bitAt:index) ~~ 0 |
1 | 300 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
301 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
302 |
5 isBitSet:3 => true |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
303 |
2r0101 isBitSet:2 => false |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
304 |
2r0101 isBitSet:1 => true |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
305 |
2r0101 isBitSet:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
306 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
307 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
308 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
309 |
setBit:index |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
310 |
"return a new number where the specified bit is on. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
311 |
Bits are counted from 1 starting with the least significant." |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
312 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
313 |
^ self bitOr:(1 bitShift:index-1) |
1 | 314 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
315 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
316 |
0 setBit:3 => 4 (2r100) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
317 |
0 setBit:48 => 140737488355328 (2r1000.....000) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
318 |
((0 setBit:99) setBit:100) printStringRadix:2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
319 |
" |
1 | 320 |
! ! |
321 |
||
69 | 322 |
!Integer methodsFor:'bit operators'! |
323 |
||
324 |
bitAnd:anInteger |
|
357 | 325 |
"return the bitwise-and of the receiver and the argument, anInteger. |
326 |
This is a general and slow implementation, walking over the bytes of |
|
327 |
the receiver and the argument." |
|
69 | 328 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
329 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
330 |
result byte| |
69 | 331 |
|
92 | 332 |
n := (anInteger digitLength) min:(self digitLength). |
333 |
result := self class basicNew numberOfDigits:n. |
|
69 | 334 |
|
335 |
1 to:n do:[:index | |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
336 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
337 |
result digitAt:index put:byte. |
69 | 338 |
]. |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
339 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
340 |
^ result compressed |
92 | 341 |
]. |
342 |
^ result |
|
357 | 343 |
|
344 |
" |
|
345 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
|
346 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
347 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
348 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
349 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
350 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
351 |
"Modified: 5.11.1996 / 14:06:26 / cg" |
69 | 352 |
! |
353 |
||
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
354 |
bitClear:anInteger |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
355 |
"return the bitwise-and of the receiver and the complement of the argument, anInteger, |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
356 |
returning the receiver with bits of the argument cleared." |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
357 |
|
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
358 |
^self bitAnd:(anInteger bitInvert) |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
359 |
! |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
360 |
|
85 | 361 |
bitAt:index |
92 | 362 |
"return the value of the index's bit (index starts at 1). |
363 |
Notice: the result of bitShift: on negative receivers is not |
|
159 | 364 |
defined in the language standard (since the implementation |
365 |
is free to choose any internal representation for integers)" |
|
85 | 366 |
|
367 |
|i "{Class: SmallInteger}"| |
|
368 |
||
369 |
i := index - 1. |
|
370 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
|
92 | 371 |
|
372 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
373 |
1 bitAt:1 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
374 |
1 bitAt:2 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
375 |
1 bitAt:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
376 |
2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
377 |
2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
378 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
379 |
(1 bitShift:1000) bitAt:1000 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
380 |
(1 bitShift:1000) bitAt:1001 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
381 |
(1 bitShift:1000) bitAt:1002 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
382 |
|
92 | 383 |
(1 bitShift:30) bitAt:30 |
384 |
(1 bitShift:30) bitAt:31 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
385 |
(1 bitShift:30) bitAt:32 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
386 |
(1 bitShift:31) bitAt:31 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
387 |
(1 bitShift:31) bitAt:32 |
92 | 388 |
(1 bitShift:31) bitAt:33 |
389 |
(1 bitShift:32) bitAt:32 |
|
390 |
(1 bitShift:32) bitAt:33 |
|
391 |
(1 bitShift:32) bitAt:34 |
|
392 |
(1 bitShift:64) bitAt:64 |
|
393 |
(1 bitShift:64) bitAt:65 |
|
394 |
(1 bitShift:64) bitAt:66 |
|
395 |
" |
|
85 | 396 |
! |
397 |
||
701 | 398 |
bitOr:anInteger |
399 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
400 |
This is a general and slow implementation, walking over the bytes of |
|
401 |
the receiver and the argument." |
|
402 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
403 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
404 |
result byte| |
701 | 405 |
|
406 |
n := (anInteger digitLength) max:(self digitLength). |
|
407 |
result := self class basicNew numberOfDigits:n. |
|
408 |
||
409 |
1 to:n do:[:index | |
|
410 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
|
411 |
result digitAt:index put:byte. |
|
412 |
]. |
|
413 |
"/ no need to normalize - if the operands were correct |
|
414 |
"/ byte == 0 ifTrue:[ |
|
415 |
"/ ^ result normalize |
|
416 |
"/ ]. |
|
417 |
^ result |
|
418 |
! |
|
419 |
||
85 | 420 |
bitShift:shiftCount |
421 |
"return the value of the receiver shifted by shiftCount bits; |
|
422 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 423 |
|
424 |
Notice: the result of bitShift: on negative receivers is not |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
425 |
defined in the language standard (since the implementation |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
426 |
is free to choose any internal representation for integers)" |
85 | 427 |
|
92 | 428 |
|result |
429 |
prev "{ Class: SmallInteger }" |
|
430 |
next "{ Class: SmallInteger }" |
|
431 |
byte "{ Class: SmallInteger }" |
|
359 | 432 |
byte2 "{ Class: SmallInteger }" |
92 | 433 |
bitShift "{ Class: SmallInteger }" |
434 |
revShift "{ Class: SmallInteger }" |
|
435 |
digitShift "{ Class: SmallInteger }" |
|
436 |
n "{ Class: SmallInteger }" |
|
3044 | 437 |
nn "{ Class: SmallInteger }" |
438 |
nDigits "{ Class: SmallInteger }" | |
|
85 | 439 |
|
440 |
shiftCount > 0 ifTrue:[ |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
441 |
"left shift" |
85 | 442 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
443 |
digitShift := shiftCount // 8. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
444 |
bitShift := shiftCount \\ 8. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
445 |
n := self digitLength. |
92 | 446 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
447 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
448 |
modulo 8 shifts can be done faster ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
449 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
450 |
bitShift == 0 ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
451 |
result := self class basicNew numberOfDigits:n + digitShift. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
452 |
result sign:self sign. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
453 |
result digits replaceFrom:(digitShift + 1) with:self digits. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
454 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
455 |
no normalize needed, since receiver was already normalized |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
456 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
457 |
^ result |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
458 |
]. |
92 | 459 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
460 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
461 |
less-than-8 shifts can be done faster ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
462 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
463 |
digitShift == 0 ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
464 |
n := n + 1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
465 |
result := self class basicNew numberOfDigits:n. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
466 |
result sign:self sign. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
467 |
prev := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
468 |
1 to:n-1 do:[:index | |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
469 |
byte := self digitAt:index. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
470 |
byte := (byte bitShift:bitShift) bitOr:prev. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
471 |
result digitAt:index put:(byte bitAnd:16rFF). |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
472 |
prev := byte bitShift:-8. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
473 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
474 |
result digitAt:n put:prev. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
475 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
476 |
might have stored a 0-byte ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
477 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
478 |
prev == 0 ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
479 |
^ result compressed |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
480 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
481 |
^ result. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
482 |
]. |
92 | 483 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
484 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
485 |
slow case ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
486 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
487 |
n := n + digitShift + 1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
488 |
result := self class basicNew numberOfDigits:n. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
489 |
result sign:self sign. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
490 |
byte := self digitAt:1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
491 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
492 |
result digitAt:(digitShift + 1) put:byte. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
493 |
revShift := -8 + bitShift. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
494 |
nDigits := self digitLength. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
495 |
2 to:nDigits do:[:index | |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
496 |
byte := self digitAt:index. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
497 |
byte2 := self digitAt:index-1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
498 |
byte := byte bitShift:bitShift. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
499 |
byte2 := byte2 bitShift:revShift. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
500 |
byte := (byte bitOr:byte2) bitAnd:16rFF. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
501 |
result digitAt:(index + digitShift) put:byte. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
502 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
503 |
byte2 := self digitAt:nDigits. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
504 |
byte2 := (byte2 bitShift:revShift) bitAnd:16rFF. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
505 |
result digitAt:(nDigits + digitShift + 1) put:byte2. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
506 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
507 |
might have stored a 0-byte ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
508 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
509 |
byte2 == 0 ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
510 |
^ result compressed |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
511 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
512 |
^ result |
92 | 513 |
]. |
514 |
||
515 |
shiftCount < 0 ifTrue:[ |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
516 |
"right shift" |
85 | 517 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
518 |
digitShift := shiftCount negated // 8. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
519 |
bitShift := shiftCount negated \\ 8. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
520 |
n := self digitLength. |
92 | 521 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
522 |
digitShift >= n ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
523 |
^ 0 |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
524 |
]. |
92 | 525 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
526 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
527 |
modulo 8 shifts can be done faster ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
528 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
529 |
bitShift == 0 ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
530 |
n := n-digitShift. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
531 |
result := self class basicNew numberOfDigits:n. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
532 |
result sign:self sign. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
533 |
result digits replaceFrom:1 with:self digits startingAt:(digitShift + 1) . |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
534 |
n <= SmallInteger maxBytes ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
535 |
^ result compressed |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
536 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
537 |
^ result |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
538 |
]. |
85 | 539 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
540 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
541 |
less-than-8 shifts can be done faster ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
542 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
543 |
digitShift == 0 ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
544 |
result := self class basicNew numberOfDigits:n. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
545 |
result sign:self sign. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
546 |
prev := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
547 |
bitShift := bitShift negated. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
548 |
revShift := 8 + bitShift. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
549 |
n to:1 by:-1 do:[:index | |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
550 |
byte := self digitAt:index. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
551 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
552 |
byte := (byte bitShift:bitShift) bitOr:prev. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
553 |
result digitAt:index put:(byte bitAnd:16rFF). |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
554 |
prev := next. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
555 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
556 |
(n <= 5) ifTrue:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
557 |
^ result compressed |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
558 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
559 |
^ result |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
560 |
]. |
92 | 561 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
562 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
563 |
slow case ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
564 |
" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
565 |
nn := n-digitShift. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
566 |
result := self class basicNew numberOfDigits:nn. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
567 |
result sign:self sign. |
85 | 568 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
569 |
prev := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
570 |
bitShift := bitShift negated. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
571 |
revShift := 8 + bitShift. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
572 |
n to:(1 + digitShift) by:-1 do:[:index | |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
573 |
byte := self digitAt:index. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
574 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
575 |
byte := (byte bitShift:bitShift) bitOr:prev. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
576 |
result digitAt:(index - digitShift) put:byte. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
577 |
prev := next. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
578 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
579 |
"the last stored byte ..." |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
580 |
^ result compressed |
85 | 581 |
]. |
92 | 582 |
|
583 |
^ self "no shift" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
584 |
|
3135 | 585 |
"Modified: / 5.11.1996 / 14:06:37 / cg" |
586 |
"Modified: / 18.12.1997 / 17:17:29 / stefan" |
|
701 | 587 |
! |
1 | 588 |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
589 |
bitTest:anInteger |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
590 |
"return true, if the bitwise-and of the receiver and the argument, anInteger |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
591 |
is non-0, false otherwise. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
592 |
This is a general and slow implementation, walking over the bytes of |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
593 |
the receiver and the argument." |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
594 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
595 |
|n "{ Class: SmallInteger }" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
596 |
result byte| |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
597 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
598 |
n := (anInteger digitLength) min:(self digitLength). |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
599 |
result := self class basicNew numberOfDigits:n. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
600 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
601 |
1 to:n do:[:index | |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
602 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
603 |
byte ~~ 0 ifTrue:[^ true]. |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
604 |
]. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
605 |
^ false |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
606 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
607 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
608 |
16r112233445566778899 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
609 |
16r112233445566778800 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
610 |
16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
611 |
16r112233445566778899 bitTest:16rFF0000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
612 |
16r112233445566778899 bitTest:16r00000000000000FFFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
613 |
16r1234567800000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
614 |
16r8765432100000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
615 |
16r12345678 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
616 |
16r87654321 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
617 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
618 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
619 |
"Modified: 16.5.1996 / 16:29:50 / cg" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
620 |
! |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
621 |
|
701 | 622 |
bitXor:anInteger |
623 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
624 |
This is a general and slow implementation, walking over the bytes of |
|
625 |
the receiver and the argument." |
|
626 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
627 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
628 |
result byte| |
701 | 629 |
|
630 |
n := (anInteger digitLength) max:(self digitLength). |
|
631 |
result := self class basicNew numberOfDigits:n. |
|
1 | 632 |
|
701 | 633 |
1 to:n do:[:index | |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
634 |
byte := (anInteger digitAt:index) bitXor:(self digitAt:index). |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
635 |
result digitAt:index put:byte. |
701 | 636 |
]. |
637 |
byte == 0 ifTrue:[ |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
638 |
^ result compressed |
701 | 639 |
]. |
640 |
^ result |
|
1 | 641 |
|
701 | 642 |
" |
643 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
|
644 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
|
645 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
646 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
|
647 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
648 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
649 |
"Modified: 5.11.1996 / 14:06:40 / cg" |
1 | 650 |
! |
651 |
||
701 | 652 |
highBit |
653 |
"return the bitIndex of the highest bit set. The returned bitIndex |
|
654 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
655 |
||
656 |
|byteNr highByte| |
|
657 |
||
658 |
byteNr := self digitLength. |
|
659 |
highByte := self digitAt:byteNr. |
|
660 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 661 |
|
701 | 662 |
" |
663 |
1 highBit |
|
664 |
(1 bitShift:1) highBit |
|
665 |
(1 bitShift:30) highBit |
|
666 |
(1 bitShift:31) highBit |
|
667 |
(1 bitShift:32) highBit |
|
668 |
(1 bitShift:33) highBit |
|
669 |
(1 bitShift:64) highBit |
|
670 |
(1 bitShift:1000) highBit |
|
671 |
((1 bitShift:64)-1) highBit |
|
672 |
" |
|
2429 | 673 |
! |
674 |
||
675 |
lowBit |
|
676 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
677 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
678 |
||
679 |
|maxBytes "{ Class: SmallInteger }" |
|
680 |
bitIndex "{ Class: SmallInteger }" |
|
681 |
byte| |
|
682 |
||
683 |
maxBytes := self digitLength. |
|
684 |
bitIndex := 0. |
|
685 |
1 to:maxBytes do:[:byteIndex | |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
686 |
byte := self digitAt:byteIndex. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
687 |
byte ~~ 0 ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
688 |
^ bitIndex + byte lowBit |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
689 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
690 |
bitIndex := bitIndex + 8 |
2429 | 691 |
]. |
692 |
^ -1 |
|
693 |
||
694 |
" |
|
695 |
0 lowBit |
|
696 |
1 lowBit |
|
697 |
(1 bitShift:1) lowBit |
|
698 |
(1 bitShift:1) highBit |
|
699 |
(1 bitShift:30) lowBit |
|
700 |
(1 bitShift:30) highBit |
|
701 |
(1 bitShift:31) lowBit |
|
702 |
(1 bitShift:31) highBit |
|
703 |
(1 bitShift:32) lowBit |
|
704 |
(1 bitShift:32) highBit |
|
705 |
(1 bitShift:33) lowBit |
|
706 |
(1 bitShift:33) highBit |
|
707 |
(1 bitShift:64) lowBit |
|
708 |
(1 bitShift:64) highBit |
|
709 |
(1 bitShift:1000) lowBit |
|
710 |
(1 bitShift:1000) highBit |
|
711 |
((1 bitShift:64)-1) lowBit |
|
712 |
((1 bitShift:64)-1) highBit |
|
713 |
" |
|
714 |
||
715 |
"Modified: 1.3.1997 / 16:54:23 / cg" |
|
701 | 716 |
! ! |
717 |
||
718 |
!Integer methodsFor:'coercing and converting'! |
|
719 |
||
1888 | 720 |
asFixedPoint |
721 |
"return the receiver as a fixedPoint number" |
|
722 |
||
723 |
^ FixedPoint numerator:self denominator:1 scale:1 |
|
724 |
||
725 |
" |
|
726 |
100 asFixedPoint |
|
727 |
100 asFixedPoint + 0.1 asFixedPoint |
|
728 |
" |
|
729 |
||
730 |
"Modified: 5.11.1996 / 15:13:17 / cg" |
|
731 |
! |
|
732 |
||
2141 | 733 |
asFixedPoint:scale |
734 |
"return the receiver as fixedPoint number, with the given number |
|
735 |
of post-decimal-point digits." |
|
736 |
||
737 |
^ FixedPoint numerator:self denominator:1 scale:scale |
|
738 |
||
739 |
" |
|
740 |
100 asFixedPoint:2 |
|
741 |
100 asFixedPoint + (0.1 asFixedPoint:2) |
|
742 |
" |
|
743 |
||
744 |
"Modified: 10.1.1997 / 20:00:08 / cg" |
|
745 |
! |
|
746 |
||
701 | 747 |
asFraction |
748 |
"return a Fraction with same value as receiver" |
|
749 |
||
2794
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
750 |
^ Fraction basicNew setNumerator:self denominator:1 |
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
751 |
|
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
752 |
"Modified: 28.7.1997 / 19:26:06 / cg" |
1 | 753 |
! |
754 |
||
701 | 755 |
asInteger |
1 | 756 |
"return the receiver truncated towards zero - |
757 |
for integers this is self" |
|
758 |
||
759 |
^ self |
|
1337 | 760 |
! |
761 |
||
762 |
signExtendedByteValue |
|
763 |
"return a smallInteger from sign-extending the 8'th bit. |
|
764 |
May be useful for communication interfaces" |
|
765 |
||
766 |
^ (self bitAnd:16rFF) signExtendedByteValue |
|
767 |
||
768 |
"Created: 7.5.1996 / 09:31:52 / cg" |
|
769 |
! |
|
770 |
||
771 |
signExtendedShortValue |
|
772 |
"return a smallInteger from sign-extending the 16'th bit. |
|
773 |
May be useful for communication interfaces" |
|
774 |
||
775 |
^ (self bitAnd:16rFFFF) signExtendedShortValue |
|
776 |
||
777 |
"Modified: 7.5.1996 / 09:31:57 / cg" |
|
1 | 778 |
! ! |
779 |
||
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
780 |
!Integer methodsFor:'comparing'! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
781 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
782 |
hash |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
783 |
"redefined to return smallInteger hashValues" |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
784 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
785 |
|v "{ Class: SmallInteger }" | |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
786 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
787 |
v := self bitAnd:16r1FFFFFFF. |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
788 |
self < 0 ifTrue:[^ v negated]. |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
789 |
^ v |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
790 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
791 |
"Created: 14.11.1996 / 12:12:27 / cg" |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
792 |
! ! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
793 |
|
701 | 794 |
!Integer methodsFor:'double dispatching'! |
795 |
||
796 |
differenceFromFraction:aFraction |
|
797 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 798 |
|
701 | 799 |
|d| |
1 | 800 |
|
701 | 801 |
d := aFraction denominator. |
802 |
^ (Fraction numerator:(aFraction numerator - (self * d)) |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
803 |
denominator:d) |
2793 | 804 |
|
805 |
"Modified: 28.7.1997 / 19:08:30 / cg" |
|
1 | 806 |
! |
807 |
||
701 | 808 |
productFromFraction:aFraction |
809 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 810 |
|
701 | 811 |
^ (Fraction numerator:(self * aFraction numerator) |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
812 |
denominator:aFraction denominator) |
2793 | 813 |
|
814 |
"Modified: 28.7.1997 / 19:08:27 / cg" |
|
1 | 815 |
! |
816 |
||
701 | 817 |
quotientFromFraction:aFraction |
818 |
"sent when a fraction does not know how to divide the receiver, an integer" |
|
819 |
||
2793 | 820 |
^ Fraction |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
821 |
numerator:aFraction numerator |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
822 |
denominator:(self * aFraction denominator) |
2793 | 823 |
|
824 |
"Modified: 28.7.1997 / 19:08:23 / cg" |
|
701 | 825 |
! |
1 | 826 |
|
701 | 827 |
sumFromFraction:aFraction |
828 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
829 |
||
830 |
|d| |
|
831 |
||
832 |
d := aFraction denominator. |
|
2793 | 833 |
^ Fraction numerator:(aFraction numerator + (self * d)) |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
834 |
denominator:d |
2793 | 835 |
|
836 |
"Modified: 28.7.1997 / 19:08:11 / cg" |
|
1 | 837 |
! ! |
838 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
839 |
!Integer methodsFor:'helpers'! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
840 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
841 |
gcd_helper:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
842 |
"a helper for the greatest common divisor of the receiver and anInteger. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
843 |
Knuth's algorithm for large positive integers, with receiver being |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
844 |
larger than the arg." |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
845 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
846 |
| selfAbs argAbs selfLowBit argLowBit shift t | |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
847 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
848 |
selfAbs := self. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
849 |
argAbs := anInteger. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
850 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
851 |
selfLowBit := selfAbs lowBit - 1. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
852 |
argLowBit := argAbs lowBit - 1. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
853 |
shift := selfLowBit min:argLowBit. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
854 |
argAbs := argAbs bitShift:(argLowBit negated). |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
855 |
[selfAbs = 0] whileFalse:[ |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
856 |
selfAbs := selfAbs bitShift:(selfLowBit negated). |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
857 |
selfAbs < argAbs ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
858 |
t := selfAbs. selfAbs := argAbs. argAbs := t |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
859 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
860 |
selfAbs := selfAbs - argAbs. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
861 |
selfLowBit := selfAbs lowBit - 1. |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
862 |
]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
863 |
^ argAbs bitShift:shift |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
864 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
865 |
"Created: 1.3.1997 / 16:38:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
866 |
"Modified: 1.3.1997 / 16:45:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
867 |
! ! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
868 |
|
1 | 869 |
!Integer methodsFor:'misc math'! |
870 |
||
701 | 871 |
acker:n |
1560 | 872 |
"return the value of acker(self, n). |
873 |
;-) Do not try with receivers > 3" |
|
701 | 874 |
|
875 |
(self == 0) ifTrue:[^ n + 1]. |
|
876 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
877 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
878 |
||
879 |
" |
|
880 |
3 acker:2 |
|
881 |
" |
|
1560 | 882 |
|
883 |
"Modified: 18.7.1996 / 13:08:16 / cg" |
|
701 | 884 |
! |
885 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
886 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
887 |
"return an array filled with self // aNumber and |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
888 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
889 |
The result is only defined for positive receiver and argument. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
890 |
This may be redefined in some integer classes for |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
891 |
more performance (where the remainder is generated as a side effect of division)" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
892 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
893 |
^ Array |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
894 |
with:(self // aNumber) |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
895 |
with:(self \\ aNumber) |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
896 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
897 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
898 |
10 divMod:3 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
899 |
100 factorial divMod:103 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
900 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
901 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
902 |
"Modified: 29.10.1996 / 21:18:58 / cg" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
903 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
904 |
|
1 | 905 |
factorial |
1560 | 906 |
"return fac(self) (i.e. 1*2*3...*self) using an iterative algorithm. |
907 |
This is slightly faster than the recursive algorithm, and does not |
|
908 |
suffer from stack overflow problems (with big receivers)" |
|
909 |
||
2745 | 910 |
|p i| |
1560 | 911 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
912 |
(self < 2) ifTrue:[ |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
913 |
self < 0 ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
914 |
"/ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
915 |
"/ requested factorial of a negative number |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
916 |
"/ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
917 |
^ DomainErrorSignal raiseErrorString:'factorial of negative number' |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
918 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
919 |
^ 1 |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
920 |
]. |
1560 | 921 |
p := 2. |
2745 | 922 |
i := 3. |
923 |
[i <= self] whileTrue:[ |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
924 |
p := p * i. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
925 |
i := i + 1. |
1560 | 926 |
]. |
927 |
^ p |
|
928 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
929 |
" |
1560 | 930 |
10 factorial |
931 |
1000 factorial |
|
932 |
10000 factorial |
|
933 |
10000 factorialR |
|
934 |
||
935 |
Time millisecondsToRun:[2000 factorial] |
|
936 |
Time millisecondsToRun:[2000 factorialR] |
|
2745 | 937 |
-1 factorial |
1560 | 938 |
" |
939 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
940 |
"Modified: 29.10.1996 / 20:58:43 / cg" |
1560 | 941 |
! |
942 |
||
943 |
factorialR |
|
944 |
"return fac(self) (i.e. 1*2*3...*self) using a recursive algorithm." |
|
1 | 945 |
|
356 | 946 |
(self >= 2) ifTrue:[ |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
947 |
^ self * (self - 1) factorialR |
1 | 948 |
]. |
356 | 949 |
^ 1 |
212 | 950 |
|
951 |
" |
|
1560 | 952 |
10 factorialR |
953 |
1000 factorialR |
|
212 | 954 |
" |
1560 | 955 |
|
956 |
"Created: 18.7.1996 / 12:48:36 / cg" |
|
1 | 957 |
! |
958 |
||
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
959 |
fib |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
960 |
"compute the fibionacci number for the receiver. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
961 |
(same result as returned by #recursiveFib. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
962 |
This method shows how a changed algorithm can |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
963 |
change things much more drastic than tuning ... |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
964 |
... compare 30 recursiveFib with 30 fib; |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
965 |
and dont even try 60 recursiveFib)" |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
966 |
|
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
967 |
|a1 a2 an| |
701 | 968 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
969 |
a1 := 1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
970 |
a2 := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
971 |
an := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
972 |
self timesRepeat:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
973 |
an := a1 + a2. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
974 |
a2 := a1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
975 |
a1 := an |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
976 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
977 |
^ an |
701 | 978 |
|
979 |
" |
|
1422 | 980 |
Transcript showCR:(Time millisecondsToRun:[30 fib]) |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
981 |
Transcript showCR:(Time millisecondsToRun:[30 recursiveFib]) |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
982 |
Transcript showCR:(Time millisecondsToRun:[60 fib]) |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
983 |
Transcript showCR:(Time millisecondsToRun:[200 fib]) |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
984 |
Transcript showCR:(Time millisecondsToRun:[1500 fib]) |
701 | 985 |
" |
986 |
! |
|
987 |
||
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
988 |
recursiveFib |
1559 | 989 |
"compute the fibionacci number for the receiver. |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
990 |
WARNING: |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
991 |
Dont use this method if you need fibionacci numbers - |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
992 |
this method is for benchmarking purposes only. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
993 |
(use #fib instead and dont ever try 60 recursiveFib ...)" |
701 | 994 |
|
995 |
(self > 1) ifTrue:[ |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
996 |
^ (self - 1) recursiveFib + (self - 2) recursiveFib |
701 | 997 |
]. |
998 |
^ 1 |
|
999 |
||
1000 |
" |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1001 |
30 fib |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1002 |
30 recursiveFib |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1003 |
Transcript showCR:(Time millisecondsToRun:[30 recursiveFib]) |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1004 |
Transcript showCR:(Time millisecondsToRun:[30 fib]) |
701 | 1005 |
" |
1559 | 1006 |
|
1007 |
"Modified: 18.7.1996 / 12:47:19 / cg" |
|
701 | 1008 |
! |
1009 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1010 |
gcd:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1011 |
"return the greatest common divisor of the receiver and anInteger. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1012 |
Euclids & Knuths algorithm." |
1 | 1013 |
|
3060 | 1014 |
|selfAbs argAbs t| |
1 | 1015 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1016 |
selfAbs := self abs. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1017 |
argAbs := anInteger abs. |
1888 | 1018 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1019 |
selfAbs < argAbs ifTrue:[ |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1020 |
t := selfAbs. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1021 |
selfAbs := argAbs. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1022 |
argAbs := t. |
1 | 1023 |
]. |
52 | 1024 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1025 |
argAbs = 0 ifTrue: [^ selfAbs]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1026 |
selfAbs := selfAbs \\ argAbs. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1027 |
selfAbs = 0 ifTrue:[^ argAbs]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1028 |
^ argAbs gcd_helper:selfAbs |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1029 |
|
3060 | 1030 |
" |
1031 |
3141589999999999 gcd:1000000000000000 |
|
1032 |
" |
|
1033 |
||
1034 |
"Modified: / 25.10.1997 / 16:08:45 / cg" |
|
1 | 1035 |
! |
1036 |
||
1037 |
lcm:anInteger |
|
52 | 1038 |
"return the least common multiple (using gcd:)" |
1039 |
||
1040 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
1041 |
||
92 | 1042 |
" |
1043 |
65 lcm:15 |
|
1044 |
3 lcm:15 |
|
1045 |
" |
|
1 | 1046 |
! ! |
1047 |
||
1048 |
!Integer methodsFor:'printing & storing'! |
|
1049 |
||
49 | 1050 |
displayString |
1051 |
"return a string for displaying in a view (as in inspector). |
|
1052 |
The output radix is usually 10, but can be changed by setting |
|
1053 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
1054 |
||
54 | 1055 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
159 | 1056 |
^ self printString |
54 | 1057 |
]. |
49 | 1058 |
^ self radixPrintStringRadix:DefaultDisplayRadix |
1059 |
||
92 | 1060 |
" |
1061 |
Integer displayRadix:16. 12345 |
|
1062 |
Integer displayRadix:2. 12345 |
|
1063 |
Integer displayRadix:10. 12345 |
|
1064 |
" |
|
49 | 1065 |
! |
1066 |
||
701 | 1067 |
errorPrintHex |
1068 |
"print the receiver as a hex number on the standard error stream" |
|
1069 |
||
1070 |
(self printStringRadix:16) errorPrint |
|
1071 |
! |
|
1 | 1072 |
|
701 | 1073 |
hexPrintString |
1074 |
"return a hex string representation of the receiver" |
|
1075 |
||
1076 |
^ self printStringRadix:16 |
|
1077 |
! |
|
1078 |
||
2408 | 1079 |
hexPrintString:size |
1080 |
"return a hex string representation of the receiver, |
|
1081 |
padded to size characters" |
|
1082 |
||
1083 |
^ (self printStringRadix:16) leftPaddedTo:size with:$0 |
|
1084 |
||
1085 |
"Created: 18.2.1997 / 13:32:33 / cg" |
|
1086 |
! |
|
1087 |
||
701 | 1088 |
printHex |
1089 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 1090 |
|
701 | 1091 |
(self printStringRadix:16) print |
1092 |
! |
|
1093 |
||
1094 |
printOn:aStream |
|
1095 |
"append a printed description of the receiver to aStream" |
|
1096 |
||
1097 |
aStream nextPutAll:(self printStringRadix:10) |
|
1098 |
! |
|
1099 |
||
1431
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1100 |
printOn:aStream base:base |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1101 |
"append a printed description of the receiver to aStream. |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1102 |
The receiver is printed in radix base (instead of the default, 10)." |
701 | 1103 |
|
1104 |
aStream nextPutAll:(self printStringRadix:base) |
|
1431
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1105 |
|
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1106 |
"Created: 20.5.1996 / 11:53:38 / cg" |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1107 |
! |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1108 |
|
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1109 |
printOn:aStream radix:base |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1110 |
"append a printed description of the receiver to aStream. |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1111 |
The receiver is printed in radix base (instead of the default, 10). |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1112 |
This method is obsoleted by #printOn:base:, which is ST-80 compatible." |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1113 |
|
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1114 |
aStream nextPutAll:(self printStringRadix:base) |
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1115 |
|
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1116 |
"Modified: 20.5.1996 / 11:54:10 / cg" |
701 | 1117 |
! |
1118 |
||
1119 |
printString |
|
1120 |
"return a string representation of the receiver" |
|
1121 |
||
1122 |
^ self printStringRadix:10 |
|
1 | 1123 |
! |
1124 |
||
1125 |
printStringRadix:aRadix |
|
1126 |
"return a string representation of the receiver in the specified |
|
1127 |
radix (without the initial XXr)" |
|
1128 |
||
3136
91dbf1ea13a6
print in junks of 6 digits (instead of 8 digits)
Claus Gittinger <cg@exept.de>
parents:
3135
diff
changeset
|
1129 |
|num s divMod mod r r2 r4 nD numN| |
1 | 1130 |
|
54 | 1131 |
(aRadix between:2 and:36) ifFalse:[ |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1132 |
self error:'invalid radix'. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1133 |
^ self printStringRadix:10 |
54 | 1134 |
]. |
1135 |
||
1 | 1136 |
(self = 0) ifTrue:[^ '0']. |
1137 |
(self < 0) ifTrue:[ |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1138 |
^ '-' , (self negated printStringRadix:aRadix) |
1 | 1139 |
]. |
92 | 1140 |
|
1141 |
" |
|
1142 |
claus: changed it from a recursive algorithm; |
|
1143 |
(it used to trigger stack-overflow exceptions when printing |
|
1144 |
3000 factorial ...) |
|
1145 |
" |
|
1146 |
"/ leftPart := self // aRadix. |
|
1147 |
"/ (leftPart ~= 0) ifTrue:[ |
|
1148 |
"/ ^ (leftPart printStringRadix:aRadix) copyWith:(Character digitValue:(self \\ aRadix)) |
|
1149 |
"/ ]. |
|
1150 |
"/ ^ (Character digitValue:self) asString |
|
1151 |
||
1152 |
num := self. |
|
2951 | 1153 |
num = 0 ifTrue:[^ '0']. |
1154 |
||
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1155 |
s := WriteStream on:''. |
2951 | 1156 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1157 |
"/ instead of computing the quotient and remainder |
3136
91dbf1ea13a6
print in junks of 6 digits (instead of 8 digits)
Claus Gittinger <cg@exept.de>
parents:
3135
diff
changeset
|
1158 |
"/ against radix, do it in junks of 5 or 6 digits. |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1159 |
"/ This reduces the number of LargeInt-divisions |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1160 |
"/ by that factor (turning them into smallInt divisions |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1161 |
"/ within that junk) and speeds up the conversions noticably. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1162 |
|
3136
91dbf1ea13a6
print in junks of 6 digits (instead of 8 digits)
Claus Gittinger <cg@exept.de>
parents:
3135
diff
changeset
|
1163 |
r2 := aRadix*aRadix. "/ radix^2 |
3138 | 1164 |
r4 := r2*r2. "/ radix^4 |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1165 |
aRadix <= 10 ifTrue:[ |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1166 |
r := r4*r2. "/ radix^6 |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1167 |
nD := 6. |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1168 |
] ifFalse:[ |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1169 |
r := r4*aRadix. "/ radix^5 |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1170 |
nD := 5. |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1171 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1172 |
|
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1173 |
[num >= r] whileTrue:[ |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1174 |
"/ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1175 |
"/ chop off nD digits. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1176 |
"/ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1177 |
divMod := num divMod:r. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1178 |
num := divMod at:1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1179 |
numN := divMod at:2. |
2951 | 1180 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1181 |
"/ process them |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1182 |
nD timesRepeat:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1183 |
divMod := numN divMod:aRadix. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1184 |
numN := divMod at:1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1185 |
mod := divMod at:2. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1186 |
s nextPut: (Character digitValue:mod). |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1187 |
]. |
2951 | 1188 |
]. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1189 |
|
92 | 1190 |
[num ~= 0] whileTrue:[ |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1191 |
divMod := num divMod:aRadix. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1192 |
num := divMod at:1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1193 |
mod := divMod at:2. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1194 |
s nextPut: (Character digitValue:mod). |
1 | 1195 |
]. |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1196 |
^ s contents reverse |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1197 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1198 |
"Modified: / 17.11.1997 / 16:27:45 / cg" |
1 | 1199 |
! |
1200 |
||
1201 |
printStringRadix:aRadix size:sz fill:fillCharacter |
|
1202 |
"return a string representation of the receiver in the specified |
|
1203 |
radix. The string is padded on the left with fillCharacter to make |
|
1204 |
its size as specified in sz." |
|
1205 |
||
2951 | 1206 |
|s actualSize| |
1 | 1207 |
|
1208 |
s := self printStringRadix:aRadix. |
|
2951 | 1209 |
actualSize := s size. |
1210 |
actualSize < sz ifTrue:[ |
|
1211 |
s := ((String new:(sz - actualSize)) atAllPut:fillCharacter) , s |
|
1 | 1212 |
]. |
1213 |
^ s |
|
1214 |
||
92 | 1215 |
" |
1216 |
1024 printStringRadix:16 size:4 fill:$0 |
|
1217 |
1024 printStringRadix:2 size:16 fill:$. |
|
1218 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
1219 |
" |
|
216 | 1220 |
! |
1221 |
||
701 | 1222 |
radixPrintStringRadix:aRadix |
1223 |
"return a string representation of the receiver in the specified |
|
1224 |
radix; prepend XXr to the string" |
|
1225 |
||
1226 |
^ (aRadix printString) , 'r', (self printStringRadix:aRadix) |
|
216 | 1227 |
|
701 | 1228 |
" |
1229 |
31 radixPrintStringRadix:2 |
|
1230 |
31 radixPrintStringRadix:3 |
|
1231 |
31 radixPrintStringRadix:36 |
|
1232 |
" |
|
1 | 1233 |
! ! |
1234 |
||
701 | 1235 |
!Integer methodsFor:'queries'! |
202 | 1236 |
|
701 | 1237 |
digitAt:n |
1238 |
"return the n-th byte of the binary representation. |
|
1239 |
This is a very stupid implementation, and should be redefined in |
|
1240 |
concrete subclasses." |
|
202 | 1241 |
|
701 | 1242 |
|num count| |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1243 |
|
701 | 1244 |
num := self. |
1245 |
count := n. |
|
1246 |
[count > 1] whileTrue:[ |
|
1247 |
num := num // 256. |
|
1248 |
count := count - 1 |
|
202 | 1249 |
]. |
701 | 1250 |
^ num \\ 256 |
159 | 1251 |
|
1252 |
" |
|
701 | 1253 |
16r44332211 digitAt:1 |
1254 |
16r44332211 digitAt:2 |
|
1255 |
16r44332211 digitAt:3 |
|
1256 |
16r44332211 digitAt:4 |
|
1257 |
16r44332211 digitAt:5 |
|
1258 |
16r00332211 digitAt:4 |
|
1259 |
16r00332211 digitAt:5 |
|
159 | 1260 |
" |
1 | 1261 |
! |
1262 |
||
701 | 1263 |
digitLength |
1264 |
"return the number of bytes needed for the binary representation |
|
2816 | 1265 |
of the receiver. |
1266 |
This method is redefined in concrete classes - the fallback here is |
|
1267 |
never really used." |
|
701 | 1268 |
|
1269 |
^ (self log:256) ceiling asInteger |
|
2816 | 1270 |
|
1271 |
"Modified: 31.7.1997 / 13:19:06 / cg" |
|
701 | 1272 |
! |
1273 |
||
1274 |
isInteger |
|
1275 |
"return true, if the receiver is some kind of integer number" |
|
1276 |
||
1277 |
^ true |
|
3036 | 1278 |
! |
1279 |
||
1280 |
isPowerOfTwo |
|
1281 |
"return true, if the receiver is a power of 2" |
|
1282 |
||
1283 |
^ (self bitAnd:(self - 1)) == 0 |
|
1284 |
||
1285 |
" |
|
1286 |
0 isPowerOfTwo |
|
1287 |
1 isPowerOfTwo |
|
1288 |
2 isPowerOfTwo |
|
1289 |
3 isPowerOfTwo |
|
1290 |
4 isPowerOfTwo |
|
1291 |
16r8000000000000000 isPowerOfTwo |
|
1292 |
16r8000000000000001 isPowerOfTwo |
|
1293 |
" |
|
1294 |
||
1295 |
"Modified: 15.10.1997 / 18:43:49 / cg" |
|
701 | 1296 |
! ! |
202 | 1297 |
|
2458 | 1298 |
!Integer methodsFor:'special bit operators'! |
1299 |
||
1300 |
bitAnd_32:anInteger |
|
1301 |
"return a C-semantic 32bit locical-and of the receiver and |
|
1302 |
the argument. Both must be either Small- or LargeIntegers. |
|
1303 |
This (nonstandard) specialized method is provided to allow simulation of |
|
1304 |
and operations with C semantics." |
|
1305 |
||
1306 |
%{ /* NOCONTEXT */ |
|
1307 |
int val1, val2, rslt; |
|
1308 |
||
1309 |
if (__isSmallInteger(self)) { |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1310 |
val1 = __intVal(self); |
2458 | 1311 |
} else if (__isLargeInteger(self)) { |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1312 |
val1 = __longIntVal(self); |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1313 |
if (!val1) goto bad; |
2458 | 1314 |
} else { |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1315 |
goto bad; |
2458 | 1316 |
} |
1317 |
if (__isSmallInteger(anInteger)) { |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1318 |
val2 = __intVal(anInteger); |
2458 | 1319 |
} else if (__isLargeInteger(anInteger)) { |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1320 |
val2 = __longIntVal(anInteger); |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1321 |
if (!val2) goto bad; |
2458 | 1322 |
} else { |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1323 |
goto bad; |
2458 | 1324 |
} |
1325 |
rslt = val1 & val2; |
|
1326 |
RETURN(__MKINT(rslt)); |
|
1327 |
||
1328 |
bad: ; |
|
1329 |
%}. |
|
1330 |
self primitiveFailed. |
|
1331 |
! |
|
1332 |
||
1333 |
bitOr_32:anInteger |
|
1334 |
"return a C-semantic 32bit locical-or of the receiver and |
|
1335 |
the argument. Both must be either Small- or LargeIntegers. |
|
1336 |
This (nonstandard) specialized method is provided to allow simulation of |
|
1337 |
and operations with C semantics." |
|
1338 |
||
1339 |
%{ /* NOCONTEXT */ |
|
1340 |
int val1, val2, rslt; |
|
1341 |
||
1342 |
if (__isSmallInteger(self)) { |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1343 |
val1 = __intVal(self); |
2458 | 1344 |
} else if (__isLargeInteger(self)) { |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1345 |
val1 = __longIntVal(self); |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1346 |
if (!val1) goto bad; |
2458 | 1347 |
} else { |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1348 |
goto bad; |
2458 | 1349 |
} |
1350 |
if (__isSmallInteger(anInteger)) { |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1351 |
val2 = __intVal(anInteger); |
2458 | 1352 |
} else if (__isLargeInteger(anInteger)) { |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1353 |
val2 = __longIntVal(anInteger); |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1354 |
if (!val2) goto bad; |
2458 | 1355 |
} else { |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1356 |
goto bad; |
2458 | 1357 |
} |
1358 |
rslt = val1 | val2; |
|
1359 |
RETURN(__MKINT(rslt)); |
|
1360 |
||
1361 |
bad: ; |
|
1362 |
%}. |
|
1363 |
self primitiveFailed. |
|
1364 |
! ! |
|
1365 |
||
701 | 1366 |
!Integer methodsFor:'truncation & rounding'! |
1367 |
||
1368 |
ceiling |
|
1558 | 1369 |
"return the smallest integer which is larger or equal to the receiver. |
1370 |
For integers, this is the receiver itself." |
|
345 | 1371 |
|
701 | 1372 |
^ self |
1558 | 1373 |
|
1374 |
"Modified: 18.7.1996 / 12:44:06 / cg" |
|
701 | 1375 |
! |
345 | 1376 |
|
701 | 1377 |
compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1378 |
"if the receiver can be represented as a SmallInteger, return |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1379 |
a SmallInteger with my value; otherwise return self with leading |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1380 |
zeros removed. This method is redefined in LargeInteger." |
701 | 1381 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1382 |
^ self |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1383 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1384 |
"Modified: 5.11.1996 / 14:07:41 / cg" |
202 | 1385 |
! |
1386 |
||
701 | 1387 |
floor |
1558 | 1388 |
"return the largest integer which is smaller or equal to the receiver. |
1389 |
For integers, this is the receiver itself." |
|
701 | 1390 |
|
1391 |
^ self |
|
1558 | 1392 |
|
1393 |
"Modified: 18.7.1996 / 12:44:00 / cg" |
|
701 | 1394 |
! |
202 | 1395 |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1396 |
fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1397 |
"return a number with value from digits after the decimal point. |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1398 |
(i.e. the receiver minus its truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1399 |
Since integers have no fraction, return 0 here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1400 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1401 |
^ 0 |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1402 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1403 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1404 |
1234.56789 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1405 |
1.2345e6 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1406 |
1000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1407 |
10000000000000000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1408 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1409 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1410 |
"Modified: 4.11.1996 / 20:27:44 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1411 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1412 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1413 |
integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1414 |
"return a number with value from digits before the decimal point. |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1415 |
(i.e. the receivers truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1416 |
Since integers have no fraction, return the receiver here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1417 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1418 |
^ self |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1419 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1420 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1421 |
1234.56789 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1422 |
1.2345e6 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1423 |
1000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1424 |
10000000000000000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1425 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1426 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1427 |
"Modified: 4.11.1996 / 20:28:22 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1428 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1429 |
|
701 | 1430 |
normalize |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1431 |
"if the receiver can be represented as a SmallInteger, return |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1432 |
a SmallInteger with my value; otherwise return self with leading |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1433 |
zeros removed. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1434 |
This method is left for backward compatibility - it has been |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1435 |
renamed to #compressed for ST-80 compatibility." |
1 | 1436 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1437 |
^ self compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1438 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1439 |
"Modified: 5.11.1996 / 14:08:24 / cg" |
701 | 1440 |
! |
1 | 1441 |
|
701 | 1442 |
rounded |
1443 |
"return the receiver rounded toward the next Integer - |
|
1558 | 1444 |
for integers this is the receiver itself." |
701 | 1445 |
|
1446 |
^ self |
|
1558 | 1447 |
|
1448 |
"Modified: 18.7.1996 / 12:44:24 / cg" |
|
701 | 1449 |
! |
69 | 1450 |
|
701 | 1451 |
truncated |
1452 |
"return the receiver truncated towards zero - |
|
1558 | 1453 |
for integers this is the receiver itself." |
159 | 1454 |
|
701 | 1455 |
^ self |
1558 | 1456 |
|
1457 |
"Modified: 18.7.1996 / 12:44:33 / cg" |
|
701 | 1458 |
! ! |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1459 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1460 |
!Integer class methodsFor:'documentation'! |
701 | 1461 |
|
1462 |
version |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1463 |
^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.76 1998-01-12 13:19:43 cg Exp $' |
1 | 1464 |
! ! |