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