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