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