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