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