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