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