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