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