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