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