author | Claus Gittinger <cg@exept.de> |
Thu, 09 Aug 2018 17:33:11 +0200 | |
changeset 23271 | 676915d2e795 |
parent 23119 | 8ca65ffe917f |
child 23382 | e11a8bed0f9e |
permissions | -rw-r--r-- |
22460 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1988 by Claus Gittinger |
19078 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
15 |
|
17488 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
Number subclass:#Integer |
19327 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'BCDConversionErrorSignal PrimeCache' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Magnitude-Numbers' |
|
1 | 23 |
! |
24 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
25 |
Object subclass:#ModuloNumber |
19327 | 26 |
instanceVariableNames:'modulus reciprocal shift' |
27 |
classVariableNames:'' |
|
28 |
poolDictionaries:'' |
|
29 |
privateIn:Integer |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
30 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
31 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
32 |
!Integer class methodsFor:'documentation'! |
88 | 33 |
|
34 |
copyright |
|
35 |
" |
|
36 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
19078 | 37 |
All Rights Reserved |
1 | 38 |
|
88 | 39 |
This software is furnished under a license and may be used |
40 |
only in accordance with the terms of that license and with the |
|
41 |
inclusion of the above copyright notice. This software may not |
|
42 |
be provided or otherwise made available to, or used by, any |
|
43 |
other person. No title to or ownership of the software is |
|
44 |
hereby transferred. |
|
45 |
" |
|
46 |
! |
|
1 | 47 |
|
88 | 48 |
documentation |
49 |
" |
|
50 |
abstract superclass for all integer numbers. |
|
51 |
See details in concrete subclasses LargeInteger and SmallInteger. |
|
212 | 52 |
|
1892 | 53 |
Mixed mode arithmetic: |
22071 | 54 |
int <op> int -> int |
55 |
int <op> fraction -> fraction |
|
56 |
int <op> float -> float |
|
57 |
int <op> fix -> fix; scale is fix's scale |
|
1892 | 58 |
|
1295 | 59 |
[author:] |
19078 | 60 |
Claus Gittinger |
1556 | 61 |
|
62 |
[see also:] |
|
19078 | 63 |
Number |
64 |
LargeInteger SmallInteger |
|
65 |
Float ShortFloat Fraction FixedPoint |
|
88 | 66 |
" |
67 |
! ! |
|
1 | 68 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
69 |
!Integer class methodsFor:'instance creation'! |
92 | 70 |
|
3044 | 71 |
byte1:b1 byte2:b2 byte3:b3 byte4:b4 |
72 |
"Squeak compatibility: |
|
73 |
Return an Integer given four value bytes. |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
74 |
The returned integer is either a Small- or a LargeInteger |
15241 | 75 |
(on 32bit systems - on 64bit systems, it will be always a SmallInteger)" |
3044 | 76 |
|
77 |
|t| |
|
78 |
||
79 |
t := b4. |
|
80 |
t := (t bitShift:8) + b3. |
|
81 |
t := (t bitShift:8) + b2. |
|
82 |
^ (t bitShift:8) + b1. |
|
83 |
||
84 |
" |
|
85 |
(Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString |
|
86 |
(Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString |
|
87 |
" |
|
88 |
||
89 |
"Created: 19.10.1997 / 18:08:52 / cg" |
|
90 |
"Modified: 19.10.1997 / 18:09:04 / cg" |
|
91 |
! |
|
92 |
||
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
93 |
fastFromString:aString at:startIndex |
11587 | 94 |
"return the next unsigned Integer from the string |
4610 | 95 |
as a decimal number, starting at startIndex. |
19276 | 96 |
The number must be in the native machine's int range |
4610 | 97 |
(i.e. 63bit on alpha / 31 on all others); |
98 |
However, for portability, only use it for 31bit numbers. |
|
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
99 |
No spaces are skipped. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
100 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
101 |
This is a specially tuned entry (using a low-level C-call), which |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
102 |
returns garbage if the argument string is not a small integer number. |
11945 | 103 |
It has been added to allow high speed string decomposition into numbers, |
104 |
especially for mass-data." |
|
105 |
||
106 |
%{ /* NOCONTEXT */ |
|
12480
01a98f46e297
change __isString() to __isStringLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
12422
diff
changeset
|
107 |
if (__isStringLike(aString) && __isSmallInteger(startIndex)) { |
19078 | 108 |
char *cp = (char *)(__stringVal(aString)); |
109 |
int idx = __intVal(startIndex) - 1; |
|
110 |
unsigned INT val; |
|
111 |
||
112 |
if ((unsigned)idx < __stringSize(aString)) { |
|
19327 | 113 |
extern int atoi(); |
114 |
extern long atol(); |
|
19276 | 115 |
|
116 |
val = atol(cp + idx); |
|
19078 | 117 |
if (val <= _MAX_INT) { |
118 |
RETURN(__mkSmallInteger(val)); |
|
119 |
} |
|
120 |
RETURN (__MKUINT(val)); |
|
121 |
} |
|
11945 | 122 |
} |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
123 |
%}. |
11945 | 124 |
self primitiveFailed. |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
125 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
126 |
" |
11587 | 127 |
Integer fastFromString:'12345' at:1 |
128 |
Integer fastFromString:'12345' at:2 |
|
129 |
Integer fastFromString:'12345' at:3 |
|
130 |
Integer fastFromString:'12345' at:4 |
|
131 |
Integer fastFromString:'12345' at:5 |
|
132 |
Integer fastFromString:'1234512345' at:1 |
|
133 |
Integer fastFromString:'2147483647' at:1 |
|
134 |
||
135 |
Integer fastFromString:'4294967295' at:1 |
|
136 |
Integer fastFromString:'12345' at:6 |
|
137 |
Integer fastFromString:'12345' at:0 |
|
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
138 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
139 |
Time millisecondsToRun:[ |
19078 | 140 |
100000 timesRepeat:[ |
141 |
Integer readFrom:'12345' |
|
142 |
] |
|
11587 | 143 |
] |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
144 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
145 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
146 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
147 |
Time millisecondsToRun:[ |
19078 | 148 |
100000 timesRepeat:[ |
149 |
Integer fastFromString:'12345' at:1 |
|
150 |
] |
|
11587 | 151 |
] |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
152 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
153 |
! |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
154 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
155 |
fromBCDBytes:aByteArray |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
156 |
"given a byteArray in BCD format, return an appropriate integer. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
157 |
The byteArray must contain the BCD encoded decimal string, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
158 |
starting with the most significant digits. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
159 |
This conversion is useful for some communication protocols, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
160 |
or control systems, which represent big numbers this way... |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
161 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
162 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
163 |
|val| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
164 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
165 |
val := 0. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
166 |
aByteArray do:[:twoDigits | |
19078 | 167 |
|hi lo| |
168 |
||
169 |
hi := (twoDigits bitShift:-4) bitAnd:16r0F. |
|
170 |
lo := twoDigits bitAnd:16r0F. |
|
171 |
val := (val * 100) + (hi * 10) + lo |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
172 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
173 |
^ val |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
174 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
175 |
" |
11587 | 176 |
Integer fromBCDBytes:#[16r12 16r34 16r56] |
177 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78] |
|
178 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90] |
|
179 |
Integer fromBCDBytes:#[16r98 16r76 16r54] |
|
180 |
Integer fromBCDBytes:#[16r98 16r76 16r54 16r32] |
|
181 |
Integer fromBCDBytes:#[16r98 16r76 16r54 16r32 16r10] |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
182 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
183 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
184 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
185 |
|
5171 | 186 |
fromSwappedBCDBytes:aByteArray |
187 |
"given a byteArray in BCD format, return an appropriate integer. |
|
188 |
The byteArray must contain the BCD encoded decimal string, |
|
189 |
starting with the LEAST significant digits. |
|
190 |
This conversion is useful for some communication protocols, |
|
191 |
or control systems (e.g. SMC), which represent big numbers this way... |
|
192 |
" |
|
193 |
||
194 |
|val| |
|
195 |
||
196 |
val := 0. |
|
197 |
aByteArray do:[:twoDigits | |
|
19078 | 198 |
|hi lo| |
199 |
||
200 |
lo := (twoDigits bitShift:-4) bitAnd:16r0F. |
|
201 |
hi := twoDigits bitAnd:16r0F. |
|
202 |
lo <= 9 ifTrue:[ |
|
203 |
val := (val * 100) + (hi * 10) + lo |
|
204 |
] ifFalse:[ |
|
205 |
"16rF is used to encode an odd number of digits" |
|
206 |
val := (val * 10) + hi. |
|
207 |
]. |
|
5171 | 208 |
]. |
209 |
^ val |
|
210 |
||
211 |
" |
|
11587 | 212 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56] |
213 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16rF6] |
|
214 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78] |
|
215 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90] |
|
216 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54] |
|
217 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32] |
|
218 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32 16r10] |
|
5171 | 219 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90] |
220 |
" |
|
221 |
! |
|
222 |
||
92 | 223 |
new:numberOfBytes neg:negative |
224 |
"for ST-80 compatibility: |
|
225 |
Return an empty Integer (uninitialized value) with space for |
|
226 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 227 |
negative specifies if the result should be a negative number. |
228 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 229 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
230 |
^ LargeInteger basicNew |
19078 | 231 |
numberOfDigits:numberOfBytes |
232 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
|
701 | 233 |
! |
234 |
||
11587 | 235 |
readFrom:aStringOrStream |
20329 | 236 |
"return the next Integer from the (character-)stream aStream |
11587 | 237 |
as decimal number. |
238 |
||
239 |
NOTICE: |
|
701 | 240 |
This behaves different from the default readFrom:, in returning |
241 |
0 (instead of raising an error) in case no number can be read. |
|
242 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
243 |
- depending on the upcoming ANSI standard, this may change." |
|
244 |
||
245 |
^ self readFrom:aStringOrStream onError:0 |
|
246 |
||
247 |
" |
|
11587 | 248 |
Integer readFrom:(ReadStream on:'foobar') |
22460 | 249 |
Integer readFrom:(ReadStream on:'123foobar') |
11587 | 250 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
701 | 251 |
" |
22460 | 252 |
|
253 |
"Modified (comment): / 17-01-2018 / 18:27:26 / stefan" |
|
701 | 254 |
! |
255 |
||
21636 | 256 |
readFrom:aStringOrStream allowRadix:allowRadix onError:exceptionBlock |
701 | 257 |
"return the next Integer from the (character-)stream aStream, |
21637 | 258 |
possibly handling initial XXr for arbitrary radix numbers and initial sign. |
701 | 259 |
Also, all initial whitespace is skipped. |
260 |
If the string does not represent a valid integer number, |
|
261 |
return the value of exceptionBlock." |
|
262 |
||
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
263 |
|value| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
264 |
|
7093
60bad14d38fc
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
6907
diff
changeset
|
265 |
Error handle:[:ex | |
19078 | 266 |
^ exceptionBlock value |
1700 | 267 |
] do:[ |
19078 | 268 |
|str nextChar negative| |
269 |
||
270 |
str := aStringOrStream readStream. |
|
271 |
||
272 |
nextChar := str skipSeparators. |
|
273 |
(nextChar == $-) ifTrue:[ |
|
274 |
negative := true. |
|
275 |
str next. |
|
276 |
nextChar := str peekOrNil |
|
277 |
] ifFalse:[ |
|
278 |
negative := false |
|
279 |
]. |
|
280 |
(nextChar isNil or:[nextChar isDigit not]) ifTrue:[ |
|
281 |
" |
|
282 |
the string does not represent an integer |
|
283 |
" |
|
284 |
^ exceptionBlock value |
|
285 |
]. |
|
286 |
value := self readFrom:str radix:10. |
|
287 |
nextChar := str peekOrNil. |
|
21636 | 288 |
(allowRadix and:[((nextChar == $r) or:[ nextChar == $R])]) ifTrue:[ |
19078 | 289 |
"-xxr<number> is invalid; should be xxr-<val>" |
290 |
||
291 |
negative ifTrue:[ |
|
20332 | 292 |
'Integer [warning]: invalid (negative) radix; please use xxr-<val> instead of -xxr<number>' errorPrintCR. |
293 |
"/ negative := false |
|
19078 | 294 |
]. |
295 |
str next. |
|
20332 | 296 |
nextChar := str peekOrNil. |
297 |
nextChar == $- ifTrue:[ |
|
298 |
negative ifTrue:[ |
|
299 |
'Integer [warning]: invalid double sign; please use xxr-<val> instead of -xxr<number>' errorPrintCR. |
|
300 |
]. |
|
301 |
negative := true. |
|
302 |
str next. |
|
303 |
]. |
|
19078 | 304 |
value := self readFrom:str radix:value |
305 |
]. |
|
306 |
negative ifTrue:[ |
|
307 |
value := value negated |
|
308 |
]. |
|
701 | 309 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
310 |
^ value |
701 | 311 |
|
312 |
" |
|
11587 | 313 |
Integer readFrom:'12345' onError:['wrong'] |
314 |
Integer readFrom:'-12345' onError:['wrong'] |
|
315 |
Integer readFrom:'+12345' onError:['wrong'] |
|
316 |
Integer readFrom:'16rFFFF' onError:['wrong'] |
|
317 |
Integer readFrom:'12345.1234' onError:['wrong'] |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
318 |
Integer readFrom:'foo' onError:['wrong'] |
11587 | 319 |
Integer readFrom:'foo' |
701 | 320 |
" |
321 |
||
21636 | 322 |
"Created: / 09-03-2017 / 16:26:28 / cg" |
323 |
! |
|
324 |
||
325 |
readFrom:aStringOrStream onError:exceptionBlock |
|
326 |
"return the next Integer from the (character-)stream aStream, |
|
327 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
328 |
Also, all initial whitespace is skipped. |
|
329 |
If the string does not represent a valid integer number, |
|
330 |
return the value of exceptionBlock." |
|
331 |
||
332 |
^ self readFrom:aStringOrStream allowRadix:true onError:exceptionBlock |
|
333 |
||
334 |
" |
|
335 |
Integer readFrom:'12345' onError:['wrong'] |
|
336 |
Integer readFrom:'-12345' onError:['wrong'] |
|
337 |
Integer readFrom:'+12345' onError:['wrong'] |
|
338 |
Integer readFrom:'16rFFFF' onError:['wrong'] |
|
339 |
Integer readFrom:'12345.1234' onError:['wrong'] |
|
340 |
Integer readFrom:'foo' onError:['wrong'] |
|
341 |
Integer readFrom:'foo' |
|
342 |
||
343 |
Integer readFrom:'16rFFFF' allowRadix:false onError:['wrong'] |
|
344 |
" |
|
345 |
||
346 |
"Created: / 16-11-1995 / 22:48:59 / cg" |
|
347 |
"Modified (comment): / 09-03-2017 / 16:27:14 / cg" |
|
701 | 348 |
! |
349 |
||
350 |
readFrom:aStringOrStream radix:radix |
|
20329 | 351 |
"return the next UNSIGNED Integer from the (character-)stream aStream in radix; |
352 |
(assumes that the initial XXr has already been read). |
|
701 | 353 |
No whitespace-skipping is done. |
354 |
Returns 0 if no number available. |
|
355 |
||
11587 | 356 |
NOTICE: |
701 | 357 |
This behaves different from the default readFrom:, in returning |
358 |
0 (instead of raising an error) in case no number can be read. |
|
359 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
360 |
- depending on the upcoming ANSI standard, this may change." |
|
361 |
||
362 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
363 |
! |
|
364 |
||
365 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
20329 | 366 |
"return the next UNSIGNED Integer from the (character-)stream aStream in radix; |
367 |
(assumes that the initial XXr has already been read). |
|
701 | 368 |
No whitespace-skipping is done. |
369 |
Returns the value of exceptionBlock, if no number is available." |
|
370 |
||
20130 | 371 |
|str nextChar value |
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
372 |
r "{ Class: SmallInteger }" |
11654 | 373 |
r2 "{ Class: SmallInteger }" |
374 |
r3 "{ Class: SmallInteger }" |
|
375 |
r4 "{ Class: SmallInteger }" |
|
376 |
digit1 digit2 digit3 digit4 | |
|
701 | 377 |
|
378 |
str := aStringOrStream readStream. |
|
379 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
380 |
nextChar := str peekOrNil. |
20130 | 381 |
(nextChar isNil or:[(value := nextChar digitValueRadix:radix) isNil]) ifTrue:[ |
19078 | 382 |
^ exceptionBlock value |
701 | 383 |
]. |
384 |
||
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
385 |
"/ OLD code |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
386 |
"/ [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
387 |
"/ str next. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
388 |
"/ value := value * radix + nextChar digitValue. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
389 |
"/ nextChar := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
390 |
"/ ]. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
391 |
"/ ^ value. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
392 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
393 |
"/ the code below does the same, but is much faster, if the |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
394 |
"/ converted number is large |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
395 |
"/ (requires only half as many LargeInt multiplications and additions) |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
396 |
"/ It should not be slower for smallIntegers. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
397 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
398 |
r := radix. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
399 |
r2 := r * r. |
11654 | 400 |
r4 := r2 * r2. |
401 |
||
20130 | 402 |
[ |
403 |
nextChar := str nextPeekOrNil. |
|
404 |
nextChar notNil and:[(digit1 := nextChar digitValueRadix:r) notNil] |
|
405 |
] whileTrue:[ |
|
19078 | 406 |
"/ read 4 chars and pre-compute their value to avoid largeInt operations. |
407 |
||
20130 | 408 |
nextChar := str nextPeekOrNil. |
409 |
(nextChar isNil or:[(digit2 := nextChar digitValueRadix:r) isNil]) ifTrue:[ |
|
19078 | 410 |
^ (value * r) + digit1. |
411 |
]. |
|
412 |
||
20130 | 413 |
nextChar := str nextPeekOrNil. |
414 |
(nextChar isNil or:[(digit3 := nextChar digitValueRadix:r) isNil]) ifTrue:[ |
|
415 |
^ (value * r2) + ((digit1*r) + digit2) . |
|
19078 | 416 |
]. |
417 |
||
20130 | 418 |
nextChar := str nextPeekOrNil. |
419 |
(nextChar isNil or:[ (digit4 := nextChar digitValueRadix:r) isNil]) ifTrue:[ |
|
420 |
r3 := r2 * r. |
|
19078 | 421 |
^ (value * r3) + ((((digit1*r) + digit2)*r) + digit3). |
422 |
]. |
|
423 |
||
424 |
value := (value * r4) + ((((((digit1*r) + digit2)*r) + digit3)*r) + digit4). |
|
701 | 425 |
]. |
426 |
^ value |
|
427 |
||
11945 | 428 |
" |
11587 | 429 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
430 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
431 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
432 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
433 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
434 |
Integer readFrom:'gg' radix:10 onError:0 |
|
20130 | 435 |
Integer readFrom:'' radix:10 onError:'wrong' |
436 |
||
437 |
|s| |
|
438 |
s := String new:1000 withAll:$1. |
|
11654 | 439 |
Time millisecondsToRun:[ |
19078 | 440 |
1000 timesRepeat:[ |
20130 | 441 |
s asInteger |
19078 | 442 |
] |
11945 | 443 |
] |
701 | 444 |
" |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
445 |
|
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
446 |
"Modified: / 14.4.1998 / 19:16:46 / cg" |
6635 | 447 |
! |
448 |
||
6673 | 449 |
readFromRomanString:aStringOrStream |
450 |
"convert a string or stream containing a roman representation into an integer. |
|
11125 | 451 |
Raises a RomanNumberFormatError, if the inputs format is completely wrong. |
21429 | 452 |
Raises BadRomanNumberFormatError if it's wrong, but could be parsed. |
6682 | 453 |
Notifies via NaiveRomanNumberFormatNotification, if its a bit wrong (naive format). |
454 |
Will read both real and naive roman numbers (see printRomanOn: vs. printRomanOn:naive:), |
|
20378 | 455 |
however, a notification is raised for naive numbers (catch it if you are interested in it)." |
6635 | 456 |
|
11587 | 457 |
|romanValues s c val digitVal prevDigitVal countSame delta |
6635 | 458 |
stopOnSeparator finish| |
459 |
||
11587 | 460 |
romanValues := Dictionary |
19078 | 461 |
withKeys:'MDCLXVI' "/ #($M $D $C $L $X $V $I) |
462 |
andValues:#(1000 500 100 50 10 5 1). |
|
6635 | 463 |
|
464 |
(stopOnSeparator := aStringOrStream isStream) ifFalse:[ |
|
19078 | 465 |
s := aStringOrStream readStream. |
6635 | 466 |
]. |
11587 | 467 |
s atEnd ifTrue:[ |
19078 | 468 |
^ RomanNumberFormatError raiseErrorString:'empty string' |
6635 | 469 |
]. |
470 |
val := 0. |
|
471 |
prevDigitVal := 99999. |
|
472 |
countSame := 1. |
|
473 |
finish := false. |
|
474 |
||
475 |
[s atEnd or:[finish]] whileFalse:[ |
|
19078 | 476 |
c := s next asUppercase. |
477 |
c isSeparator ifTrue:[ |
|
478 |
stopOnSeparator ifFalse:[ |
|
479 |
^ RomanNumberFormatError raiseErrorString:'garbage at the end' |
|
480 |
]. |
|
481 |
finish := true. |
|
482 |
] ifFalse:[ |
|
483 |
digitVal := romanValues at:c ifAbsent:nil. |
|
484 |
digitVal isNil ifTrue:[ |
|
485 |
^ RomanNumberFormatError raiseErrorString:'invalid character' |
|
486 |
]. |
|
487 |
||
488 |
digitVal = prevDigitVal ifTrue:[ |
|
489 |
( #( 1 10 100 1000) includes:digitVal) ifFalse:[ |
|
490 |
^ RomanNumberFormatError raiseErrorString:'character may not be repeated' |
|
491 |
]. |
|
492 |
val := val + digitVal. |
|
493 |
countSame := countSame + 1. |
|
494 |
countSame >= 4 ifTrue:[ |
|
495 |
digitVal ~= 1000 ifTrue:[ |
|
496 |
countSame > 4 ifTrue:[ |
|
497 |
"/ this is a bad roman number (such as MCCCCCCCCXXXXXXII); |
|
498 |
"/ Its not correct, but sometimes encountered on buildings. |
|
499 |
"/ If you do not want to be too picky, |
|
500 |
"/ provide a proceeding handler in order to proceed the conversion. |
|
501 |
BadRomanNumberFormatError raiseRequestErrorString:'more than 4 occurrences of same character' |
|
502 |
] ifFalse:[ |
|
503 |
"/ this is a naive roman number (such as VIIII); |
|
504 |
"/ Its not correct, but very often encountered (especially as page numbers). |
|
505 |
"/ The notification below normally goes unnoticed, unless some input validator |
|
506 |
"/ wants to be very picky, and treat this as an error. |
|
507 |
"/ To do so, provide a handler for NaiveRomanNumberFormatNotification. |
|
508 |
NaiveRomanNumberFormatNotification raiseRequestErrorString:'more than 3 occurrences of same character'. |
|
509 |
] |
|
510 |
] |
|
511 |
]. |
|
512 |
] ifFalse:[ |
|
513 |
digitVal < prevDigitVal ifTrue:[ |
|
514 |
val := val + digitVal. |
|
515 |
] ifFalse:[ |
|
516 |
countSame == 1 ifFalse:[ |
|
517 |
^ RomanNumberFormatError raiseErrorString:'invalid character combination' |
|
518 |
]. |
|
519 |
delta := digitVal - prevDigitVal. |
|
520 |
( #( 4 9 40 90 400 900) includes:delta) ifFalse:[ |
|
521 |
^ RomanNumberFormatError raiseErrorString:'invalid character combination' |
|
522 |
]. |
|
523 |
val := val - prevDigitVal. |
|
524 |
val := val + delta. |
|
525 |
digitVal := prevDigitVal - 0.1. "/ trick: prevent prevDigit from arriving again. |
|
526 |
]. |
|
527 |
countSame := 1. |
|
528 |
]. |
|
529 |
prevDigitVal := digitVal. |
|
530 |
]. |
|
6635 | 531 |
]. |
532 |
"/ val > 5000 ifTrue:[ |
|
20622 | 533 |
"/ ^ RomanNumberFormatError raiseErrorString:'number out of range (1..5000)' |
11587 | 534 |
"/ ]. |
6635 | 535 |
^ val. |
536 |
||
537 |
" |
|
11587 | 538 |
Integer readFromRomanString:'I' |
539 |
Integer readFromRomanString:'II' |
|
540 |
Integer readFromRomanString:'III' |
|
541 |
Integer readFromRomanString:'IV' |
|
542 |
Integer readFromRomanString:'clix' |
|
543 |
Integer readFromRomanString:'MIX' |
|
544 |
Integer readFromRomanString:'MCMXCIX' |
|
6673 | 545 |
|
6682 | 546 |
Naive cases (which are accepted): |
11587 | 547 |
Integer readFromRomanString:'IIII' |
548 |
Integer readFromRomanString:'VIIII' |
|
549 |
Integer readFromRomanString:'CLXXXXVIIII' |
|
6682 | 550 |
|
551 |
Error case (not proceedable): |
|
11587 | 552 |
Integer readFromRomanString:'LC' |
6682 | 553 |
|
554 |
Error case (proceedable): |
|
11587 | 555 |
Integer readFromRomanString:'MCCCCCCCCXXXXXXIIIIII' |
6682 | 556 |
|
557 |
BadRomanNumberFormatError ignoreIn:[ |
|
19078 | 558 |
Integer readFromRomanString:'MCCCCCCCCXXXXXXIIIIII' |
11587 | 559 |
] |
6682 | 560 |
" |
561 |
||
562 |
||
563 |
"naive cases: |
|
564 |
#( |
|
19078 | 565 |
'MCMXCIX' 1999 |
566 |
'MCMXCVIIII' 1999 |
|
567 |
'MCMLXXXXIX' 1999 |
|
568 |
'MDCCCCXCIX' 1999 |
|
569 |
'MDCCCCXCVIIII' 1999 |
|
570 |
'MDCCCCLXXXXIX' 1999 |
|
571 |
'MDCCCCLXXXXVIIII' 1999 |
|
6682 | 572 |
) pairWiseDo:[:goodString :expectedValue | |
19078 | 573 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6682 | 574 |
] |
6635 | 575 |
" |
576 |
||
6673 | 577 |
|
6635 | 578 |
"error cases: |
11587 | 579 |
#( |
19078 | 580 |
'XIIX' |
581 |
'VV' |
|
582 |
'VVV' |
|
583 |
'XXL' |
|
584 |
'XLX' |
|
585 |
'LC' |
|
586 |
'LL' |
|
587 |
'DD' |
|
6635 | 588 |
) do:[:badString | |
19078 | 589 |
(Integer readFromRomanString:badString onError:nil) notNil ifTrue:[self halt]. |
6635 | 590 |
] |
591 |
" |
|
592 |
||
593 |
"good cases: |
|
594 |
#( 'I' 1 |
|
19078 | 595 |
'II' 2 |
596 |
'III' 3 |
|
597 |
'IV' 4 |
|
598 |
'V' 5 |
|
599 |
'VI' 6 |
|
600 |
'VII' 7 |
|
601 |
'VIII' 8 |
|
602 |
'IX' 9 |
|
603 |
'X' 10 |
|
604 |
'XI' 11 |
|
605 |
'XII' 12 |
|
606 |
'XIII' 13 |
|
607 |
'XIV' 14 |
|
608 |
'XV' 15 |
|
609 |
'XVI' 16 |
|
610 |
'XVII' 17 |
|
611 |
'XVIII' 18 |
|
612 |
'XIX' 19 |
|
613 |
'XX' 20 |
|
614 |
'XXX' 30 |
|
615 |
'L' 50 |
|
616 |
'XL' 40 |
|
617 |
'LX' 60 |
|
618 |
'LXX' 70 |
|
619 |
'LXXX' 80 |
|
620 |
'CXL' 140 |
|
621 |
'CL' 150 |
|
622 |
'CLX' 160 |
|
623 |
'MMM' 3000 |
|
624 |
'MMMM' 4000 |
|
625 |
'MMMMCMXCIX' 4999 |
|
626 |
'MMMMMMMMMCMXCIX' 9999 |
|
6635 | 627 |
) pairWiseDo:[:goodString :expectedValue | |
19078 | 628 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6635 | 629 |
] |
630 |
" |
|
631 |
||
632 |
" |
|
633 |
1 to:9999 do:[:n | |
|
19078 | 634 |
|romanString| |
635 |
||
636 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
637 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6673 | 638 |
] |
639 |
" |
|
21429 | 640 |
|
641 |
"Modified (comment): / 13-02-2017 / 20:23:02 / cg" |
|
6673 | 642 |
! |
643 |
||
644 |
readFromRomanString:aStringOrStream onError:exceptionalValue |
|
645 |
"convert a string or stream containing a roman representation into an integer. |
|
11587 | 646 |
Raises an exception, if the inputs format is wrong. |
647 |
Does allow reading of naive (more than 3 in a row) and |
|
6682 | 648 |
bad (not using L and D) roman numbers. |
649 |
(Such numbers can be seen on some medevial buildings. " |
|
6673 | 650 |
|
21386 | 651 |
^ RomanNumberFormatError |
652 |
handle:[:ex | exceptionalValue value ] |
|
653 |
do:[ self readFromRomanString:aStringOrStream ]. |
|
6673 | 654 |
|
655 |
||
656 |
" |
|
11587 | 657 |
Integer readFromRomanString:'I' onError:nil |
658 |
Integer readFromRomanString:'II' onError:nil |
|
659 |
Integer readFromRomanString:'III' onError:nil |
|
660 |
Integer readFromRomanString:'IV' onError:nil |
|
661 |
Integer readFromRomanString:'clix' onError:nil |
|
662 |
Integer readFromRomanString:'MCMXCIX' onError:nil |
|
6673 | 663 |
|
6682 | 664 |
Naive cases (which are accepted): |
11587 | 665 |
Integer readFromRomanString:'IIII' onError:nil |
666 |
Integer readFromRomanString:'VIIII' onError:nil |
|
667 |
Integer readFromRomanString:'CLXXXXVIIII' onError:nil |
|
6682 | 668 |
|
669 |
Error cases: |
|
21386 | 670 |
Integer readFromRomanString:'LC' onError:nil |
6673 | 671 |
" |
672 |
||
673 |
"error cases: |
|
11587 | 674 |
#( |
19078 | 675 |
'XIIX' |
676 |
'VV' |
|
677 |
'VVV' |
|
678 |
'XXL' |
|
679 |
'XLX' |
|
680 |
'LC' |
|
681 |
'LL' |
|
682 |
'DD' |
|
6673 | 683 |
) do:[:badString | |
19078 | 684 |
(Integer readFromRomanString:badString onError:nil) notNil ifTrue:[self halt]. |
6673 | 685 |
] |
686 |
" |
|
687 |
||
6682 | 688 |
"naive (but handled) cases: |
11587 | 689 |
#( |
19078 | 690 |
'IIII' 4 |
691 |
'VIIII' 9 |
|
692 |
'XIIII' 14 |
|
693 |
'XVIIII' 19 |
|
6682 | 694 |
) pairWiseDo:[:goodString :expectedValue | |
19078 | 695 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6682 | 696 |
] |
697 |
" |
|
698 |
||
6673 | 699 |
"good cases: |
700 |
#( 'I' 1 |
|
19078 | 701 |
'II' 2 |
702 |
'III' 3 |
|
703 |
'IV' 4 |
|
704 |
'V' 5 |
|
705 |
'VI' 6 |
|
706 |
'VII' 7 |
|
707 |
'VIII' 8 |
|
708 |
'IX' 9 |
|
709 |
'X' 10 |
|
710 |
'XI' 11 |
|
711 |
'XII' 12 |
|
712 |
'XIII' 13 |
|
713 |
'XIV' 14 |
|
714 |
'XV' 15 |
|
715 |
'XVI' 16 |
|
716 |
'XVII' 17 |
|
717 |
'XVIII' 18 |
|
718 |
'XIX' 19 |
|
719 |
'XX' 20 |
|
720 |
'XXX' 30 |
|
721 |
'L' 50 |
|
722 |
'XL' 40 |
|
723 |
'LX' 60 |
|
724 |
'LXX' 70 |
|
725 |
'LXXX' 80 |
|
726 |
'CXL' 140 |
|
727 |
'CL' 150 |
|
728 |
'CLX' 160 |
|
729 |
'MMM' 3000 |
|
730 |
'MMMM' 4000 |
|
731 |
'MMMMCMXCIX' 4999 |
|
732 |
'MMMMMMMMMCMXCIX' 9999 |
|
6673 | 733 |
) pairWiseDo:[:goodString :expectedValue | |
19078 | 734 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6673 | 735 |
] |
736 |
" |
|
737 |
||
738 |
" |
|
739 |
1 to:9999 do:[:n | |
|
19078 | 740 |
|romanString| |
741 |
||
742 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
743 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6673 | 744 |
] |
745 |
" |
|
746 |
||
747 |
"reading naive numbers: |
|
748 |
||
749 |
1 to:9999 do:[:n | |
|
19078 | 750 |
|romanString| |
751 |
||
752 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:true]. |
|
753 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6635 | 754 |
] |
755 |
" |
|
21386 | 756 |
|
757 |
"Modified (comment): / 08-02-2017 / 19:07:00 / stefan" |
|
8897
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
758 |
! |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
759 |
|
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
760 |
readFromString:aString radix:base onError:exceptionBlock |
20329 | 761 |
"return the next UNSIGNED Integer from the (character-)aString in radix; |
762 |
(assumes that the initial XXr has already been read). |
|
763 |
No whitespace-skipping is done. |
|
764 |
Expects that NO garbage is at the end of the string. |
|
765 |
Returns the value from exceptionBlock, if no valid integer is in the string." |
|
766 |
||
8897
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
767 |
|str val| |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
768 |
|
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
769 |
str := ReadStream on:aString. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
770 |
val := self readFrom:str radix:base onError:[^ exceptionBlock value]. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
771 |
str atEnd ifFalse:[ ^ exceptionBlock value]. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
772 |
^ val |
20329 | 773 |
|
774 |
" |
|
775 |
Integer readFromString:'1234' radix:10 onError:[nil] |
|
776 |
Integer readFromString:'-1234' radix:10 onError:[nil] - I only read unsigned numbers |
|
777 |
Integer readFromString:' 1234' radix:10 onError:[nil] - I do not skip whitespace |
|
778 |
Integer readFromString:'1234 ' radix:10 onError:[nil] - I do not accept anything after the number |
|
779 |
||
780 |
" |
|
701 | 781 |
! ! |
782 |
||
9141 | 783 |
!Integer class methodsFor:'Compatibility-Squeak'! |
784 |
||
785 |
readFrom:aStringOrStream base:aBase |
|
786 |
"for squeak compatibility" |
|
787 |
||
788 |
^ self readFrom:aStringOrStream radix:aBase |
|
789 |
! ! |
|
790 |
||
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
791 |
!Integer class methodsFor:'Signal constants'! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
792 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
793 |
bcdConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
794 |
"return the signal which is raised when bcd conversion fails |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
795 |
(i.e. when trying to decode an invalid BCD number)" |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
796 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
797 |
^ BCDConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
798 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
799 |
"Modified: / 15.11.1999 / 20:35:20 / cg" |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
800 |
! ! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
801 |
|
7444 | 802 |
!Integer class methodsFor:'class initialization'! |
803 |
||
804 |
initialize |
|
805 |
BCDConversionErrorSignal isNil ifTrue:[ |
|
19078 | 806 |
BCDConversionErrorSignal := ConversionError newSignal. |
807 |
BCDConversionErrorSignal nameClass:self message:#bcdConversionErrorSignal. |
|
808 |
BCDConversionErrorSignal notifierString:'bcd conversion error'. |
|
7444 | 809 |
]. |
810 |
||
811 |
"Modified: / 15.11.1999 / 20:36:04 / cg" |
|
812 |
! ! |
|
813 |
||
21822 | 814 |
!Integer class methodsFor:'coercing & converting'! |
815 |
||
816 |
coerce:aNumber |
|
817 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
818 |
||
819 |
^ aNumber asInteger |
|
820 |
! ! |
|
821 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
822 |
!Integer class methodsFor:'constants'! |
701 | 823 |
|
7727 | 824 |
epsilon |
21903 | 825 |
"return the maximum relative spacing of instances of mySelf |
826 |
(i.e. the value-delta of the least significant bit)" |
|
7727 | 827 |
|
828 |
"don't know, what to really return here. |
|
8635 | 829 |
Returning 1 gives stupid values when doing some taylor series approximations |
830 |
(although it is correct)" |
|
7727 | 831 |
|
832 |
^ Float epsilon. |
|
8635 | 833 |
|
834 |
" |
|
11587 | 835 |
2 sqrt_withAccuracy:(Integer epsilon) |
836 |
2 sqrt_withAccuracy:1 |
|
8635 | 837 |
" |
21903 | 838 |
|
839 |
"Modified (comment): / 22-06-2017 / 13:44:17 / cg" |
|
7727 | 840 |
! |
841 |
||
701 | 842 |
unity |
1555 | 843 |
"return the neutral element for multiplication (1)" |
701 | 844 |
|
845 |
^ 1 |
|
1555 | 846 |
|
847 |
"Modified: 18.7.1996 / 12:26:43 / cg" |
|
701 | 848 |
! |
849 |
||
850 |
zero |
|
1555 | 851 |
"return the neutral element for addition (0)" |
701 | 852 |
|
853 |
^ 0 |
|
1555 | 854 |
|
855 |
"Modified: 18.7.1996 / 12:26:38 / cg" |
|
92 | 856 |
! ! |
857 |
||
22600 | 858 |
|
9125 | 859 |
!Integer class methodsFor:'prime numbers'! |
860 |
||
11520 | 861 |
flushPrimeCache |
18865 | 862 |
"cleanup after using a primeCache. |
863 |
See comment in initializePrimeCacheUpTo:limit" |
|
11520 | 864 |
|
865 |
PrimeCache := nil. |
|
866 |
||
867 |
" |
|
868 |
Integer initializePrimeCacheUpTo:1000000 |
|
869 |
Integer flushPrimeCache. |
|
870 |
" |
|
871 |
! |
|
872 |
||
873 |
initializePrimeCacheUpTo:limit |
|
11582 | 874 |
"if many operations are to be done using primes, we can keep them around... |
875 |
You will need n/8/2 bytes to keep fast info about primes up to n |
|
876 |
(i.e. 100Mb is good for primes up to 1.6*10^9)" |
|
11520 | 877 |
|
878 |
|bits| |
|
879 |
||
11563
a91c8d348760
could have returned an uncompressed largeint after bitShift
Claus Gittinger <cg@exept.de>
parents:
11521
diff
changeset
|
880 |
PrimeCache := nil. |
11582 | 881 |
bits := BooleanArray new:limit//2. |
11520 | 882 |
self primesUpTo:limit do:[:p | |
19078 | 883 |
bits at:p//2 put:true |
11520 | 884 |
]. |
885 |
PrimeCache := bits. |
|
886 |
||
887 |
" |
|
11521 | 888 |
Integer initializePrimeCacheUpTo:1000000. |
11654 | 889 |
Integer initializePrimeCacheUpTo:10000000. |
890 |
Integer initializePrimeCacheUpTo:100000000. |
|
11563
a91c8d348760
could have returned an uncompressed largeint after bitShift
Claus Gittinger <cg@exept.de>
parents:
11521
diff
changeset
|
891 |
Integer initializePrimeCacheUpTo:1000000000. |
11521 | 892 |
Integer flushPrimeCache. |
893 |
" |
|
894 |
||
895 |
" |
|
896 |
Integer flushPrimeCache. |
|
897 |
Transcript showCR:( |
|
19078 | 898 |
Time millisecondsToRun:[ 1 to:100000 do:[:n | n isPrime] ] |
11521 | 899 |
). |
900 |
Integer initializePrimeCacheUpTo:100000. |
|
901 |
Transcript showCR:( |
|
19078 | 902 |
Time millisecondsToRun:[ 1 to:100000 do:[:n | n isPrime] ] |
11521 | 903 |
). |
11520 | 904 |
Integer flushPrimeCache. |
905 |
" |
|
906 |
! |
|
907 |
||
9125 | 908 |
largePrimesUpTo: max do: aBlock |
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
909 |
"Evaluate aBlock with all primes up and including maxValue. |
9125 | 910 |
The Algorithm is adapted from http://www.rsok.com/~jrm/printprimes.html |
11587 | 911 |
It encodes prime numbers much more compactly than #primesUpTo: |
9125 | 912 |
38.5 integer per byte (2310 numbers per 60 byte) allow for some fun large primes. |
913 |
(all primes up to SmallInteger maxVal can be computed within ~27MB of memory; |
|
914 |
the regular #primesUpTo: would require 4 *GIGA*bytes). |
|
915 |
Note: The algorithm could be re-written to produce the first primes (which require |
|
916 |
the longest time to sieve) faster but only at the cost of clarity." |
|
917 |
||
918 |
| limit flags maskBitIndex bitIndex maskBit byteIndex index primesUpTo2310 indexLimit | |
|
919 |
||
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
920 |
limit := max asInteger. |
9125 | 921 |
indexLimit := max sqrt truncated + 1. |
922 |
"Create the array of flags." |
|
923 |
flags := ByteArray new: (limit + 2309) // 2310 * 60 + 60. |
|
924 |
flags atAllPut: 16rFF. "set all to true" |
|
925 |
||
926 |
"Compute the primes up to 2310" |
|
927 |
primesUpTo2310 := self primesUpTo: 2310. |
|
928 |
||
929 |
"Create a mapping from 2310 integers to 480 bits (60 byte)" |
|
930 |
maskBitIndex := Array new: 2310. |
|
931 |
bitIndex := -1. "for pre-increment" |
|
932 |
maskBitIndex at: 1 put: (bitIndex := bitIndex + 1). |
|
933 |
maskBitIndex at: 2 put: (bitIndex := bitIndex + 1). |
|
934 |
||
935 |
1 to: 5 do:[:i| aBlock value: (primesUpTo2310 at: i)]. |
|
936 |
||
937 |
index := 6. |
|
938 |
2 to: 2309 do:[:n| |
|
19078 | 939 |
[(primesUpTo2310 at: index) < n] |
940 |
whileTrue:[index := index + 1]. |
|
941 |
n = (primesUpTo2310 at: index) ifTrue:[ |
|
942 |
maskBitIndex at: n+1 put: (bitIndex := bitIndex + 1). |
|
943 |
] ifFalse:[ |
|
944 |
"if modulo any of the prime factors of 2310, then could not be prime" |
|
945 |
(n \\ 2 = 0 or:[n \\ 3 = 0 or:[n \\ 5 = 0 or:[n \\ 7 = 0 or:[n \\ 11 = 0]]]]) |
|
946 |
ifTrue:[maskBitIndex at: n+1 put: 0] |
|
947 |
ifFalse:[maskBitIndex at: n+1 put: (bitIndex := bitIndex + 1)]. |
|
948 |
]. |
|
9125 | 949 |
]. |
950 |
||
951 |
"Now the real work begins... |
|
952 |
Start with 13 since multiples of 2,3,5,7,11 are handled by the storage method; |
|
953 |
increment by 2 for odd numbers only." |
|
954 |
13 to: limit by: 2 do:[:n| |
|
19078 | 955 |
(maskBit := maskBitIndex at: (n \\ 2310 + 1)) = 0 ifFalse:["not a multiple of 2,3,5,7,11" |
956 |
byteIndex := n // 2310 * 60 + (maskBit-1 bitShift: -3) + 1. |
|
957 |
bitIndex := 1 bitShift: (maskBit bitAnd: 7). |
|
958 |
((flags at: byteIndex) bitAnd: bitIndex) = 0 ifFalse:["not marked -- n is prime" |
|
959 |
aBlock value: n. |
|
960 |
"Start with n*n since any integer < n has already been sieved |
|
961 |
(e.g., any multiple of n with a number k < n has been cleared |
|
962 |
when k was sieved); add 2 * i to avoid even numbers and |
|
963 |
mark all multiples of this prime. Note: n < indexLimit below |
|
964 |
limits running into LargeInts -- nothing more." |
|
965 |
n < indexLimit ifTrue:[ |
|
966 |
index := n * n. |
|
967 |
(index bitAnd: 1) = 0 ifTrue:[index := index + n]. |
|
968 |
[index <= limit] whileTrue:[ |
|
969 |
(maskBit := maskBitIndex at: (index \\ 2310 + 1)) = 0 ifFalse:[ |
|
970 |
byteIndex := (index // 2310 * 60) + (maskBit-1 bitShift: -3) + 1. |
|
971 |
maskBit := 255 - (1 bitShift: (maskBit bitAnd: 7)). |
|
972 |
flags at: byteIndex put: ((flags at: byteIndex) bitAnd: maskBit). |
|
973 |
]. |
|
974 |
index := index + (2 * n)]. |
|
975 |
]. |
|
976 |
]. |
|
977 |
]. |
|
9125 | 978 |
]. |
979 |
||
980 |
" |
|
981 |
Integer largePrimesUpTo:1000000 do:[:i | i > 900000 ifTrue:[self halt] ] |
|
982 |
(Integer primesUpTo:1000000) inspect |
|
983 |
" |
|
984 |
! |
|
985 |
||
11582 | 986 |
primeCacheSize |
18865 | 987 |
"see comment in initializePrimeCacheUpTo:limit" |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
988 |
|
11582 | 989 |
^ PrimeCache size * 2 |
990 |
||
991 |
" |
|
992 |
Integer initializePrimeCacheUpTo:1000. |
|
993 |
Integer initializePrimeCacheUpTo:1000000. |
|
994 |
Integer initializePrimeCacheUpTo:1000000000. |
|
995 |
Integer flushPrimeCache. |
|
996 |
" |
|
997 |
||
998 |
" |
|
999 |
Integer flushPrimeCache. |
|
1000 |
Transcript showCR:( |
|
19078 | 1001 |
Time millisecondsToRun:[ 1 to:100000 do:[:n | n isPrime] ] |
11582 | 1002 |
). |
1003 |
Integer initializePrimeCacheUpTo:100000. |
|
1004 |
Transcript showCR:( |
|
19078 | 1005 |
Time millisecondsToRun:[ 1 to:100000 do:[:n | n isPrime] ] |
11582 | 1006 |
). |
1007 |
Integer flushPrimeCache. |
|
1008 |
" |
|
1009 |
! |
|
1010 |
||
19128 | 1011 |
primesUpTo5000 |
1012 |
"return a table of primes up to 5000. |
|
1013 |
Primes are heavily used to compute good container sizes in Set and Dictionary, |
|
1014 |
and in some cryprographic algorithms." |
|
11520 | 1015 |
|
1016 |
^ #( |
|
19078 | 1017 |
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 |
1018 |
73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 |
|
1019 |
173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 |
|
1020 |
277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 |
|
1021 |
397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 |
|
1022 |
509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 |
|
1023 |
641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 |
|
1024 |
761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 |
|
1025 |
887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 |
|
1026 |
||
1027 |
1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 |
|
1028 |
1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 |
|
1029 |
1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 |
|
1030 |
1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 |
|
1031 |
1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 |
|
1032 |
1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 |
|
1033 |
1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 |
|
1034 |
1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 |
|
1035 |
1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 |
|
19128 | 1036 |
|
1037 |
2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 |
|
1038 |
2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 |
|
1039 |
2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 |
|
1040 |
2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 |
|
1041 |
2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 |
|
1042 |
2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 |
|
1043 |
2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 |
|
1044 |
2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 |
|
1045 |
2939 2953 2957 2963 2969 2971 2999 |
|
1046 |
||
1047 |
3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 |
|
1048 |
3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 |
|
1049 |
3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 |
|
1050 |
3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 |
|
1051 |
3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 |
|
1052 |
3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 |
|
1053 |
3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 |
|
1054 |
3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 |
|
1055 |
||
1056 |
4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 |
|
1057 |
4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 |
|
1058 |
4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 |
|
1059 |
4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 |
|
1060 |
4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 |
|
1061 |
4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 |
|
1062 |
4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 |
|
1063 |
4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973 4987 4993 4999 |
|
19078 | 1064 |
). |
11520 | 1065 |
! |
1066 |
||
9125 | 1067 |
primesUpTo: max |
15578 | 1068 |
"Return a list of prime integers up to and including the given integer." |
9125 | 1069 |
|
19231 | 1070 |
|cls| |
1071 |
||
20646 | 1072 |
"/ sigh: IntegerArray is in libbasic2... |
1073 |
(IntegerArray notNil |
|
1074 |
and:[ max < IntegerArray maxVal]) ifTrue:[ |
|
19231 | 1075 |
cls := IntegerArray. |
1076 |
] ifFalse:[ |
|
1077 |
cls := Array. |
|
1078 |
]. |
|
1079 |
||
1080 |
^ cls streamContents:[:s| self primesUpTo: max do:[:prime| s nextPut: prime]] |
|
9125 | 1081 |
|
1082 |
" |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1083 |
Integer primesUpTo: 100 |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1084 |
Integer primesUpTo: 13 |
11587 | 1085 |
(Integer primesUpTo: 100) select:[:p | p between:10 and:99] |
9125 | 1086 |
" |
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1087 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1088 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1089 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1090 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1091 |
N := 1000. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1092 |
p := 1. |
11587 | 1093 |
a := (1 to:1000) |
19078 | 1094 |
collect:[:i | p := p nextPrime. p ] |
1095 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1096 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1097 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1098 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1099 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1100 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1101 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1102 |
N := 1000 nextPrime. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1103 |
p := 1. |
11587 | 1104 |
a := (1 to:1000) |
19078 | 1105 |
collect:[:i | p := p nextPrime. p ] |
1106 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1107 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1108 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1109 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1110 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1111 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1112 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1113 |
N := 1000 nextPrime-1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1114 |
p := 1. |
11587 | 1115 |
a := (1 to:1000) |
19078 | 1116 |
collect:[:i | p := p nextPrime. p ] |
1117 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1118 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1119 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1120 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1121 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1122 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1123 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1124 |
N := 100000. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1125 |
p := 1. |
11587 | 1126 |
a := (1 to:N) |
19078 | 1127 |
collect:[:i | p := p nextPrime. p ] |
1128 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1129 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1130 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1131 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1132 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1133 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1134 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1135 |
N := 100000 nextPrime. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1136 |
p := 1. |
11587 | 1137 |
a := (1 to:N) |
19078 | 1138 |
collect:[:i | p := p nextPrime. p ] |
1139 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1140 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1141 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1142 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1143 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1144 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1145 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1146 |
N := 100000 nextPrime-1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1147 |
p := 1. |
11587 | 1148 |
a := (1 to:N) |
19078 | 1149 |
collect:[:i | p := p nextPrime. p ] |
1150 |
thenSelect:[:p | p <= N]. |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1151 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1152 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1153 |
" |
9125 | 1154 |
! |
1155 |
||
1156 |
primesUpTo: max do: aBlock |
|
18865 | 1157 |
"Compute aBlock with all prime integers up to and including the given integer. |
1158 |
See comment in initializePrimeCacheUpTo:limit" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1159 |
|
19128 | 1160 |
| limit |
19131 | 1161 |
iLimit "{ Class: SmallInteger }" |
19128 | 1162 |
flags |
19131 | 1163 |
prime "{ Class: SmallInteger }" |
1164 |
k "{ Class: SmallInteger }" |
|
19128 | 1165 |
wellKnownPrimes| |
1166 |
||
1167 |
wellKnownPrimes := self primesUpTo5000. |
|
1168 |
max <= (wellKnownPrimes last+1) ifTrue:[ |
|
1169 |
wellKnownPrimes do:[:p | |
|
19078 | 1170 |
p > max ifTrue:[^ self]. |
1171 |
aBlock value:p. |
|
1172 |
]. |
|
1173 |
^ self. |
|
11521 | 1174 |
]. |
19128 | 1175 |
|
11582 | 1176 |
max <= self primeCacheSize ifTrue:[ |
19078 | 1177 |
aBlock value:2. |
1178 |
3 to:max by:2 do:[:p | |
|
1179 |
(PrimeCache at:p//2) ifTrue:[ |
|
1180 |
aBlock value:p |
|
1181 |
]. |
|
1182 |
]. |
|
1183 |
^ self. |
|
11520 | 1184 |
]. |
1185 |
||
9125 | 1186 |
limit := max asInteger - 1. |
11587 | 1187 |
"Fall back into #largePrimesUpTo:do: if we'd require more than 100k of memory; |
9125 | 1188 |
the alternative will only requre 1/154th of the amount we need here and is almost as fast." |
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
1189 |
limit > 25000 ifTrue:[^ self largePrimesUpTo: max do: aBlock]. |
11492 | 1190 |
|
19128 | 1191 |
iLimit := limit. |
1192 |
||
11492 | 1193 |
"/ sieve, on the fly |
19128 | 1194 |
flags := ByteArray new: iLimit. |
1195 |
aBlock value: 2. |
|
1196 |
||
1197 |
2 to: iLimit by:2 do: [:i | |
|
1198 |
(flags at: i) == 0 ifTrue: [ |
|
19078 | 1199 |
prime := i + 1. |
1200 |
k := i + prime. |
|
19128 | 1201 |
[k <= iLimit] whileTrue: [ |
1202 |
flags at: k put: 1. |
|
19078 | 1203 |
k := k + prime |
1204 |
]. |
|
1205 |
aBlock value: prime |
|
1206 |
] |
|
9125 | 1207 |
]. |
1208 |
||
1209 |
" |
|
1210 |
Integer primesUpTo: 100 |
|
19128 | 1211 |
Integer primesUpTo:20000 do:[:p | ] |
9125 | 1212 |
" |
1213 |
! ! |
|
1214 |
||
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1215 |
!Integer class methodsFor:'queries'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1216 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1217 |
hasSharedInstances |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1218 |
"return true if this class has shared instances, that is, instances |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1219 |
with the same value are identical. |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1220 |
Although not always shared (LargeIntegers), these should be treated |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1221 |
so, to be independent of the number of bits in a SmallInt" |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1222 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1223 |
^ true |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1224 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1225 |
|
8892 | 1226 |
! |
1227 |
||
1228 |
isAbstract |
|
11223 | 1229 |
"Return if this class is an abstract class. |
1230 |
True is returned for Integer here; false for subclasses. |
|
19455 | 1231 |
Abstract subclasses must redefine this again." |
11223 | 1232 |
|
8892 | 1233 |
^ self == Integer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1234 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1235 |
|
22600 | 1236 |
|
7261 | 1237 |
!Integer methodsFor:'Compatibility-Dolphin'! |
6249 | 1238 |
|
1239 |
highWord |
|
1240 |
"return the high 16 bits of a 32 bit value" |
|
1241 |
||
1242 |
^ self bitShift:-16 |
|
1243 |
||
1244 |
" |
|
11587 | 1245 |
(16r12345678 highWord) hexPrintString |
1246 |
(16r12345678 lowWord) hexPrintString |
|
6249 | 1247 |
" |
1248 |
! |
|
1249 |
||
1250 |
lowWord |
|
1251 |
"return the low 16 bits of a 32 bit value" |
|
1252 |
||
1253 |
^ self bitAnd:16rFFFF |
|
1254 |
||
1255 |
" |
|
11587 | 1256 |
(16r12345678 lowWord) hexPrintString |
1257 |
(16r12345678 highWord) hexPrintString |
|
6249 | 1258 |
" |
1259 |
! |
|
1260 |
||
1261 |
mask:integerMask set:aBoolean |
|
1262 |
"Answer the result of setting/resetting the specified mask in the receiver." |
|
1263 |
||
11587 | 1264 |
^ aBoolean |
19078 | 1265 |
ifTrue: [self bitOr:integerMask] |
1266 |
ifFalse: [self bitClear:integerMask] |
|
6249 | 1267 |
|
1268 |
"turn on the 1-bit: |
|
19078 | 1269 |
|v| |
1270 |
||
1271 |
v := 2r0100. |
|
1272 |
v mask:1 set:true |
|
6249 | 1273 |
|
1274 |
turn off the 1-bit: |
|
19078 | 1275 |
|v| |
1276 |
||
1277 |
v := 2r0101. |
|
1278 |
v mask:1 set:false |
|
6249 | 1279 |
" |
6875 | 1280 |
! |
1281 |
||
17248 | 1282 |
maskClear:aMaskInteger |
1283 |
"return an integer with all bits cleared which are set in aMaskInteger. |
|
1284 |
An alias for bitClear: for compatibility." |
|
1285 |
||
1286 |
^ self bitClear:aMaskInteger |
|
1287 |
||
1288 |
" |
|
1289 |
3 maskClear:1 |
|
1290 |
" |
|
1291 |
! |
|
1292 |
||
1293 |
maskSet:aMaskInteger |
|
1294 |
"return an integer with all bits set which are set in aMaskInteger. |
|
1295 |
An alias for bitSet: for compatibility." |
|
1296 |
||
1297 |
^ self bitOr:aMaskInteger |
|
1298 |
! |
|
1299 |
||
6875 | 1300 |
printStringRadix:aRadix padTo:sz |
11936 | 1301 |
"return a printed representation of the receiver in a given radix, |
15765 | 1302 |
padded with zeros (at the left) up to size. |
11936 | 1303 |
If the printString is longer than size, |
1304 |
it is returned unchanged (i.e. not truncated). |
|
1305 |
See also printStringRadix:size:fill:" |
|
1306 |
||
6875 | 1307 |
^ self printStringRadix:aRadix size:sz fill:$0 |
1308 |
||
1309 |
" |
|
11945 | 1310 |
1024 printStringRadix:16 padTo:4 |
1311 |
16rABCD printStringRadix:16 padTo:3 |
|
1312 |
1024 printStringRadix:2 padTo:16 |
|
1313 |
1024 printStringRadix:16 padTo:8 |
|
6875 | 1314 |
" |
6907 | 1315 |
! |
1316 |
||
1317 |
| aNumber |
|
11936 | 1318 |
"return the bitwise-or of the receiver and the argument, anInteger. |
14448 | 1319 |
Same as bitOr: - added for compatibility with Dolphin Smalltalk. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1320 |
Notice: |
19093 | 1321 |
PLEASE DO NOT USE | for integers in new code; it makes the code harder |
1322 |
to understand, as it may be not obvious, whether a boolean-or a bitWise-or is intended. |
|
1323 |
For integers, use bitOr: to make the intention explicit. |
|
19094 | 1324 |
Also, consider using or: for booleans, which is does not evaluate the right part if the left is true." |
11936 | 1325 |
|
6907 | 1326 |
^ self bitOr:aNumber |
1327 |
||
1328 |
" |
|
11587 | 1329 |
14 | 1 |
1330 |
9 & 8 |
|
6907 | 1331 |
" |
6249 | 1332 |
! ! |
1333 |
||
7261 | 1334 |
!Integer methodsFor:'Compatibility-Squeak'! |
4867 | 1335 |
|
12122 | 1336 |
asByteArray |
15937 | 1337 |
"return my hexBytes in MSB. |
1338 |
Do not use: |
|
19078 | 1339 |
This is a very stupid squeak-compatibility method, |
21993 | 1340 |
as normally, you'd expect the bytes to be ordered in the machine's native order" |
12147 | 1341 |
|
15567 | 1342 |
^ self digitBytesMSB |
21993 | 1343 |
|
1344 |
"Modified (comment): / 11-07-2017 / 16:13:03 / mawalch" |
|
12122 | 1345 |
! |
1346 |
||
12147 | 1347 |
asByteArrayOfSize:size |
1348 |
"return my hexBytes in MSB, optionally padded at the left with zeros" |
|
1349 |
||
12122 | 1350 |
"((( |
19078 | 1351 |
| repeats number | |
1352 |
repeats := 1000000. |
|
1353 |
number := 123456789123456789123456789123456789123456789123456789. |
|
1354 |
[repeats timesRepeat: (number asByteArrayOfSize: 1024) ] timeToRun. |
|
12122 | 1355 |
)))" |
1356 |
||
15567 | 1357 |
| bytes bytesSize| |
1358 |
||
1359 |
bytes := self digitBytesMSB. |
|
1360 |
bytesSize := bytes size. |
|
1361 |
size < bytesSize ifTrue: [ |
|
19078 | 1362 |
^ ConversionError raiseRequestWith:self errorString:'number too big for ', size asString |
14796 | 1363 |
]. |
15567 | 1364 |
^ (ByteArray new:size) |
19078 | 1365 |
replaceFrom:size-bytesSize+1 to:size with:bytes startingAt:1. |
12147 | 1366 |
|
1367 |
" |
|
1368 |
123 asByteArrayOfSize:1 #[123] |
|
1369 |
123 asByteArrayOfSize:2 #[0 123] |
|
1370 |
123 asByteArrayOfSize:4 #[0 0 0 123] |
|
1371 |
||
1372 |
255 asByteArrayOfSize:1 #[255] |
|
1373 |
||
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1374 |
256 asByteArrayOfSize:1 |
12147 | 1375 |
256 asByteArrayOfSize:2 |
1376 |
256 asByteArrayOfSize:4 |
|
22567 | 1377 |
256 asByteArrayOfSize:8 |
12147 | 1378 |
" |
12122 | 1379 |
! |
1380 |
||
4867 | 1381 |
atRandom |
1382 |
"return a random number between 1 amd myself" |
|
1383 |
||
8573 | 1384 |
^ self atRandom:Random. |
8572 | 1385 |
|
1386 |
" |
|
1387 |
100 atRandom |
|
1388 |
1000 atRandom |
|
1389 |
" |
|
4867 | 1390 |
! |
4376 | 1391 |
|
8573 | 1392 |
atRandom:aRandomGenerator |
15456 | 1393 |
"return a random number between 1 and myself" |
8573 | 1394 |
|
1395 |
self < 1 ifTrue:[^ self]. |
|
1396 |
^ aRandomGenerator nextIntegerBetween:1 and:self |
|
1397 |
||
1398 |
" |
|
1399 |
100 atRandom:(Random new) |
|
1400 |
1000 atRandom:(Random new) |
|
1401 |
" |
|
1402 |
! |
|
1403 |
||
22600 | 1404 |
bitShiftMagnitude:shift |
1405 |
^ self bitShift:shift. |
|
1406 |
||
1407 |
" |
|
1408 |
-1 bitShiftMagnitude:1 |
|
1409 |
-2 bitShift:-1 |
|
1410 |
-2 bitShift:-1 |
|
1411 |
" |
|
1412 |
! |
|
1413 |
||
14385
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1414 |
printLeftPaddedWith:padChar to:size base:base |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1415 |
"prints left-padded" |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1416 |
|
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1417 |
^ (self printStringRadix:base) leftPaddedTo:size with:padChar |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1418 |
|
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1419 |
" |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1420 |
1234 printPaddedWith:$0 to:4 base:16 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1421 |
1234 printLeftPaddedWith:$0 to:4 base:16 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
1422 |
128 printLeftPaddedWith:$0 to:2 base:16 |
14385
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1423 |
" |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1424 |
! |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1425 |
|
11858 | 1426 |
printPaddedWith:padChar to:size base:base |
14385
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1427 |
"attention: prints right-padded; see printLeftPadded." |
1bb6b27c59f9
added: #printLeftPaddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
14294
diff
changeset
|
1428 |
|
11858 | 1429 |
^ (self printStringRadix:base) paddedTo:size with:padChar |
1430 |
||
1431 |
" |
|
1432 |
1234 printPaddedWith:$0 to:4 base:16 |
|
1433 |
" |
|
1434 |
! |
|
1435 |
||
4376 | 1436 |
printStringBase:base |
4867 | 1437 |
"return my printString in a base; |
1438 |
same as printStringRadix:" |
|
1439 |
||
4376 | 1440 |
^ self printStringRadix:base |
12284
a6e7b47c08c4
comment/format in: #printStringBase:
Claus Gittinger <cg@exept.de>
parents:
12280
diff
changeset
|
1441 |
|
a6e7b47c08c4
comment/format in: #printStringBase:
Claus Gittinger <cg@exept.de>
parents:
12280
diff
changeset
|
1442 |
" |
a6e7b47c08c4
comment/format in: #printStringBase:
Claus Gittinger <cg@exept.de>
parents:
12280
diff
changeset
|
1443 |
1234 printStringBase:16 |
a6e7b47c08c4
comment/format in: #printStringBase:
Claus Gittinger <cg@exept.de>
parents:
12280
diff
changeset
|
1444 |
" |
9068 | 1445 |
! |
1446 |
||
9125 | 1447 |
printStringHex |
1448 |
"return my printString in base 16; |
|
1449 |
same as printStringRadix:" |
|
1450 |
||
1451 |
^ self printStringRadix:16 |
|
1452 |
||
1453 |
" |
|
1454 |
4096 printStringHex |
|
1455 |
" |
|
1456 |
! |
|
1457 |
||
9068 | 1458 |
printStringRoman |
9069 | 1459 |
"return my roman printString; |
1460 |
almost the same as romanPrintString:" |
|
1461 |
||
9068 | 1462 |
"funny - although the romans did not have negative numbers - squeak has" |
1463 |
self negative ifTrue:[ |
|
19078 | 1464 |
^ '-' , self negated romanPrintString |
9068 | 1465 |
]. |
1466 |
^ self romanPrintString |
|
12280 | 1467 |
! |
1468 |
||
1469 |
raisedTo:exp modulo:mod |
|
1470 |
^ self raisedTo:exp mod:mod |
|
4376 | 1471 |
! ! |
1472 |
||
7261 | 1473 |
!Integer methodsFor:'Compatibility-V''Age'! |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1474 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1475 |
<< aNumber |
7155 | 1476 |
"V'Age compatibility: left shift" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1477 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1478 |
^ self bitShift:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1479 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1480 |
" |
11587 | 1481 |
1 << 5 |
1482 |
64 << -5 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1483 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1484 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1485 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1486 |
>> aNumber |
7155 | 1487 |
"V'Age compatibility: right shift" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1488 |
|
22213 | 1489 |
^ self rightShift:aNumber |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1490 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1491 |
" |
11587 | 1492 |
1 >> -5 |
1493 |
64 >> 5 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1494 |
" |
22213 | 1495 |
|
1496 |
"Modified: / 25-08-2017 / 12:32:47 / cg" |
|
1 | 1497 |
! ! |
1498 |
||
22600 | 1499 |
|
4423 | 1500 |
!Integer methodsFor:'bcd conversion'! |
1501 |
||
1502 |
decodeFromBCD |
|
1503 |
"return a number representing the value of the BCD encoded receiver." |
|
1504 |
||
4424 | 1505 |
|v rslt multiplier nibble| |
4423 | 1506 |
|
1507 |
v := self. |
|
1508 |
rslt := 0. |
|
1509 |
multiplier := 1. |
|
1510 |
[v > 0] whileTrue:[ |
|
19078 | 1511 |
nibble := v bitAnd:16r0F. |
1512 |
nibble > 9 ifTrue:[ |
|
1513 |
^ BCDConversionErrorSignal |
|
1514 |
raiseRequestWith:self |
|
1515 |
errorString:'bad BCD coded value' |
|
1516 |
]. |
|
1517 |
rslt := rslt + (nibble * multiplier). |
|
1518 |
multiplier := multiplier * 10. |
|
1519 |
v := v bitShift:-4. |
|
4423 | 1520 |
]. |
1521 |
^ rslt |
|
1522 |
||
1523 |
" |
|
4949 | 1524 |
16r1234567890123 decodeFromBCD |
11587 | 1525 |
16r1073741823 decodeFromBCD |
1526 |
16r1073741824 decodeFromBCD |
|
1527 |
16r1073741825 decodeFromBCD |
|
1528 |
||
1529 |
16r55 decodeFromBCD |
|
1530 |
16r127 decodeFromBCD |
|
1531 |
16r800000 decodeFromBCD |
|
1532 |
16r8000000 decodeFromBCD |
|
1533 |
16r80000000 decodeFromBCD |
|
1534 |
16r800000000 decodeFromBCD |
|
1535 |
16r127567890 decodeFromBCD |
|
1536 |
16r1234567890 decodeFromBCD |
|
1537 |
||
1538 |
16r5A decodeFromBCD |
|
1539 |
16rFF decodeFromBCD |
|
4423 | 1540 |
" |
1541 |
||
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
1542 |
"Modified: / 15.11.1999 / 20:37:20 / cg" |
4423 | 1543 |
! |
1544 |
||
1545 |
encodeAsBCD |
|
1546 |
"return a BCD encoded number representing the same value as the |
|
1547 |
receiver." |
|
1548 |
||
1549 |
|v rslt shift| |
|
1550 |
||
1551 |
v := self. |
|
1552 |
rslt := shift := 0. |
|
1553 |
[v > 0] whileTrue:[ |
|
19078 | 1554 |
rslt := rslt + ((v \\ 10) bitShift:shift). |
1555 |
shift := shift + 4. |
|
1556 |
v := v // 10. |
|
4423 | 1557 |
]. |
1558 |
^ rslt |
|
1559 |
||
1560 |
" |
|
11587 | 1561 |
55 encodeAsBCD hexPrintString |
1562 |
127 encodeAsBCD hexPrintString |
|
1563 |
127 encodeAsBCD hexPrintString |
|
1564 |
8912345 encodeAsBCD hexPrintString |
|
1565 |
89123456 encodeAsBCD hexPrintString |
|
1566 |
891234567 encodeAsBCD hexPrintString |
|
1567 |
900000000 encodeAsBCD hexPrintString |
|
1568 |
1073741823 encodeAsBCD hexPrintString |
|
1569 |
1073741824 encodeAsBCD hexPrintString |
|
1570 |
1073741825 encodeAsBCD hexPrintString |
|
1571 |
1891234567 encodeAsBCD hexPrintString |
|
1572 |
8912345678 encodeAsBCD hexPrintString |
|
1573 |
1234567890 encodeAsBCD hexPrintString |
|
4423 | 1574 |
" |
1575 |
||
1576 |
! ! |
|
1577 |
||
69 | 1578 |
!Integer methodsFor:'bit operators'! |
1579 |
||
17248 | 1580 |
allMask:aMaskInteger |
1581 |
"return true if all 1-bits in aMaskInteger are also 1 in the receiver" |
|
1582 |
||
1583 |
^ (self bitAnd:aMaskInteger) == aMaskInteger |
|
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1584 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1585 |
"2r00001111 allMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1586 |
"2r00001111 allMask:2r00011110" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1587 |
"2r00001111 allMask:2r00000000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1588 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1589 |
|
17248 | 1590 |
anyMask:aMaskInteger |
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1591 |
"return true if any 1-bits in anInteger is also 1 in the receiver. |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1592 |
(somewhat incorrect, if the mask is zero)" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1593 |
|
17248 | 1594 |
^ (self bitAnd:aMaskInteger) ~~ 0 |
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1595 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1596 |
"2r00001111 anyMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1597 |
"2r00001111 anyMask:2r11110000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1598 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1599 |
|
17248 | 1600 |
bitAnd:aMaskInteger |
357 | 1601 |
"return the bitwise-and of the receiver and the argument, anInteger. |
1602 |
This is a general and slow implementation, walking over the bytes of |
|
1603 |
the receiver and the argument." |
|
69 | 1604 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1605 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1606 |
result byte| |
69 | 1607 |
|
17248 | 1608 |
aMaskInteger isInteger ifFalse:[ |
19078 | 1609 |
^ aMaskInteger bitAndFromInteger:self. |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1610 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1611 |
|
17248 | 1612 |
n := (aMaskInteger digitLength) min:(self digitLength). |
92 | 1613 |
result := self class basicNew numberOfDigits:n. |
69 | 1614 |
|
1615 |
1 to:n do:[:index | |
|
19078 | 1616 |
byte := (aMaskInteger digitAt:index) bitAnd:(self digitAt:index). |
1617 |
result digitAt:index put:byte. |
|
69 | 1618 |
]. |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1619 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
19078 | 1620 |
^ result compressed |
92 | 1621 |
]. |
1622 |
^ result |
|
357 | 1623 |
|
1624 |
" |
|
11587 | 1625 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
1626 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
1627 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
1628 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
357 | 1629 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1630 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1631 |
"Modified: 5.11.1996 / 14:06:26 / cg" |
69 | 1632 |
! |
1633 |
||
17248 | 1634 |
bitClear:aMaskInteger |
19071 | 1635 |
"return the bitwise-and of the receiver and the complement of the argument, anInteger, |
16379 | 1636 |
returning the receiver with bits of the argument cleared. |
19071 | 1637 |
(i.e. the same as self bitAnd:aMaskInteger bitInvert). |
3884 | 1638 |
This is a general and slow implementation, walking over the bytes of |
1639 |
the receiver and the argument." |
|
1640 |
||
1641 |
|n "{ Class: SmallInteger }" |
|
1642 |
result byte| |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1643 |
|
17248 | 1644 |
n := (aMaskInteger digitLength) max:(self digitLength). |
3884 | 1645 |
result := self class basicNew numberOfDigits:n. |
1646 |
||
1647 |
1 to:n do:[:index | |
|
19078 | 1648 |
byte := (self digitAt:index) bitClear:(aMaskInteger digitAt:index). |
1649 |
result digitAt:index put:byte. |
|
3884 | 1650 |
]. |
1651 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
|
19078 | 1652 |
^ result compressed |
3884 | 1653 |
]. |
1654 |
^ result |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1655 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1656 |
|
11492 | 1657 |
bitCount |
1658 |
"return the number of 1-bits in the receiver" |
|
1659 |
||
1660 |
|n "{ Class: SmallInteger }" |
|
1661 |
cnt byte| |
|
1662 |
||
1663 |
n := self digitLength. |
|
1664 |
cnt := 0. |
|
1665 |
||
1666 |
1 to:n do:[:index | |
|
19078 | 1667 |
byte := self digitAt:index. |
1668 |
cnt := cnt + (byte bitCount) |
|
11492 | 1669 |
]. |
1670 |
^ cnt |
|
1671 |
||
1672 |
" |
|
11587 | 1673 |
2r100000000000000000000000000000000000000000000000000000000001 bitCount |
11492 | 1674 |
2r111111111111111111111111111111111111111111111111111111111111111111 bitCount |
13914 | 1675 |
100 factorial bitCount -> 207 |
1676 |
1000 factorial bitCount -> 3788 |
|
11492 | 1677 |
" |
13914 | 1678 |
|
1679 |
"Modified (comment): / 09-01-2012 / 19:51:00 / cg" |
|
11492 | 1680 |
! |
1681 |
||
22219 | 1682 |
bitDeinterleave:n |
1683 |
"extract count integers from an n-way Morton number as a vector; |
|
22222 | 1684 |
This is the inverse operation from bitInterleave: - see comment there. |
22219 | 1685 |
i.e. if count is 3, |
1686 |
and the receiver's bits are |
|
1687 |
cN bN aN ... c2 b2 a2 c1 b1 a1 c0 b0 a0 |
|
1688 |
then the result will be a vector containing the numbers a,b,c with bits: |
|
1689 |
aN ... a2 a1 a0 |
|
1690 |
bN ... b2 b1 b0 |
|
22222 | 1691 |
cN ... c2 c1 c0." |
22219 | 1692 |
|
1693 |
|v shift tuple| |
|
1694 |
||
1695 |
tuple := Array new:n withAll:0. |
|
1696 |
||
1697 |
shift := 0. |
|
1698 |
v := self. |
|
1699 |
[ v > 0 ] whileTrue:[ |
|
1700 |
1 to:n do:[:i | |
|
1701 |
tuple at:i put:((tuple at:i) bitOr:((v bitAnd:1) bitShift:shift)). |
|
1702 |
v := v rightShift:1. |
|
1703 |
]. |
|
1704 |
shift := shift + 1. |
|
1705 |
]. |
|
1706 |
^ tuple |
|
1707 |
||
1708 |
" |
|
1709 |
(2r1100 bitInterleaveWith:2r1001) -> 2r11100001 |
|
1710 |
(2r11000110 bitInterleaveWith:2r10011100 and:2r10100101) -> 2r111100001010010111100001. |
|
1711 |
||
1712 |
2r11100001 bitDeinterleave:2 |
|
1713 |
||
1714 |
(2r11000110 bitInterleaveWith:2r10011100 and:2r10100101) |
|
1715 |
(198 bitInterleaveWith:156 and:165) bitDeinterleave:3 |
|
1716 |
" |
|
1717 |
||
1718 |
"Created: / 28-08-2017 / 15:02:31 / cg" |
|
22222 | 1719 |
"Modified (comment): / 28-08-2017 / 18:45:21 / cg" |
22219 | 1720 |
! |
1721 |
||
1722 |
bitInterleaveWith:anInteger |
|
1723 |
"generate a Morton number (-> https://en.wikipedia.org/wiki/Morton_number_(number_theory)) |
|
1724 |
by interleaving bits of the receiver (at odd positions if counting from 1) |
|
1725 |
with bits of the argument (at even bit positions). |
|
1726 |
||
1727 |
Thus, if the bits of the receiver are |
|
1728 |
aN ... a2 a1 a0 |
|
1729 |
and those of the argument are: |
|
1730 |
bN ... b2 b1 b0 |
|
1731 |
the result is |
|
1732 |
bN aN ... b2 a2 b1 a1 b0 a0. |
|
1733 |
||
1734 |
Morton numbers are great to linearize 2D coordinates |
|
1735 |
eg. to sort 2D points by distances" |
|
1736 |
||
1737 |
"/ a naive and slow fallback here, using a small map to process cunks of 4 bits |
|
1738 |
||
1739 |
|a b shift ma mb val| |
|
1740 |
||
22222 | 1741 |
self assert:(self >= 0). |
1742 |
self assert:(a >= 0). |
|
1743 |
||
22219 | 1744 |
a := self. |
1745 |
b := anInteger. |
|
1746 |
val := 0. |
|
1747 |
shift := 0. |
|
1748 |
[ (a == 0) and:[b == 0]] whileFalse:[ |
|
1749 |
"/ strip off 4 bits from each... |
|
1750 |
"/ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 |
|
1751 |
mb := #[ 2r00000000 2r00000010 2r00001000 2r00001010 2r00100000 2r00100010 2r00101000 2r00101010 2r10000000 2r10000010 2r10001000 2r10001010 2r10100000 2r10100010 2r10101000 2r10101010 ] |
|
1752 |
at:((b bitAnd:2r1111)+1). |
|
1753 |
ma := #[ 2r00000000 2r00000001 2r00000100 2r00000101 2r00010000 2r00010001 2r00010100 2r00010101 2r01000000 2r01000001 2r01000100 2r01000101 2r01010000 2r01010001 2r01010100 2r01010101 ] |
|
1754 |
at:((a bitAnd:2r1111)+1). |
|
1755 |
val := val bitOr:((ma bitOr:mb) bitShift:shift). |
|
1756 |
shift := shift + 8. |
|
1757 |
a := a rightShift:4. |
|
1758 |
b := b rightShift:4. |
|
1759 |
]. |
|
1760 |
^ val. |
|
1761 |
||
1762 |
" |
|
1763 |
(2r11000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
|
1764 |
bitInterleaveWith:2r10010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) |
|
1765 |
-> 2r1101001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
|
1766 |
||
1767 |
(2r11000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
|
1768 |
bitInterleaveWith:2r10010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) |
|
1769 |
-> 2r1101001000010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
|
1770 |
" |
|
1771 |
||
1772 |
"Created: / 28-08-2017 / 14:33:08 / cg" |
|
22222 | 1773 |
"Modified: / 28-08-2017 / 19:19:31 / cg" |
22219 | 1774 |
! |
1775 |
||
1776 |
bitInterleaveWith:integer1 and:integer2 |
|
1777 |
"generate a Morton3 number (-> https://en.wikipedia.org/wiki/Morton_number_(number_theory)) |
|
1778 |
by interleaving bits of the receiver with bits of the arguments. |
|
1779 |
Thus, if the bits of the receiver are |
|
1780 |
aN ... a2 a1 a0 |
|
1781 |
and those of the integer1 are: |
|
1782 |
bN ... b2 b1 b0 |
|
1783 |
and those of the integer2 are: |
|
1784 |
cN ... c2 c1 c0 |
|
1785 |
the result is |
|
1786 |
cN bN aN ... c2 b2 a2 c1 b1 a1 c0 b0 a0. |
|
1787 |
||
1788 |
Morton3 numbers are great to linearize 3D coordinates |
|
1789 |
eg. to sort 3D points by distances" |
|
1790 |
||
1791 |
|a b c shift ma mb mc val| |
|
1792 |
||
1793 |
a := self. |
|
1794 |
b := integer1. |
|
1795 |
c := integer2. |
|
22469 | 1796 |
self assert:(a >= 0). |
1797 |
self assert:(b >= 0). |
|
1798 |
self assert:(c >= 0). |
|
1799 |
||
22219 | 1800 |
val := 0. |
1801 |
shift := 0. |
|
22469 | 1802 |
[ a ~~ 0 or:[b ~~ 0 or:[c ~~ 0]] ] whileTrue:[ |
22219 | 1803 |
"/ strip off 4 bits from each... |
1804 |
"/ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 |
|
1805 |
mc := #( 2r000000000000 2r000000000100 2r000000100000 2r000000100100 2r000100000000 2r000100000100 2r000100100000 2r000100100100 2r100000000000 2r100000000100 2r100000100000 2r100000100100 2r100100000000 2r100100000100 2r100100100000 2r100100100100 ) |
|
1806 |
at:((c bitAnd:2r1111)+1). |
|
1807 |
mb := #( 2r000000000000 2r000000000010 2r000000010000 2r000000010010 2r000010000000 2r000010000010 2r000010010000 2r000010010010 2r010000000000 2r010000000010 2r010000010000 2r010000010010 2r010010000000 2r010010000010 2r010010010000 2r010010010010 ) |
|
1808 |
at:((b bitAnd:2r1111)+1). |
|
1809 |
ma := #( 2r000000000000 2r000000000001 2r000000001000 2r000000001001 2r000001000000 2r000001000001 2r000001001000 2r000001001001 2r001000000000 2r001000000001 2r001000001000 2r001000001001 2r001001000000 2r001001000001 2r001001001000 2r001001001001 ) |
|
1810 |
at:((a bitAnd:2r1111)+1). |
|
1811 |
val := val bitOr:(((ma bitOr:mb) bitOr:mc) bitShift:shift). |
|
1812 |
shift := shift + 12. |
|
1813 |
a := a rightShift:4. |
|
1814 |
b := b rightShift:4. |
|
1815 |
c := c rightShift:4. |
|
1816 |
]. |
|
1817 |
^ val. |
|
1818 |
||
1819 |
" |
|
1820 |
(2r1100 bitInterleaveWith:2r1001 and:2r1010) printStringRadix:2 -> '111 001 100 010' |
|
22222 | 1821 |
|
22219 | 1822 |
(2r11000110 bitInterleaveWith:2r10011100 and:2r10100101) printStringRadix:2 -> '111 001 100 010 010 111 001 100' |
22222 | 1823 |
(1 bitInterleaveWith:1 and:16) |
1824 |
||
1825 |
((1<<31) bitInterleaveWith:(1<<31) and:(1<<31)) bitDeinterleave:3 |
|
1826 |
((1<<31) bitInterleaveWith:(1<<63) and:(1<<95)) bitDeinterleave:3 |
|
22219 | 1827 |
" |
1828 |
||
1829 |
"Created: / 28-08-2017 / 14:33:04 / cg" |
|
22222 | 1830 |
"Modified: / 28-08-2017 / 19:19:25 / cg" |
22469 | 1831 |
"Modified: / 19-01-2018 / 11:29:04 / stefan" |
22219 | 1832 |
! |
1833 |
||
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1834 |
bitInvert |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1835 |
"return a new integer, where all bits are complemented. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1836 |
This does not really make sense for negative largeIntegers, |
12255 | 1837 |
since the digits are stored as absolute value. |
1838 |
Q: is this specified in a language standard ?" |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1839 |
|
4614 | 1840 |
"/ ^ -1 - self |
1841 |
||
15841 | 1842 |
|n "{ Class: SmallInteger }" |
1843 |
byte "{ Class: SmallInteger }" |
|
1844 |
result| |
|
4614 | 1845 |
|
1846 |
n := self digitLength. |
|
1847 |
result := self class basicNew numberOfDigits:n. |
|
1848 |
||
1849 |
1 to:n do:[:index | |
|
19078 | 1850 |
byte := self digitAt:index. |
1851 |
byte := byte bitInvert bitAnd:16rFF. |
|
1852 |
result digitAt:index put:byte. |
|
4614 | 1853 |
]. |
15841 | 1854 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
19078 | 1855 |
"if last byte is zero we can normalize" |
1856 |
^ result compressed |
|
4614 | 1857 |
]. |
1858 |
^ result |
|
1859 |
||
19068 | 1860 |
" |
1861 |
16rff bitInvert bitAnd:16rff |
|
1862 |
16rffffffff bitInvert |
|
1863 |
16rff00ff00 bitInvert hexPrintString |
|
1864 |
" |
|
1865 |
! |
|
1866 |
||
1867 |
bitInvertByte |
|
1868 |
"return a new integer, where the low 8 bits are masked and complemented. |
|
1869 |
This returns an unsigned version of what bitInvert would return. |
|
1870 |
(i.e. same as self bitInvert bitAnd:16rFF)" |
|
1871 |
||
1872 |
^ (self digitAt:1) bitInvert bitAnd:16rFF |
|
1873 |
||
1874 |
" |
|
1875 |
16rff bitInvert |
|
1876 |
16rff bitInvertByte |
|
1877 |
" |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1878 |
! |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1879 |
|
17248 | 1880 |
bitOr:aMaskInteger |
701 | 1881 |
"return the bitwise-or of the receiver and the argument, anInteger. |
1882 |
This is a general and slow implementation, walking over the bytes of |
|
23118 | 1883 |
the receiver and the argument. |
1884 |
It is redefined in concrete subclasses (especially SmallInteger) for performance." |
|
701 | 1885 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1886 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1887 |
result byte| |
701 | 1888 |
|
17248 | 1889 |
aMaskInteger isInteger ifFalse:[ |
19078 | 1890 |
^ aMaskInteger bitOrFromInteger:self. |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1891 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1892 |
|
17248 | 1893 |
n := (aMaskInteger digitLength) max:(self digitLength). |
701 | 1894 |
result := self class basicNew numberOfDigits:n. |
1895 |
||
1896 |
1 to:n do:[:index | |
|
19078 | 1897 |
byte := (aMaskInteger digitAt:index) bitOr:(self digitAt:index). |
1898 |
result digitAt:index put:byte. |
|
701 | 1899 |
]. |
1900 |
"/ no need to normalize - if the operands were correct |
|
1901 |
"/ byte == 0 ifTrue:[ |
|
1902 |
"/ ^ result normalize |
|
1903 |
"/ ]. |
|
1904 |
^ result |
|
23118 | 1905 |
|
1906 |
"Modified (comment): / 20-06-2018 / 14:47:43 / Claus Gittinger" |
|
701 | 1907 |
! |
1908 |
||
85 | 1909 |
bitShift:shiftCount |
1910 |
"return the value of the receiver shifted by shiftCount bits; |
|
1911 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 1912 |
|
11587 | 1913 |
Notice: the result of bitShift: on negative receivers is not |
19078 | 1914 |
defined in the language standard (since the implementation |
1915 |
is free to choose any internal representation for integers) |
|
1916 |
However, ST/X preserves the sign." |
|
11587 | 1917 |
|
1918 |
|result |
|
92 | 1919 |
prev "{ Class: SmallInteger }" |
1920 |
next "{ Class: SmallInteger }" |
|
1921 |
byte "{ Class: SmallInteger }" |
|
359 | 1922 |
byte2 "{ Class: SmallInteger }" |
92 | 1923 |
bitShift "{ Class: SmallInteger }" |
1924 |
revShift "{ Class: SmallInteger }" |
|
1925 |
digitShift "{ Class: SmallInteger }" |
|
3044 | 1926 |
nn "{ Class: SmallInteger }" |
1927 |
nDigits "{ Class: SmallInteger }" | |
|
85 | 1928 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1929 |
shiftCount isInteger ifFalse:[ |
19078 | 1930 |
^ shiftCount bitShiftFromInteger:self. |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1931 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1932 |
|
85 | 1933 |
shiftCount > 0 ifTrue:[ |
19078 | 1934 |
"left shift" |
1935 |
||
19874 | 1936 |
nDigits := self digitLength. |
19078 | 1937 |
digitShift := shiftCount // 8. |
1938 |
bitShift := shiftCount \\ 8. |
|
1939 |
||
1940 |
" |
|
1941 |
modulo 8 shifts can be done faster ... |
|
1942 |
" |
|
1943 |
bitShift == 0 ifTrue:[ |
|
19874 | 1944 |
nn := nDigits + digitShift. |
1945 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
|
1946 |
result digitBytes replaceFrom:(digitShift + 1) to:nn with:self digitBytes. |
|
19078 | 1947 |
" |
1948 |
no normalize needed, since receiver was already normalized |
|
1949 |
" |
|
1950 |
^ result |
|
1951 |
]. |
|
1952 |
||
1953 |
" |
|
1954 |
less-than-8 shifts can be done faster ... |
|
1955 |
" |
|
1956 |
digitShift == 0 ifTrue:[ |
|
19874 | 1957 |
nn := nDigits+1. |
19078 | 1958 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
1959 |
prev := 0. |
|
19874 | 1960 |
1 to:nDigits do:[:index | |
19078 | 1961 |
byte := self digitAt:index. |
1962 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
1963 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
1964 |
prev := byte bitShift:-8. |
|
1965 |
]. |
|
1966 |
result digitAt:nn put:prev. |
|
1967 |
" |
|
1968 |
might have stored a 0-byte ... |
|
1969 |
" |
|
1970 |
prev == 0 ifTrue:[ |
|
1971 |
^ result compressed |
|
1972 |
]. |
|
1973 |
^ result. |
|
1974 |
]. |
|
1975 |
||
1976 |
" |
|
1977 |
slow case ... |
|
1978 |
" |
|
19874 | 1979 |
nn := nDigits + digitShift + 1. |
1980 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
|
19078 | 1981 |
byte := self digitAt:1. |
1982 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
|
1983 |
result digitAt:(digitShift + 1) put:byte. |
|
1984 |
revShift := -8 + bitShift. |
|
1985 |
2 to:nDigits do:[:index | |
|
1986 |
byte := self digitAt:index. |
|
1987 |
byte2 := self digitAt:index-1. |
|
1988 |
byte := byte bitShift:bitShift. |
|
1989 |
byte2 := byte2 bitShift:revShift. |
|
1990 |
byte := (byte bitOr:byte2) bitAnd:16rFF. |
|
1991 |
result digitAt:(index + digitShift) put:byte. |
|
1992 |
]. |
|
1993 |
byte2 := self digitAt:nDigits. |
|
1994 |
byte2 := (byte2 bitShift:revShift) bitAnd:16rFF. |
|
1995 |
result digitAt:(nDigits + digitShift + 1) put:byte2. |
|
1996 |
" |
|
1997 |
might have stored a 0-byte ... |
|
1998 |
" |
|
1999 |
byte2 == 0 ifTrue:[ |
|
2000 |
^ result compressed |
|
2001 |
]. |
|
2002 |
^ result |
|
92 | 2003 |
]. |
2004 |
||
2005 |
shiftCount < 0 ifTrue:[ |
|
19078 | 2006 |
"right shift" |
2007 |
||
19874 | 2008 |
nDigits := self digitLength. |
19078 | 2009 |
digitShift := shiftCount negated // 8. |
2010 |
bitShift := shiftCount negated \\ 8. |
|
19874 | 2011 |
|
2012 |
digitShift >= nDigits ifTrue:[ |
|
19078 | 2013 |
^ 0 |
2014 |
]. |
|
2015 |
||
2016 |
" |
|
2017 |
modulo 8 shifts can be done faster ... |
|
2018 |
" |
|
2019 |
bitShift == 0 ifTrue:[ |
|
19874 | 2020 |
nn := nDigits-digitShift. |
2021 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
|
2022 |
result digitBytes replaceFrom:1 to:nn with:self digitBytes startingAt:(digitShift + 1) . |
|
2023 |
nn <= SmallInteger maxBytes ifTrue:[ |
|
19078 | 2024 |
^ result compressed |
2025 |
]. |
|
2026 |
^ result |
|
2027 |
]. |
|
2028 |
||
2029 |
" |
|
2030 |
less-than-8 shifts can be done faster ... |
|
2031 |
" |
|
2032 |
digitShift == 0 ifTrue:[ |
|
19874 | 2033 |
result := self class basicNew numberOfDigits:nDigits sign:self sign. |
19078 | 2034 |
prev := 0. |
2035 |
bitShift := bitShift negated. |
|
2036 |
revShift := 8 + bitShift. |
|
19874 | 2037 |
nDigits to:1 by:-1 do:[:index | |
19078 | 2038 |
byte := self digitAt:index. |
2039 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
2040 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
2041 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
2042 |
prev := next. |
|
2043 |
]. |
|
2044 |
^ result compressed |
|
2045 |
]. |
|
2046 |
||
2047 |
" |
|
2048 |
slow case ... |
|
2049 |
" |
|
19874 | 2050 |
nn := nDigits-digitShift. |
19078 | 2051 |
result := self class basicNew numberOfDigits:nn sign:self sign. |
2052 |
||
2053 |
prev := 0. |
|
2054 |
bitShift := bitShift negated. |
|
2055 |
revShift := 8 + bitShift. |
|
2056 |
nn := digitShift + 1. |
|
19874 | 2057 |
nDigits to:nn by:-1 do:[:index | |
19078 | 2058 |
byte := self digitAt:index. |
2059 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
2060 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
2061 |
result digitAt:(index - digitShift) put:byte. |
|
2062 |
prev := next. |
|
2063 |
]. |
|
2064 |
"the last stored byte ..." |
|
2065 |
^ result compressed |
|
85 | 2066 |
]. |
92 | 2067 |
|
2068 |
^ self "no shift" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2069 |
|
3638 | 2070 |
"Modified: / 8.7.1998 / 12:45:24 / cg" |
4151 | 2071 |
"Modified: / 5.5.1999 / 16:05:05 / stefan" |
701 | 2072 |
! |
1 | 2073 |
|
17248 | 2074 |
bitTest:aMaskInteger |
5782 | 2075 |
"return true, if any bit from aMask is set in the receiver. |
2076 |
I.e. true, if the bitwise-AND of the receiver and the argument, anInteger |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2077 |
is non-0, false otherwise. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2078 |
This is a general and slow implementation, walking over the bytes of |
23118 | 2079 |
the receiver and the argument. |
2080 |
It is redefined in concrete subclasses (especially SmallInteger) for performance." |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2081 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2082 |
|n "{ Class: SmallInteger }" |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2083 |
byte| |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2084 |
|
17248 | 2085 |
n := (aMaskInteger digitLength) min:(self digitLength). |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2086 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2087 |
1 to:n do:[:index | |
19078 | 2088 |
byte := (aMaskInteger digitAt:index) bitAnd:(self digitAt:index). |
2089 |
byte ~~ 0 ifTrue:[^ true]. |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2090 |
]. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2091 |
^ false |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2092 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2093 |
" |
11587 | 2094 |
16r112233445566778899 bitTest:16rFF |
2095 |
16r112233445566778800 bitTest:16rFF |
|
2096 |
16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00 |
|
2097 |
16r112233445566778899 bitTest:16rFF0000000000000000 |
|
2098 |
16r112233445566778899 bitTest:16r00000000000000FFFF |
|
2099 |
16r1234567800000000 bitTest:16r8000000000000000 |
|
2100 |
16r8765432100000000 bitTest:16r8000000000000000 |
|
2101 |
16r12345678 bitTest:16r80000000 |
|
2102 |
16r87654321 bitTest:16r80000000 |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2103 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2104 |
|
23118 | 2105 |
"Modified: / 06-06-1999 / 15:10:33 / cg" |
2106 |
"Modified (comment): / 20-06-2018 / 14:47:28 / Claus Gittinger" |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2107 |
! |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
2108 |
|
701 | 2109 |
bitXor:anInteger |
2110 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
2111 |
This is a general and slow implementation, walking over the bytes of |
|
23118 | 2112 |
the receiver and the argument. |
2113 |
It is redefined in concrete subclasses (especially SmallInteger) for performance." |
|
701 | 2114 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
2115 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
2116 |
result byte| |
701 | 2117 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2118 |
anInteger isInteger ifFalse:[ |
19078 | 2119 |
^ anInteger bitXorFromInteger:self. |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2120 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
2121 |
|
701 | 2122 |
n := (anInteger digitLength) max:(self digitLength). |
2123 |
result := self class basicNew numberOfDigits:n. |
|
1 | 2124 |
|
701 | 2125 |
1 to:n do:[:index | |
19078 | 2126 |
byte := (anInteger digitAt:index) bitXor:(self digitAt:index). |
2127 |
result digitAt:index put:byte. |
|
701 | 2128 |
]. |
3993
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
2129 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
19078 | 2130 |
^ result compressed |
701 | 2131 |
]. |
2132 |
^ result |
|
1 | 2133 |
|
701 | 2134 |
" |
11587 | 2135 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
701 | 2136 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
2137 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
11587 | 2138 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
701 | 2139 |
" |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2140 |
|
23118 | 2141 |
"Modified: / 05-11-1996 / 14:06:40 / cg" |
2142 |
"Modified (comment): / 20-06-2018 / 14:47:55 / Claus Gittinger" |
|
1 | 2143 |
! |
2144 |
||
12670 | 2145 |
changeMask:mask to:aBooleanOrNumber |
2146 |
"return a new number where the specified mask-bit is on or off, |
|
2147 |
depending on aBooleanOrNumber. |
|
14404
49a54ea47bf8
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14403
diff
changeset
|
2148 |
The method's name may be misleading: the receiver is not changed, |
12670 | 2149 |
but a new number is returned. Should be named #withMask:changedTo:" |
2150 |
||
2151 |
(aBooleanOrNumber == 0 or:[aBooleanOrNumber == false]) ifTrue:[ |
|
19078 | 2152 |
^ self bitClear:mask |
12670 | 2153 |
]. |
2154 |
^ self bitOr:mask |
|
2155 |
||
2156 |
" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
2157 |
(16r3fffffff changeMask:16r80 to:0) hexPrintString |
12670 | 2158 |
(16r3fff0000 changeMask:16r80 to:1) hexPrintString |
2159 |
" |
|
2160 |
! |
|
2161 |
||
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2162 |
even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2163 |
"return true if the receiver is even" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2164 |
|
9401 | 2165 |
^ (self bitAt:1) == 0 |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2166 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2167 |
" |
11587 | 2168 |
16r112233445566778899 even |
2169 |
16r112233445566778800 even |
|
2170 |
1 even |
|
2171 |
2 even |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2172 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2173 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2174 |
"Created: / 6.6.1999 / 15:00:40 / cg" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2175 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2176 |
|
701 | 2177 |
highBit |
19600 | 2178 |
"return the bitIndex of the highest bit set. |
2179 |
The returned bitIndex starts at 1 for the least significant bit. |
|
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2180 |
Returns 0 if no bit is set." |
701 | 2181 |
|
2182 |
|byteNr highByte| |
|
2183 |
||
2184 |
byteNr := self digitLength. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2185 |
byteNr == 0 ifTrue:[ |
19078 | 2186 |
^ 0 |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2187 |
]. |
701 | 2188 |
highByte := self digitAt:byteNr. |
2189 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 2190 |
|
701 | 2191 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2192 |
0 highBit |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2193 |
-1 highBit |
11587 | 2194 |
(1 bitShift:1) highBit |
2195 |
(1 bitShift:30) highBit |
|
2196 |
(1 bitShift:31) highBit |
|
2197 |
(1 bitShift:32) highBit |
|
2198 |
(1 bitShift:33) highBit |
|
2199 |
(1 bitShift:64) highBit |
|
2200 |
(1 bitShift:1000) highBit |
|
2201 |
(1 bitShift:1000) negated highBit |
|
2202 |
((1 bitShift:64)-1) highBit |
|
701 | 2203 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2204 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2205 |
"Modified: / 3.5.1999 / 09:20:57 / stefan" |
2429 | 2206 |
! |
2207 |
||
16071 | 2208 |
leftShift:shiftCount |
2209 |
"return the value of the receiver shifted left by shiftCount bits; |
|
2210 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
2211 |
||
2212 |
Notice: the result of bitShift: on negative receivers is not |
|
19078 | 2213 |
defined in the language standard (since the implementation |
2214 |
is free to choose any internal representation for integers) |
|
2215 |
However, ST/X preserves the sign." |
|
16071 | 2216 |
|
2217 |
^ self bitShift:shiftCount |
|
2218 |
||
2219 |
" |
|
2220 |
16r100000000 leftShift:1 |
|
2221 |
16r100000000 negated leftShift:1 |
|
2222 |
" |
|
2223 |
! |
|
2224 |
||
2429 | 2225 |
lowBit |
2226 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
11587 | 2227 |
starts at 1 for the least significant bit. |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
2228 |
Returns 0 if no bit is set." |
2429 | 2229 |
|
2230 |
|maxBytes "{ Class: SmallInteger }" |
|
2231 |
byte| |
|
2232 |
||
2233 |
maxBytes := self digitLength. |
|
2234 |
1 to:maxBytes do:[:byteIndex | |
|
19078 | 2235 |
byte := self digitAt:byteIndex. |
2236 |
byte ~~ 0 ifTrue:[ |
|
2237 |
^ (byteIndex-1)*8 + (byte lowBit) |
|
2238 |
]. |
|
2429 | 2239 |
]. |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
2240 |
^ 0 "/ should not happen |
2429 | 2241 |
|
2242 |
" |
|
11587 | 2243 |
0 lowBit |
2244 |
1 lowBit |
|
2245 |
(1 bitShift:1) lowBit |
|
2246 |
(1 bitShift:1) highBit |
|
2247 |
(1 bitShift:30) lowBit |
|
2248 |
(1 bitShift:30) highBit |
|
2249 |
(1 bitShift:31) lowBit |
|
2250 |
(1 bitShift:31) highBit |
|
2251 |
(1 bitShift:32) lowBit |
|
2252 |
(1 bitShift:32) highBit |
|
2253 |
(1 bitShift:33) lowBit |
|
2254 |
(1 bitShift:33) highBit |
|
2255 |
(1 bitShift:64) lowBit |
|
2256 |
(1 bitShift:64) highBit |
|
2257 |
(1 bitShift:1000) lowBit |
|
2258 |
(1 bitShift:1000) highBit |
|
2259 |
((1 bitShift:64)-1) lowBit |
|
2260 |
((1 bitShift:64)-1) highBit |
|
2429 | 2261 |
" |
2262 |
||
2263 |
"Modified: 1.3.1997 / 16:54:23 / cg" |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2264 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2265 |
|
17248 | 2266 |
noMask:aMaskInteger |
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
2267 |
"return true if no 1-bit in anInteger is 1 in the receiver" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
2268 |
|
17248 | 2269 |
^ (self bitAnd:aMaskInteger) == 0 |
2270 |
||
2271 |
" |
|
2272 |
2r00001111 noMask:2r00000001 |
|
2273 |
2r00001111 noMask:2r11110000 |
|
2274 |
" |
|
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
2275 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
2276 |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2277 |
odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2278 |
"return true if the receiver is odd" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2279 |
|
9401 | 2280 |
^ (self bitAt:1) == 1 |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2281 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2282 |
" |
11587 | 2283 |
16r112233445566778899 odd |
2284 |
16r112233445566778800 odd |
|
2285 |
1 odd |
|
2286 |
2 odd |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2287 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2288 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
2289 |
"Created: / 6.6.1999 / 15:00:55 / cg" |
5209 | 2290 |
! |
2291 |
||
7155 | 2292 |
rightShift:shiftCount |
2293 |
"return the value of the receiver shifted right by shiftCount bits; |
|
2294 |
rightShift if shiftCount > 0; leftShift otherwise. |
|
2295 |
||
11587 | 2296 |
Notice: the result of bitShift: on negative receivers is not |
19078 | 2297 |
defined in the language standard (since the implementation |
2298 |
is free to choose any internal representation for integers) |
|
2299 |
However, ST/X preserves the sign." |
|
7155 | 2300 |
|
2301 |
^ self bitShift:(shiftCount negated) |
|
2302 |
||
2303 |
" |
|
11587 | 2304 |
16r100000000 rightShift:1 |
2305 |
16r100000000 negated rightShift:1 |
|
2306 |
||
2307 |
16r100000000 rightShift:2 |
|
2308 |
16r100000000 negated rightShift:2 |
|
2309 |
||
2310 |
16r100000000 rightShift:3 |
|
2311 |
16r100000000 negated rightShift:3 |
|
2312 |
||
2313 |
((16r100000000 rightShift:1) rightShift:1) rightShift:1 |
|
2314 |
((16r100000000 negated rightShift:1) rightShift:1) rightShift:1 |
|
7155 | 2315 |
" |
19053 | 2316 |
! ! |
2317 |
||
2318 |
!Integer methodsFor:'bit operators - indexed'! |
|
2319 |
||
2320 |
bitAt:index |
|
2321 |
"return the value of the index's bit (index starts at 1) as 0 or 1. |
|
2322 |
Notice: the result of bitAt: on negative receivers is not |
|
19078 | 2323 |
defined in the language standard (since the implementation |
2324 |
is free to choose any internal representation for integers)" |
|
19053 | 2325 |
|
2326 |
|i "{Class: SmallInteger}"| |
|
2327 |
||
2328 |
i := index - 1. |
|
2329 |
i < 0 ifTrue:[ |
|
19078 | 2330 |
^ SubscriptOutOfBoundsError |
2331 |
raiseRequestWith:index |
|
2332 |
errorString:'index out of bounds' |
|
19053 | 2333 |
]. |
2334 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
|
2335 |
||
2336 |
" |
|
2337 |
1 bitAt:1 => 1 |
|
2338 |
1 bitAt:2 => 0 |
|
2339 |
1 bitAt:0 index error |
|
2340 |
2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1 |
|
2341 |
2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0 |
|
2342 |
||
2343 |
(1 bitShift:1000) bitAt:1000 => 0 |
|
2344 |
(1 bitShift:1000) bitAt:1001 => 1 |
|
2345 |
(1 bitShift:1000) bitAt:1002 => 0 |
|
2346 |
||
2347 |
(1 bitShift:30) bitAt:30 |
|
2348 |
(1 bitShift:30) bitAt:31 |
|
2349 |
(1 bitShift:30) bitAt:32 |
|
2350 |
(1 bitShift:31) bitAt:31 |
|
2351 |
(1 bitShift:31) bitAt:32 |
|
2352 |
(1 bitShift:31) bitAt:33 |
|
2353 |
(1 bitShift:32) bitAt:32 |
|
2354 |
(1 bitShift:32) bitAt:33 |
|
2355 |
(1 bitShift:32) bitAt:34 |
|
2356 |
(1 bitShift:64) bitAt:64 |
|
2357 |
(1 bitShift:64) bitAt:65 |
|
2358 |
(1 bitShift:64) bitAt:66 |
|
2359 |
" |
|
2360 |
! |
|
2361 |
||
2362 |
bitIndicesOfOneBitsDo:aBlock |
|
2363 |
"evaluate aBlock for all indices of a 1-bit, starting with the index of the lowest bit. |
|
2364 |
The index for the least significant bit is 1." |
|
2365 |
||
2366 |
1 to:self digitLength do:[:i8 | |
|
19078 | 2367 |
|byte| |
2368 |
||
2369 |
byte := self digitAt:i8. |
|
2370 |
byte ~~ 0 ifTrue:[ |
|
2371 |
1 to:8 do:[:i | |
|
2372 |
(byte bitAt:i) == 1 ifTrue:[ |
|
2373 |
aBlock value:(((i8-1)*8) + i). |
|
2374 |
]. |
|
2375 |
]. |
|
2376 |
] |
|
19053 | 2377 |
]. |
2378 |
||
2379 |
" |
|
2380 |
1 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i]. |
|
2381 |
2 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i] |
|
2382 |
4 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i] |
|
2383 |
12 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i] |
|
2384 |
127 bitIndicesOfOneBitsDo:[:i | Transcript showCR:i] |
|
2385 |
" |
|
2386 |
! |
|
2387 |
||
2388 |
bitIndicesOfOneBitsReverseDo:aBlock |
|
2389 |
"evaluate aBlock for all indices of a 1-bit, starting with the index of the highest |
|
2390 |
and ending with the lowest bit. |
|
2391 |
The index for the least significant bit is 1." |
|
2392 |
||
2393 |
self digitLength downTo:1 do:[:i8 | |
|
19078 | 2394 |
|byte| |
2395 |
||
2396 |
byte := self digitAt:i8. |
|
2397 |
byte ~~ 0 ifTrue:[ |
|
2398 |
8 downTo:1 do:[:i | |
|
2399 |
(byte bitAt:i) == 1 ifTrue:[ |
|
2400 |
aBlock value:(((i8-1)*8) + i). |
|
2401 |
]. |
|
2402 |
]. |
|
2403 |
] |
|
19053 | 2404 |
]. |
2405 |
||
2406 |
" |
|
2407 |
1 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i]. |
|
2408 |
2 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i] |
|
2409 |
4 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i] |
|
2410 |
12 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i] |
|
2411 |
127 bitIndicesOfOneBitsReverseDo:[:i | Transcript showCR:i] |
|
2412 |
" |
|
2413 |
! |
|
2414 |
||
2415 |
changeBit:index to:aBooleanOrNumber |
|
2416 |
"return a new number where the specified bit is on or off, |
|
2417 |
depending on aBooleanOrNumber. |
|
2418 |
Bits are counted from 1 starting with the least significant. |
|
2419 |
The method's name may be misleading: the receiver is not changed, |
|
2420 |
but a new number is returned. Should be named #withBit:changedTo:" |
|
2421 |
||
2422 |
(aBooleanOrNumber == 0 or:[aBooleanOrNumber == false]) ifTrue:[ |
|
19078 | 2423 |
^ self clearBit:index |
19053 | 2424 |
]. |
2425 |
^ self setBit:index |
|
2426 |
||
2427 |
" |
|
2428 |
(16r3fffffff changeBit:31 to:1) hexPrintString |
|
2429 |
(16r3fffffff asLargeInteger setBit:31) hexPrintString |
|
2430 |
" |
|
2431 |
! |
|
2432 |
||
2433 |
clearBit:index |
|
2434 |
"return a new integer where the specified bit is off. |
|
2435 |
Bits are counted from 1 starting with the least significant. |
|
2436 |
The method's name may be misleading: the receiver is not changed, |
|
2437 |
but a new number is returned. Should be named #withBitCleared:" |
|
2438 |
||
2439 |
|n "{ Class: SmallInteger }" |
|
2440 |
byteIndex "{ Class: SmallInteger }" |
|
2441 |
bitIndex "{ Class: SmallInteger }" |
|
2442 |
result byte| |
|
2443 |
||
2444 |
index <= 0 ifTrue:[ |
|
19078 | 2445 |
^ SubscriptOutOfBoundsSignal |
2446 |
raiseRequestWith:index |
|
2447 |
errorString:'bit index out of bounds' |
|
19053 | 2448 |
]. |
2449 |
byteIndex := ((index - 1) // 8) + 1. |
|
2450 |
n := self digitLength. |
|
2451 |
byteIndex > n ifTrue:[ |
|
19078 | 2452 |
^ self |
19053 | 2453 |
]. |
2454 |
||
2455 |
result := self simpleDeepCopy. |
|
2456 |
bitIndex := ((index - 1) \\ 8) + 1. |
|
2457 |
byte := (result digitAt:byteIndex) clearBit:bitIndex. |
|
2458 |
result digitAt:byteIndex put:byte. |
|
2459 |
(byte == 0 or:[n == byteIndex and:[n <= SmallInteger maxBytes]]) ifTrue:[ |
|
19078 | 2460 |
^ result compressed |
19053 | 2461 |
]. |
2462 |
^ result |
|
2463 |
||
2464 |
" |
|
2465 |
3111111111 clearBit:1 |
|
2466 |
" |
|
2467 |
||
2468 |
"Modified: / 28.7.1998 / 18:35:50 / cg" |
|
2469 |
! |
|
2470 |
||
2471 |
invertBit:index |
|
2472 |
"return a new number where the specified bit is inverted. |
|
2473 |
Bits are counted from 1 starting with the least significant. |
|
2474 |
The method's name may be misleading: the receiver is not changed, |
|
2475 |
but a new number is returned. Should be named #withBitInverted:" |
|
2476 |
||
2477 |
index <= 0 ifTrue:[ |
|
19078 | 2478 |
^ SubscriptOutOfBoundsSignal |
2479 |
raiseRequestWith:index |
|
2480 |
errorString:'index out of bounds' |
|
19053 | 2481 |
]. |
2482 |
^ self bitXor:(1 bitShift:index-1) |
|
2483 |
||
2484 |
" |
|
2485 |
0 invertBit:3 => 4 (2r100) |
|
2486 |
0 invertBit:48 => 140737488355328 (2r1000.....000) |
|
2487 |
((0 invertBit:99) invertBit:100) printStringRadix:2 |
|
2488 |
" |
|
2489 |
! |
|
2490 |
||
2491 |
isBitClear:index |
|
2492 |
"return true if the index' bit is clear; false otherwise. |
|
2493 |
Bits are counted from 1 starting with the least significant." |
|
2494 |
||
2495 |
^ (self bitAt:index) == 0 |
|
2496 |
||
2497 |
" |
|
2498 |
5 isBitClear:1 => false |
|
2499 |
5 isBitClear:2 => true |
|
2500 |
5 isBitClear:3 => false |
|
2501 |
5 isBitClear:4 => true |
|
2502 |
5 isBitClear:10000 => true |
|
2503 |
2r0101 isBitClear:2 => true |
|
2504 |
2r0101 isBitClear:1 => false |
|
2505 |
2r0101 isBitClear:0 index error |
|
2506 |
" |
|
2507 |
! |
|
2508 |
||
2509 |
isBitSet:index |
|
2510 |
"return true if the index' bit is set; false otherwise. |
|
2511 |
Bits are counted from 1 starting with the least significant." |
|
2512 |
||
2513 |
^ (self bitAt:index) ~~ 0 |
|
2514 |
||
2515 |
" |
|
2516 |
5 isBitSet:3 => true |
|
2517 |
2r0101 isBitSet:2 => false |
|
2518 |
2r0101 isBitSet:1 => true |
|
2519 |
2r0101 isBitSet:0 index error |
|
2520 |
" |
|
7155 | 2521 |
! |
2522 |
||
5209 | 2523 |
setBit:index |
8937 | 2524 |
"return a new integer, where the specified bit is on. |
5209 | 2525 |
Bits are counted from 1 starting with the least significant. |
14404
49a54ea47bf8
comment/format in:5 methods
Claus Gittinger <cg@exept.de>
parents:
14403
diff
changeset
|
2526 |
The method's name may be misleading: the receiver is not changed, |
11499 | 2527 |
but a new number is returned. Should be named #withBitSet:" |
5209 | 2528 |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
2529 |
index <= 0 ifTrue:[ |
19078 | 2530 |
^ SubscriptOutOfBoundsSignal |
2531 |
raiseRequestWith:index |
|
2532 |
errorString:'index out of bounds' |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
2533 |
]. |
5209 | 2534 |
^ self bitOr:(1 bitShift:index-1) |
2535 |
||
2536 |
" |
|
2537 |
0 setBit:3 => 4 (2r100) |
|
2538 |
0 setBit:48 => 140737488355328 (2r1000.....000) |
|
11587 | 2539 |
((0 setBit:99) setBit:100) printStringRadix:2 |
5209 | 2540 |
" |
701 | 2541 |
! ! |
2542 |
||
3890 | 2543 |
!Integer methodsFor:'byte access'! |
2544 |
||
12784 | 2545 |
byteAt:anIndex |
2546 |
"compatibility with ByteArrays etc." |
|
2547 |
||
2548 |
^ self digitAt:anIndex |
|
2549 |
||
2550 |
" |
|
19078 | 2551 |
12345678 byteAt:2 |
2552 |
12345678 digitBytes at:2 |
|
2553 |
||
2554 |
-12345678 byteAt:2 |
|
2555 |
-12345678 digitBytes at:2 |
|
12784 | 2556 |
" |
2557 |
! |
|
2558 |
||
19130 | 2559 |
byteSwapped32 |
2560 |
"a fallback, in case unimplemented in concrete classes. |
|
2561 |
Not actually used" |
|
2562 |
||
2563 |
^ ((self digitAt:1) bitShift:24) |
|
2564 |
+ ((self digitAt:2) bitShift:16) |
|
2565 |
+ ((self digitAt:3) bitShift:8) |
|
2566 |
+ (self digitAt:4) |
|
2567 |
||
2568 |
" |
|
2569 |
16r12345678901234567890 byteSwapped32 |
|
2570 |
" |
|
2571 |
! |
|
2572 |
||
2573 |
byteSwapped64 |
|
2574 |
"a fallback, in case unimplemented in concrete classes. |
|
2575 |
Not actually used" |
|
2576 |
||
2577 |
^ ((self digitAt:1) bitShift:56) |
|
2578 |
+ ((self digitAt:2) bitShift:48) |
|
2579 |
+ ((self digitAt:3) bitShift:40) |
|
2580 |
+ ((self digitAt:4) bitShift:32) |
|
2581 |
+ ((self digitAt:5) bitShift:24) |
|
2582 |
+ ((self digitAt:6) bitShift:16) |
|
2583 |
+ ((self digitAt:7) bitShift:8) |
|
2584 |
+ (self digitAt:8) |
|
2585 |
||
2586 |
" |
|
2587 |
16r1234567890123456789 byteSwapped64 hexPrintString |
|
2588 |
" |
|
2589 |
! |
|
2590 |
||
3890 | 2591 |
digitByteLength |
19327 | 2592 |
<resource: #obsolete> |
19316 | 2593 |
"return the number bytes required for a 2's complement |
2594 |
binary representation of this Integer." |
|
2595 |
||
2596 |
self obsoleteMethodWarning:'use signedDigitLength - attention: return value fixed'. |
|
2597 |
^ self signedDigitLength |
|
2598 |
||
2599 |
" |
|
2600 |
-127 digitByteLength |
|
2601 |
-128 digitByteLength |
|
11587 | 2602 |
-129 digitByteLength |
19315 | 2603 |
-32769 digitByteLength |
2604 |
32768 digitByteLength |
|
3890 | 2605 |
" |
10488 | 2606 |
! |
2607 |
||
2608 |
digitBytes |
|
18851 | 2609 |
"return a byteArray filled with the receiver's bits |
10488 | 2610 |
(8 bits of the absolute value per element), |
2611 |
least significant byte is first" |
|
2612 |
||
2613 |
^ self subclassResponsibility |
|
2614 |
! |
|
2615 |
||
15567 | 2616 |
digitBytesMSB |
18851 | 2617 |
"return a byteArray filled with the receiver's bits |
15567 | 2618 |
(8 bits of the absolute value per element), |
2619 |
most significant byte is first" |
|
2620 |
||
2621 |
^ self subclassResponsibility |
|
2622 |
! |
|
2623 |
||
10488 | 2624 |
digitBytesMSB:msbFlag |
18851 | 2625 |
"return a byteArray filled with the receiver's bits |
10488 | 2626 |
(8 bits of the absolute value per element), |
2627 |
if msbflag = true, most significant byte is first, |
|
2628 |
otherwise least significant byte is first" |
|
2629 |
||
2630 |
msbFlag ifTrue:[ |
|
19078 | 2631 |
^ self digitBytesMSB. |
10488 | 2632 |
]. |
2633 |
^ self digitBytes |
|
2634 |
||
2635 |
" |
|
2636 |
16r12 digitBytesMSB:true |
|
2637 |
16r1234 digitBytesMSB:true |
|
2638 |
16r1234 digitBytesMSB:false |
|
2639 |
16r12345678 digitBytesMSB:true |
|
2640 |
16r12345678 digitBytesMSB:false |
|
2641 |
" |
|
14166 | 2642 |
! |
2643 |
||
19316 | 2644 |
signedDigitLength |
2645 |
"return the number bytes required for a 2's complement |
|
2646 |
binary representation of this Integer. |
|
2647 |
I.e. the number of bytes from which we have to sign extent the highest bit" |
|
2648 |
||
2649 |
|absLen "{ Class: SmallInteger }" | |
|
2650 |
||
2651 |
self >= 0 ifTrue:[ |
|
2652 |
absLen := self digitLength. |
|
2653 |
(self digitByteAt:absLen) >= 16r80 ifTrue:[ |
|
2654 |
^ absLen + 1. |
|
2655 |
]. |
|
2656 |
^ absLen. |
|
2657 |
]. |
|
2658 |
||
2659 |
absLen := self negated digitLength. |
|
2660 |
(self digitByteAt:absLen) < 16r80 ifTrue:[ |
|
2661 |
^ absLen + 1 |
|
2662 |
]. |
|
2663 |
^ absLen |
|
2664 |
||
2665 |
" |
|
2666 |
0 signedDigitLength |
|
2667 |
1 signedDigitLength |
|
2668 |
126 signedDigitLength |
|
2669 |
127 signedDigitLength |
|
2670 |
128 signedDigitLength |
|
2671 |
||
2672 |
255 signedDigitLength |
|
2673 |
256 signedDigitLength |
|
2674 |
257 signedDigitLength |
|
2675 |
||
2676 |
32767 signedDigitLength |
|
2677 |
32768 signedDigitLength |
|
2678 |
||
2679 |
-1 signedDigitLength |
|
2680 |
-127 signedDigitLength |
|
2681 |
-128 signedDigitLength |
|
2682 |
-129 signedDigitLength |
|
2683 |
||
2684 |
-32767 signedDigitLength |
|
2685 |
-32768 signedDigitLength |
|
2686 |
-32769 signedDigitLength |
|
2687 |
" |
|
2688 |
! |
|
2689 |
||
14166 | 2690 |
swapBytes |
16886 | 2691 |
"swap bytes pair-wise in a positive integer |
2692 |
i.e. a.b.c.d -> b.a.d.c |
|
2693 |
Swapping of negative integers is undefined and therefore not supported." |
|
16884 | 2694 |
|
2695 |
|digitBytes| |
|
2696 |
||
2697 |
self negative ifTrue:[ |
|
19078 | 2698 |
RangeError raiseWith:self errorString:'negative numbers are not supported in #swapBytes'. |
16884 | 2699 |
]. |
2700 |
digitBytes := self digitBytes. |
|
2701 |
digitBytes size odd ifTrue:[ |
|
19078 | 2702 |
"ByteArray<<#swapBytes needs even number of bytes. |
2703 |
Add 0 to the most significant position (the end)" |
|
2704 |
digitBytes := digitBytes copyWith:0. |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
2705 |
|
16884 | 2706 |
]. |
2707 |
^ (LargeInteger digitBytes:digitBytes swapBytes) compressed |
|
14166 | 2708 |
|
2709 |
" |
|
19078 | 2710 |
16rFFEE2211 swapBytes hexPrintString |
2711 |
16rFFEEAA2211 swapBytes hexPrintString |
|
2712 |
16r2211 swapBytes hexPrintString |
|
2713 |
16rFF3FFFFF swapBytes |
|
2714 |
self assert:(SmallInteger maxVal swapBytes swapBytes == SmallInteger maxVal) |
|
14166 | 2715 |
" |
3890 | 2716 |
! ! |
2717 |
||
5238 | 2718 |
!Integer methodsFor:'coercing & converting'! |
701 | 2719 |
|
1888 | 2720 |
asFixedPoint |
2721 |
"return the receiver as a fixedPoint number" |
|
2722 |
||
6640
2de8925ad2cd
fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents:
6639
diff
changeset
|
2723 |
^ FixedPoint basicNew |
19078 | 2724 |
setNumerator:self denominator:1 scale:1 |
1888 | 2725 |
|
2726 |
" |
|
11587 | 2727 |
100 asFixedPoint |
2728 |
100 asFixedPoint + 0.1 asFixedPoint |
|
1888 | 2729 |
" |
2730 |
||
2731 |
"Modified: 5.11.1996 / 15:13:17 / cg" |
|
2732 |
! |
|
2733 |
||
2141 | 2734 |
asFixedPoint:scale |
2735 |
"return the receiver as fixedPoint number, with the given number |
|
2736 |
of post-decimal-point digits." |
|
2737 |
||
6639
d88a51b081ae
fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents:
6635
diff
changeset
|
2738 |
^ FixedPoint basicNew |
19078 | 2739 |
setNumerator:self denominator:1 scale:scale |
2141 | 2740 |
|
2741 |
" |
|
11587 | 2742 |
100 asFixedPoint:2 |
2743 |
100 asFixedPoint + (0.1 asFixedPoint:2) |
|
2141 | 2744 |
" |
2745 |
||
2746 |
"Modified: 10.1.1997 / 20:00:08 / cg" |
|
2747 |
! |
|
2748 |
||
7444 | 2749 |
asFloat |
2750 |
"return a Float with same value as myself. |
|
11240 | 2751 |
Since floats have a limited precision, you usually loose bits when doing this." |
7444 | 2752 |
|
2753 |
^ Float fromInteger:self |
|
2754 |
||
2755 |
" |
|
11587 | 2756 |
1234567890 asFloat |
2757 |
1234567890 asFloat asInteger |
|
2758 |
12345678901234567890 asFloat |
|
2759 |
12345678901234567890 asFloat asInteger |
|
7444 | 2760 |
" |
2761 |
! |
|
2762 |
||
701 | 2763 |
asFraction |
2764 |
"return a Fraction with same value as receiver" |
|
2765 |
||
2794
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
2766 |
^ 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
|
2767 |
|
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
2768 |
"Modified: 28.7.1997 / 19:26:06 / cg" |
1 | 2769 |
! |
2770 |
||
701 | 2771 |
asInteger |
11587 | 2772 |
"return the receiver truncated towards zero - |
1 | 2773 |
for integers this is self" |
2774 |
||
2775 |
^ self |
|
1337 | 2776 |
! |
2777 |
||
7444 | 2778 |
asLargeFloat |
2779 |
"return a LargeFloat with same value as myself. |
|
2780 |
Since largeFloats have a limited precision, you usually loose bits when |
|
23053 | 2781 |
doing this. |
2782 |
If the largeFloat class is not present, a regular float is returned" |
|
2783 |
||
23055 | 2784 |
^ (LargeFloat ? LongFloat ? Float) fromInteger:self |
7444 | 2785 |
|
2786 |
" |
|
11587 | 2787 |
1234567890 asLargeFloat |
2788 |
1234567890 asLargeFloat asInteger |
|
2789 |
12345678901234567890 asLargeFloat |
|
2790 |
12345678901234567890 asLargeFloat asInteger |
|
7444 | 2791 |
" |
23053 | 2792 |
|
23055 | 2793 |
"Modified: / 07-06-2018 / 16:28:16 / Claus Gittinger" |
7444 | 2794 |
! |
2795 |
||
2796 |
asLongFloat |
|
2797 |
"return a LongFloat with same value as myself. |
|
2798 |
Since longFloats have a limited precision, you usually loose bits when |
|
2799 |
doing this." |
|
2800 |
||
2801 |
^ LongFloat fromInteger:self |
|
2802 |
||
2803 |
" |
|
11587 | 2804 |
1234567890 asFloat |
2805 |
1234567890 asFloat asInteger |
|
2806 |
12345678901234567890 asFloat |
|
2807 |
12345678901234567890 asFloat asInteger |
|
7444 | 2808 |
" |
2809 |
! |
|
2810 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2811 |
asModuloNumber |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2812 |
"return a precomputed modulo number" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2813 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2814 |
^ ModuloNumber modulus:self. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2815 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2816 |
"Created: / 3.5.1999 / 14:48:03 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2817 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2818 |
|
7444 | 2819 |
asShortFloat |
2820 |
"return a ShortFloat with same value as receiver" |
|
2821 |
||
2822 |
^ ShortFloat fromInteger:self |
|
2823 |
! |
|
2824 |
||
21822 | 2825 |
coerce:aNumber |
2826 |
"convert the argument aNumber into an instance of the receiver's class and return it." |
|
2827 |
||
2828 |
^ aNumber asInteger |
|
2829 |
! |
|
2830 |
||
14043
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
2831 |
signExtended24BitValue |
19257 | 2832 |
"return an integer from sign-extending the 24'th bit. |
19062 | 2833 |
i.e. interprets the lowest 24 bits as a signed integer, |
2834 |
ignoring higher bits. |
|
2835 |
This may be useful for communication interfaces" |
|
14043
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
2836 |
|
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
2837 |
^ (self bitAnd:16rFFFFFF) signExtended24BitValue |
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
2838 |
|
19061 | 2839 |
" |
2840 |
16r800000 signExtended24BitValue |
|
2841 |
16r7FFFFF signExtended24BitValue |
|
2842 |
16rFFFFFF signExtended24BitValue |
|
2843 |
" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
2844 |
|
14043
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
2845 |
"Modified: / 07-05-1996 / 09:31:57 / cg" |
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
2846 |
"Created: / 05-03-2012 / 14:37:55 / cg" |
19257 | 2847 |
"Modified (comment): / 26-02-2016 / 19:39:52 / cg" |
14043
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
2848 |
! |
045fd447ab4d
added: #signExtended24BitValue
Claus Gittinger <cg@exept.de>
parents:
13993
diff
changeset
|
2849 |
|
1337 | 2850 |
signExtendedByteValue |
19257 | 2851 |
"return an integer from sign-extending the 8'th bit. |
19062 | 2852 |
i.e. interprets the lowest 8 bits as a signed integer, |
2853 |
ignoring higher bits. |
|
2854 |
This may be useful for communication interfaces" |
|
1337 | 2855 |
|
2856 |
^ (self bitAnd:16rFF) signExtendedByteValue |
|
2857 |
||
19061 | 2858 |
" |
2859 |
16r80 signExtendedByteValue |
|
2860 |
16r7F signExtendedByteValue |
|
2861 |
16rFF signExtendedByteValue |
|
2862 |
" |
|
2863 |
||
19257 | 2864 |
"Created: / 07-05-1996 / 09:31:52 / cg" |
2865 |
"Modified (comment): / 26-02-2016 / 19:39:46 / cg" |
|
1337 | 2866 |
! |
2867 |
||
19062 | 2868 |
signExtendedFromBit:bitNr |
19257 | 2869 |
"return an integer from sign-extending the n'th bit. |
19062 | 2870 |
i.e. interprets the lowest n bits as a signed integer, |
2871 |
ignoring higher bits. |
|
19257 | 2872 |
The bit numbering is 1-based (i.e. the lowest bit has bitNr 1) |
22073 | 2873 |
This may be useful for communication interfaces. |
2874 |
(kind of the reverse operation to asUnsigned:)." |
|
19062 | 2875 |
|
22070 | 2876 |
^ self signExtendedFromMaskBit:(1 bitShift:bitNr-1) |
2877 |
||
2878 |
" |
|
2879 |
2r111000111 signExtendedFromBit:3 -> 2r11111....111 -> -1 |
|
2880 |
2r111000110 signExtendedFromBit:3 -> 2r11111....110 -> -2 |
|
2881 |
2r111000101 signExtendedFromBit:3 -> 2r11111....101 -> -3 |
|
2882 |
2r111000100 signExtendedFromBit:3 -> 2r11111....100 -> -4 |
|
2883 |
2r111000000 signExtendedFromBit:3 -> 2r00000....000 -> 0 |
|
2884 |
2r111000011 signExtendedFromBit:3 -> 2r00000....011 -> 3 |
|
2885 |
||
2886 |
16r800008 signExtendedFromBit:4 -> -8 |
|
2887 |
16r7FFF07 signExtendedFromBit:4 -> 7 |
|
2888 |
16r7FFF0F signExtendedFromBit:4 -> -1 |
|
2889 |
||
2890 |
16rFFFFFF signExtendedFromBit:8 -> -1 |
|
2891 |
16rFFFF7F signExtendedFromBit:8 -> 127 |
|
2892 |
16rFFFF80 signExtendedFromBit:8 -> -128 |
|
2893 |
" |
|
2894 |
||
22073 | 2895 |
"Modified (comment): / 21-07-2017 / 15:13:55 / cg" |
22070 | 2896 |
! |
2897 |
||
2898 |
signExtendedFromMaskBit:highBitMask |
|
2899 |
"return an integer from sign-extending the bit defined by highMaskBit, |
|
2900 |
which MUST be a single bit (otherwise, you'll get garbage). |
|
2901 |
i.e. interprets the lowest n bits as a signed integer, |
|
2902 |
ignoring higher bits. |
|
2903 |
This may be useful for communication interfaces and to expand bitfields into |
|
2904 |
signed values" |
|
2905 |
||
19062 | 2906 |
|masked| |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
2907 |
|
22070 | 2908 |
masked := self bitAnd:(highBitMask-1). |
2909 |
(self bitTest:highBitMask) ifTrue:[ |
|
2910 |
^ masked - highBitMask |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
2911 |
]. |
19062 | 2912 |
^ masked |
2913 |
||
2914 |
" |
|
22070 | 2915 |
2r111000111 signExtendedFromMaskBit:2r100 -> 2r11111....111 -> -1 |
2916 |
2r111000110 signExtendedFromMaskBit:2r100 -> 2r11111....110 -> -2 |
|
2917 |
2r111000101 signExtendedFromMaskBit:2r100 -> 2r11111....101 -> -3 |
|
2918 |
2r111000100 signExtendedFromMaskBit:2r100 -> 2r11111....100 -> -4 |
|
2919 |
2r111000000 signExtendedFromMaskBit:2r100 -> 2r00000....000 -> 0 |
|
2920 |
2r111000011 signExtendedFromMaskBit:2r100 -> 2r00000....011 -> 3 |
|
2921 |
||
2922 |
16r800008 signExtendedFromMaskBit:2r1000 -> -8 |
|
2923 |
16r7FFF07 signExtendedFromMaskBit:2r1000 -> 7 |
|
2924 |
16r7FFF0F signExtendedFromMaskBit:2r1000 -> -1 |
|
2925 |
||
2926 |
16rFFFFFF signExtendedFromMaskBit:2r10000000 -> -1 |
|
2927 |
16rFFFF7F signExtendedFromMaskBit:2r10000000 -> 127 |
|
2928 |
16rFFFF80 signExtendedFromMaskBit:2r10000000 -> -128 |
|
2929 |
" |
|
2930 |
||
2931 |
"Created: / 21-07-2017 / 14:52:12 / cg" |
|
19062 | 2932 |
! |
2933 |
||
19061 | 2934 |
signExtendedLongLongValue |
19257 | 2935 |
"return an integer from sign-extending the 64'th bit. |
19062 | 2936 |
i.e. interprets the lowest 64 bits as a signed integer, |
2937 |
ignoring higher bits. |
|
2938 |
This may be useful for communication interfaces" |
|
19061 | 2939 |
|
2940 |
(self bitTest:16r8000000000000000) ifTrue:[ |
|
19078 | 2941 |
^ (self bitAnd:16rFFFFFFFFFFFFFFFF)-16r10000000000000000 |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
2942 |
]. |
19061 | 2943 |
^ (self bitAnd:16r7FFFFFFFFFFFFFFF) |
2944 |
||
2945 |
" |
|
19062 | 2946 |
16r1238000000000000000 signExtendedLongLongValue |
2947 |
16r1237FFFFFFFFFFFFFFF signExtendedLongLongValue |
|
2948 |
16r123FFFFFFFFFFFFFFFF signExtendedLongLongValue |
|
19061 | 2949 |
" |
19257 | 2950 |
|
2951 |
"Modified (comment): / 26-02-2016 / 19:38:55 / cg" |
|
19061 | 2952 |
! |
2953 |
||
15084 | 2954 |
signExtendedLongValue |
19257 | 2955 |
"return an integer from sign-extending the 32'th bit. |
19062 | 2956 |
i.e. interprets the lowest 32 bits as a signed integer, |
2957 |
ignoring higher bits. |
|
2958 |
This may be useful for communication interfaces" |
|
15084 | 2959 |
|
19061 | 2960 |
(self bitTest:16r80000000) ifTrue:[ |
19078 | 2961 |
^ (self bitAnd:16rFFFFFFFF) - 16r100000000 |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
2962 |
]. |
19061 | 2963 |
^ (self bitAnd:16r7FFFFFFF) |
2964 |
||
2965 |
" |
|
2966 |
16r80000000 signExtendedLongValue |
|
2967 |
16r7FFFFFFF signExtendedLongValue |
|
2968 |
16rFFFFFFFF signExtendedLongValue |
|
2969 |
" |
|
19257 | 2970 |
|
2971 |
"Modified (comment): / 26-02-2016 / 19:39:09 / cg" |
|
15084 | 2972 |
! |
2973 |
||
1337 | 2974 |
signExtendedShortValue |
19257 | 2975 |
"return an integer from sign-extending the 16'th bit. |
19062 | 2976 |
i.e. interprets the lowest 16 bits as a signed integer, |
2977 |
ignoring higher bits. |
|
2978 |
This may be useful for communication interfaces" |
|
1337 | 2979 |
|
2980 |
^ (self bitAnd:16rFFFF) signExtendedShortValue |
|
2981 |
||
19061 | 2982 |
" |
2983 |
16r8000 signExtendedShortValue |
|
2984 |
16r7FFF signExtendedShortValue |
|
2985 |
16rFFFF signExtendedShortValue |
|
19062 | 2986 |
|
2987 |
16r1238000 signExtendedShortValue |
|
2988 |
16r1237FFF signExtendedShortValue |
|
2989 |
16r123FFFF signExtendedShortValue |
|
19061 | 2990 |
" |
2991 |
||
19257 | 2992 |
"Modified: / 07-05-1996 / 09:31:57 / cg" |
2993 |
"Modified (comment): / 26-02-2016 / 19:39:37 / cg" |
|
1 | 2994 |
! ! |
2995 |
||
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2996 |
!Integer methodsFor:'comparing'! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2997 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2998 |
hash |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2999 |
"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
|
3000 |
|
19553 | 3001 |
^ self bitAnd:SmallInteger maxVal. |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3002 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
3003 |
" |
19078 | 3004 |
-20000000000000 hash |
3005 |
20000000000000 hash |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
3006 |
" |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3007 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
3008 |
"Created: / 14.11.1996 / 12:12:27 / cg" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
3009 |
"Modified: / 24.2.1998 / 10:07:29 / stefan" |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3010 |
! ! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
3011 |
|
16293 | 3012 |
|
12978 | 3013 |
!Integer methodsFor:'dependents access'! |
3014 |
||
3015 |
addDependent:anObject |
|
3016 |
Transcript show:'*** trying to make dependent on an integer: '. |
|
3017 |
thisContext sender printOn:Transcript. Transcript cr. |
|
3018 |
||
3019 |
"Created: / 28-07-2010 / 20:29:00 / cg" |
|
3020 |
! ! |
|
3021 |
||
701 | 3022 |
!Integer methodsFor:'double dispatching'! |
3023 |
||
3024 |
differenceFromFraction:aFraction |
|
3025 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 3026 |
|
701 | 3027 |
|d| |
1 | 3028 |
|
701 | 3029 |
d := aFraction denominator. |
7356 | 3030 |
^ aFraction class |
19078 | 3031 |
numerator:(aFraction numerator - (self * d)) |
3032 |
denominator:d |
|
2793 | 3033 |
|
3034 |
"Modified: 28.7.1997 / 19:08:30 / cg" |
|
1 | 3035 |
! |
3036 |
||
11517 | 3037 |
differenceFromTimestamp:aTimestamp |
3038 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
3039 |
before aTimestamp" |
|
3040 |
||
3041 |
^ aTimestamp subtractSeconds:self. |
|
3042 |
||
3043 |
" |
|
11587 | 3044 |
Timestamp now subtractSeconds:100 |
3045 |
100 differenceFromTimestamp:Timestamp now |
|
11517 | 3046 |
" |
3047 |
! |
|
3048 |
||
7356 | 3049 |
equalFromFraction:aFraction |
3050 |
"that should never be invoked, as fractions are always normalized to integers |
|
3051 |
if resulting from an arithmetic operation. |
|
3052 |
However, this implementation is for subclasses (i.e. fixed point) and also |
|
3053 |
allows comparing unnormalized fractions as might appear within the fraction class" |
|
3054 |
||
3055 |
|denominator numerator| |
|
3056 |
||
3057 |
denominator := aFraction denominator. |
|
3058 |
numerator := aFraction numerator. |
|
3059 |
(denominator == 1) ifFalse:[ |
|
19078 | 3060 |
^ numerator = (self * denominator) |
7356 | 3061 |
]. |
3062 |
^ numerator = self |
|
3063 |
! |
|
3064 |
||
701 | 3065 |
productFromFraction:aFraction |
3066 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 3067 |
|
7356 | 3068 |
^ aFraction class |
19078 | 3069 |
numerator:(self * aFraction numerator) |
3070 |
denominator:aFraction denominator |
|
2793 | 3071 |
|
3072 |
"Modified: 28.7.1997 / 19:08:27 / cg" |
|
1 | 3073 |
! |
3074 |
||
701 | 3075 |
quotientFromFraction:aFraction |
7444 | 3076 |
"Return the quotient of the argument, aFraction and the receiver. |
3077 |
Sent when aFraction does not know how to divide by the receiver." |
|
701 | 3078 |
|
7356 | 3079 |
^ aFraction class |
19078 | 3080 |
numerator:aFraction numerator |
3081 |
denominator:(self * aFraction denominator) |
|
2793 | 3082 |
|
3083 |
"Modified: 28.7.1997 / 19:08:23 / cg" |
|
701 | 3084 |
! |
1 | 3085 |
|
701 | 3086 |
sumFromFraction:aFraction |
3087 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
3088 |
||
3089 |
|d| |
|
3090 |
||
3091 |
d := aFraction denominator. |
|
7356 | 3092 |
^ aFraction class |
19078 | 3093 |
numerator:(aFraction numerator + (self * d)) |
3094 |
denominator:d |
|
2793 | 3095 |
|
3096 |
"Modified: 28.7.1997 / 19:08:11 / cg" |
|
10681 | 3097 |
! |
3098 |
||
3099 |
sumFromTimestamp:aTimestamp |
|
3100 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
3101 |
after aTimestamp" |
|
3102 |
||
3103 |
^ aTimestamp addSeconds:self. |
|
3104 |
||
3105 |
" |
|
3106 |
Timestamp now addSeconds:100 |
|
3107 |
100 sumFromTimestamp:Timestamp now |
|
3108 |
" |
|
1 | 3109 |
! ! |
3110 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3111 |
!Integer methodsFor:'helpers'! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3112 |
|
11587 | 3113 |
gcd_helper:anInteger |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3114 |
"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
|
3115 |
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
|
3116 |
larger than the arg." |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3117 |
|
19251 | 3118 |
| a b aLowBit bLowBit shift t | |
11496 | 3119 |
|
3120 |
a := self. |
|
3121 |
b := anInteger. |
|
3122 |
||
19251 | 3123 |
aLowBit := a lowBit - 1. |
3124 |
bLowBit := b lowBit - 1. |
|
3125 |
shift := aLowBit min:bLowBit. |
|
22213 | 3126 |
b := b rightShift:bLowBit. |
11496 | 3127 |
[a = 0] whileFalse:[ |
22213 | 3128 |
a := a rightShift:aLowBit. |
19078 | 3129 |
a < b ifTrue:[ |
3130 |
t := a. a := b. b := t |
|
3131 |
]. |
|
3132 |
a := a - b. |
|
19251 | 3133 |
aLowBit := a lowBit - 1. |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3134 |
]. |
11496 | 3135 |
^ b bitShift:shift |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3136 |
|
22213 | 3137 |
"Created: / 01-03-1997 / 16:38:17 / cg" |
3138 |
"Modified: / 25-08-2017 / 12:33:09 / cg" |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3139 |
! ! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3140 |
|
10027 | 3141 |
|
12565 | 3142 |
!Integer methodsFor:'iteration'! |
3143 |
||
3144 |
to:stop collect:aBlock |
|
16347 | 3145 |
"syntactic sugar; same as (self to:stop) collect:aBlock" |
12565 | 3146 |
|
3147 |
|n a| |
|
3148 |
||
3149 |
n := stop - self + 1. |
|
3150 |
a := Array new:n. |
|
3151 |
self to:stop do:[:i | |
|
19078 | 3152 |
a at:(i-self+1) put:(aBlock value:i). |
12565 | 3153 |
]. |
3154 |
^ a. |
|
3155 |
||
3156 |
" |
|
3157 |
1 to:10 collect:[:i | i squared] |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3158 |
10 to:20 collect:[:i | i squared] |
12565 | 3159 |
(10 to:20) collect:[:i | i squared] |
3160 |
" |
|
3161 |
! ! |
|
3162 |
||
1 | 3163 |
!Integer methodsFor:'misc math'! |
3164 |
||
701 | 3165 |
acker:n |
1560 | 3166 |
"return the value of acker(self, n). |
18851 | 3167 |
;-) Do not try with receivers > 3" |
701 | 3168 |
|
3169 |
(self == 0) ifTrue:[^ n + 1]. |
|
3170 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
3171 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
3172 |
||
3173 |
" |
|
11587 | 3174 |
3 acker:2 |
3175 |
3 acker:7 |
|
701 | 3176 |
" |
1560 | 3177 |
|
3178 |
"Modified: 18.7.1996 / 13:08:16 / cg" |
|
701 | 3179 |
! |
3180 |
||
11491 | 3181 |
binco:kIn |
13023 | 3182 |
"an alternative name for the binomial coefficient for squeak compatibility" |
3183 |
||
11491 | 3184 |
^ self binomialCoefficient:kIn |
13023 | 3185 |
|
3186 |
"Modified: / 17-08-2010 / 17:29:07 / cg" |
|
11491 | 3187 |
! |
3188 |
||
19040 | 3189 |
binomialCoefficient:k |
3190 |
"The binomial coefficient (n over k) |
|
11486 | 3191 |
|
11476 | 3192 |
/ n \ with self being n, and 0 <= k <= n. |
3193 |
\ k / |
|
3194 |
||
11587 | 3195 |
is the number of ways of picking k unordered outcomes from n possibilities, |
3196 |
also known as a combination or combinatorial number. |
|
11486 | 3197 |
Sometimes also called C(n,k) (for choose k from n) |
3198 |
||
3199 |
binCo is defined as: |
|
19078 | 3200 |
n!! |
11476 | 3201 |
---------- |
3202 |
k!! (n-k)!! |
|
3203 |
||
3204 |
but there is a faster, recursive formula: |
|
3205 |
||
11587 | 3206 |
/ n \ = / n - 1 \ + / n - 1 \ |
11476 | 3207 |
\ k / \ k - 1 / \ k / |
3208 |
||
3209 |
with: |
|
3210 |
||
11587 | 3211 |
/ n \ = / n \ = 1 |
3212 |
\ 0 / \ n / |
|
11476 | 3213 |
" |
3214 |
||
19040 | 3215 |
|kRun acc| |
3216 |
||
3217 |
k > self ifTrue:[^ 0]. |
|
3218 |
||
3219 |
kRun := k. |
|
3220 |
kRun > (self / 2) ifTrue:[ |
|
19078 | 3221 |
"/ symmetry |
3222 |
kRun := self - kRun. |
|
11476 | 3223 |
]. |
3224 |
||
3225 |
acc := 1. |
|
19040 | 3226 |
1 to:kRun do:[:i | |
19078 | 3227 |
acc := acc * (self - kRun + i) / i. |
11476 | 3228 |
]. |
3229 |
^ acc |
|
3230 |
||
3231 |
" |
|
11587 | 3232 |
(7 binomialCoefficient:3) |
3233 |
(10 binomialCoefficient:5) |
|
3234 |
(100 binomialCoefficient:5) |
|
3235 |
(1000 binomialCoefficient:5) |
|
11486 | 3236 |
|
11476 | 3237 |
TestCase assert: (10 binomialCoefficient:5) = (10 factorial / (5 factorial * 5 factorial)) |
11587 | 3238 |
TestCase assert: (100 binomialCoefficient:78) = (100 factorial / (78 factorial * (100-78) factorial)) |
3239 |
TestCase assert: (1000 binomialCoefficient:5) = (1000 factorial / (5 factorial * (1000-5) factorial)) |
|
3240 |
TestCase assert: (10000 binomialCoefficient:78) = (10000 factorial / (78 factorial * (10000-78) factorial)) |
|
11486 | 3241 |
|
3242 |
Time millisecondsToRun:[ (10000 binomialCoefficient:78) ] -> 0 |
|
19252 | 3243 |
Time millisecondsToRun:[ (10000 factorial / (78 factorial * (10000-78) factorial)) ] -> 130 |
11486 | 3244 |
|
11476 | 3245 |
" |
3246 |
! |
|
3247 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3248 |
divMod:aNumber |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3249 |
"return an array filled with |
19078 | 3250 |
(self // aNumber) and (self \\ aNumber). |
17037 | 3251 |
The returned remainder has the same sign as aNumber. |
3252 |
The following is always true: |
|
19078 | 3253 |
(receiver // something) * something + (receiver \\ something) = receiver |
17037 | 3254 |
|
3255 |
Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3. |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3256 |
Especially surprising: |
19078 | 3257 |
-1 \\ 10 -> 9 (because -(1/10) is truncated towards next smaller integer, which is -1, |
3258 |
and -1 multiplied by 10 gives -10, so we have to add 9 to get the original -1). |
|
3259 |
-10 \\ 3 -> 2 (because -(10/3) is truncated towards next smaller integer, which is -4, |
|
3260 |
and -4 * 4 gives -12, so we need to add 2 to get the original -10. |
|
17037 | 3261 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3262 |
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
|
3263 |
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
|
3264 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3265 |
^ Array |
19078 | 3266 |
with:(self // aNumber) |
3267 |
with:(self \\ aNumber) |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3268 |
|
11587 | 3269 |
" |
17037 | 3270 |
10 divMod:3 -> #(3 1) because 3*3 + 1 = 10 |
3271 |
10 divMod:-3 -> #(-4 -2) because -4*-3 + (-2) = 10 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3272 |
-10 divMod:3 -> #(-4 2) because -4*-3 + 2 = -10 |
17037 | 3273 |
-10 divMod:-3 -> #(3 -1) because -3*3 + (-1) = -10 |
3274 |
||
3275 |
1000000000000000000000 divMod:3 -> #(333333333333333333333 1) |
|
3276 |
1000000000000000000000 divMod:-3 -> #(-333333333333333333334 -2) |
|
3277 |
-1000000000000000000000 divMod:3 -> #(-333333333333333333334 2) |
|
3278 |
-1000000000000000000000 divMod:-3 -> #(333333333333333333333 -1) |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3279 |
100 factorial divMod:103 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3280 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3281 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3282 |
"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
|
3283 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3284 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3285 |
extendedEuclid:tb |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3286 |
"return the solution of 'ax + by = gcd(a,b)'. |
7356 | 3287 |
An array containing x, y and gcd(a,b) is returned." |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3288 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3289 |
|a b gcd gcd1 u u1 v v1 tmp t swap shift "{SmallInteger}"| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3290 |
|
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
3291 |
self < tb ifTrue:[ |
19078 | 3292 |
a := self. |
3293 |
b := tb. |
|
3294 |
swap := false. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3295 |
] ifFalse:[ |
19078 | 3296 |
a := tb. |
3297 |
b := self. |
|
3298 |
swap := true. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3299 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3300 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3301 |
shift := ((a lowBit) min:(b lowBit))-1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3302 |
shift > 0 ifTrue:[ |
19078 | 3303 |
tmp := shift negated. |
3304 |
a := a bitShift:tmp. |
|
3305 |
b := b bitShift:tmp. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3306 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3307 |
|
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
3308 |
gcd := a. |
11587 | 3309 |
gcd1 := b. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3310 |
u := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3311 |
u1 := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3312 |
v := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3313 |
v1 := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3314 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3315 |
[ |
19251 | 3316 |
"/ The following condition is true: |
3317 |
"/ (a * u1) + (b * v1) ~= gcd1 ifTrue:[self halt]. |
|
19078 | 3318 |
t := gcd1 divMod:gcd. |
3319 |
gcd1 := gcd. |
|
3320 |
gcd := t at:2. |
|
3321 |
t := t at:1. |
|
3322 |
tmp := v. |
|
19251 | 3323 |
"/ v1 - (v * t) - v1 + (v * t) ~= 0 ifTrue:[self halt]. |
19078 | 3324 |
v := v1 - (v * t). |
3325 |
v1 := tmp. |
|
3326 |
tmp := u. |
|
19251 | 3327 |
"/ u1 - (u * t) - u1 + (u * t) ~= 0 ifTrue:[self halt]. |
19078 | 3328 |
u := u1 - (u * t). |
3329 |
u1 := tmp. |
|
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
3330 |
gcd > 0] whileTrue. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3331 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3332 |
shift > 0 ifTrue:[ |
19078 | 3333 |
gcd1 := gcd1 bitShift:shift. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3334 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3335 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3336 |
swap ifTrue:[ |
19078 | 3337 |
^ Array with:v1 with:u1 with:gcd1. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3338 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3339 |
^ Array with:u1 with:v1 with:gcd1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3340 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3341 |
|
11587 | 3342 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3343 |
14 extendedEuclid:5 |
11587 | 3344 |
14 extendedEuclid:2 |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3345 |
25 extendedEuclid:15 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3346 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3347 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3348 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
3349 |
"Modified: / 18.11.1999 / 16:19:24 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3350 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3351 |
|
1 | 3352 |
factorial |
19251 | 3353 |
"return fac(self) (i.e. 1*2*3...*self). |
19161 | 3354 |
This chooses a good algorithm, based on the receiver. |
19337 | 3355 |
Some heuristics here, which has to do with the speed of largeInteger arithmetic." |
19161 | 3356 |
|
3357 |
(self <= 20) ifTrue:[ |
|
19078 | 3358 |
self < 0 ifTrue:[ |
3359 |
"/ |
|
3360 |
"/ requested factorial of a negative number |
|
3361 |
"/ |
|
3362 |
^ self class |
|
3363 |
raise:#domainErrorSignal |
|
3364 |
receiver:self |
|
3365 |
selector:#factorial |
|
3366 |
arguments:#() |
|
3367 |
errorString:'factorial of negative number' |
|
3368 |
]. |
|
19161 | 3369 |
^ #(1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800 |
3370 |
479001600 6227020800 87178291200 1307674368000 20922789888000 |
|
3371 |
355687428096000 6402373705728000 121645100408832000 |
|
3372 |
2432902008176640000) at:self+1 |
|
3373 |
]. |
|
3374 |
||
19163 | 3375 |
"/ self < 80000 ifTrue:[ |
3376 |
"/ ^ self factorialHalf |
|
3377 |
"/ ]. |
|
19161 | 3378 |
^ self factorialEvenOdd |
3379 |
||
3380 |
" |
|
3381 |
10 factorial |
|
3382 |
100 factorial |
|
3383 |
1000 factorial |
|
3384 |
10000 factorial |
|
3385 |
100000 factorial |
|
3386 |
200000 factorial |
|
3387 |
300000 factorial |
|
3388 |
1000000 factorial |
|
3389 |
||
3390 |
Time millisecondsToRun:[10000 factorial]40 |
|
3391 |
Time millisecondsToRun:[100000 factorial]3220 |
|
3392 |
Time millisecondsToRun:[1000000 factorial]357120 |
|
19164 | 3393 |
|
3394 |
#(factorialIter factorialHalf factorialEvenOdd factorial) |
|
3395 |
do:[:sel | |
|
3396 |
#( (10000 10) |
|
3397 |
(20000 10) |
|
3398 |
(50000 10) |
|
3399 |
(70000 10) |
|
3400 |
(100000 5) |
|
3401 |
(200000 3) |
|
3402 |
(300000 3) |
|
3403 |
(400000 3)) pairsDo:[:n :repeat | |
|
3404 |
|times| |
|
3405 |
times := (1 to:repeat) collect:[:i | |
|
3406 |
Time millisecondsToRun:[ n perform:sel] |
|
3407 |
]. |
|
3408 |
||
3409 |
Transcript printf:'%12s %6d: %5d\n' with:sel with:n with:times min |
|
3410 |
] |
|
3411 |
]. |
|
3412 |
||
3413 |
factorialIter 10000: 30 |
|
3414 |
factorialIter 20000: 130 |
|
3415 |
factorialIter 50000: 790 |
|
3416 |
factorialIter 70000: 1710 |
|
3417 |
factorialIter 100000: 4880 |
|
3418 |
factorialIter 200000: 24980 |
|
3419 |
factorialIter 300000: 60060 |
|
3420 |
factorialIter 400000: 112310 |
|
3421 |
factorialHalf 10000: 20 |
|
3422 |
factorialHalf 20000: 100 |
|
3423 |
factorialHalf 50000: 690 |
|
3424 |
factorialHalf 70000: 1430 |
|
3425 |
factorialHalf 100000: 3220 |
|
3426 |
factorialHalf 200000: 28340 |
|
3427 |
factorialHalf 300000: 68740 |
|
3428 |
factorialHalf 400000: 127490 |
|
3429 |
factorialEvenOdd 10000: 10 |
|
3430 |
factorialEvenOdd 20000: 60 |
|
3431 |
factorialEvenOdd 50000: 390 |
|
3432 |
factorialEvenOdd 70000: 810 |
|
3433 |
factorialEvenOdd 100000: 2020 |
|
3434 |
factorialEvenOdd 200000: 9960 |
|
3435 |
factorialEvenOdd 300000: 24480 |
|
3436 |
factorialEvenOdd 400000: 45340 |
|
3437 |
factorial 10000: 20 |
|
3438 |
factorial 20000: 100 |
|
3439 |
factorial 50000: 680 |
|
3440 |
factorial 70000: 1400 |
|
3441 |
factorial 100000: 2040 |
|
3442 |
factorial 200000: 10130 |
|
3443 |
factorial 300000: 24670 |
|
19161 | 3444 |
" |
3445 |
! |
|
3446 |
||
3447 |
factorialEvenOdd |
|
22166 | 3448 |
"a recursive odd-even algorithm, which processes smaller largeInts in the loop. |
3449 |
Because multiplication is an O(n^2) algorithm, there is a threshold from which |
|
3450 |
more but smaller multiplications makes a noticable difference" |
|
19161 | 3451 |
|
3452 |
|pO i s2 t stop| |
|
3453 |
||
3454 |
(self <= 20) ifTrue:[ |
|
3455 |
^ #(1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800 |
|
3456 |
479001600 6227020800 87178291200 1307674368000 20922789888000 |
|
3457 |
355687428096000 6402373705728000 121645100408832000 |
|
3458 |
2432902008176640000) at:self+1 |
|
3459 |
]. |
|
3460 |
||
3461 |
"/ |
|
3462 |
"/ 3 * 4 * 5 * 6 *7 * 8 .... * n |
|
3463 |
"/ odd numbers: |
|
3464 |
"/ 3 5 7 9 ... n |
|
19251 | 3465 |
"/ even numbers: |
19161 | 3466 |
"/ 2 4 6 8 ... n |
3467 |
"/ 1 2 3 4 ... n//2 |
|
3468 |
"/ is (n/2)!! << n-1 |
|
3469 |
||
3470 |
pO := 1. |
|
3471 |
i := 3. |
|
3472 |
||
3473 |
"/ odds only in pairs as |
|
3474 |
"/ i * (i + 2) |
|
3475 |
"/ to get to the next pair, |
|
3476 |
"/ (i+4)(i+6) |
|
3477 |
"/ we add: 8i + 24 |
|
3478 |
"/ (i+4)(i+6)-(i*(i+2)) |
|
3479 |
"/ i^2 + 10i + 24 - i^2 - 2i |
|
3480 |
"/ 8i + 24 |
|
3481 |
stop := self-2. |
|
3482 |
t := i*(i+2). |
|
3483 |
[i <= stop] whileTrue:[ |
|
3484 |
"/ odd*next odd |
|
3485 |
pO := pO * t. |
|
19163 | 3486 |
t := t + ((i*8) + 24). |
19161 | 3487 |
i := i + 4. |
3488 |
]. |
|
3489 |
||
3490 |
[i <= self] whileTrue:[ |
|
3491 |
"/ odd |
|
3492 |
pO := pO * i. |
|
3493 |
i := i + 2. |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3494 |
]. |
19161 | 3495 |
|
3496 |
"/ the factorial of the evens... |
|
3497 |
s2 := (self//2). |
|
3498 |
^ (s2 factorialEvenOdd * pO) << s2. |
|
3499 |
||
3500 |
" |
|
3501 |
(6 to:2000) conform:[:i | i factorialIter = i factorialEvenOdd] |
|
3502 |
||
3503 |
Time millisecondsToRun:[20000 factorialIter] |
|
3504 |
Time millisecondsToRun:[50000 factorialIter] |
|
3505 |
Time millisecondsToRun:[70000 factorialIter] |
|
3506 |
Time millisecondsToRun:[100000 factorialIter] |
|
3507 |
Time millisecondsToRun:[200000 factorialIter] |
|
3508 |
||
3509 |
Time millisecondsToRun:[20000 factorialEvenOdd] |
|
3510 |
Time millisecondsToRun:[50000 factorialEvenOdd] |
|
3511 |
Time millisecondsToRun:[70000 factorialEvenOdd] |
|
3512 |
Time millisecondsToRun:[100000 factorialEvenOdd] |
|
3513 |
Time millisecondsToRun:[200000 factorialEvenOdd] |
|
3514 |
" |
|
22166 | 3515 |
|
3516 |
"Modified (comment): / 05-07-2017 / 16:41:41 / cg" |
|
19161 | 3517 |
! |
3518 |
||
3519 |
factorialHalf |
|
19251 | 3520 |
"an algorithm, which does it with half the number of multiplications. |
19161 | 3521 |
this is faster than factorialPM to roughly 60000." |
3522 |
||
3523 |
|p i d| |
|
3524 |
||
3525 |
i := self. |
|
3526 |
self odd ifTrue:[ |
|
3527 |
i := i - 1. |
|
3528 |
]. |
|
20269 | 3529 |
|
19161 | 3530 |
p := i. |
3531 |
d := i - 2. |
|
3532 |
[d >= 2] whileTrue:[ |
|
3533 |
i := i + d. |
|
3534 |
p := p * i. |
|
3535 |
d := d - 2. |
|
3536 |
]. |
|
3537 |
self odd ifTrue:[ |
|
3538 |
p := p * self |
|
20269 | 3539 |
]. |
19161 | 3540 |
^ p |
3541 |
||
3542 |
" |
|
3543 |
10 factorial 3628800 |
|
3544 |
10 factorialHalf 3628800 |
|
20269 | 3545 |
|
19161 | 3546 |
11 factorial 39916800 |
3547 |
11 factorialHalf 39916800 |
|
3548 |
||
3549 |
12 factorial 479001600 |
|
20269 | 3550 |
12 factorialHalf 479001600 |
19161 | 3551 |
|
3552 |
10000 factorial = 10000 factorialHalf |
|
3553 |
||
3554 |
(6 to:2000) conform:[:i | i factorialIter = i factorialHalf] |
|
20269 | 3555 |
|
19161 | 3556 |
Time microsecondsToRun:[30 factorialIter] |
3557 |
Time microsecondsToRun:[30 factorialHalf] |
|
3558 |
Time microsecondsToRun:[50 factorialIter] |
|
3559 |
Time microsecondsToRun:[50 factorialHalf] |
|
3560 |
Time microsecondsToRun:[75 factorialIter] |
|
3561 |
Time microsecondsToRun:[75 factorialHalf] |
|
3562 |
Time microsecondsToRun:[100 factorialIter] |
|
3563 |
Time microsecondsToRun:[100 factorialHalf] |
|
3564 |
Time microsecondsToRun:[500 factorialIter] |
|
3565 |
Time microsecondsToRun:[500 factorialHalf] |
|
3566 |
Time microsecondsToRun:[1000 factorialIter] |
|
3567 |
Time microsecondsToRun:[1000 factorialHalf] |
|
3568 |
Time microsecondsToRun:[2000 factorialIter] |
|
3569 |
Time microsecondsToRun:[2000 factorialHalf] |
|
3570 |
||
3571 |
Time microsecondsToRun:[500 factorial]118 120 120 |
|
3572 |
Time microsecondsToRun:[1000 factorial]339 355 406 |
|
3573 |
Time microsecondsToRun:[5000 factorial]15703 13669 7715 |
|
3574 |
Time millisecondsToRun:[10000 factorial]40 30 50 |
|
3575 |
Time millisecondsToRun:[20000 factorial]140 150 150 |
|
3576 |
Time millisecondsToRun:[40000 factorial]600 570 560 570 |
|
3577 |
Time millisecondsToRun:[60000 factorial]1220 1240 1340 |
|
3578 |
Time millisecondsToRun:[80000 factorial]2600 2580 2540 |
|
3579 |
Time millisecondsToRun:[100000 factorial]4680 4810 5280 |
|
3580 |
Time millisecondsToRun:[120000 factorial]8100 8010 7920 |
|
3581 |
Time millisecondsToRun:[150000 factorial]13830 14040 13360 |
|
20269 | 3582 |
Time millisecondsToRun:[200000 factorial]23880 23740 |
19161 | 3583 |
|
3584 |
Time microsecondsToRun:[500 factorialHalf]150 142 192 |
|
3585 |
Time microsecondsToRun:[1000 factorialHalf]383 527 684 |
|
3586 |
Time microsecondsToRun:[5000 factorialHalf]6654 9221 4629 |
|
3587 |
Time millisecondsToRun:[10000 factorialHalf]20 30 20 |
|
3588 |
Time millisecondsToRun:[20000 factorialHalf]110 110 110 |
|
3589 |
Time millisecondsToRun:[40000 factorialHalf]490 490 490 |
|
3590 |
Time millisecondsToRun:[60000 factorialHalf]1100 1090 1070 |
|
3591 |
Time millisecondsToRun:[80000 factorialHalf]1920 1920 1880 |
|
3592 |
Time millisecondsToRun:[100000 factorialHalf]3030 3010 3000 |
|
3593 |
Time millisecondsToRun:[120000 factorialHalf]4830 4770 4760 |
|
3594 |
Time millisecondsToRun:[150000 factorialHalf]14510 13940 13900 |
|
20269 | 3595 |
Time millisecondsToRun:[200000 factorialHalf]28730 28160 |
19161 | 3596 |
" |
3597 |
! |
|
3598 |
||
3599 |
factorialIter |
|
3600 |
"return fac(self) (i.e. 1*2*3...*self) using an iterative algorithm. |
|
3601 |
This is slightly faster than the recursive algorithm, and does not |
|
3602 |
suffer from stack overflow problems (with big receivers)" |
|
3603 |
||
3604 |
|p i| |
|
3605 |
||
1560 | 3606 |
p := 2. |
2745 | 3607 |
i := 3. |
3608 |
[i <= self] whileTrue:[ |
|
19078 | 3609 |
p := p * i. |
3610 |
i := i + 1. |
|
1560 | 3611 |
]. |
3612 |
^ p |
|
3613 |
||
11587 | 3614 |
" |
1560 | 3615 |
10 factorial |
3616 |
1000 factorial |
|
3617 |
10000 factorial |
|
3618 |
10000 factorialR |
|
3619 |
||
11587 | 3620 |
Time millisecondsToRun:[2000 factorial] |
3621 |
Time millisecondsToRun:[2000 factorialR] |
|
2745 | 3622 |
-1 factorial |
1560 | 3623 |
" |
3624 |
||
10027 | 3625 |
"Modified: / 04-10-2006 / 14:31:12 / cg" |
1560 | 3626 |
! |
3627 |
||
3628 |
factorialR |
|
3908 | 3629 |
"return fac(self) (i.e. 1*2*3...*self) using a recursive algorithm. |
11496 | 3630 |
|
3908 | 3631 |
This is included to demonstration purposes - if you really need |
19251 | 3632 |
factorial numbers, use the tuned #factorial, which is |
3908 | 3633 |
faster and does not suffer from stack overflow problems (with big receivers)." |
1 | 3634 |
|
356 | 3635 |
(self >= 2) ifTrue:[ |
19078 | 3636 |
^ self * (self - 1) factorialR |
1 | 3637 |
]. |
356 | 3638 |
^ 1 |
212 | 3639 |
|
3640 |
" |
|
1560 | 3641 |
10 factorialR |
3642 |
1000 factorialR |
|
4158 | 3643 |
Time millisecondsToRun:[10000 factorial] |
212 | 3644 |
" |
1560 | 3645 |
|
3908 | 3646 |
"Created: / 18.7.1996 / 12:48:36 / cg" |
4158 | 3647 |
"Modified: / 8.5.1999 / 18:40:13 / cg" |
1 | 3648 |
! |
3649 |
||
11896 | 3650 |
fib |
3651 |
"compute the fibionacci number for the receiver. |
|
19078 | 3652 |
fib(0) := 0 |
3653 |
fib(1) := 1 |
|
3654 |
fib(n) := fib(n-1) + fib(n-2)" |
|
11896 | 3655 |
|
3656 |
self <= 0 ifTrue:[ |
|
19078 | 3657 |
self == 0 ifTrue:[^ 0]. |
11896 | 3658 |
]. |
3659 |
^ self fib_helper |
|
3660 |
||
3661 |
" |
|
11945 | 3662 |
30 fib |
11896 | 3663 |
60 fib |
3664 |
1000 fib |
|
3665 |
" |
|
3666 |
! |
|
3667 |
||
3668 |
fib_helper |
|
3669 |
"compute the fibionacci number for the receiver. |
|
3670 |
||
19078 | 3671 |
Fib(n) = Fib(n-1) + Fib(n-2) |
13023 | 3672 |
|
3673 |
Knuth: |
|
19078 | 3674 |
Fib(n+m) = Fib(m) * Fib(n+1) + Fib(m-1) * Fib(n) |
13023 | 3675 |
|
3676 |
This is about 3 times faster than fib_iterative. |
|
11896 | 3677 |
" |
3678 |
||
3679 |
|fibUsingDict dict| |
|
3680 |
||
3681 |
dict := Dictionary new:100. |
|
3682 |
||
3683 |
fibUsingDict := [:x | |
|
19078 | 3684 |
|n fib fibN fibNp1 fibNm1 fibXm1 fibXm2 fibXp1| |
3685 |
||
3686 |
x <= 30 ifTrue:[ |
|
3687 |
"/ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
3688 |
fib := #(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 |
|
3689 |
) at:x |
|
3690 |
] ifFalse:[ |
|
3691 |
fib := dict at:x ifAbsent:nil. |
|
3692 |
fib isNil ifTrue:[ |
|
3693 |
fibXm1 := dict at:(x-1) ifAbsent:nil. |
|
3694 |
fibXm1 notNil ifTrue:[ |
|
3695 |
fibXm2 := dict at:(x-2) ifAbsent:nil. |
|
3696 |
fibXm2 notNil ifTrue:[ |
|
3697 |
fib := fibXm1 + fibXm2. |
|
3698 |
] ifFalse:[ |
|
3699 |
fibXp1 := dict at:(x+1) ifAbsent:nil. |
|
3700 |
fibXp1 notNil ifTrue:[ |
|
3701 |
fib := fibXp1 - fibXm1. |
|
3702 |
] |
|
3703 |
] |
|
3704 |
]. |
|
3705 |
||
3706 |
fib isNil ifTrue:[ |
|
3707 |
n := x // 2. |
|
3708 |
x odd ifTrue:[ |
|
3709 |
"/ m is set to n+1; therefore: |
|
3710 |
"/ Fib(x) = Fib(n+n+1) ; x odd; setting n = (x-1)/2 |
|
3711 |
"/ using Knuth: |
|
3712 |
"/ Fib(n+n+1) = Fib(n+1) * Fib(n+1) + Fib(n+1-1) * Fib(n) |
|
3713 |
"/ = (Fib(n+1) ^ 2) + (Fib(n) ^ 2) |
|
3714 |
fibN := fibUsingDict value:n. |
|
3715 |
fibNp1 := fibUsingDict value:(n+1). |
|
3716 |
fib := fibN squared + fibNp1 squared |
|
3717 |
] ifFalse:[ |
|
3718 |
"/ as |
|
3719 |
"/ Fib(n+1) = Fib(n) + Fib(n-1) |
|
3720 |
"/ therefore: |
|
3721 |
"/ Fib(n) = Fib(n+1) - Fib(n-1) |
|
3722 |
"/ and, since n is even, n+1 and n-1 are odd, and can be computed as above. |
|
3723 |
"/ This gives us: |
|
3724 |
"/ Fib(x) = Fib(x+1) - Fib(x-1) ; x even; setting n = x/2 |
|
3725 |
"/ = Fib(n+n+1) - Fib(n+n-1) |
|
3726 |
"/ = Fib(n+n+1) - Fib((n-1)+(n-1)+1) |
|
3727 |
"/ = ((Fib(n+1)^2) + (Fib(n)^2)) - ((Fib((n-1)+1)^2) + (Fib((n-1))^2)) |
|
3728 |
"/ = (Fib(n+1)^2) + (Fib(n)^2) - (Fib(n)^2) - (Fib((n-1))^2) |
|
3729 |
"/ = (Fib(n+1)^2) - (Fib((n-1))^2) |
|
3730 |
fibNm1 := fibUsingDict value:(n-1). |
|
3731 |
fibNp1 := fibUsingDict value:(n+1). |
|
3732 |
fib := fibNp1 squared - fibNm1 squared |
|
3733 |
]. |
|
3734 |
]. |
|
3735 |
dict at:x put:fib. |
|
3736 |
] |
|
3737 |
]. |
|
3738 |
fib |
|
11896 | 3739 |
]. |
3740 |
||
3741 |
^ fibUsingDict value:self |
|
3742 |
||
3743 |
"the running time is mostly dictated by the LargeInteger multiplication performance... |
|
22460 | 3744 |
(therefore, we get O(n²) execution times, even for a linear number of multiplications) |
11896 | 3745 |
|
3746 |
Time millisecondsToRun:[50000 fib_iterative] 312 (DUO 1.7Ghz CPU) |
|
3747 |
Time millisecondsToRun:[50000 fib_helper] 109 |
|
3748 |
||
11945 | 3749 |
Time millisecondsToRun:[100000 fib_iterative] 1248 |
11896 | 3750 |
Time millisecondsToRun:[100000 fib_helper] 374 |
3751 |
||
11945 | 3752 |
Time millisecondsToRun:[200000 fib_iterative] 4758 |
11896 | 3753 |
Time millisecondsToRun:[200000 fib_helper] 1544 |
3754 |
||
11945 | 3755 |
Time millisecondsToRun:[400000 fib_iterative] 18892 |
11896 | 3756 |
Time millisecondsToRun:[400000 fib_helper] 6084 |
3757 |
||
3758 |
1 to:100 do:[:i | self assert:(i fib_iterative = i fib_helper) ] |
|
3759 |
1 to:100 do:[:i | self assert:(i fib_iterative = i fib) ] |
|
3760 |
" |
|
13023 | 3761 |
|
3762 |
"Modified: / 17-08-2010 / 17:29:34 / cg" |
|
11896 | 3763 |
! |
3764 |
||
11587 | 3765 |
gcd:anInteger |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
3766 |
"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
|
3767 |
Euclids & Knuths algorithm." |
1 | 3768 |
|
11496 | 3769 |
|a b t| |
3770 |
||
3771 |
a := self abs. |
|
3772 |
b := anInteger abs. |
|
3773 |
||
3774 |
a < b ifTrue:[ |
|
19078 | 3775 |
t := a. |
3776 |
a := b. |
|
3777 |
b := t. |
|
1 | 3778 |
]. |
52 | 3779 |
|
11496 | 3780 |
b = 0 ifTrue: [^ a]. |
3781 |
a := a \\ b. |
|
3782 |
a = 0 ifTrue:[^ b]. |
|
3783 |
^ b gcd_helper:a |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3784 |
|
11587 | 3785 |
" |
3786 |
3141589999999999 gcd:1000000000000000 |
|
11496 | 3787 |
|
3788 |
Time millisecondsToRun:[ |
|
19078 | 3789 |
10000 timesRepeat:[ |
3790 |
123456789012345678901234567890 gcd: 9876543210987654321 |
|
3791 |
] |
|
11496 | 3792 |
] |
3060 | 3793 |
" |
3794 |
||
3795 |
"Modified: / 25.10.1997 / 16:08:45 / cg" |
|
1 | 3796 |
! |
3797 |
||
21938 | 3798 |
integerLog10 |
3799 |
"return the floor of log10 of the receiver. |
|
3800 |
This is the same as (self log:10) floor. |
|
3801 |
Used to find out the number of digits needed |
|
3802 |
to print a number/and for conversion to a LargeInteger." |
|
3803 |
||
3804 |
^ self asFloat log10 floor |
|
3805 |
||
3806 |
" |
|
3807 |
10 integerLog10 |
|
3808 |
1000 integerLog10 |
|
3809 |
||
3810 |
10000000000000000.0 log:10 |
|
3811 |
10000000000000000 integerLog10 |
|
3812 |
100000000000000000 integerLog10 |
|
3813 |
1000000000000000000 integerLog10 |
|
3814 |
10000000000000000000 integerLog10 |
|
3815 |
100000000000000000000 integerLog10 |
|
3816 |
1000000000000000000000 integerLog10 -> 21 |
|
3817 |
1000000000000000000000000000000 integerLog10 -> 30 |
|
3818 |
10000000000000000000000000000000000000000 integerLog10 -> 40 |
|
3819 |
||
3820 |
1 to:10000 by:10 do:[:i | |
|
3821 |
self assert:(i factorial printString size == (i factorial integerLog10+1)) |
|
3822 |
]. |
|
3823 |
21 factorial printString size |
|
3824 |
21 factorial integerLog10 |
|
3825 |
51 factorial printString size |
|
3826 |
51 factorial integerLog10 |
|
3827 |
" |
|
3828 |
||
3829 |
"Created: / 02-07-2017 / 01:20:49 / cg" |
|
3830 |
"Modified: / 02-07-2017 / 10:16:40 / cg" |
|
3831 |
! |
|
3832 |
||
14097 | 3833 |
integerLog2 |
14491 | 3834 |
"return the floor of log2 of the receiver. |
3835 |
This is the same as (self log:2) floor." |
|
14097 | 3836 |
|
3837 |
self <= 0 ifTrue:[ |
|
19078 | 3838 |
^ self class |
3839 |
raise:#domainErrorSignal |
|
3840 |
receiver:self |
|
3841 |
selector:#integerLog2 |
|
3842 |
arguments:#() |
|
3843 |
errorString:'logarithm of negative integer' |
|
14097 | 3844 |
]. |
3845 |
^ self highBit - 1. |
|
3846 |
||
3847 |
" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3848 |
2 log:2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3849 |
2 integerLog2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3850 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3851 |
3 log:2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3852 |
3 integerLog2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3853 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3854 |
4 log:2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3855 |
4 integerLog2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3856 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3857 |
64 integerLog2 |
14097 | 3858 |
100 integerLog2 |
3859 |
100 log:2 |
|
3860 |
999 integerLog2 |
|
3861 |
999 log:2 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3862 |
120000 integerLog2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3863 |
120000 log:2 |
14097 | 3864 |
-1 integerLog2 |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3865 |
50 factorial integerLog2 |
14767 | 3866 |
50 factorial log:2 |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3867 |
1000 factorial integerLog2 |
14767 | 3868 |
1000 factorial log:2 -- float error!! |
14097 | 3869 |
" |
3870 |
! |
|
3871 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3872 |
integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3873 |
"return an integer representing 1/self * 2**n. |
11896 | 3874 |
Where an integer is one bit longer than self. |
3875 |
This is a helper for modulu numbers" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3876 |
|
19231 | 3877 |
|b "{Class: SmallInteger}" rem result digitBytes| |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3878 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3879 |
b := self highBit. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3880 |
rem := 1 bitShift:b. |
10488 | 3881 |
result := LargeInteger basicNew numberOfDigits:(b // 8)+1. |
19231 | 3882 |
digitBytes := result digitBytes. |
3883 |
b+1 to:1 by:-1 do:[:idx| |
|
19078 | 3884 |
rem >= self ifTrue:[ |
19231 | 3885 |
rem := rem - self. |
3886 |
digitBytes bitSetAt:idx. |
|
19078 | 3887 |
]. |
3888 |
rem := rem mul2. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3889 |
]. |
11587 | 3890 |
^ result compressed. |
3891 |
||
3892 |
" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3893 |
333 integerReciprocal (2 raisedTo:18) // 333 |
11587 | 3894 |
393 integerReciprocal |
11945 | 3895 |
8 integerReciprocal |
3896 |
15 integerReciprocal |
|
11587 | 3897 |
15112233445566 integerReciprocal |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3898 |
10239552311579 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3899 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3900 |
|
13023 | 3901 |
"Modified: / 03-05-1999 / 14:27:18 / stefan" |
3902 |
"Modified: / 17-08-2010 / 17:30:22 / cg" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3903 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3904 |
|
12588 | 3905 |
integerSqrt |
21931 | 3906 |
"return the largest integer which is less or equal to the receiver's square root. |
3907 |
For large integers, this provides better results than the float sqrt method |
|
3908 |
(which actually fails for very large numbers) |
|
12586
1b8a0ecda4f2
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12585
diff
changeset
|
3909 |
This might be needed for some number theoretic problems with large numbers |
21931 | 3910 |
(and also in cryptography). |
3911 |
Uses Newton's method" |
|
12586
1b8a0ecda4f2
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12585
diff
changeset
|
3912 |
|
1b8a0ecda4f2
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12585
diff
changeset
|
3913 |
|guess prevGuess guessSquared| |
12581 | 3914 |
|
18851 | 3915 |
self negative ifTrue:[ |
19078 | 3916 |
^ self class |
3917 |
raise:#imaginaryResultSignal |
|
3918 |
receiver:self |
|
3919 |
selector:#integerSqrt |
|
3920 |
arguments:#() |
|
3921 |
errorString:'bad (negative) receiver in sqrt' |
|
18851 | 3922 |
]. |
19078 | 3923 |
self == 0 ifTrue:[^ 0]. |
20269 | 3924 |
|
12582
7afa0bb19bbe
comment/format in: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12581
diff
changeset
|
3925 |
guess := (1 bitShift:(self highBit // 2)). |
12581 | 3926 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3927 |
[ |
19078 | 3928 |
prevGuess ~= guess |
3929 |
and:[ ((guessSquared := guess squared) - self) abs >= guess ] |
|
12581 | 3930 |
] whileTrue:[ |
19078 | 3931 |
prevGuess := guess. |
3932 |
guess := (guess + (self / guess)) // 2. |
|
12581 | 3933 |
]. |
12586
1b8a0ecda4f2
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12585
diff
changeset
|
3934 |
guessSquared > self ifTrue:[ |
19078 | 3935 |
guess := guess - 1. |
12581 | 3936 |
]. |
12585
e8ce2be6049a
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12582
diff
changeset
|
3937 |
"/ self assert:(guess squared <= self). |
e8ce2be6049a
changed: #integerSquareRoot
Claus Gittinger <cg@exept.de>
parents:
12582
diff
changeset
|
3938 |
"/ self assert:((guess + 1) squared > self). |
12581 | 3939 |
|
3940 |
^ guess. |
|
3941 |
||
3942 |
" |
|
22107 | 3943 |
333 sqrt -> 18.2482875908947 |
3944 |
324 sqrt -> 18.0 |
|
3945 |
323 sqrt -> 17.9722007556114 |
|
3946 |
||
3947 |
333 integerSqrt -> 18 |
|
3948 |
325 integerSqrt -> 18 |
|
3949 |
324 integerSqrt -> 18 |
|
3950 |
323 integerSqrt -> 17 |
|
21931 | 3951 |
|
3952 |
10239552004900 integerSqrt |
|
3953 |
10239552004900 sqrt |
|
12588 | 3954 |
10239552311579 integerSqrt |
21931 | 3955 |
10239552311579 sqrt |
3956 |
||
3957 |
5397346292805549782720214077673687804022210808238353958670041357153884304 integerSqrt squared |
|
3958 |
5397346292805549782720214077673687804022210808238353958670041357153884304 sqrt squared |
|
3959 |
||
12588 | 3960 |
5397346292805549782720214077673687806275517530364350655459511599582614290 integerSqrt |
21931 | 3961 |
5397346292805549782720214077673687806275517530364350655459511599582614290 sqrt |
18851 | 3962 |
1000 factorial integerSqrt |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
3963 |
|
18851 | 3964 |
1000 factorial - 1000 factorial integerSqrt squared |
3965 |
1000 factorial - (1000 factorial integerSqrt + 1) squared |
|
21931 | 3966 |
1000 factorial between:(1000 factorial integerSqrt squared) and:((1000 factorial integerSqrt + 1) squared) |
12581 | 3967 |
" |
21931 | 3968 |
|
22107 | 3969 |
"Modified (comment): / 25-07-2017 / 17:52:14 / cg" |
12581 | 3970 |
! |
3971 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3972 |
inverseMod:n |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3973 |
"find the modular inverse for myself to n. |
11896 | 3974 |
This is defined as the solution of: '1 = (self * x) mod n. |
3975 |
This is a helper for modulu numbers" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3976 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3977 |
|e ret| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3978 |
|
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
3979 |
"the following expression returns #(x y (self gcd:n)), the solution of the equation |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
3980 |
(self * x) + (n * y) = self gcd:n" |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
3981 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3982 |
e := self extendedEuclid:n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3983 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3984 |
(e at:3) == 1 ifTrue:[ |
19078 | 3985 |
ret := e at:1. |
3986 |
ret negative ifTrue:[ |
|
3987 |
^ ret + n |
|
3988 |
]. |
|
3989 |
^ ret. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3990 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3991 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3992 |
^ 0 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3993 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3994 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3995 |
14 inverseMod:5 -> 4 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3996 |
5 inverseMod:14 -> 3 |
11587 | 3997 |
14 inverseMod:11 -> 4 (4 * 14) \\ 11 |
3998 |
11 inverseMod:14 -> 9 (9 * 11) \\ 14 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
3999 |
79 inverseMod:3220 -> 1019 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4000 |
3220 inverseMod:79 -> 54 (54 * 3220) \\ 79 |
11587 | 4001 |
1234567891 inverseMod:1111111111119 |
19078 | 4002 |
-> 148726663534 (148726663534*1234567891) \\ 1111111111119 |
11587 | 4003 |
|
4004 |
||
4005 |
14 extendedEuclid:11 |
|
4006 |
5 extendedEuclid:14 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4007 |
14 extendedEuclid:2 |
11587 | 4008 |
3220 extendedEuclid:79 |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4009 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4010 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4011 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
4012 |
"Modified: / 18.11.1999 / 16:21:37 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4013 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
4014 |
|
1 | 4015 |
lcm:anInteger |
52 | 4016 |
"return the least common multiple (using gcd:)" |
4017 |
||
4018 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
4019 |
||
92 | 4020 |
" |
4021 |
65 lcm:15 |
|
4022 |
3 lcm:15 |
|
4023 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4024 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4025 |
|
11496 | 4026 |
primeFactors |
4027 |
"return a collection of prime factors of the receiver. |
|
4028 |
For prime numbers, an empty collection is returned. |
|
13021 | 4029 |
Can take a long time for big numbers" |
11496 | 4030 |
|
11508 | 4031 |
^ self primeFactorsUpTo:nil |
4032 |
||
4033 |
" |
|
4034 |
2 to:10000 do:[:n | |
|
19078 | 4035 |
self assert:((n isPrime and:[ n primeFactors isEmpty]) |
4036 |
or:[ n isPrime not and:[n primeFactors product = n]]) |
|
11508 | 4037 |
] |
4038 |
3 to:10000 do:[:n | |
|
19078 | 4039 |
self assert:(n factorial primeFactors product = n factorial) |
11508 | 4040 |
] |
4041 |
||
11587 | 4042 |
13195 primeFactors |
4043 |
12 primeFactors |
|
4044 |
2 primeFactors |
|
4045 |
3 primeFactors |
|
4046 |
5 primeFactors |
|
4047 |
14 primeFactors |
|
4048 |
13423453625634765 primeFactors |
|
4049 |
13423453625634765 isPrime |
|
4050 |
13423453625634765 gcd:(3 * 5 * 19 * 29) |
|
4051 |
13423453625634765 / 8265 |
|
4052 |
1624132320101 isPrime |
|
11508 | 4053 |
1624132320101 gcd: 8265 |
4054 |
||
11587 | 4055 |
1000000 primeFactors |
4056 |
100000000 primeFactors |
|
4057 |
1000000000 primeFactors |
|
11508 | 4058 |
|
4059 |
Time millisecondsToRun:[ |
|
19078 | 4060 |
1000 timesRepeat:[ |
4061 |
10000000000000000000000000000000000000 primeFactors |
|
4062 |
] |
|
11508 | 4063 |
] 421 |
4064 |
" |
|
13021 | 4065 |
|
4066 |
"Modified: / 17-08-2010 / 17:27:33 / cg" |
|
11508 | 4067 |
! |
4068 |
||
4069 |
primeFactorsUpTo:limitArgOrNil |
|
4070 |
"return a collection of prime factors of the receiver. |
|
4071 |
For prime numbers, an empty collection is returned. |
|
11587 | 4072 |
Can take a long time for big numbers |
13021 | 4073 |
(win a nobel price, if you find something quick (*) |
4074 |
||
4075 |
(*):which does not mean that the code below is optimal - far from it !!" |
|
11508 | 4076 |
|
18865 | 4077 |
"See comment in initializePrimeCacheUpTo:limit" |
4078 |
||
19128 | 4079 |
|rest n factors limit wellKnownPrimes lastPrime checkThisFactor nextTry| |
11496 | 4080 |
|
4081 |
factors := Bag new. |
|
4082 |
rest := self. |
|
11508 | 4083 |
limit := (rest // 2). |
4084 |
limitArgOrNil notNil ifTrue:[ |
|
19078 | 4085 |
limit := limit min:limitArgOrNil. |
11508 | 4086 |
]. |
11496 | 4087 |
|
4088 |
"/ try to get the number down fast: |
|
4089 |
n := rest lowBit. |
|
4090 |
n ~~ 1 ifTrue:[ |
|
19078 | 4091 |
self == 2 ifTrue:[^ #() ]. |
4092 |
factors add:2 withOccurrences:(n-1). |
|
4093 |
rest := rest rightShift:(n-1). |
|
11496 | 4094 |
]. |
4095 |
||
20269 | 4096 |
checkThisFactor := |
19128 | 4097 |
[:prime | |
4098 |
(prime*prime) > rest ifTrue:[ |
|
19078 | 4099 |
(rest ~~ 1 and:[factors notEmpty]) ifTrue:[ factors add:rest ]. |
4100 |
^ factors. |
|
4101 |
]. |
|
4102 |
||
4103 |
[rest \\ prime == 0] whileTrue:[ |
|
4104 |
factors add:prime. |
|
4105 |
rest := rest // prime. |
|
4106 |
rest == 1 ifTrue:[^ factors]. |
|
4107 |
]. |
|
4108 |
lastPrime := prime. |
|
4109 |
]. |
|
11496 | 4110 |
|
20269 | 4111 |
wellKnownPrimes := Integer primesUpTo5000. |
19128 | 4112 |
limit <= (wellKnownPrimes last+1) ifTrue:[ |
4113 |
wellKnownPrimes do:checkThisFactor. |
|
19078 | 4114 |
^ factors |
11496 | 4115 |
]. |
4116 |
||
4117 |
"/ actually, all of the code is duplicated; once for primes from a table, |
|
4118 |
"/ and then primes as generated on the fly. The prime generation involves a prime-test, |
|
4119 |
"/ which may slow things down quite a bit. |
|
4120 |
"/ (the primesUpTo uses a faster sieve, but can only represent primes to upTo (say) |
|
13022 | 4121 |
"/ a few millions). |
11496 | 4122 |
|
11938 | 4123 |
Integer primesUpTo:(limit min:(1000000 max:Integer primeCacheSize)) do:checkThisFactor. |
11496 | 4124 |
|
4125 |
nextTry := lastPrime + 2. |
|
4126 |
[ nextTry <= limit ] whileTrue:[ |
|
19078 | 4127 |
"/ now, we are beyond the list of pre-generated primes. |
4128 |
"/ change our strategy to: see if it divides an odd number; |
|
4129 |
"/ if so, add the divisor's prime factors. |
|
19128 | 4130 |
(nextTry*nextTry) > rest ifTrue:[ |
19078 | 4131 |
(rest ~~ 1 and:[factors notEmpty]) ifTrue:[ factors add:rest ]. |
4132 |
^ factors. |
|
4133 |
]. |
|
4134 |
[(rest \\ nextTry) == 0] whileTrue:[ |
|
4135 |
"/ can only happen relatively late after the last prime, |
|
4136 |
"/ because otherwise, the primeFactors of nextTry would already have |
|
4137 |
"/ been found as divisors. |
|
4138 |
"/ first chance is: (lastPrime + 2) squared |
|
4139 |
nextTry < lastPrime squared ifTrue:[ |
|
4140 |
"/ nextTry is a prime !! |
|
4141 |
factors add:nextTry |
|
4142 |
] ifFalse:[ |
|
4143 |
factors addAll:(nextTry primeFactors). |
|
4144 |
]. |
|
4145 |
rest := rest // nextTry. |
|
4146 |
]. |
|
4147 |
nextTry := nextTry + 2. |
|
11496 | 4148 |
]. |
4149 |
^ factors |
|
4150 |
||
4151 |
" |
|
4152 |
2 to:10000 do:[:n | |
|
19078 | 4153 |
self assert:((n isPrime and:[ n primeFactors isEmpty]) |
4154 |
or:[ n isPrime not and:[n primeFactors product = n]]) |
|
11496 | 4155 |
] |
4156 |
3 to:10000 do:[:n | |
|
19078 | 4157 |
self assert:(n factorial primeFactors product = n factorial) |
11496 | 4158 |
] |
4159 |
||
11587 | 4160 |
13195 primeFactors |
4161 |
12 primeFactors |
|
4162 |
2 primeFactors |
|
4163 |
3 primeFactors |
|
4164 |
5 primeFactors |
|
4165 |
14 primeFactors |
|
4166 |
13423453625634765 primeFactors |
|
4167 |
13423453625634765 isPrime |
|
4168 |
13423453625634765 gcd:(3 * 5 * 19 * 29) |
|
4169 |
13423453625634765 / 8265 |
|
4170 |
1624132320101 isPrime |
|
11496 | 4171 |
1624132320101 gcd: 8265 |
4172 |
||
11587 | 4173 |
1000000 primeFactors |
4174 |
100000000 primeFactors |
|
4175 |
1000000000 primeFactors |
|
11496 | 4176 |
|
4177 |
Time millisecondsToRun:[ |
|
19078 | 4178 |
1000 timesRepeat:[ |
4179 |
10000000000000000000000000000000000000 primeFactors |
|
4180 |
] |
|
11496 | 4181 |
] 421 |
4182 |
" |
|
13021 | 4183 |
|
13022 | 4184 |
"Modified: / 17-08-2010 / 17:28:05 / cg" |
11496 | 4185 |
! |
4186 |
||
4973 | 4187 |
raisedTo:exp mod:mod |
12280 | 4188 |
"return the modulo (remainder) of |
4973 | 4189 |
the receiver raised to exp (an Integer) and mod (another Integer)" |
4190 |
||
11587 | 4191 |
|result m t |
19874 | 4192 |
eI "{Class: SmallInteger}" |
5816 | 4193 |
bits "{Class: SmallInteger}"| |
4973 | 4194 |
|
4195 |
"use the addition chaining algorithm" |
|
4196 |
||
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4197 |
exp == 0 ifTrue:[ |
19078 | 4198 |
^ 1 |
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4199 |
]. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4200 |
exp == 1 ifTrue:[ |
19078 | 4201 |
mod isNumber ifTrue:[ |
4202 |
^ self \\ mod. |
|
4203 |
] |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4204 |
]. |
4973 | 4205 |
exp negative ifTrue:[ |
19078 | 4206 |
^ self class |
4207 |
raise:#domainErrorSignal |
|
4208 |
receiver:self |
|
4209 |
selector:#raisedTo:mod: |
|
4210 |
arguments:(Array with:exp with:mod) |
|
4211 |
errorString:'modulo arithmethic with negative exponent' |
|
4973 | 4212 |
]. |
4213 |
||
4214 |
m := mod asModuloNumber. |
|
4215 |
||
4216 |
result := 1. |
|
4217 |
t := m modulusOf:self. |
|
4218 |
||
19874 | 4219 |
(exp class == SmallInteger) ifTrue:[ |
4220 |
eI := exp. |
|
4221 |
[eI ~~ 0] whileTrue:[ |
|
4222 |
(eI bitAnd:1) == 1 ifTrue:[ |
|
4223 |
result := m modulusOf:(result * t). |
|
4224 |
]. |
|
4225 |
eI := eI bitShift:-1. |
|
4226 |
eI ~~ 0 ifTrue:[ |
|
4227 |
t := m modulusOf:(t * t). |
|
4228 |
]. |
|
4229 |
]. |
|
4230 |
^ result. |
|
4231 |
]. |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4232 |
|
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4233 |
bits := exp highBit. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4234 |
|
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4235 |
1 to:bits-1 do:[:i| |
19078 | 4236 |
(exp bitAt:i) == 1 ifTrue:[ |
4237 |
result := m modulusOf:(result * t). |
|
4238 |
]. |
|
4239 |
t := m modulusOf:(t * t). |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4240 |
]. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4241 |
result := m modulusOf:(result * t). |
4973 | 4242 |
|
4243 |
^ result |
|
4244 |
||
11587 | 4245 |
" |
22561 | 4246 |
Time millisecondsToRun: [100000 timesRepeat: [12345678907 raisedTo: 3 modulo: 12345678917]] |
4247 |
||
11587 | 4248 |
2 raisedTo:2 mod:3 |
4973 | 4249 |
20000000000000 raisedTo:200 mod:190 -> 30 |
11587 | 4250 |
(20000000000000 raisedTo:200) \\ 190 |
4973 | 4251 |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4252 |
Time millisecondsToRun:[10000 timesRepeat:[ |
19874 | 4253 |
200000000000000000000000 raisedTo:65537 mod:1900000000000000000000000 |
19078 | 4254 |
] |
4255 |
] |
|
4973 | 4256 |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
4257 |
Time millisecondsToRun:[1000 timesRepeat:[ |
19874 | 4258 |
(200000000000000000000000 raisedTo:65537) \\ 1900000000000000000000000 |
19078 | 4259 |
] |
4260 |
] |
|
4973 | 4261 |
" |
4262 |
||
4263 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
|
4264 |
"Modified: / 5.5.1999 / 11:18:20 / stefan" |
|
6195 | 4265 |
"Modified: / 16.11.2001 / 14:15:21 / cg" |
4973 | 4266 |
! |
4267 |
||
4268 |
raisedToCrtModP:p q:q ep:ep eq:eq u:u |
|
4269 |
"Application of the Chinese Remainder Theorem (CRT). |
|
4270 |
||
4271 |
This is a faster modexp for moduli with a known factorisation into two |
|
4272 |
relatively prime factors p and q, and an input relatively prime to the |
|
4273 |
modulus, the Chinese Remainder Theorem to do the computation mod p and |
|
4274 |
mod q, and then combine the results. This relies on a number of |
|
4275 |
precomputed values, but does not actually require the modulus n or the |
|
4276 |
exponent e. |
|
4277 |
||
4278 |
expout = expin ^ e mod (p*q). |
|
4279 |
We form this by evaluating |
|
4280 |
p2 = (expin ^ e) mod p and |
|
4281 |
q2 = (expin ^ e) mod q |
|
4282 |
and then combining the two by the CRT. |
|
4283 |
||
4284 |
Two optimisations of this are possible. First, we can reduce expin |
|
4285 |
modulo p and q before starting. |
|
4286 |
||
4287 |
Second, since we know the factorisation of p and q (trivially derived |
|
4288 |
from the factorisation of n = p*q), and expin is relatively prime to |
|
4289 |
both p and q, we can use Euler's theorem, expin^phi(m) = 1 (mod m), |
|
4290 |
to throw away multiples of phi(p) or phi(q) in e. |
|
4291 |
Letting ep = e mod phi(p) and |
|
19078 | 4292 |
eq = e mod phi(q) |
4973 | 4293 |
then combining these two speedups, we only need to evaluate |
4294 |
p2 = ((expin mod p) ^ ep) mod p and |
|
4295 |
q2 = ((expin mod q) ^ eq) mod q. |
|
4296 |
||
4297 |
Now we need to apply the CRT. Starting with |
|
4298 |
expout = p2 (mod p) and |
|
4299 |
expout = q2 (mod q) |
|
4300 |
we can say that expout = p2 + p * k, and if we assume that 0 <= p2 < p, |
|
4301 |
then 0 <= expout < p*q for some 0 <= k < q. Since we want expout = q2 |
|
4302 |
(mod q), then p*k = q2-p2 (mod q). Since p and q are relatively prime, |
|
4303 |
p has a multiplicative inverse u mod q. In other words, u = 1/p (mod q). |
|
4304 |
||
4305 |
Multiplying by u on both sides gives k = u*(q2-p2) (mod q). |
|
4306 |
Since we want 0 <= k < q, we can thus find k as |
|
4307 |
k = (u * (q2-p2)) mod q. |
|
4308 |
||
4309 |
Once we have k, evaluating p2 + p * k is easy, and |
|
4310 |
that gives us the result |
|
11587 | 4311 |
" |
4973 | 4312 |
|
4313 |
|result t mp mq| |
|
4314 |
||
4315 |
mp := p asModuloNumber. |
|
19874 | 4316 |
result := self raisedTo:ep mod:mp. |
4973 | 4317 |
|
4318 |
mq := q asModuloNumber. |
|
19874 | 4319 |
t := self raisedTo:eq mod:mq. |
4973 | 4320 |
|
4321 |
"now p2 is in result, q2 in t" |
|
4322 |
||
19231 | 4323 |
t := t - result. |
4324 |
t negative ifTrue:[ |
|
19078 | 4325 |
t := t + q. |
4973 | 4326 |
]. |
19231 | 4327 |
t := t * u. |
4973 | 4328 |
t := mq modulusOf:t. |
19231 | 4329 |
t := t * p. |
4330 |
result := result + t. |
|
4973 | 4331 |
|
4332 |
^ result. |
|
4333 |
||
4334 |
"Created: / 30.4.1999 / 15:53:15 / stefan" |
|
4335 |
"Modified: / 5.5.1999 / 11:01:15 / stefan" |
|
1 | 4336 |
! ! |
4337 |
||
4338 |
!Integer methodsFor:'printing & storing'! |
|
4339 |
||
16038 | 4340 |
asBCD |
4341 |
"return an integer which represents the BCD encoded value of the receiver; |
|
4342 |
that is: each digit of its decimal representation is placed into a nibble |
|
4343 |
of the result. (aka 162 -> 0x162). |
|
4344 |
This conversion is useful for some communication protocols, |
|
16040 | 4345 |
or control systems, which represent numbers this way... |
4346 |
This fallback code is not particularily tuned or optimized for speed." |
|
16038 | 4347 |
|
4348 |
|rest twoDigits hi lo shift out| |
|
4349 |
||
4350 |
out := 0. shift := 0. |
|
4351 |
rest := self. |
|
4352 |
[rest > 0] whileTrue:[ |
|
19078 | 4353 |
twoDigits := rest \\ 100. |
4354 |
rest := rest // 100. |
|
4355 |
hi := twoDigits // 10. |
|
4356 |
lo := twoDigits \\ 10. |
|
4357 |
out := out bitOr:(((hi bitShift:4)+lo) bitShift:shift). |
|
4358 |
shift := shift + 8. |
|
16038 | 4359 |
]. |
4360 |
||
4361 |
^ out |
|
4362 |
||
4363 |
" |
|
4364 |
(100 factorial) asBCD |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4365 |
999999999 asBCD |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4366 |
100000000 asBCD |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4367 |
123456789 asBCD |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4368 |
99999999 asBCD |
16038 | 4369 |
12345678 asBCD |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4370 |
12345678 asBCD |
16038 | 4371 |
12345678 asBCD hexPrintString |
4372 |
12345678901234567890 asBCD |
|
4373 |
" |
|
4374 |
! |
|
4375 |
||
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4376 |
asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4377 |
"return a byteArray containing the receiver in BCD encoding. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4378 |
The byteArray will contain the BCD encoded decimal string, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4379 |
starting with the most significant digits first. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4380 |
This conversion is useful for some communication protocols, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4381 |
or control systems, which represent big numbers this way... |
16038 | 4382 |
This is not particularily tuned or optimized for speed." |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4383 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4384 |
|s rest twoDigits hi lo| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4385 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4386 |
self == 0 ifTrue:[ |
19078 | 4387 |
^ #[ 16r00 ] |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4388 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4389 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4390 |
"/ a very rough estimate on the final size ... |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4391 |
s := (ByteArray new:(self digitLength * 2)) writeStream. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4392 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4393 |
rest := self. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4394 |
[rest > 0] whileTrue:[ |
19078 | 4395 |
twoDigits := rest \\ 100. |
4396 |
rest := rest // 100. |
|
4397 |
hi := twoDigits \\ 10. |
|
4398 |
lo := twoDigits // 10. |
|
4399 |
s nextPut:(lo bitShift:4)+hi |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4400 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4401 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4402 |
^ s contents reverse |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4403 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4404 |
" |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4405 |
12345678 asBCDBytes |
16038 | 4406 |
12345678 asBCDBytes hexPrintString |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4407 |
12345678901234567890 asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4408 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4409 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
4410 |
|
701 | 4411 |
errorPrintHex |
4412 |
"print the receiver as a hex number on the standard error stream" |
|
4413 |
||
4414 |
(self printStringRadix:16) errorPrint |
|
4415 |
! |
|
1 | 4416 |
|
701 | 4417 |
hexPrintString |
4418 |
"return a hex string representation of the receiver" |
|
4419 |
||
4420 |
^ self printStringRadix:16 |
|
3874 | 4421 |
|
4422 |
" |
|
4423 |
127 hexPrintString |
|
4424 |
-1 hexPrintString |
|
4425 |
" |
|
4426 |
||
4427 |
"Modified: / 11.10.1998 / 01:15:43 / cg" |
|
701 | 4428 |
! |
4429 |
||
2408 | 4430 |
hexPrintString:size |
4431 |
"return a hex string representation of the receiver, |
|
4432 |
padded to size characters" |
|
4433 |
||
5799 | 4434 |
"/ ^ (self printStringRadix:16) leftPaddedTo:size with:$0 |
4435 |
^ self printStringRadix:16 size:size fill:$0 |
|
4436 |
||
4437 |
" |
|
11587 | 4438 |
12345 hexPrintString:4 |
4439 |
123 hexPrintString:4 |
|
5799 | 4440 |
" |
2408 | 4441 |
|
4442 |
"Created: 18.2.1997 / 13:32:33 / cg" |
|
4443 |
! |
|
4444 |
||
701 | 4445 |
printHex |
4446 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 4447 |
|
701 | 4448 |
(self printStringRadix:16) print |
21938 | 4449 |
|
4450 |
" |
|
4451 |
16rAFFE printStringRadix:16 |
|
4452 |
" |
|
4453 |
||
4454 |
"Modified (comment): / 02-07-2017 / 10:20:59 / cg" |
|
701 | 4455 |
! |
4456 |
||
20140 | 4457 |
printOn:aStream base:base showRadix:showRadix |
5988 | 4458 |
"append a string representation of the receiver in the specified numberBase to aStream |
4459 |
(if showRadix is true, with initial XXr) |
|
20140 | 4460 |
The base argument should be between 2 and 36. |
4461 |
If it is negative, digits > 9 are printed as lowecase a-z." |
|
4462 |
||
4463 |
|absBase num s divMod mod r r2 r4 nD numN string| |
|
4464 |
||
4465 |
(base isInteger and:[absBase := base abs. absBase between:2 and:36]) ifFalse:[ |
|
4466 |
ConversionError raiseRequestWith:self errorString:' - invalid base: ', absBase printString. |
|
4467 |
absBase := 10. |
|
54 | 4468 |
]. |
4469 |
||
5988 | 4470 |
showRadix ifTrue:[ |
20140 | 4471 |
absBase printOn:aStream. |
19078 | 4472 |
aStream nextPut:$r. |
5988 | 4473 |
]. |
4474 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4475 |
(self = 0) ifTrue:[aStream nextPut:$0. ^ self]. |
20140 | 4476 |
self negative ifTrue:[ |
19078 | 4477 |
aStream nextPut:$- . |
4478 |
num := self negated. |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4479 |
] ifFalse:[ |
19078 | 4480 |
num := self. |
1 | 4481 |
]. |
92 | 4482 |
|
4483 |
" |
|
4484 |
claus: changed it from a recursive algorithm; |
|
18851 | 4485 |
(it used to trigger stack-overflow exceptions when printing 3000 factorial ...) |
92 | 4486 |
" |
18865 | 4487 |
"/ leftPart := num // base. |
4488 |
"/ (leftPart ~= 0) ifTrue:[ |
|
4489 |
"/ leftPart printOn:aStream base:base. |
|
4490 |
"/ aStream nextPut:(Character digitValue:(num \\ base). |
|
4491 |
"/ ^ self |
|
4492 |
"/ ]. |
|
4493 |
"/ aStream nextPut:(Character digitValue:num). |
|
2951 | 4494 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
4495 |
"/ 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
|
4496 |
"/ 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
|
4497 |
"/ 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
|
4498 |
"/ 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
|
4499 |
"/ 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
|
4500 |
|
20140 | 4501 |
r2 := absBase*absBase. "/ radix^2 |
18851 | 4502 |
r4 := r2*r2. "/ radix^4 |
20140 | 4503 |
absBase <= 10 ifTrue:[ |
19078 | 4504 |
r := r4*r2. "/ radix^6 (chunks of 6 digits) |
4505 |
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
|
4506 |
] ifFalse:[ |
20140 | 4507 |
r := r4*absBase. "/ radix^5 (chunks of 5 digits) |
19078 | 4508 |
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
|
4509 |
]. |
18851 | 4510 |
SmallInteger maxBits >= 63 ifTrue:[ |
19159 | 4511 |
r := r*r2. "/ radix^7 (chunks of 6 digits) |
4512 |
nD := nD + 2. |
|
18851 | 4513 |
]. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4514 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4515 |
"get a Stream with space for the digits we are going to print. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4516 |
We need ((num log:base) ceiling) digits, which is equivalent |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4517 |
to ((num log:2)/(base log:2) ceiling) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4518 |
" |
18851 | 4519 |
s := WriteStream on:(String new:10). |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4520 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
4521 |
[num >= r] whileTrue:[ |
19078 | 4522 |
"/ |
4523 |
"/ chop off nD digits. |
|
4524 |
"/ |
|
4525 |
divMod := num divMod:r. |
|
4526 |
num := divMod at:1. |
|
4527 |
numN := divMod at:2. |
|
4528 |
||
4529 |
"/ process them |
|
4530 |
nD timesRepeat:[ |
|
20140 | 4531 |
divMod := numN divMod:absBase. |
19078 | 4532 |
numN := divMod at:1. |
4533 |
mod := divMod at:2. |
|
4534 |
s nextPut:(Character digitValue:mod). |
|
4535 |
]. |
|
2951 | 4536 |
]. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
4537 |
|
19089 | 4538 |
[num ~~ 0] whileTrue:[ |
20140 | 4539 |
divMod := num divMod:absBase. |
19078 | 4540 |
num := divMod at:1. |
4541 |
mod := divMod at:2. |
|
4542 |
s nextPut:(Character digitValue:mod). |
|
1 | 4543 |
]. |
20140 | 4544 |
string := s contents reverse. |
4545 |
base < 0 ifTrue:[ |
|
4546 |
string := string asLowercase. |
|
4547 |
]. |
|
4548 |
aStream nextPutAll:string. |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4549 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4550 |
" |
19078 | 4551 |
3000 factorial printOn:Transcript base:10 |
4552 |
10 printOn:Transcript base:3 |
|
4553 |
31 printOn:Transcript base:3 |
|
4554 |
10 printOn:Transcript base:2 |
|
4555 |
31 printOn:Transcript base:2 |
|
20140 | 4556 |
-28 printOn:Transcript base:16 |
4557 |
-28 printOn:Transcript base:-16 |
|
19078 | 4558 |
-20 printOn:Transcript base:10 |
19159 | 4559 |
Time millisecondsToRun:[10000 factorial printString] |
4560 |
'%012d' printf:{ (2 raisedTo:20) } |
|
12976
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
4561 |
" |
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
4562 |
|
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
4563 |
"Modified: / 20-01-1998 / 18:05:02 / stefan" |
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
4564 |
"Created: / 07-09-2001 / 13:51:33 / cg" |
a12aab15ce5a
changed: #printOn:base:showRadix:
Claus Gittinger <cg@exept.de>
parents:
12875
diff
changeset
|
4565 |
"Modified: / 02-08-2010 / 12:24:14 / cg" |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4566 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4567 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4568 |
printOn:aStream base:baseInteger size:sz |
15775 | 4569 |
"print a string representation of the receiver in the specified |
4570 |
base. The string is padded on the left with fillCharacter to make |
|
4571 |
its size as specified in sz." |
|
4572 |
||
4573 |
self printOn:aStream base:baseInteger size:sz fill:$0 |
|
4574 |
||
4575 |
" |
|
20268 | 4576 |
1024 printOn:Transcript base:16 size:4. |
15775 | 4577 |
1024 printOn:Transcript base:2 size:16. |
4578 |
1024 printOn:Transcript base:16 size:8. |
|
4579 |
" |
|
4580 |
! |
|
4581 |
||
7173 | 4582 |
printOn:aStream base:baseInteger size:sz fill:fillCharacter |
4583 |
"print a string representation of the receiver in the specified |
|
4584 |
base. The string is padded on the left with fillCharacter to make |
|
4585 |
its size as specified in sz." |
|
4586 |
||
20140 | 4587 |
|string actualSize| |
4588 |
||
4589 |
string := self printStringRadix:baseInteger. |
|
7173 | 4590 |
actualSize := string size. |
4591 |
actualSize < sz ifTrue:[ |
|
19078 | 4592 |
aStream next:sz-actualSize put:fillCharacter. |
7173 | 4593 |
]. |
4594 |
aStream nextPutAll:string. |
|
4595 |
||
4596 |
" |
|
20269 | 4597 |
1024 printOn:Transcript base:16 size:4 fill:$0. |
7173 | 4598 |
1024 printOn:Transcript base:2 size:16 fill:$. |
4599 |
1024 printOn:Transcript base:16 size:8 fill:Character space. |
|
4600 |
" |
|
4601 |
! |
|
4602 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4603 |
printOn:aStream radix:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4604 |
"append a printed description of the receiver to aStream. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4605 |
The receiver is printed in radix base (instead of the default, 10). |
5988 | 4606 |
This method is obsoleted by #printOn:base:, which is ST-80 & ANSI compatible." |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4607 |
|
8897
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
4608 |
<resource: #obsolete> |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
4609 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4610 |
self printOn:aStream base:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4611 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4612 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
5988 | 4613 |
"Modified: / 7.9.2001 / 13:58:29 / cg" |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4614 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
4615 |
|
6635 | 4616 |
printRomanOn:aStream |
6673 | 4617 |
"print the receiver as roman number to the receiver, aStream. |
4618 |
This converts correct (i.e. prefix notation for 4,9,40,90, etc.)." |
|
4619 |
||
4620 |
^ self printRomanOn:aStream naive:false |
|
6635 | 4621 |
|
4622 |
" |
|
6673 | 4623 |
1 to:10 do:[:i | i printRomanOn:Transcript. Transcript cr.]. |
6635 | 4624 |
1999 printRomanOn:Transcript. Transcript cr. |
4625 |
Date today year printRomanOn:Transcript. Transcript cr. |
|
4626 |
" |
|
4627 |
||
4628 |
"test all between 1 and 9999: |
|
4629 |
1 to:9999 do:[:n | |
|
19078 | 4630 |
|romanString| |
4631 |
||
4632 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
4633 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6673 | 4634 |
] |
4635 |
" |
|
4636 |
! |
|
4637 |
||
4638 |
printRomanOn:aStream naive:naive |
|
4639 |
"print the receiver as roman number to the receiver, aStream. |
|
4640 |
The naive argument controls if the conversion is |
|
6682 | 4641 |
correct (i.e. subtracting prefix notation for 4,9,40,90, etc.), |
4642 |
or naive (i.e. print 4 as IIII and 9 as VIIII); also called simple. |
|
6673 | 4643 |
The naive version is often used for page numbers in documents." |
4644 |
||
4645 |
|restValue spec| |
|
4646 |
||
4647 |
restValue := self. |
|
4648 |
restValue > 0 ifFalse:[self error:'negative roman']. |
|
4649 |
||
4650 |
naive ifTrue:[ |
|
19078 | 4651 |
spec := #( |
4652 |
" value string repeat " |
|
4653 |
1000 'M' true |
|
4654 |
500 'D' false |
|
4655 |
100 'C' true |
|
4656 |
50 'L' false |
|
4657 |
10 'X' true |
|
4658 |
5 'V' false |
|
4659 |
1 'I' true |
|
4660 |
). |
|
6673 | 4661 |
] ifFalse:[ |
19078 | 4662 |
spec := #( |
4663 |
" value string repeat " |
|
4664 |
1000 'M' true |
|
4665 |
900 'CM' false |
|
4666 |
500 'D' false |
|
4667 |
400 'CD' false |
|
4668 |
100 'C' true |
|
4669 |
90 'XC' false |
|
4670 |
50 'L' false |
|
4671 |
40 'XL' false |
|
4672 |
10 'X' true |
|
4673 |
9 'IX' false |
|
4674 |
5 'V' false |
|
4675 |
4 'IV' false |
|
4676 |
1 'I' true |
|
4677 |
). |
|
6673 | 4678 |
]. |
4679 |
||
11587 | 4680 |
spec |
19078 | 4681 |
inGroupsOf:3 |
4682 |
do:[:rValue :rString :repeatFlag | |
|
4683 |
||
4684 |
[ |
|
4685 |
(restValue >= rValue) ifTrue:[ |
|
4686 |
aStream nextPutAll:rString. |
|
4687 |
restValue := restValue - rValue. |
|
4688 |
]. |
|
4689 |
] doWhile:[ repeatFlag and:[ restValue >= rValue] ]. |
|
4690 |
]. |
|
6673 | 4691 |
|
4692 |
" |
|
4693 |
1 to:10 do:[:i | i printRomanOn:Transcript naive:false. Transcript cr.]. |
|
4694 |
1 to:10 do:[:i | i printRomanOn:Transcript naive:true. Transcript cr.]. |
|
4695 |
||
4696 |
1999 printRomanOn:Transcript. Transcript cr. |
|
4697 |
Date today year printRomanOn:Transcript. Transcript cr. |
|
4698 |
" |
|
4699 |
||
4700 |
"test all between 1 and 9999: |
|
4701 |
1 to:9999 do:[:n | |
|
19078 | 4702 |
|romanString| |
4703 |
||
4704 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:false]. |
|
4705 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6673 | 4706 |
] |
4707 |
" |
|
4708 |
||
4709 |
"test naive all between 1 and 9999: |
|
4710 |
1 to:9999 do:[:n | |
|
19078 | 4711 |
|romanString| |
4712 |
||
4713 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:true]. |
|
4714 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
6635 | 4715 |
] |
4716 |
" |
|
4717 |
! |
|
4718 |
||
1 | 4719 |
printStringRadix:aRadix size:sz fill:fillCharacter |
4720 |
"return a string representation of the receiver in the specified |
|
4721 |
radix. The string is padded on the left with fillCharacter to make |
|
4722 |
its size as specified in sz." |
|
4723 |
||
2951 | 4724 |
|s actualSize| |
1 | 4725 |
|
4726 |
s := self printStringRadix:aRadix. |
|
2951 | 4727 |
actualSize := s size. |
4728 |
actualSize < sz ifTrue:[ |
|
19078 | 4729 |
s := ((String new:(sz - actualSize)) atAllPut:fillCharacter) , s |
1 | 4730 |
]. |
4731 |
^ s |
|
4732 |
||
92 | 4733 |
" |
20269 | 4734 |
1024 printStringRadix:16 size:4 fill:$0. |
92 | 4735 |
1024 printStringRadix:2 size:16 fill:$. |
4736 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
4737 |
" |
|
216 | 4738 |
! |
4739 |
||
6635 | 4740 |
romanPrintString |
4741 |
"return a roman number representation of the receiver as a string" |
|
4742 |
||
4743 |
^ String streamContents:[:stream | self printRomanOn:stream]. |
|
4744 |
||
4745 |
" |
|
11587 | 4746 |
1999 romanPrintString. |
6635 | 4747 |
Date today year romanPrintString. |
4748 |
" |
|
1 | 4749 |
! ! |
4750 |
||
701 | 4751 |
!Integer methodsFor:'queries'! |
202 | 4752 |
|
701 | 4753 |
digitAt:n |
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4754 |
"return the n-th byte of the binary representation." |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4755 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4756 |
self subclassResponsibility |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4757 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4758 |
"This is a very stupid implementation, and should be redefined in |
701 | 4759 |
concrete subclasses." |
202 | 4760 |
|
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4761 |
"/ |num count| |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4762 |
"/ |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4763 |
"/ num := self. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4764 |
"/ count := n. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4765 |
"/ [count > 1] whileTrue:[ |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4766 |
"/ num := num // 256. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4767 |
"/ count := count - 1 |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4768 |
"/ ]. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4769 |
"/ ^ num \\ 256 |
159 | 4770 |
|
4771 |
" |
|
11587 | 4772 |
16r44332211 digitAt:1 |
4773 |
16r44332211 digitAt:2 |
|
4774 |
16r44332211 digitAt:3 |
|
4775 |
16r44332211 digitAt:4 |
|
4776 |
16r44332211 digitAt:5 |
|
4777 |
16r00332211 digitAt:4 |
|
4778 |
16r00332211 digitAt:5 |
|
159 | 4779 |
" |
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4780 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4781 |
"Modified: / 26.9.2001 / 21:21:21 / cg" |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4782 |
! |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4783 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4784 |
digitByteAt:n |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4785 |
"return 8 bits of my signed value, starting at byte index. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4786 |
For positive receivers, this is the same as #digitAt:; |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4787 |
for negative ones, the actual bit representation is returned." |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4788 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4789 |
self subclassResponsibility |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4790 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4791 |
"Created: / 26.9.2001 / 21:18:43 / cg" |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
4792 |
"Modified: / 26.9.2001 / 21:20:19 / cg" |
1 | 4793 |
! |
4794 |
||
701 | 4795 |
digitLength |
19319 | 4796 |
"return the number of bytes needed for the unsigned binary representation of the receiver. |
4797 |
For negative receivers, the result is not defined by the language standard. |
|
20269 | 4798 |
This method is redefined in concrete classes |
19319 | 4799 |
- the fallback here is actually never used." |
701 | 4800 |
|
4801 |
^ (self log:256) ceiling asInteger |
|
2816 | 4802 |
|
4803 |
"Modified: 31.7.1997 / 13:19:06 / cg" |
|
701 | 4804 |
! |
4805 |
||
13725 | 4806 |
exponent |
4807 |
"return what would be the normalized float's exponent if I were a float. |
|
4808 |
This is not for general use - it has been added for dolphin (soap) compatibility. |
|
4809 |
This assumes that the mantissa is normalized to |
|
4810 |
0.5 .. 1.0 and the number's value is: mantissa * 2^exp" |
|
4811 |
||
4812 |
^ self abs highBit |
|
4813 |
||
4814 |
" |
|
4815 |
self assert:( 1.0 exponent = 1 exponent ). |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4816 |
self assert:( 2.0 exponent = 2 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4817 |
self assert:( 3.0 exponent = 3 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4818 |
self assert:( 4.0 exponent = 4 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4819 |
self assert:( 12345.0 exponent = 12345 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4820 |
self assert:( 0.0 exponent = 0 exponent ). |
13725 | 4821 |
|
4822 |
self assert:( -1.0 exponent = -1 exponent ). |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4823 |
self assert:( -2.0 exponent = -2 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4824 |
self assert:( -3.0 exponent = -3 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4825 |
self assert:( -4.0 exponent = -4 exponent ). |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4826 |
self assert:( -12345.0 exponent = -12345 exponent ). |
13725 | 4827 |
" |
4828 |
! |
|
4829 |
||
701 | 4830 |
isInteger |
4831 |
"return true, if the receiver is some kind of integer number" |
|
4832 |
||
4833 |
^ true |
|
3036 | 4834 |
! |
4835 |
||
13815 | 4836 |
isLiteral |
4837 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
4838 |
(i.e. can be used in constant arrays)" |
|
4839 |
||
4840 |
^ true |
|
4841 |
! |
|
4842 |
||
18865 | 4843 |
isPerfectSquare |
4844 |
"return true if I am a perfect square. |
|
4845 |
That is a number for which the square root is an integer." |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4846 |
|
18865 | 4847 |
|intSqrt realSqrt| |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4848 |
|
18865 | 4849 |
self strictlyPositive ifFalse:[ |
19078 | 4850 |
self == 0 ifTrue:[^ true]. |
4851 |
"/ should we raise a domain error for negative receivers? |
|
4852 |
^ false |
|
18865 | 4853 |
]. |
4854 |
||
4855 |
"/ q&d check for common small squares |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4856 |
self < 400 ifTrue:[ |
19078 | 4857 |
^ #(1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 ) includes:self. |
19058 | 4858 |
]. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4859 |
self < 1024 ifTrue:[ |
19078 | 4860 |
^ #(400 441 484 529 576 625 676 729 784 841 900 961) includes:self. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4861 |
]. |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4862 |
|
18865 | 4863 |
"/ try powers of 2 |
4864 |
self isPowerOfTwo ifTrue:[ |
|
19078 | 4865 |
^ self lowBit odd |
18865 | 4866 |
]. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4867 |
|
18865 | 4868 |
"/ guess |
4869 |
realSqrt := self sqrt. |
|
4870 |
realSqrt isFinite ifTrue:[ |
|
19078 | 4871 |
realSqrt = realSqrt truncated ifTrue:[ |
4872 |
"/ still have to check due to rounding errors. |
|
4873 |
intSqrt := realSqrt truncated asInteger. |
|
4874 |
^ intSqrt squared = self |
|
4875 |
]. |
|
18865 | 4876 |
]. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4877 |
|
18865 | 4878 |
"/ slow code |
4879 |
intSqrt := self integerSqrt. |
|
4880 |
^ intSqrt squared = self |
|
4881 |
||
4882 |
" |
|
19058 | 4883 |
0 isPerfectSquare |
4884 |
3 isPerfectSquare |
|
4885 |
4 isPerfectSquare |
|
4886 |
9 isPerfectSquare |
|
18865 | 4887 |
(1 to:1000000) count:[:n | n isPerfectSquare] 1000 |
4888 |
12345678987654321234567 isPerfectSquare |
|
4889 |
123123123432 squared isPerfectSquare |
|
4890 |
(123123123432 raisedTo:7) isPerfectSquare |
|
4891 |
((123456789123456789 raisedTo:7)) isPerfectSquare |
|
4892 |
((123456789123456789 raisedTo:7)-1) isPerfectSquare |
|
4893 |
Time microsecondsToRun:[12345678987654321234567 isPerfectSquare] |
|
4894 |
" |
|
4895 |
! |
|
4896 |
||
9134 | 4897 |
isPowerOf:p |
4898 |
"return true, if the receiver is a power of p" |
|
4899 |
||
4900 |
p == 2 ifTrue:[ ^ self isPowerOfTwo]. |
|
4901 |
||
4902 |
"/ the following is a q&d hack, using existing code. |
|
4903 |
^ (Integer |
|
19078 | 4904 |
readFromString:(self printStringRadix:p) |
4905 |
radix:2 |
|
4906 |
onError:-1) isPowerOfTwo |
|
9134 | 4907 |
|
4908 |
" |
|
19058 | 4909 |
0 isPowerOf:2 |
4910 |
1 isPowerOf:2 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4911 |
|
11587 | 4912 |
16r0000000000000000 isPowerOf:2 |
4913 |
16r0000004000000000 isPowerOf:2 |
|
4914 |
16r0000004000000001 isPowerOf:2 |
|
4915 |
||
4916 |
16r0000000000000001 isPowerOf:2 |
|
4917 |
16r0000000000000002 isPowerOf:2 |
|
4918 |
16r0000000000000004 isPowerOf:2 |
|
4919 |
16r0000000000000008 isPowerOf:2 |
|
4920 |
||
4921 |
16r0000000000000001 isPowerOf:4 |
|
4922 |
16r0000000000000002 isPowerOf:4 |
|
4923 |
16r0000000000000004 isPowerOf:4 |
|
4924 |
16r0000000000000008 isPowerOf:4 |
|
4925 |
16r0000000000000010 isPowerOf:4 |
|
4926 |
16r0000000000000020 isPowerOf:4 |
|
4927 |
||
4928 |
3r0000000000000001 isPowerOf:3 |
|
4929 |
3r0000000000000010 isPowerOf:3 |
|
4930 |
3r0000000000000100 isPowerOf:3 |
|
4931 |
3r0000000000001000 isPowerOf:3 |
|
4932 |
3r0000000000001001 isPowerOf:3 |
|
4933 |
3r0000000000002000 isPowerOf:3 |
|
4934 |
||
4935 |
10 isPowerOf:10 |
|
4936 |
20 isPowerOf:10 |
|
4937 |
100 isPowerOf:10 |
|
4938 |
110 isPowerOf:10 |
|
4939 |
200 isPowerOf:10 |
|
4940 |
1000 isPowerOf:10 |
|
4941 |
10000 isPowerOf:10 |
|
4942 |
100000 isPowerOf:10 |
|
4943 |
100001 isPowerOf:10 |
|
9134 | 4944 |
" |
13815 | 4945 |
! |
4946 |
||
4947 |
isPowerOfTwo |
|
4948 |
"return true, if the receiver is a power of 2" |
|
4949 |
||
4950 |
"redefined, because the hacker's algorithm in smallInt is much slower for large numbers" |
|
4951 |
||
4952 |
|maxBytes "{ Class: SmallInteger }"| |
|
4953 |
||
4954 |
maxBytes := self digitLength. |
|
4955 |
(self digitAt:maxBytes) isPowerOfTwo ifFalse:[^ false]. |
|
4956 |
1 to:maxBytes-1 do:[:byteIndex | |
|
19078 | 4957 |
(self digitAt:byteIndex) ~~ 0 ifTrue:[^ false]. |
13815 | 4958 |
]. |
4959 |
^ true |
|
4960 |
||
4961 |
" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4962 |
10000 factorial isPowerOfTwo |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4963 |
|n| n := 10000 factorial. Time millisecondsToRun:[1000 timesRepeat:[ n isPowerOfTwo]] |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4964 |
" |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4965 |
" |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4966 |
(2 raisedTo:10000) isPowerOfTwo |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
4967 |
|n| n := (2 raisedTo:10000). Time millisecondsToRun:[1000 timesRepeat:[ n isPowerOfTwo]] |
13815 | 4968 |
" |
4969 |
||
4970 |
"Modified: / 20-06-2011 / 12:43:05 / cg" |
|
4971 |
! |
|
4972 |
||
4973 |
isPrime |
|
4974 |
"return true if I am a prime Number. |
|
22114 | 4975 |
Pre-condition: I am positive. |
13815 | 4976 |
This is a q&d hack, which may need optimization if heavily used." |
4977 |
||
4978 |
|limit firstFewPrimes| |
|
4979 |
||
4980 |
self even ifTrue:[^ self == 2 ]. |
|
4981 |
self == 1 ifTrue:[^ false ]. |
|
4982 |
||
18865 | 4983 |
"/ See comment in initializePrimeCacheUpTo:limit |
4984 |
"/ Integer initializePrimeCacheUpTo:(10 raisedTo:7) |
|
4985 |
"/ Integer flushPrimeCache |
|
4986 |
"/ |
|
4987 |
"/ by default, no primeCache is used. |
|
4988 |
"/ if you do lots of number-stuff with primes, you may want to enable it with |
|
4989 |
"/ Integer initializePrimeCacheUpTo:1000000 |
|
4990 |
"/ and when done, cleanup with flushPrimeCache |
|
19128 | 4991 |
PrimeCache notNil ifTrue:[ |
4992 |
self <= (PrimeCache size*2) ifTrue:[ |
|
4993 |
^ PrimeCache at:self//2. |
|
4994 |
]. |
|
13815 | 4995 |
]. |
20269 | 4996 |
|
19128 | 4997 |
firstFewPrimes := self class primesUpTo5000. |
4998 |
limit := firstFewPrimes last + 1. |
|
4999 |
self < (limit*limit) ifTrue:[ |
|
5000 |
limit := self sqrt asInteger. |
|
20269 | 5001 |
]. |
13815 | 5002 |
firstFewPrimes do:[:p | |
19078 | 5003 |
p > limit ifTrue:[^ true]. |
5004 |
(self \\ p) == 0 ifTrue:[ ^ false ]. |
|
13815 | 5005 |
]. |
5006 |
||
19118 | 5007 |
PrimeNumberGenerator notNil ifTrue:[ "from exept:libcrypt" |
5008 |
"speed up a lot for large primes" |
|
5009 |
^ PrimeNumberGenerator isPrime:self. |
|
5010 |
]. |
|
5011 |
||
22114 | 5012 |
"/ Not absolutely correct, but was broken much worse before. |
22118 | 5013 |
limit := self integerSqrt. |
13815 | 5014 |
(firstFewPrimes last+2) to:limit by:2 do:[:i | |
19078 | 5015 |
(self \\ i) == 0 ifTrue:[ ^ false ]. |
13815 | 5016 |
]. |
5017 |
^ true |
|
5018 |
||
5019 |
" |
|
5020 |
Integer primesUpTo:1000 |
|
5021 |
(1 to:1000000) count:[:n | n isPrime] 78498 |
|
5022 |
Time millisecondsToRun:[ (1 to:1000000) count:[:n | n isPrime]] 1295 w.o firstFewPrimes |
|
5023 |
Time millisecondsToRun:[ (1 to:1000000) count:[:n | n isPrime]] 936 with firstFewPrimes (less tests) |
|
5024 |
Time millisecondsToRun:[ (1 to:1000000) count:[:n | n isPrime]] 343 with primeCache |
|
5025 |
" |
|
22114 | 5026 |
|
5027 |
"Modified (comment): / 26-07-2017 / 12:58:45 / mawalch" |
|
13815 | 5028 |
! |
5029 |
||
23119 | 5030 |
nextMultipleOf:n |
14680 | 5031 |
"return the multiple of n at or above the receiver. |
23119 | 5032 |
(?? The name of this method may be a bit misleading, |
5033 |
as it returns the receiver iff it is already a multiple) |
|
16800 | 5034 |
Useful for padding, aligning or rounding, |
21774 | 5035 |
especially when reading aligned binary data." |
14677 | 5036 |
|
5037 |
|rest| |
|
5038 |
||
5039 |
rest := self \\ n. |
|
5040 |
rest == 0 ifTrue:[ ^ self ]. |
|
5041 |
^ self + (n - rest) |
|
5042 |
||
5043 |
" |
|
19128 | 5044 |
0 nextMultipleOf: 4 -> 0 |
5045 |
1 nextMultipleOf: 4 -> 4 |
|
5046 |
2 nextMultipleOf: 4 -> 4 |
|
20269 | 5047 |
3 nextMultipleOf: 4 -> 4 |
5048 |
4 nextMultipleOf: 4 -> 4 |
|
5049 |
5 nextMultipleOf: 4 -> 8 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5050 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5051 |
22 nextMultipleOf: 4 |
19128 | 5052 |
100 factorial nextMultipleOf: 4 |
14677 | 5053 |
" |
21774 | 5054 |
|
5055 |
"Modified (comment): / 29-05-2017 / 15:40:45 / mawalch" |
|
23119 | 5056 |
"Modified (comment): / 20-06-2018 / 15:36:33 / Claus Gittinger" |
14677 | 5057 |
! |
5058 |
||
14680 | 5059 |
nextPowerOf2 |
5060 |
"return the power of 2 at or above the receiver. |
|
18769 | 5061 |
Useful for padding. |
19058 | 5062 |
Notice, that for a powerOf2, the receiver is returned. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5063 |
Also notice, that (because it is used for padding), |
19058 | 5064 |
0 is returned for zero." |
14680 | 5065 |
|
5066 |
|x t sh| |
|
5067 |
||
5068 |
x := self - 1. |
|
5069 |
x := x bitOr: (x bitShift: -1). |
|
5070 |
x := x bitOr: (x bitShift: -2). |
|
5071 |
x := x bitOr: (x bitShift: -4). |
|
5072 |
x := x bitOr: (x bitShift: -8). |
|
5073 |
x := x bitOr: (t := x bitShift: -16). |
|
5074 |
t == 0 ifFalse:[ |
|
19078 | 5075 |
sh := -32. |
5076 |
[ |
|
5077 |
x := x bitOr: (t := x bitShift: sh). |
|
5078 |
sh := sh + sh. |
|
5079 |
] doWhile: [t ~~ 0] |
|
14680 | 5080 |
]. |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5081 |
^ x + 1 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5082 |
|
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5083 |
" |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5084 |
0 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5085 |
1 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5086 |
2 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5087 |
3 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5088 |
4 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5089 |
5 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5090 |
6 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5091 |
7 nextPowerOf2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5092 |
8 nextPowerOf2 |
14680 | 5093 |
|
5094 |
22 nextPowerOf2 |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5095 |
12 factorial nextPowerOf2 isPowerOf:2 |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5096 |
100 factorial nextPowerOf2 isPowerOf:2 |
18905 | 5097 |
1000 factorial nextPowerOf2 isPowerOf:2 |
5098 |
Time millisecondsToRun:[ |
|
19078 | 5099 |
|v| |
5100 |
v := 1000 factorial. |
|
5101 |
1000 timesRepeat:[ |
|
5102 |
v nextPowerOf2 |
|
5103 |
] |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5104 |
] |
14680 | 5105 |
" |
5106 |
! |
|
5107 |
||
13815 | 5108 |
nextPrime |
5109 |
"return the next prime after the receiver" |
|
5110 |
||
5111 |
|num| |
|
5112 |
||
5113 |
num := self + 1. |
|
19015 | 5114 |
num <= 2 ifTrue:[^ 2]. |
13815 | 5115 |
num even ifTrue:[ |
19078 | 5116 |
num := num + 1 |
13815 | 5117 |
]. |
5118 |
[num isPrime] whileFalse:[ |
|
19078 | 5119 |
num := num + 2 |
13815 | 5120 |
]. |
5121 |
^ num |
|
5122 |
||
5123 |
" |
|
19015 | 5124 |
0 nextPrime |
13815 | 5125 |
1 nextPrime |
19015 | 5126 |
2 nextPrime |
13815 | 5127 |
22 nextPrime |
5128 |
37 nextPrime |
|
5129 |
36 nextPrime |
|
5130 |
3456737 nextPrime |
|
18865 | 5131 |
1000 factorial nextPrime |
13815 | 5132 |
" |
13905 | 5133 |
! |
5134 |
||
13906 | 5135 |
parityOdd |
13914 | 5136 |
"return true, if an odd number of bits are set in the receiver, false otherwise. |
13905 | 5137 |
(i.e. true for odd parity) |
5138 |
Undefined for negative values (smalltalk does not require the machine to use 2's complement)" |
|
5139 |
||
5140 |
^ self bitCount odd |
|
5141 |
||
5142 |
" |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5143 |
0 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5144 |
1 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5145 |
2 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5146 |
4 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5147 |
5 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5148 |
7 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5149 |
33 parityOdd |
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5150 |
6 parityOdd |
13905 | 5151 |
|
5152 |
1 to:1000000 do:[:n | |
|
19078 | 5153 |
self assert:(n parityOdd = ((n printStringRadix:2) occurrencesOf:$1) odd). |
13905 | 5154 |
] |
13906 | 5155 |
|
5156 |
0 to:255 do:[:n | |
|
19078 | 5157 |
|p| |
5158 |
||
5159 |
p := |
|
5160 |
(((((((((n rightShift: 7) |
|
5161 |
bitXor: (n rightShift: 6)) |
|
5162 |
bitXor: (n rightShift: 5)) |
|
5163 |
bitXor: (n rightShift: 4)) |
|
5164 |
bitXor: (n rightShift: 3)) |
|
5165 |
bitXor: (n rightShift: 2)) |
|
5166 |
bitXor: (n rightShift: 1)) |
|
5167 |
bitXor: n) bitAnd:1) == 1. |
|
5168 |
self assert:(n parityOdd = p). |
|
13906 | 5169 |
] |
5170 |
" |
|
5171 |
||
5172 |
"Created: / 09-01-2012 / 17:18:06 / cg" |
|
701 | 5173 |
! ! |
202 | 5174 |
|
22460 | 5175 |
|
16293 | 5176 |
!Integer methodsFor:'special modulo arithmetic'! |
2458 | 5177 |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5178 |
add_32:anInteger |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5179 |
"return a C-semantic 32bit sum of the receiver and the argument. |
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5180 |
Both must be either Small- or LargeIntegers. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5181 |
Returns a signed 32bit number. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5182 |
This (nonstandard) specialized method is provided to allow simulation of |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5183 |
modulu operations with C semantics." |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5184 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5185 |
%{ /* NOCONTEXT */ |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5186 |
int val1, val2, rslt; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5187 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5188 |
if (__isSmallInteger(self)) { |
19078 | 5189 |
val1 = __intVal(self); |
12652 | 5190 |
} else { |
19078 | 5191 |
val1 = __longIntVal(self); |
5192 |
if (!val1) goto bad; |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5193 |
} |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5194 |
if (__isSmallInteger(anInteger)) { |
19078 | 5195 |
val2 = __intVal(anInteger); |
12652 | 5196 |
} else { |
19078 | 5197 |
val2 = __longIntVal(anInteger); |
5198 |
if (!val2) goto bad; |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5199 |
} |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5200 |
rslt = val1 + val2; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5201 |
RETURN(__MKINT(rslt)); |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5202 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5203 |
bad: ; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5204 |
%}. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5205 |
self primitiveFailed. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5206 |
! |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5207 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5208 |
add_32u:anInteger |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5209 |
"return a C-semantic 32bit unsigned sum of the receiver and the argument. |
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5210 |
Both must be either Small- or LargeIntegers. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5211 |
Returns an unsigned 32bit number. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5212 |
This (nonstandard) specialized method is provided to allow simulation of |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5213 |
modulu operations with C semantics." |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5214 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5215 |
%{ /* NOCONTEXT */ |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5216 |
int val1, val2, rslt; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5217 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5218 |
if (__isSmallInteger(self)) { |
19078 | 5219 |
val1 = __intVal(self); |
12652 | 5220 |
} else { |
19078 | 5221 |
val1 = __longIntVal(self); |
5222 |
if (!val1) goto bad; |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5223 |
} |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5224 |
if (__isSmallInteger(anInteger)) { |
19078 | 5225 |
val2 = __intVal(anInteger); |
12652 | 5226 |
} else { |
19078 | 5227 |
val2 = __longIntVal(anInteger); |
5228 |
if (!val2) goto bad; |
|
12605
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5229 |
} |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5230 |
rslt = val1 + val2; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5231 |
RETURN(__MKUINT((unsigned)rslt)); |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5232 |
|
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5233 |
bad: ; |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5234 |
%}. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5235 |
self primitiveFailed. |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5236 |
! |
9bf77c63d4b2
added:8 methods for 32bit modulu arithmetic
Claus Gittinger <cg@exept.de>
parents:
12588
diff
changeset
|
5237 |
|
13725 | 5238 |
mul_32:anInteger |
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5239 |
"return a C-semantic 32bit product of the receiver and the argument. |
13725 | 5240 |
Both must be either Small- or LargeIntegers. |
5241 |
Returns a signed 32bit number. |
|
5242 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5243 |
modulu operations with C semantics." |
|
5244 |
||
5245 |
%{ /* NOCONTEXT */ |
|
5246 |
int val1, val2, rslt; |
|
5247 |
||
5248 |
if (__isSmallInteger(self)) { |
|
19078 | 5249 |
val1 = __intVal(self); |
13725 | 5250 |
} else { |
19078 | 5251 |
val1 = __longIntVal(self); |
5252 |
if (!val1) goto bad; |
|
13725 | 5253 |
} |
5254 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5255 |
val2 = __intVal(anInteger); |
13725 | 5256 |
} else { |
19078 | 5257 |
val2 = __longIntVal(anInteger); |
5258 |
if (!val2) goto bad; |
|
13725 | 5259 |
} |
5260 |
rslt = val1 * val2; |
|
5261 |
RETURN(__MKINT(rslt)); |
|
5262 |
||
5263 |
bad: ; |
|
5264 |
%}. |
|
5265 |
self primitiveFailed. |
|
5266 |
! |
|
5267 |
||
5268 |
mul_32u:anInteger |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5269 |
"return a C-semantic 32bit unsigned product of the receiver and the argument. |
13725 | 5270 |
Both must be either Small- or LargeIntegers. |
5271 |
Returns an unsigned 32bit number. |
|
5272 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5273 |
modulu operations with C semantics." |
|
5274 |
||
5275 |
%{ /* NOCONTEXT */ |
|
5276 |
int val1, val2, rslt; |
|
5277 |
||
5278 |
if (__isSmallInteger(self)) { |
|
19078 | 5279 |
val1 = __intVal(self); |
13725 | 5280 |
} else { |
19078 | 5281 |
val1 = __longIntVal(self); |
5282 |
if (!val1) goto bad; |
|
13725 | 5283 |
} |
5284 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5285 |
val2 = __intVal(anInteger); |
13725 | 5286 |
} else { |
19078 | 5287 |
val2 = __longIntVal(anInteger); |
5288 |
if (!val2) goto bad; |
|
13725 | 5289 |
} |
5290 |
rslt = val1 * val2; |
|
5291 |
RETURN(__MKUINT((unsigned)rslt)); |
|
5292 |
||
5293 |
bad: ; |
|
5294 |
%}. |
|
5295 |
self primitiveFailed. |
|
5296 |
! |
|
5297 |
||
5298 |
sub_32:anInteger |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5299 |
"return a C-semantic 32bit difference of the receiver and the argument. |
13725 | 5300 |
Both must be either Small- or LargeIntegers. |
5301 |
Returns a signed 32bit number. |
|
5302 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5303 |
modulu operations with C semantics." |
|
5304 |
||
5305 |
%{ /* NOCONTEXT */ |
|
5306 |
int val1, val2, rslt; |
|
5307 |
||
5308 |
if (__isSmallInteger(self)) { |
|
19078 | 5309 |
val1 = __intVal(self); |
13725 | 5310 |
} else { |
19078 | 5311 |
val1 = __longIntVal(self); |
5312 |
if (!val1) goto bad; |
|
13725 | 5313 |
} |
5314 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5315 |
val2 = __intVal(anInteger); |
13725 | 5316 |
} else { |
19078 | 5317 |
val2 = __longIntVal(anInteger); |
5318 |
if (!val2) goto bad; |
|
13725 | 5319 |
} |
5320 |
rslt = val1 - val2; |
|
5321 |
RETURN(__MKINT(rslt)); |
|
5322 |
||
5323 |
bad: ; |
|
5324 |
%}. |
|
5325 |
self primitiveFailed. |
|
5326 |
! |
|
5327 |
||
5328 |
sub_32u:anInteger |
|
19076
6765dbf123d8
now compilable with NUMBER-as-binexpr fix
Claus Gittinger <cg@exept.de>
parents:
19071
diff
changeset
|
5329 |
"return a C-semantic 32bit unsigned difference of the receiver and the argument. |
13725 | 5330 |
Both must be either Small- or LargeIntegers. |
5331 |
Returns an unsigned 32bit number. |
|
5332 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5333 |
modulu operations with C semantics." |
|
5334 |
||
5335 |
%{ /* NOCONTEXT */ |
|
5336 |
int val1, val2, rslt; |
|
5337 |
||
5338 |
if (__isSmallInteger(self)) { |
|
19078 | 5339 |
val1 = __intVal(self); |
13725 | 5340 |
} else { |
19078 | 5341 |
val1 = __longIntVal(self); |
5342 |
if (!val1) goto bad; |
|
13725 | 5343 |
} |
5344 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5345 |
val2 = __intVal(anInteger); |
13725 | 5346 |
} else { |
19078 | 5347 |
val2 = __longIntVal(anInteger); |
5348 |
if (!val2) goto bad; |
|
13725 | 5349 |
} |
5350 |
rslt = val1 - val2; |
|
5351 |
RETURN(__MKUINT((unsigned)rslt)); |
|
5352 |
||
5353 |
bad: ; |
|
5354 |
%}. |
|
5355 |
self primitiveFailed. |
|
5356 |
! ! |
|
5357 |
||
16293 | 5358 |
!Integer methodsFor:'special modulo bit operators'! |
5359 |
||
5360 |
asSigned32 |
|
5361 |
"return a 32-bit integer with my bit-pattern. For protocol completeness." |
|
5362 |
||
5363 |
^ self |
|
5364 |
! |
|
5365 |
||
22069 | 5366 |
asUnsigned128 |
5367 |
"return a 128-bit integer with my bit-pattern, but positive. |
|
5368 |
May be required for bit operations on the sign-bit and/or to |
|
5369 |
convert C numbers." |
|
5370 |
||
5371 |
self < 0 ifTrue:[ |
|
5372 |
^ 16r1000000000000000000000000000000000 + self |
|
5373 |
]. |
|
5374 |
^ self |
|
5375 |
||
5376 |
" |
|
5377 |
(-1 asUnsigned128) hexPrintString |
|
5378 |
1 asUnsigned128 |
|
5379 |
(SmallInteger minVal asUnsigned128) hexPrintString |
|
5380 |
(SmallInteger maxVal asUnsigned128) hexPrintString |
|
5381 |
" |
|
5382 |
||
5383 |
"Created: / 21-07-2017 / 14:44:19 / cg" |
|
5384 |
! |
|
5385 |
||
16293 | 5386 |
asUnsigned32 |
5387 |
"return a 32-bit integer with my bit-pattern, but positive. |
|
5388 |
May be required for bit operations on the sign-bit and/or to |
|
5389 |
convert C/Java numbers." |
|
5390 |
||
5391 |
self < 0 ifTrue:[ |
|
19078 | 5392 |
^ 16r100000000 + self |
16293 | 5393 |
]. |
5394 |
^ self |
|
5395 |
||
5396 |
" |
|
5397 |
(-1 asUnsigned32) hexPrintString |
|
5398 |
1 asUnsigned32 |
|
5399 |
(SmallInteger minVal asUnsigned32) hexPrintString |
|
5400 |
(SmallInteger maxVal asUnsigned32) hexPrintString |
|
5401 |
" |
|
5402 |
! |
|
5403 |
||
5404 |
asUnsigned64 |
|
5405 |
"return a 64-bit integer with my bit-pattern, but positive. |
|
5406 |
May be required for bit operations on the sign-bit and/or to |
|
5407 |
convert C/Java numbers." |
|
5408 |
||
5409 |
self < 0 ifTrue:[ |
|
19078 | 5410 |
^ 16r10000000000000000 + self |
16293 | 5411 |
]. |
5412 |
^ self |
|
5413 |
||
5414 |
" |
|
5415 |
(-1 asUnsigned64) hexPrintString |
|
5416 |
1 asUnsigned64 |
|
5417 |
(SmallInteger minVal asUnsigned64) hexPrintString |
|
5418 |
(SmallInteger maxVal asUnsigned64) hexPrintString |
|
5419 |
" |
|
5420 |
||
5421 |
"Created: / 26-07-2013 / 13:45:11 / cg" |
|
5422 |
! |
|
13725 | 5423 |
|
22072 | 5424 |
asUnsigned:numBits |
5425 |
"return a numBits integer with my bit-pattern, but positive. |
|
5426 |
May be required for bit operations on the sign-bit and/or to |
|
22073 | 5427 |
convert C/Java numbers, or to generate bitields from signed numbers |
5428 |
(kind of the reverse operation to signExtenedFromBit:)." |
|
22072 | 5429 |
|
5430 |
self < 0 ifTrue:[ |
|
5431 |
^ (1 bitShift:numBits) + self |
|
5432 |
]. |
|
5433 |
^ self |
|
5434 |
||
5435 |
" |
|
5436 |
(-1 asUnsigned:64) hexPrintString |
|
5437 |
1 asUnsigned:64 |
|
5438 |
(SmallInteger minVal asUnsigned:64) hexPrintString |
|
5439 |
(SmallInteger maxVal asUnsigned:64) hexPrintString |
|
5440 |
||
5441 |
(-1 asUnsigned:4) hexPrintString |
|
5442 |
(-7 asUnsigned:4) hexPrintString |
|
5443 |
(-8 asUnsigned:4) hexPrintString |
|
5444 |
1 asUnsigned:4 |
|
5445 |
" |
|
5446 |
||
5447 |
"Created: / 21-07-2017 / 15:11:40 / cg" |
|
5448 |
! |
|
5449 |
||
2458 | 5450 |
bitAnd_32:anInteger |
5451 |
"return a C-semantic 32bit locical-and of the receiver and |
|
5452 |
the argument. Both must be either Small- or LargeIntegers. |
|
12255 | 5453 |
Returns a signed 32bit number. |
2458 | 5454 |
This (nonstandard) specialized method is provided to allow simulation of |
12255 | 5455 |
bit operations with C semantics." |
5456 |
||
5457 |
%{ /* NOCONTEXT */ |
|
5458 |
int val1, val2, rslt; |
|
5459 |
||
5460 |
if (__isSmallInteger(self)) { |
|
19078 | 5461 |
val1 = __intVal(self); |
12652 | 5462 |
} else { |
19078 | 5463 |
val1 = __longIntVal(self); |
5464 |
if (!val1) goto bad; |
|
12255 | 5465 |
} |
5466 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5467 |
val2 = __intVal(anInteger); |
12652 | 5468 |
} else { |
19078 | 5469 |
val2 = __longIntVal(anInteger); |
5470 |
if (!val2) goto bad; |
|
12255 | 5471 |
} |
5472 |
rslt = val1 & val2; |
|
5473 |
RETURN(__MKINT(rslt)); |
|
5474 |
||
5475 |
bad: ; |
|
5476 |
%}. |
|
5477 |
self primitiveFailed. |
|
5478 |
! |
|
5479 |
||
5480 |
bitAnd_32u:anInteger |
|
5481 |
"return a C-semantic 32bit locical-and of the receiver and |
|
5482 |
the argument. Both must be either Small- or LargeIntegers. |
|
5483 |
Returns an unsigned 32bit number. |
|
5484 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5485 |
bit operations with C semantics." |
|
2458 | 5486 |
|
5487 |
%{ /* NOCONTEXT */ |
|
5488 |
int val1, val2, rslt; |
|
5489 |
||
5490 |
if (__isSmallInteger(self)) { |
|
19078 | 5491 |
val1 = __intVal(self); |
12652 | 5492 |
} else { |
19078 | 5493 |
val1 = __longIntVal(self); |
5494 |
if (!val1) goto bad; |
|
2458 | 5495 |
} |
5496 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5497 |
val2 = __intVal(anInteger); |
12652 | 5498 |
} else { |
19078 | 5499 |
val2 = __longIntVal(anInteger); |
5500 |
if (!val2) goto bad; |
|
2458 | 5501 |
} |
5502 |
rslt = val1 & val2; |
|
12255 | 5503 |
RETURN(__MKUINT(rslt)); |
5504 |
||
5505 |
bad: ; |
|
5506 |
%}. |
|
5507 |
self primitiveFailed. |
|
5508 |
! |
|
5509 |
||
5510 |
bitInvert_32 |
|
5511 |
"return a C-semantic 32bit complement of the receiver, |
|
5512 |
which must be either Small- or LargeIntegers. |
|
5513 |
Returns a signed 32bit number. |
|
5514 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5515 |
bit operations with C semantics." |
|
5516 |
||
5517 |
%{ /* NOCONTEXT */ |
|
5518 |
int val, rslt; |
|
5519 |
||
5520 |
if (__isSmallInteger(self)) { |
|
19078 | 5521 |
val = __intVal(self); |
12652 | 5522 |
} else { |
19078 | 5523 |
val = __longIntVal(self); |
5524 |
if (!val) goto bad; |
|
12255 | 5525 |
} |
5526 |
rslt = ~val; |
|
2458 | 5527 |
RETURN(__MKINT(rslt)); |
5528 |
||
5529 |
bad: ; |
|
5530 |
%}. |
|
5531 |
self primitiveFailed. |
|
5532 |
! |
|
5533 |
||
12255 | 5534 |
bitInvert_32u |
5535 |
"return a C-semantic 32bit complement of the receiver, |
|
5536 |
which must be either Small- or LargeIntegers. |
|
5537 |
Returns an unsigned 32bit number. |
|
5538 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5539 |
bit operations with C semantics." |
|
5540 |
||
5541 |
%{ /* NOCONTEXT */ |
|
5542 |
int val, rslt; |
|
5543 |
||
5544 |
if (__isSmallInteger(self)) { |
|
19078 | 5545 |
val = __intVal(self); |
12652 | 5546 |
} else { |
19078 | 5547 |
val = __longIntVal(self); |
5548 |
if (!val) goto bad; |
|
12255 | 5549 |
} |
5550 |
rslt = ~val; |
|
5551 |
RETURN(__MKUINT(rslt)); |
|
5552 |
||
5553 |
bad: ; |
|
5554 |
%}. |
|
5555 |
self primitiveFailed. |
|
5556 |
! |
|
5557 |
||
2458 | 5558 |
bitOr_32:anInteger |
5559 |
"return a C-semantic 32bit locical-or of the receiver and |
|
5560 |
the argument. Both must be either Small- or LargeIntegers. |
|
12255 | 5561 |
Returns a signed 32bit number. |
2458 | 5562 |
This (nonstandard) specialized method is provided to allow simulation of |
12255 | 5563 |
bit operations with C semantics." |
5564 |
||
5565 |
%{ /* NOCONTEXT */ |
|
5566 |
int val1, val2, rslt; |
|
5567 |
||
5568 |
if (__isSmallInteger(self)) { |
|
19078 | 5569 |
val1 = __intVal(self); |
12652 | 5570 |
} else { |
19078 | 5571 |
val1 = __longIntVal(self); |
5572 |
if (!val1) goto bad; |
|
12255 | 5573 |
} |
5574 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5575 |
val2 = __intVal(anInteger); |
12652 | 5576 |
} else { |
19078 | 5577 |
val2 = __longIntVal(anInteger); |
5578 |
if (!val2) goto bad; |
|
12255 | 5579 |
} |
5580 |
rslt = val1 | val2; |
|
5581 |
RETURN(__MKINT(rslt)); |
|
5582 |
||
5583 |
bad: ; |
|
5584 |
%}. |
|
5585 |
self primitiveFailed. |
|
5586 |
! |
|
5587 |
||
5588 |
bitOr_32u:anInteger |
|
5589 |
"return a C-semantic 32bit locical-or of the receiver and |
|
5590 |
the argument. Both must be either Small- or LargeIntegers. |
|
5591 |
Returns an unsigned 32bit number. |
|
5592 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5593 |
bit operations with C semantics." |
|
2458 | 5594 |
|
5595 |
%{ /* NOCONTEXT */ |
|
5596 |
int val1, val2, rslt; |
|
5597 |
||
5598 |
if (__isSmallInteger(self)) { |
|
19078 | 5599 |
val1 = __intVal(self); |
12652 | 5600 |
} else { |
19078 | 5601 |
val1 = __longIntVal(self); |
5602 |
if (!val1) goto bad; |
|
12255 | 5603 |
} |
5604 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5605 |
val2 = __intVal(anInteger); |
12652 | 5606 |
} else { |
19078 | 5607 |
val2 = __longIntVal(anInteger); |
5608 |
if (!val2) goto bad; |
|
12255 | 5609 |
} |
5610 |
rslt = val1 | val2; |
|
5611 |
RETURN(__MKUINT(rslt)); |
|
5612 |
||
5613 |
bad: ; |
|
5614 |
%}. |
|
5615 |
self primitiveFailed. |
|
5616 |
! |
|
5617 |
||
5618 |
bitXor_32:anInteger |
|
5619 |
"return a C-semantic 32bit locical-xor of the receiver and |
|
5620 |
the argument. Both must be either Small- or LargeIntegers. |
|
5621 |
Returns a signed 32bit number. |
|
5622 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5623 |
bit operations with C semantics." |
|
5624 |
||
5625 |
%{ /* NOCONTEXT */ |
|
5626 |
int val1, val2, rslt; |
|
5627 |
||
5628 |
if (__isSmallInteger(self)) { |
|
19078 | 5629 |
val1 = __intVal(self); |
12652 | 5630 |
} else { |
19078 | 5631 |
val1 = __longIntVal(self); |
5632 |
if (!val1) goto bad; |
|
2458 | 5633 |
} |
5634 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5635 |
val2 = __intVal(anInteger); |
12652 | 5636 |
} else { |
19078 | 5637 |
val2 = __longIntVal(anInteger); |
5638 |
if (!val2) goto bad; |
|
2458 | 5639 |
} |
12255 | 5640 |
rslt = val1 ^ val2; |
2458 | 5641 |
RETURN(__MKINT(rslt)); |
5642 |
||
5643 |
bad: ; |
|
5644 |
%}. |
|
5645 |
self primitiveFailed. |
|
12255 | 5646 |
! |
5647 |
||
5648 |
bitXor_32u:anInteger |
|
5649 |
"return a C-semantic 32bit locical-xor of the receiver and |
|
5650 |
the argument. Both must be either Small- or LargeIntegers. |
|
5651 |
Returns an unsigned 32bit number. |
|
5652 |
This (nonstandard) specialized method is provided to allow simulation of |
|
5653 |
bit operations with C semantics." |
|
5654 |
||
5655 |
%{ /* NOCONTEXT */ |
|
5656 |
int val1, val2, rslt; |
|
5657 |
||
5658 |
if (__isSmallInteger(self)) { |
|
19078 | 5659 |
val1 = __intVal(self); |
12652 | 5660 |
} else { |
19078 | 5661 |
val1 = __longIntVal(self); |
5662 |
if (!val1) goto bad; |
|
12255 | 5663 |
} |
5664 |
if (__isSmallInteger(anInteger)) { |
|
19078 | 5665 |
val2 = __intVal(anInteger); |
12652 | 5666 |
} else { |
19078 | 5667 |
val2 = __longIntVal(anInteger); |
5668 |
if (!val2) goto bad; |
|
12255 | 5669 |
} |
5670 |
rslt = val1 ^ val2; |
|
5671 |
RETURN(__MKUINT(rslt)); |
|
5672 |
||
5673 |
bad: ; |
|
5674 |
%}. |
|
5675 |
self primitiveFailed. |
|
2458 | 5676 |
! ! |
5677 |
||
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
5678 |
!Integer methodsFor:'tracing'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
5679 |
|
4682 | 5680 |
traceInto:aRequestor level:level from:referrer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
5681 |
"double dispatch into tracer, passing my type implicitely in the selector" |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
5682 |
|
4682 | 5683 |
^ aRequestor traceInteger:self level:level from:referrer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
5684 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
5685 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
5686 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
5687 |
|
701 | 5688 |
!Integer methodsFor:'truncation & rounding'! |
5689 |
||
5690 |
ceiling |
|
1558 | 5691 |
"return the smallest integer which is larger or equal to the receiver. |
5692 |
For integers, this is the receiver itself." |
|
345 | 5693 |
|
701 | 5694 |
^ self |
1558 | 5695 |
|
5696 |
"Modified: 18.7.1996 / 12:44:06 / cg" |
|
701 | 5697 |
! |
345 | 5698 |
|
701 | 5699 |
compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5700 |
"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
|
5701 |
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
|
5702 |
zeros removed. This method is redefined in LargeInteger." |
701 | 5703 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5704 |
^ self |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5705 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5706 |
"Modified: 5.11.1996 / 14:07:41 / cg" |
202 | 5707 |
! |
5708 |
||
701 | 5709 |
floor |
1558 | 5710 |
"return the largest integer which is smaller or equal to the receiver. |
5711 |
For integers, this is the receiver itself." |
|
701 | 5712 |
|
5713 |
^ self |
|
1558 | 5714 |
|
5715 |
"Modified: 18.7.1996 / 12:44:00 / cg" |
|
701 | 5716 |
! |
202 | 5717 |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5718 |
fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5719 |
"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
|
5720 |
(i.e. the receiver minus its truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5721 |
Since integers have no fraction, return 0 here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5722 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5723 |
^ 0 |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5724 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5725 |
" |
11587 | 5726 |
1234.56789 fractionPart |
5727 |
1.2345e6 fractionPart |
|
5728 |
1000 fractionPart |
|
5729 |
10000000000000000 fractionPart |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5730 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5731 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5732 |
"Modified: 4.11.1996 / 20:27:44 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5733 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5734 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5735 |
integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5736 |
"return a number with value from digits before the decimal point. |
18851 | 5737 |
(i.e. the receiver's truncated value) |
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5738 |
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
|
5739 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5740 |
^ self |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5741 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5742 |
" |
11587 | 5743 |
1234.56789 integerPart |
5744 |
1.2345e6 integerPart |
|
5745 |
1000 integerPart |
|
5746 |
10000000000000000 integerPart |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5747 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5748 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5749 |
"Modified: 4.11.1996 / 20:28:22 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5750 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
5751 |
|
701 | 5752 |
normalize |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5753 |
"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
|
5754 |
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
|
5755 |
zeros removed. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5756 |
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
|
5757 |
renamed to #compressed for ST-80 compatibility." |
1 | 5758 |
|
9261 | 5759 |
<resource: #obsolete> |
5760 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5761 |
^ self compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5762 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
5763 |
"Modified: 5.11.1996 / 14:08:24 / cg" |
701 | 5764 |
! |
1 | 5765 |
|
701 | 5766 |
rounded |
5767 |
"return the receiver rounded toward the next Integer - |
|
1558 | 5768 |
for integers this is the receiver itself." |
701 | 5769 |
|
5770 |
^ self |
|
1558 | 5771 |
|
5772 |
"Modified: 18.7.1996 / 12:44:24 / cg" |
|
701 | 5773 |
! |
69 | 5774 |
|
701 | 5775 |
truncated |
11587 | 5776 |
"return the receiver truncated towards zero as Integer |
1558 | 5777 |
for integers this is the receiver itself." |
159 | 5778 |
|
701 | 5779 |
^ self |
1558 | 5780 |
|
5781 |
"Modified: 18.7.1996 / 12:44:33 / cg" |
|
701 | 5782 |
! ! |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
5783 |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
5784 |
!Integer methodsFor:'visiting'! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
5785 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
5786 |
acceptVisitor:aVisitor with:aParameter |
16723 | 5787 |
"dispatch for visitor pattern; send #visitInteger:with: to aVisitor" |
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
5788 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
5789 |
^ aVisitor visitInteger:self with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
5790 |
! ! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
5791 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5792 |
!Integer::ModuloNumber class methodsFor:'documentation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5793 |
|
4803 | 5794 |
copyright |
5795 |
" |
|
5796 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
19078 | 5797 |
All Rights Reserved |
4803 | 5798 |
|
5799 |
This software is furnished under a license and may be used |
|
5800 |
only in accordance with the terms of that license and with the |
|
5801 |
inclusion of the above copyright notice. This software may not |
|
5802 |
be provided or otherwise made available to, or used by, any |
|
5803 |
other person. No title to or ownership of the software is |
|
5804 |
hereby transferred. |
|
5805 |
" |
|
5806 |
||
5807 |
! |
|
5808 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5809 |
documentation |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5810 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5811 |
This is a helper class to perform fast computation of the modulus. |
4609 | 5812 |
(with big numbers, this does make a difference) |
4803 | 5813 |
|
5814 |
WARNING: this does only work with numbers which have no common |
|
19078 | 5815 |
divisor (which is true for cryptographic applications). |
5816 |
So, use this only if you know what you are doing ... |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5817 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5818 |
[author:] |
19078 | 5819 |
Stefan Vogel |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5820 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5821 |
[see also:] |
19078 | 5822 |
Integer SmallInteger LargeInsteger |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5823 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5824 |
[instance variables:] |
19078 | 5825 |
modulus the modulus |
5826 |
reciprocal reciprocal of the modulus |
|
5827 |
shift shift count to cut off some bits |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5828 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5829 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5830 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5831 |
examples |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5832 |
" |
19078 | 5833 |
[exBegin] |
5834 |
17 asModuloNumber modulusOf:38 |
|
5835 |
[exEnd] |
|
5836 |
||
5837 |
[exBegin] |
|
5838 |
38 \\ 17 |
|
5839 |
[exEnd] |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5840 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5841 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5842 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5843 |
!Integer::ModuloNumber class methodsFor:'instance creation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5844 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5845 |
modulus:anInteger |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5846 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5847 |
^ self new modulus:anInteger |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5848 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5849 |
"Created: / 3.5.1999 / 11:13:15 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5850 |
"Modified: / 3.5.1999 / 11:18:37 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5851 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5852 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5853 |
!Integer::ModuloNumber methodsFor:'accessing'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5854 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5855 |
modulus |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5856 |
"return the modulus" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5857 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5858 |
^ modulus |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5859 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5860 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5861 |
modulus:n |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5862 |
"set the modulus" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5863 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5864 |
modulus := n. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5865 |
reciprocal := n integerReciprocal. |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5866 |
shift := n highBit negated. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5867 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5868 |
"Created: / 3.5.1999 / 10:02:39 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5869 |
"Modified: / 3.5.1999 / 14:30:49 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5870 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5871 |
|
5238 | 5872 |
!Integer::ModuloNumber methodsFor:'arithmetic'! |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5873 |
|
19125 | 5874 |
modulusOf:dividend |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5875 |
"compute the aNumber modulo myself. |
19231 | 5876 |
The shortcut works only, if dividend is < modulo * modulo |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5877 |
(When doing arithmethic modulo something). |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5878 |
Otherwise do it the long way" |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5879 |
|
19125 | 5880 |
|e t cnt abs isNegative| |
5881 |
||
5882 |
isNegative := dividend negative. |
|
5883 |
isNegative ifTrue:[ |
|
5884 |
abs := dividend negated. |
|
5885 |
] ifFalse:[ |
|
5886 |
abs := dividend. |
|
5887 |
]. |
|
19874 | 5888 |
abs < modulus ifTrue:[ |
5889 |
^ abs. |
|
5890 |
]. |
|
19125 | 5891 |
|
19231 | 5892 |
"/ self assert:dividend < (modulus * modulus) |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5893 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5894 |
"throw off low nbits(modulus)" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5895 |
|
19231 | 5896 |
e := (abs bitShift:shift) * reciprocal. |
5897 |
e := (e bitShift:shift) * modulus. |
|
19125 | 5898 |
e := abs - e. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5899 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5900 |
"this subtract is done max 2 times" |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5901 |
cnt := 2. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5902 |
[(t := e - modulus) >= 0] whileTrue:[ |
19078 | 5903 |
e := t. |
5904 |
cnt == 0 ifTrue:[ |
|
5905 |
"shortcut didn't work, do it the long way" |
|
5906 |
^ e \\ modulus. |
|
5907 |
]. |
|
5908 |
cnt := cnt - 1. |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5909 |
]. |
19125 | 5910 |
isNegative ifTrue:[ |
5911 |
^ modulus - e. |
|
5912 |
]. |
|
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5913 |
^ e. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5914 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5915 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5916 |
|m| |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5917 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5918 |
m := self new modulus:7. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5919 |
m modulusOf:55. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5920 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5921 |
|
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
5922 |
"Shortcut does not work: (size of divisor vs. dividend): |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5923 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5924 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5925 |
m := self new modulus:7. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5926 |
m modulusOf:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5927 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5928 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5929 |
"SLOW (using standard \\ operation): |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5930 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5931 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5932 |
m := 123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5933 |
Time millisecondsToRun:[ |
19078 | 5934 |
100000 timesRepeat:[ |
5935 |
874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658 \\ m |
|
5936 |
] |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5937 |
] |
11587 | 5938 |
" |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5939 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5940 |
"fast (using moduloNumber with almost same-sized dividend and divisor): |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5941 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5942 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5943 |
m := self new modulus:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
19231 | 5944 |
Time millisecondsToRun:[ |
5945 |
100000 timesRepeat:[ |
|
5946 |
m modulusOf:874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
|
5947 |
] |
|
5948 |
]. |
|
5949 |
||
5950 |
10730930127807326146398409623772237722337234475792709784029183368622308259008044569184592041059181058049458041058052 |
|
11587 | 5951 |
" |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
5952 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5953 |
"Modified: / 3.5.1999 / 14:30:32 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5954 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5955 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5956 |
!Integer::ModuloNumber methodsFor:'converting'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5957 |
|
19405 | 5958 |
asInteger |
5959 |
"return the modulus" |
|
5960 |
||
5961 |
^ modulus |
|
5962 |
! |
|
5963 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5964 |
asModuloNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5965 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5966 |
^ self |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5967 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5968 |
"Created: / 3.5.1999 / 14:48:27 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5969 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
5970 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
5971 |
!Integer class methodsFor:'documentation'! |
701 | 5972 |
|
5973 |
version |
|
18769 | 5974 |
^ '$Header$' |
12122 | 5975 |
! |
5976 |
||
5977 |
version_CVS |
|
18769 | 5978 |
^ '$Header$' |
1 | 5979 |
! ! |
6858 | 5980 |
|
14677 | 5981 |
|
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
5982 |
Integer initialize! |