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