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