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