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