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