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