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