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