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