author | Claus Gittinger <cg@exept.de> |
Wed, 04 Aug 1999 16:10:22 +0200 | |
changeset 4522 | 29a6625dc7b1 |
parent 4424 | f0f658ed8995 |
child 4609 | 6a1819c7a743 |
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 |
||
13 |
Number subclass:#Integer |
|
1210 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'DefaultDisplayRadix' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
20 |
Object subclass:#ModuloNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
21 |
instanceVariableNames:'modulus reciprocal shift' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
22 |
classVariableNames:'' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
23 |
poolDictionaries:'' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
24 |
privateIn:Integer |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
25 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
26 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
27 |
!Integer class methodsFor:'documentation'! |
88 | 28 |
|
29 |
copyright |
|
30 |
" |
|
31 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 32 |
All Rights Reserved |
1 | 33 |
|
88 | 34 |
This software is furnished under a license and may be used |
35 |
only in accordance with the terms of that license and with the |
|
36 |
inclusion of the above copyright notice. This software may not |
|
37 |
be provided or otherwise made available to, or used by, any |
|
38 |
other person. No title to or ownership of the software is |
|
39 |
hereby transferred. |
|
40 |
" |
|
41 |
! |
|
1 | 42 |
|
88 | 43 |
documentation |
44 |
" |
|
45 |
abstract superclass for all integer numbers. |
|
46 |
See details in concrete subclasses LargeInteger and SmallInteger. |
|
212 | 47 |
|
1892 | 48 |
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
|
49 |
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
|
50 |
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
|
51 |
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
|
52 |
int op float -> float |
1892 | 53 |
|
1295 | 54 |
[Class variables:] |
212 | 55 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
(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
|
61 |
- I know - I am lazy ;-). Default is 10. |
212 | 62 |
|
1295 | 63 |
|
64 |
[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
|
65 |
Claus Gittinger |
1556 | 66 |
|
67 |
[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
|
68 |
Number |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
69 |
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
|
70 |
Float ShortFloat Fraction FixedPoint |
88 | 71 |
" |
72 |
! ! |
|
1 | 73 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
74 |
!Integer class methodsFor:'instance creation'! |
92 | 75 |
|
3044 | 76 |
byte1:b1 byte2:b2 byte3:b3 byte4:b4 |
77 |
"Squeak compatibility: |
|
78 |
Return an Integer given four value bytes. |
|
79 |
The returned integer is either a Small- or a LargeInteger" |
|
80 |
||
81 |
|t| |
|
82 |
||
83 |
t := b4. |
|
84 |
t := (t bitShift:8) + b3. |
|
85 |
t := (t bitShift:8) + b2. |
|
86 |
^ (t bitShift:8) + b1. |
|
87 |
||
88 |
" |
|
89 |
(Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString |
|
90 |
(Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString |
|
91 |
" |
|
92 |
||
93 |
"Created: 19.10.1997 / 18:08:52 / cg" |
|
94 |
"Modified: 19.10.1997 / 18:09:04 / cg" |
|
95 |
! |
|
96 |
||
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
97 |
fastFromString:aString at:startIndex |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
98 |
"return the next unsigned Integer from the string |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
99 |
as a decimal number, starting at startIndex. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
100 |
No spaces are skipped. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
101 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
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
|
105 |
numbers." |
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 |
if (__isString(aString) && __isSmallInteger(startIndex)) { |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
108 |
char *cp = (char *)(__stringVal(aString)); |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
109 |
int idx = __intVal(startIndex) - 1; |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
110 |
INT val; |
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 ((unsigned)idx < __stringSize(aString)) { |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
113 |
val = atoi(cp + idx); |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
114 |
RETURN (__MKINT(val)); |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
115 |
} |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
116 |
} |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
117 |
%}. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
118 |
self primitiveFailed. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
119 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
120 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
121 |
Integer fastFromString:'12345' at:1 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
122 |
Integer fastFromString:'12345' at:2 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
123 |
Integer fastFromString:'12345' at:3 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
124 |
Integer fastFromString:'12345' at:4 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
125 |
Integer fastFromString:'12345' at:5 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
126 |
Integer fastFromString:'12345' at:6 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
127 |
Integer fastFromString:'12345' at:0 |
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 |
Time millisecondsToRun:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
130 |
100000 timesRepeat:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
131 |
Integer readFrom:'12345' |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
132 |
] |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
133 |
] |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
134 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
135 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
136 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
137 |
Time millisecondsToRun:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
138 |
100000 timesRepeat:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
139 |
Integer fastFromString:'12345' at:1 |
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 |
] |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
142 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
143 |
! |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
144 |
|
92 | 145 |
new:numberOfBytes neg:negative |
146 |
"for ST-80 compatibility: |
|
147 |
Return an empty Integer (uninitialized value) with space for |
|
148 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 149 |
negative specifies if the result should be a negative number. |
150 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 151 |
|
132 | 152 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
159 | 153 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
701 | 154 |
! |
155 |
||
156 |
readFrom:aStringOrStream |
|
157 |
"return the next unsigned Integer from the (character-)stream aStream |
|
158 |
as decimal number. |
|
159 |
||
160 |
NOTICE: |
|
161 |
This behaves different from the default readFrom:, in returning |
|
162 |
0 (instead of raising an error) in case no number can be read. |
|
163 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
164 |
- depending on the upcoming ANSI standard, this may change." |
|
165 |
||
166 |
^ self readFrom:aStringOrStream onError:0 |
|
167 |
||
168 |
" |
|
169 |
Integer readFrom:(ReadStream on:'foobar') |
|
170 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
|
171 |
" |
|
172 |
! |
|
173 |
||
174 |
readFrom:aStringOrStream onError:exceptionBlock |
|
175 |
"return the next Integer from the (character-)stream aStream, |
|
176 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
177 |
Also, all initial whitespace is skipped. |
|
178 |
If the string does not represent a valid integer number, |
|
179 |
return the value of exceptionBlock." |
|
180 |
||
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
181 |
|value| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
182 |
|
1700 | 183 |
ErrorSignal handle:[:ex | |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
184 |
^ exceptionBlock value |
1700 | 185 |
] do:[ |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
186 |
|str nextChar negative| |
701 | 187 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
188 |
str := aStringOrStream readStream. |
701 | 189 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
190 |
nextChar := str skipSeparators. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
191 |
(nextChar == $-) ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
192 |
negative := true. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
193 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
194 |
nextChar := str peekOrNil |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
195 |
] ifFalse:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
196 |
negative := false |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
197 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
198 |
(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
|
199 |
" |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
200 |
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
|
201 |
" |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
202 |
^ exceptionBlock value |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
203 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
204 |
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
|
205 |
nextChar := str peekOrNil. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
206 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
207 |
"-xxr<number> is invalid; should be xxr-<val>" |
1700 | 208 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
209 |
negative ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
210 |
'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
|
211 |
negative := false |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
212 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
213 |
str next. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
214 |
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
|
215 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
216 |
negative ifTrue:[ |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
217 |
value := value negated |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
218 |
]. |
701 | 219 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
220 |
^ value |
701 | 221 |
|
222 |
" |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
223 |
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
|
224 |
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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
Integer readFrom:'foo' |
701 | 230 |
" |
231 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
232 |
"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
|
233 |
"Modified: / 14.4.1998 / 19:17:28 / cg" |
701 | 234 |
! |
235 |
||
236 |
readFrom:aStringOrStream radix:radix |
|
237 |
"return the next unsigned Integer from the (character-)stream aStream |
|
238 |
in radix; (assumes that the initial XXr has already been read). |
|
239 |
No whitespace-skipping is done. |
|
240 |
Returns 0 if no number available. |
|
241 |
||
242 |
NOTICE: |
|
243 |
This behaves different from the default readFrom:, in returning |
|
244 |
0 (instead of raising an error) in case no number can be read. |
|
245 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
246 |
- depending on the upcoming ANSI standard, this may change." |
|
247 |
||
248 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
249 |
! |
|
250 |
||
251 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
252 |
"return the next unsigned Integer from the (character-)stream aStream |
|
253 |
in radix; (assumes that the initial XXr has already been read). |
|
254 |
No whitespace-skipping is done. |
|
255 |
Returns the value of exceptionBlock, if no number is available." |
|
256 |
||
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
257 |
|str nextChar nextChar2 value |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
258 |
r "{ Class: SmallInteger }" |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
259 |
r2 "{ Class: SmallInteger }"| |
701 | 260 |
|
261 |
str := aStringOrStream readStream. |
|
262 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
263 |
nextChar := str peekOrNil. |
701 | 264 |
(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
|
265 |
^ exceptionBlock value |
701 | 266 |
]. |
267 |
||
268 |
value := nextChar digitValue. |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
269 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
270 |
nextChar := str peekOrNil. |
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
271 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
272 |
"/ OLD code |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
273 |
"/ [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
274 |
"/ str next. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
275 |
"/ value := value * radix + nextChar digitValue. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
276 |
"/ nextChar := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
277 |
"/ ]. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
278 |
"/ ^ value. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
279 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
280 |
"/ 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
|
281 |
"/ converted number is large |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
282 |
"/ (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
|
283 |
"/ It should not be slower for smallIntegers. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
284 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
285 |
r := radix. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
286 |
r2 := r * r. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
287 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
288 |
[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
|
289 |
str next. |
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
290 |
nextChar2 := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
291 |
(nextChar2 notNil and:[nextChar2 isDigitRadix:r]) ifTrue:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
292 |
str next. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
293 |
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
|
294 |
nextChar := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
295 |
] ifFalse:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
296 |
^ (value * r) + nextChar digitValue. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
297 |
] |
701 | 298 |
]. |
299 |
^ value |
|
300 |
||
301 |
" |
|
302 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
|
303 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
304 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
305 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
306 |
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
|
307 |
Integer readFrom:'gg' radix:10 onError:0 |
701 | 308 |
" |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
309 |
|
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
310 |
"Modified: / 14.4.1998 / 19:16:46 / cg" |
701 | 311 |
! ! |
312 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
313 |
!Integer class methodsFor:'constants'! |
701 | 314 |
|
315 |
unity |
|
1555 | 316 |
"return the neutral element for multiplication (1)" |
701 | 317 |
|
318 |
^ 1 |
|
1555 | 319 |
|
320 |
"Modified: 18.7.1996 / 12:26:43 / cg" |
|
701 | 321 |
! |
322 |
||
323 |
zero |
|
1555 | 324 |
"return the neutral element for addition (0)" |
701 | 325 |
|
326 |
^ 0 |
|
1555 | 327 |
|
328 |
"Modified: 18.7.1996 / 12:26:38 / cg" |
|
92 | 329 |
! ! |
330 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
331 |
!Integer class methodsFor:'misc'! |
49 | 332 |
|
333 |
displayRadix:aNumber |
|
54 | 334 |
"being tired of always sending #printStringRadix: in the inspectors, |
49 | 335 |
this allows you to change the default print radix for the displayString |
336 |
method." |
|
337 |
||
338 |
DefaultDisplayRadix := aNumber |
|
132 | 339 |
|
340 |
" |
|
341 |
Integer displayRadix:16. 123456 inspect |
|
342 |
Integer displayRadix:10. 123456 inspect |
|
343 |
" |
|
49 | 344 |
! ! |
345 |
||
4376 | 346 |
!Integer methodsFor:'Squeak compatibility'! |
347 |
||
348 |
printStringBase:base |
|
349 |
^ self printStringRadix:base |
|
350 |
! ! |
|
351 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
352 |
!Integer methodsFor:'VisualAge compatibility'! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
353 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
354 |
<< aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
355 |
"left shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
356 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
357 |
^ self bitShift:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
358 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
359 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
360 |
1 << 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
361 |
64 << -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
362 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
363 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
364 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
365 |
>> aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
366 |
"right shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
367 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
368 |
^ self bitShift:(aNumber negated) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
369 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
370 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
371 |
1 >> -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
372 |
64 >> 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
373 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
374 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
375 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
376 |
isBitSet:index |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
377 |
"return true if the index' bit is set; false otherwise. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
378 |
Bits are counted from 1 starting with the least significant." |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
379 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
380 |
^ (self bitAt:index) ~~ 0 |
1 | 381 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
382 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
383 |
5 isBitSet:3 => true |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
384 |
2r0101 isBitSet:2 => false |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
385 |
2r0101 isBitSet:1 => true |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
386 |
2r0101 isBitSet:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
387 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
388 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
389 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
390 |
setBit:index |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
391 |
"return a new number where the specified bit is on. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
392 |
Bits are counted from 1 starting with the least significant." |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
393 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
394 |
^ self bitOr:(1 bitShift:index-1) |
1 | 395 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
396 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
397 |
0 setBit:3 => 4 (2r100) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
398 |
0 setBit:48 => 140737488355328 (2r1000.....000) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
399 |
((0 setBit:99) setBit:100) printStringRadix:2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
400 |
" |
1 | 401 |
! ! |
402 |
||
4423 | 403 |
!Integer methodsFor:'bcd conversion'! |
404 |
||
405 |
decodeFromBCD |
|
406 |
"return a number representing the value of the BCD encoded receiver." |
|
407 |
||
4424 | 408 |
|v rslt multiplier nibble| |
4423 | 409 |
|
410 |
v := self. |
|
411 |
rslt := 0. |
|
412 |
multiplier := 1. |
|
413 |
[v > 0] whileTrue:[ |
|
4424 | 414 |
nibble := v bitAnd:16r0F. |
415 |
nibble > 9 ifTrue:[ |
|
416 |
^ self error:'bad BCD coded value' |
|
417 |
]. |
|
418 |
rslt := rslt + (nibble * multiplier). |
|
4423 | 419 |
multiplier := multiplier * 10. |
420 |
v := v bitShift:-4. |
|
421 |
]. |
|
422 |
^ rslt |
|
423 |
||
424 |
" |
|
425 |
16r55 decodeFromBCD |
|
426 |
16r127 decodeFromBCD |
|
427 |
16r1234567890 decodeFromBCD |
|
4424 | 428 |
|
429 |
16r5A decodeFromBCD |
|
4423 | 430 |
" |
431 |
||
432 |
! |
|
433 |
||
434 |
encodeAsBCD |
|
435 |
"return a BCD encoded number representing the same value as the |
|
436 |
receiver." |
|
437 |
||
438 |
|v rslt shift| |
|
439 |
||
440 |
v := self. |
|
441 |
rslt := shift := 0. |
|
442 |
[v > 0] whileTrue:[ |
|
443 |
rslt := rslt + ((v \\ 10) bitShift:shift). |
|
444 |
shift := shift + 4. |
|
445 |
v := v // 10. |
|
446 |
]. |
|
447 |
^ rslt |
|
448 |
||
449 |
" |
|
450 |
55 encodeAsBCD hexPrintString |
|
451 |
127 encodeAsBCD hexPrintString |
|
452 |
1234567890 encodeAsBCD hexPrintString |
|
453 |
" |
|
454 |
||
455 |
! ! |
|
456 |
||
69 | 457 |
!Integer methodsFor:'bit operators'! |
458 |
||
459 |
bitAnd:anInteger |
|
357 | 460 |
"return the bitwise-and of the receiver and the argument, anInteger. |
461 |
This is a general and slow implementation, walking over the bytes of |
|
462 |
the receiver and the argument." |
|
69 | 463 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
464 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
465 |
result byte| |
69 | 466 |
|
92 | 467 |
n := (anInteger digitLength) min:(self digitLength). |
468 |
result := self class basicNew numberOfDigits:n. |
|
69 | 469 |
|
470 |
1 to:n do:[:index | |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
471 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
472 |
result digitAt:index put:byte. |
69 | 473 |
]. |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
474 |
(byte == 0 or:[n <= SmallInteger maxBytes]) 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
|
475 |
^ result compressed |
92 | 476 |
]. |
477 |
^ result |
|
357 | 478 |
|
479 |
" |
|
480 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
|
481 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
482 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
483 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
484 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
485 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
486 |
"Modified: 5.11.1996 / 14:06:26 / cg" |
69 | 487 |
! |
488 |
||
85 | 489 |
bitAt:index |
92 | 490 |
"return the value of the index's bit (index starts at 1). |
491 |
Notice: the result of bitShift: on negative receivers is not |
|
159 | 492 |
defined in the language standard (since the implementation |
493 |
is free to choose any internal representation for integers)" |
|
85 | 494 |
|
495 |
|i "{Class: SmallInteger}"| |
|
496 |
||
497 |
i := index - 1. |
|
498 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
|
92 | 499 |
|
500 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
501 |
1 bitAt:1 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
502 |
1 bitAt:2 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
503 |
1 bitAt:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
504 |
2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
505 |
2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
506 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
507 |
(1 bitShift:1000) bitAt:1000 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
508 |
(1 bitShift:1000) bitAt:1001 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
509 |
(1 bitShift:1000) bitAt:1002 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
510 |
|
92 | 511 |
(1 bitShift:30) bitAt:30 |
512 |
(1 bitShift:30) bitAt:31 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
513 |
(1 bitShift:30) bitAt:32 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
514 |
(1 bitShift:31) bitAt:31 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
515 |
(1 bitShift:31) bitAt:32 |
92 | 516 |
(1 bitShift:31) bitAt:33 |
517 |
(1 bitShift:32) bitAt:32 |
|
518 |
(1 bitShift:32) bitAt:33 |
|
519 |
(1 bitShift:32) bitAt:34 |
|
520 |
(1 bitShift:64) bitAt:64 |
|
521 |
(1 bitShift:64) bitAt:65 |
|
522 |
(1 bitShift:64) bitAt:66 |
|
523 |
" |
|
85 | 524 |
! |
525 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
526 |
bitClear:anInteger |
3884 | 527 |
"return the bitwise-and of the receiver and the complement of argument, anInteger. |
528 |
This is a general and slow implementation, walking over the bytes of |
|
529 |
the receiver and the argument." |
|
530 |
||
531 |
|n "{ Class: SmallInteger }" |
|
532 |
result byte| |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
533 |
|
3884 | 534 |
n := (anInteger digitLength) max:(self digitLength). |
535 |
result := self class basicNew numberOfDigits:n. |
|
536 |
||
537 |
1 to:n do:[:index | |
|
538 |
byte := (self digitAt:index) bitClear:(anInteger digitAt:index). |
|
539 |
result digitAt:index put:byte. |
|
540 |
]. |
|
541 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
|
542 |
^ result compressed |
|
543 |
]. |
|
544 |
^ result |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
545 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
546 |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
547 |
bitInvert |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
548 |
"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
|
549 |
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
|
550 |
since the digits are stored as absolute value." |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
551 |
|
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
552 |
|n "{ Class: SmallInteger }" |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
553 |
result byte| |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
554 |
|
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
555 |
n := self digitLength. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
556 |
result := self class basicNew numberOfDigits:n. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
557 |
|
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
558 |
1 to:n do:[:index | |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
559 |
byte := self digitAt:index. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
560 |
result digitAt:index put:(byte bitInvert bitAnd:16rFF). |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
561 |
]. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
562 |
(byte == 255 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
563 |
^ result compressed |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
564 |
]. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
565 |
^ result |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
566 |
! |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
567 |
|
4376 | 568 |
bitInvert32 |
569 |
"return a new integer, where the low32 bits are complemented." |
|
570 |
||
571 |
^ self bitXor: 16rFFFFFFFF |
|
572 |
||
573 |
" |
|
574 |
16r80000000 bitInvert32 |
|
575 |
16r7FFFFFFF bitInvert32 |
|
576 |
" |
|
577 |
! |
|
578 |
||
701 | 579 |
bitOr:anInteger |
580 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
581 |
This is a general and slow implementation, walking over the bytes of |
|
582 |
the receiver and the argument." |
|
583 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
584 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
585 |
result byte| |
701 | 586 |
|
587 |
n := (anInteger digitLength) max:(self digitLength). |
|
588 |
result := self class basicNew numberOfDigits:n. |
|
589 |
||
590 |
1 to:n do:[:index | |
|
591 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
|
592 |
result digitAt:index put:byte. |
|
593 |
]. |
|
594 |
"/ no need to normalize - if the operands were correct |
|
595 |
"/ byte == 0 ifTrue:[ |
|
596 |
"/ ^ result normalize |
|
597 |
"/ ]. |
|
598 |
^ result |
|
599 |
! |
|
600 |
||
85 | 601 |
bitShift:shiftCount |
602 |
"return the value of the receiver shifted by shiftCount bits; |
|
603 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 604 |
|
605 |
Notice: the result of bitShift: on negative receivers is not |
|
3638 | 606 |
defined in the language standard (since the implementation |
607 |
is free to choose any internal representation for integers)" |
|
85 | 608 |
|
92 | 609 |
|result |
610 |
prev "{ Class: SmallInteger }" |
|
611 |
next "{ Class: SmallInteger }" |
|
612 |
byte "{ Class: SmallInteger }" |
|
359 | 613 |
byte2 "{ Class: SmallInteger }" |
92 | 614 |
bitShift "{ Class: SmallInteger }" |
615 |
revShift "{ Class: SmallInteger }" |
|
616 |
digitShift "{ Class: SmallInteger }" |
|
617 |
n "{ Class: SmallInteger }" |
|
3044 | 618 |
nn "{ Class: SmallInteger }" |
619 |
nDigits "{ Class: SmallInteger }" | |
|
85 | 620 |
|
621 |
shiftCount > 0 ifTrue:[ |
|
3638 | 622 |
"left shift" |
85 | 623 |
|
3638 | 624 |
digitShift := shiftCount // 8. |
625 |
bitShift := shiftCount \\ 8. |
|
626 |
n := self digitLength. |
|
92 | 627 |
|
3638 | 628 |
" |
629 |
modulo 8 shifts can be done faster ... |
|
630 |
" |
|
631 |
bitShift == 0 ifTrue:[ |
|
632 |
result := self class basicNew numberOfDigits:n + digitShift. |
|
633 |
result sign:self sign. |
|
634 |
result digits replaceFrom:(digitShift + 1) with:self digits. |
|
635 |
" |
|
636 |
no normalize needed, since receiver was already normalized |
|
637 |
" |
|
638 |
^ result |
|
639 |
]. |
|
92 | 640 |
|
3638 | 641 |
" |
642 |
less-than-8 shifts can be done faster ... |
|
643 |
" |
|
644 |
digitShift == 0 ifTrue:[ |
|
4151 | 645 |
nn := n+1. |
646 |
result := self class basicNew numberOfDigits:nn. |
|
3638 | 647 |
result sign:self sign. |
648 |
prev := 0. |
|
4151 | 649 |
1 to:n do:[:index | |
3638 | 650 |
byte := self digitAt:index. |
651 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
652 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
653 |
prev := byte bitShift:-8. |
|
654 |
]. |
|
4151 | 655 |
result digitAt:nn put:prev. |
3638 | 656 |
" |
657 |
might have stored a 0-byte ... |
|
658 |
" |
|
659 |
prev == 0 ifTrue:[ |
|
660 |
^ result compressed |
|
661 |
]. |
|
662 |
^ result. |
|
663 |
]. |
|
92 | 664 |
|
3638 | 665 |
" |
666 |
slow case ... |
|
667 |
" |
|
668 |
n := n + digitShift + 1. |
|
669 |
result := self class basicNew numberOfDigits:n. |
|
670 |
result sign:self sign. |
|
671 |
byte := self digitAt:1. |
|
672 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
|
673 |
result digitAt:(digitShift + 1) put:byte. |
|
674 |
revShift := -8 + bitShift. |
|
675 |
nDigits := self digitLength. |
|
676 |
2 to:nDigits do:[:index | |
|
677 |
byte := self digitAt:index. |
|
678 |
byte2 := self digitAt:index-1. |
|
679 |
byte := byte bitShift:bitShift. |
|
680 |
byte2 := byte2 bitShift:revShift. |
|
681 |
byte := (byte bitOr:byte2) bitAnd:16rFF. |
|
682 |
result digitAt:(index + digitShift) put:byte. |
|
683 |
]. |
|
684 |
byte2 := self digitAt:nDigits. |
|
685 |
byte2 := (byte2 bitShift:revShift) bitAnd:16rFF. |
|
686 |
result digitAt:(nDigits + digitShift + 1) put:byte2. |
|
687 |
" |
|
688 |
might have stored a 0-byte ... |
|
689 |
" |
|
690 |
byte2 == 0 ifTrue:[ |
|
691 |
^ result compressed |
|
692 |
]. |
|
693 |
^ result |
|
92 | 694 |
]. |
695 |
||
696 |
shiftCount < 0 ifTrue:[ |
|
3638 | 697 |
"right shift" |
85 | 698 |
|
3638 | 699 |
digitShift := shiftCount negated // 8. |
700 |
bitShift := shiftCount negated \\ 8. |
|
701 |
n := self digitLength. |
|
92 | 702 |
|
3638 | 703 |
digitShift >= n ifTrue:[ |
704 |
^ 0 |
|
705 |
]. |
|
92 | 706 |
|
3638 | 707 |
" |
708 |
modulo 8 shifts can be done faster ... |
|
709 |
" |
|
710 |
bitShift == 0 ifTrue:[ |
|
711 |
n := n-digitShift. |
|
712 |
result := self class basicNew numberOfDigits:n. |
|
713 |
result sign:self sign. |
|
714 |
result digits replaceFrom:1 to:n with:self digits startingAt:(digitShift + 1) . |
|
715 |
n <= SmallInteger maxBytes ifTrue:[ |
|
716 |
^ result compressed |
|
717 |
]. |
|
718 |
^ result |
|
719 |
]. |
|
85 | 720 |
|
3638 | 721 |
" |
722 |
less-than-8 shifts can be done faster ... |
|
723 |
" |
|
724 |
digitShift == 0 ifTrue:[ |
|
725 |
result := self class basicNew numberOfDigits:n. |
|
726 |
result sign:self sign. |
|
727 |
prev := 0. |
|
728 |
bitShift := bitShift negated. |
|
729 |
revShift := 8 + bitShift. |
|
730 |
n to:1 by:-1 do:[:index | |
|
731 |
byte := self digitAt:index. |
|
732 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
733 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
734 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
735 |
prev := next. |
|
736 |
]. |
|
737 |
(n <= 5) ifTrue:[ |
|
738 |
^ result compressed |
|
739 |
]. |
|
740 |
^ result |
|
741 |
]. |
|
92 | 742 |
|
3638 | 743 |
" |
744 |
slow case ... |
|
745 |
" |
|
746 |
nn := n-digitShift. |
|
747 |
result := self class basicNew numberOfDigits:nn. |
|
748 |
result sign:self sign. |
|
85 | 749 |
|
3638 | 750 |
prev := 0. |
751 |
bitShift := bitShift negated. |
|
752 |
revShift := 8 + bitShift. |
|
4151 | 753 |
nn := digitShift + 1. |
754 |
n to:nn by:-1 do:[:index | |
|
3638 | 755 |
byte := self digitAt:index. |
756 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
757 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
758 |
result digitAt:(index - digitShift) put:byte. |
|
759 |
prev := next. |
|
760 |
]. |
|
761 |
"the last stored byte ..." |
|
762 |
^ result compressed |
|
85 | 763 |
]. |
92 | 764 |
|
765 |
^ self "no shift" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
766 |
|
3638 | 767 |
"Modified: / 8.7.1998 / 12:45:24 / cg" |
4151 | 768 |
"Modified: / 5.5.1999 / 16:05:05 / stefan" |
701 | 769 |
! |
1 | 770 |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
771 |
bitTest:anInteger |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
772 |
"return true, if the bitwise-and of the receiver and the argument, anInteger |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
773 |
is non-0, false otherwise. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
774 |
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
|
775 |
the receiver and the argument." |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
776 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
777 |
|n "{ Class: SmallInteger }" |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
778 |
byte| |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
779 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
780 |
n := (anInteger digitLength) min:(self digitLength). |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
781 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
782 |
1 to:n do:[:index | |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
783 |
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
|
784 |
byte ~~ 0 ifTrue:[^ true]. |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
785 |
]. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
786 |
^ false |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
787 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
788 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
789 |
16r112233445566778899 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
790 |
16r112233445566778800 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
791 |
16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
792 |
16r112233445566778899 bitTest:16rFF0000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
793 |
16r112233445566778899 bitTest:16r00000000000000FFFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
794 |
16r1234567800000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
795 |
16r8765432100000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
796 |
16r12345678 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
797 |
16r87654321 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
798 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
799 |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
800 |
"Modified: / 6.6.1999 / 15:10:33 / cg" |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
801 |
! |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
802 |
|
701 | 803 |
bitXor:anInteger |
804 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
805 |
This is a general and slow implementation, walking over the bytes of |
|
806 |
the receiver and the argument." |
|
807 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
808 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
809 |
result byte| |
701 | 810 |
|
811 |
n := (anInteger digitLength) max:(self digitLength). |
|
812 |
result := self class basicNew numberOfDigits:n. |
|
1 | 813 |
|
701 | 814 |
1 to:n do:[:index | |
3993
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
815 |
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
|
816 |
result digitAt:index put:byte. |
701 | 817 |
]. |
3993
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
818 |
(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
|
819 |
^ result compressed |
701 | 820 |
]. |
821 |
^ result |
|
1 | 822 |
|
701 | 823 |
" |
824 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
|
825 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
|
826 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
827 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
|
828 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
829 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
830 |
"Modified: 5.11.1996 / 14:06:40 / cg" |
1 | 831 |
! |
832 |
||
3683 | 833 |
clearBit:index |
834 |
"return a new number where the specified bit is off. |
|
835 |
Bits are counted from 1 starting with the least significant." |
|
836 |
||
837 |
|n "{ Class: SmallInteger }" |
|
838 |
byteIndex "{ Class: SmallInteger }" |
|
839 |
bitIndex "{ Class: SmallInteger }" |
|
840 |
result byte| |
|
841 |
||
842 |
byteIndex := ((index - 1) // 8) + 1. |
|
843 |
n := self digitLength. |
|
844 |
byteIndex > n ifTrue:[ |
|
845 |
^ self |
|
846 |
]. |
|
847 |
||
848 |
result := self copy. |
|
849 |
bitIndex := ((index - 1) \\ 8) + 1. |
|
850 |
byte := (result digitAt:byteIndex) clearBit:bitIndex. |
|
851 |
result digitAt:byteIndex put:byte. |
|
852 |
(byte == 0 or:[n == byteIndex and:[n <= SmallInteger maxBytes]]) ifTrue:[ |
|
853 |
^ result compressed |
|
854 |
]. |
|
855 |
^ result |
|
856 |
||
857 |
" |
|
858 |
3111111111 clearBit:1 |
|
859 |
" |
|
860 |
||
861 |
"Modified: / 28.7.1998 / 18:35:50 / cg" |
|
862 |
! |
|
863 |
||
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
864 |
even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
865 |
"return true if the receiver is even" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
866 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
867 |
^ self bitTest:1 |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
868 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
869 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
870 |
16r112233445566778899 even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
871 |
16r112233445566778800 even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
872 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
873 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
874 |
"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
|
875 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
876 |
|
701 | 877 |
highBit |
878 |
"return the bitIndex of the highest bit set. The returned bitIndex |
|
879 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
880 |
||
881 |
|byteNr highByte| |
|
882 |
||
883 |
byteNr := self digitLength. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
884 |
byteNr == 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
885 |
^ -1 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
886 |
]. |
701 | 887 |
highByte := self digitAt:byteNr. |
888 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 889 |
|
701 | 890 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
891 |
0 highBit |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
892 |
-1 highBit |
701 | 893 |
(1 bitShift:1) highBit |
894 |
(1 bitShift:30) highBit |
|
895 |
(1 bitShift:31) highBit |
|
896 |
(1 bitShift:32) highBit |
|
897 |
(1 bitShift:33) highBit |
|
898 |
(1 bitShift:64) highBit |
|
899 |
(1 bitShift:1000) highBit |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
900 |
(1 bitShift:1000) negated highBit |
701 | 901 |
((1 bitShift:64)-1) highBit |
902 |
" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
903 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
904 |
"Modified: / 3.5.1999 / 09:20:57 / stefan" |
2429 | 905 |
! |
906 |
||
907 |
lowBit |
|
908 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
909 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
910 |
||
911 |
|maxBytes "{ Class: SmallInteger }" |
|
912 |
bitIndex "{ Class: SmallInteger }" |
|
913 |
byte| |
|
914 |
||
915 |
maxBytes := self digitLength. |
|
916 |
bitIndex := 0. |
|
917 |
1 to:maxBytes do:[:byteIndex | |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
918 |
byte := self digitAt:byteIndex. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
919 |
byte ~~ 0 ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
920 |
^ bitIndex + byte lowBit |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
921 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
922 |
bitIndex := bitIndex + 8 |
2429 | 923 |
]. |
924 |
^ -1 |
|
925 |
||
926 |
" |
|
927 |
0 lowBit |
|
928 |
1 lowBit |
|
929 |
(1 bitShift:1) lowBit |
|
930 |
(1 bitShift:1) highBit |
|
931 |
(1 bitShift:30) lowBit |
|
932 |
(1 bitShift:30) highBit |
|
933 |
(1 bitShift:31) lowBit |
|
934 |
(1 bitShift:31) highBit |
|
935 |
(1 bitShift:32) lowBit |
|
936 |
(1 bitShift:32) highBit |
|
937 |
(1 bitShift:33) lowBit |
|
938 |
(1 bitShift:33) highBit |
|
939 |
(1 bitShift:64) lowBit |
|
940 |
(1 bitShift:64) highBit |
|
941 |
(1 bitShift:1000) lowBit |
|
942 |
(1 bitShift:1000) highBit |
|
943 |
((1 bitShift:64)-1) lowBit |
|
944 |
((1 bitShift:64)-1) highBit |
|
945 |
" |
|
946 |
||
947 |
"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
|
948 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
949 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
950 |
odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
951 |
"return true if the receiver is odd" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
952 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
953 |
^ (self bitTest:1) not |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
954 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
955 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
956 |
16r112233445566778899 odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
957 |
16r112233445566778800 odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
958 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
959 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
960 |
"Created: / 6.6.1999 / 15:00:55 / cg" |
701 | 961 |
! ! |
962 |
||
3890 | 963 |
!Integer methodsFor:'byte access'! |
964 |
||
965 |
digitByteLength |
|
966 |
"return the number bytes required for a 2's complement |
|
967 |
binary representation of this Integer. |
|
968 |
For positive receivers, thats the same as the digitLength." |
|
969 |
||
970 |
" |
|
971 |
check if there is a 0-byte ... |
|
972 |
this allows to ask unnormalized LargeIntegers |
|
973 |
for their digitLength |
|
974 |
" |
|
975 |
|l "{ Class: SmallInteger }" | |
|
976 |
||
977 |
self >= 0 ifTrue:[^ self digitLength]. |
|
978 |
||
979 |
l := self negated digitLength. |
|
980 |
(self digitByteAt:l) == 16rFF ifTrue:[ |
|
981 |
^ (l - 1) max:1 |
|
982 |
]. |
|
983 |
^ l |
|
984 |
||
985 |
" |
|
986 |
-129 digitByteLength |
|
987 |
" |
|
988 |
||
989 |
"Created: / 25.10.1998 / 14:58:09 / cg" |
|
990 |
"Modified: / 25.10.1998 / 19:20:33 / cg" |
|
991 |
! ! |
|
992 |
||
701 | 993 |
!Integer methodsFor:'coercing and converting'! |
994 |
||
1888 | 995 |
asFixedPoint |
996 |
"return the receiver as a fixedPoint number" |
|
997 |
||
998 |
^ FixedPoint numerator:self denominator:1 scale:1 |
|
999 |
||
1000 |
" |
|
1001 |
100 asFixedPoint |
|
1002 |
100 asFixedPoint + 0.1 asFixedPoint |
|
1003 |
" |
|
1004 |
||
1005 |
"Modified: 5.11.1996 / 15:13:17 / cg" |
|
1006 |
! |
|
1007 |
||
2141 | 1008 |
asFixedPoint:scale |
1009 |
"return the receiver as fixedPoint number, with the given number |
|
1010 |
of post-decimal-point digits." |
|
1011 |
||
1012 |
^ FixedPoint numerator:self denominator:1 scale:scale |
|
1013 |
||
1014 |
" |
|
1015 |
100 asFixedPoint:2 |
|
1016 |
100 asFixedPoint + (0.1 asFixedPoint:2) |
|
1017 |
" |
|
1018 |
||
1019 |
"Modified: 10.1.1997 / 20:00:08 / cg" |
|
1020 |
! |
|
1021 |
||
701 | 1022 |
asFraction |
1023 |
"return a Fraction with same value as receiver" |
|
1024 |
||
2794
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
1025 |
^ 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
|
1026 |
|
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
1027 |
"Modified: 28.7.1997 / 19:26:06 / cg" |
1 | 1028 |
! |
1029 |
||
701 | 1030 |
asInteger |
1 | 1031 |
"return the receiver truncated towards zero - |
1032 |
for integers this is self" |
|
1033 |
||
1034 |
^ self |
|
1337 | 1035 |
! |
1036 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1037 |
asModuloNumber |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1038 |
"return a precomputed modulo number" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1039 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1040 |
^ ModuloNumber mod:self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1041 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1042 |
"Created: / 3.5.1999 / 14:48:03 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1043 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1044 |
|
1337 | 1045 |
signExtendedByteValue |
1046 |
"return a smallInteger from sign-extending the 8'th bit. |
|
1047 |
May be useful for communication interfaces" |
|
1048 |
||
1049 |
^ (self bitAnd:16rFF) signExtendedByteValue |
|
1050 |
||
1051 |
"Created: 7.5.1996 / 09:31:52 / cg" |
|
1052 |
! |
|
1053 |
||
1054 |
signExtendedShortValue |
|
1055 |
"return a smallInteger from sign-extending the 16'th bit. |
|
1056 |
May be useful for communication interfaces" |
|
1057 |
||
1058 |
^ (self bitAnd:16rFFFF) signExtendedShortValue |
|
1059 |
||
1060 |
"Modified: 7.5.1996 / 09:31:57 / cg" |
|
1 | 1061 |
! ! |
1062 |
||
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1063 |
!Integer methodsFor:'comparing'! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1064 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1065 |
hash |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1066 |
"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
|
1067 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1068 |
^ self bitAnd:16r3FFFFFFF. |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1069 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1070 |
" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1071 |
-20000000000000 hash |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1072 |
20000000000000 hash |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1073 |
" |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1074 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1075 |
"Created: / 14.11.1996 / 12:12:27 / cg" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1076 |
"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
|
1077 |
! ! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1078 |
|
701 | 1079 |
!Integer methodsFor:'double dispatching'! |
1080 |
||
1081 |
differenceFromFraction:aFraction |
|
1082 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 1083 |
|
701 | 1084 |
|d| |
1 | 1085 |
|
701 | 1086 |
d := aFraction denominator. |
1087 |
^ (Fraction numerator:(aFraction numerator - (self * d)) |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1088 |
denominator:d) |
2793 | 1089 |
|
1090 |
"Modified: 28.7.1997 / 19:08:30 / cg" |
|
1 | 1091 |
! |
1092 |
||
701 | 1093 |
productFromFraction:aFraction |
1094 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 1095 |
|
701 | 1096 |
^ (Fraction numerator:(self * aFraction numerator) |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1097 |
denominator:aFraction denominator) |
2793 | 1098 |
|
1099 |
"Modified: 28.7.1997 / 19:08:27 / cg" |
|
1 | 1100 |
! |
1101 |
||
701 | 1102 |
quotientFromFraction:aFraction |
1103 |
"sent when a fraction does not know how to divide the receiver, an integer" |
|
1104 |
||
2793 | 1105 |
^ Fraction |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1106 |
numerator:aFraction numerator |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1107 |
denominator:(self * aFraction denominator) |
2793 | 1108 |
|
1109 |
"Modified: 28.7.1997 / 19:08:23 / cg" |
|
701 | 1110 |
! |
1 | 1111 |
|
701 | 1112 |
sumFromFraction:aFraction |
1113 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
1114 |
||
1115 |
|d| |
|
1116 |
||
1117 |
d := aFraction denominator. |
|
2793 | 1118 |
^ Fraction numerator:(aFraction numerator + (self * d)) |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1119 |
denominator:d |
2793 | 1120 |
|
1121 |
"Modified: 28.7.1997 / 19:08:11 / cg" |
|
1 | 1122 |
! ! |
1123 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1124 |
!Integer methodsFor:'helpers'! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1125 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1126 |
gcd_helper:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1127 |
"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
|
1128 |
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
|
1129 |
larger than the arg." |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1130 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1131 |
| selfAbs argAbs selfLowBit argLowBit shift t | |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1132 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1133 |
selfAbs := self. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1134 |
argAbs := anInteger. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1135 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1136 |
selfLowBit := selfAbs lowBit - 1. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1137 |
argLowBit := argAbs lowBit - 1. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1138 |
shift := selfLowBit min:argLowBit. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1139 |
argAbs := argAbs bitShift:(argLowBit negated). |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1140 |
[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
|
1141 |
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
|
1142 |
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
|
1143 |
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
|
1144 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1145 |
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
|
1146 |
selfLowBit := selfAbs lowBit - 1. |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1147 |
]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1148 |
^ argAbs bitShift:shift |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1149 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1150 |
"Created: 1.3.1997 / 16:38:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1151 |
"Modified: 1.3.1997 / 16:45:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1152 |
! ! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1153 |
|
1 | 1154 |
!Integer methodsFor:'misc math'! |
1155 |
||
701 | 1156 |
acker:n |
1560 | 1157 |
"return the value of acker(self, n). |
1158 |
;-) Do not try with receivers > 3" |
|
701 | 1159 |
|
1160 |
(self == 0) ifTrue:[^ n + 1]. |
|
1161 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
1162 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
1163 |
||
1164 |
" |
|
1165 |
3 acker:2 |
|
1166 |
" |
|
1560 | 1167 |
|
1168 |
"Modified: 18.7.1996 / 13:08:16 / cg" |
|
701 | 1169 |
! |
1170 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1171 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1172 |
"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
|
1173 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1174 |
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
|
1175 |
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
|
1176 |
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
|
1177 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1178 |
^ 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
|
1179 |
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
|
1180 |
with:(self \\ aNumber) |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1181 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1182 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1183 |
10 divMod:3 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1184 |
100 factorial divMod:103 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1185 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1186 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1187 |
"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
|
1188 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1189 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1190 |
extendedEuclid:tb |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1191 |
"return the solution of 'ax + by = gcd(a,b)'. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1192 |
An array conatining x, y and gcd(a,b) is returned." |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1193 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1194 |
|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
|
1195 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1196 |
self > tb ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1197 |
a := self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1198 |
b := tb. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1199 |
swap := false. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1200 |
] ifFalse:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1201 |
a := tb. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1202 |
b := self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1203 |
swap := true. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1204 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1205 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1206 |
shift := ((a lowBit) min:(b lowBit))-1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1207 |
shift > 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1208 |
tmp := shift negated. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1209 |
a := a bitShift:tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1210 |
b := b bitShift:tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1211 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1212 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1213 |
gcd := a copy. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1214 |
gcd1 := b copy. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1215 |
u := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1216 |
u1 := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1217 |
v := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1218 |
v1 := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1219 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1220 |
[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1221 |
t := gcd1 divMod:gcd. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1222 |
gcd1 := gcd. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1223 |
gcd := t at:2. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1224 |
t := t at:1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1225 |
tmp := v. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1226 |
v := v1 - (v * t). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1227 |
v1 := tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1228 |
tmp := u. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1229 |
u := u1 - (u * t). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1230 |
u1 := tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1231 |
] doWhile:[gcd > 0]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1232 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1233 |
shift > 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1234 |
gcd1 := gcd1 bitShift:shift. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1235 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1236 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1237 |
swap ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1238 |
^ Array with:v1 with:u1 with:gcd1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1239 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1240 |
^ Array with:u1 with:v1 with:gcd1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1241 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1242 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1243 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1244 |
14 extendedEuclid:5 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1245 |
14 extendedEuclid:2 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1246 |
25 extendedEuclid:15 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1247 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1248 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1249 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1250 |
"Modified: / 3.5.1999 / 07:30:02 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1251 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1252 |
|
1 | 1253 |
factorial |
1560 | 1254 |
"return fac(self) (i.e. 1*2*3...*self) using an iterative algorithm. |
1255 |
This is slightly faster than the recursive algorithm, and does not |
|
1256 |
suffer from stack overflow problems (with big receivers)" |
|
1257 |
||
2745 | 1258 |
|p i| |
1560 | 1259 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1260 |
(self < 2) 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
|
1261 |
self < 0 ifTrue:[ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1262 |
"/ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1263 |
"/ requested factorial of a negative number |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1264 |
"/ |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1265 |
^ self class |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1266 |
raise:#domainErrorSignal |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1267 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1268 |
selector:#factorial |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1269 |
errorString:'factorial of negative number' |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1270 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1271 |
^ 1 |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1272 |
]. |
1560 | 1273 |
p := 2. |
2745 | 1274 |
i := 3. |
1275 |
[i <= self] whileTrue:[ |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1276 |
p := p * i. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1277 |
i := i + 1. |
1560 | 1278 |
]. |
1279 |
^ p |
|
1280 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1281 |
" |
1560 | 1282 |
10 factorial |
1283 |
1000 factorial |
|
1284 |
10000 factorial |
|
1285 |
10000 factorialR |
|
1286 |
||
1287 |
Time millisecondsToRun:[2000 factorial] |
|
1288 |
Time millisecondsToRun:[2000 factorialR] |
|
2745 | 1289 |
-1 factorial |
1560 | 1290 |
" |
1291 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1292 |
"Modified: 29.10.1996 / 20:58:43 / cg" |
1560 | 1293 |
! |
1294 |
||
1295 |
factorialR |
|
3908 | 1296 |
"return fac(self) (i.e. 1*2*3...*self) using a recursive algorithm. |
1297 |
This is included to demonstration purposes - if you really need |
|
1298 |
factorial numbers, use the iterative #factorial, which is slightly |
|
1299 |
faster and does not suffer from stack overflow problems (with big receivers)." |
|
1 | 1300 |
|
356 | 1301 |
(self >= 2) ifTrue:[ |
3908 | 1302 |
^ self * (self - 1) factorialR |
1 | 1303 |
]. |
356 | 1304 |
^ 1 |
212 | 1305 |
|
1306 |
" |
|
1560 | 1307 |
10 factorialR |
1308 |
1000 factorialR |
|
4158 | 1309 |
Time millisecondsToRun:[10000 factorial] |
212 | 1310 |
" |
1560 | 1311 |
|
3908 | 1312 |
"Created: / 18.7.1996 / 12:48:36 / cg" |
4158 | 1313 |
"Modified: / 8.5.1999 / 18:40:13 / cg" |
1 | 1314 |
! |
1315 |
||
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1316 |
fib |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1317 |
"compute the fibionacci number for the receiver. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1318 |
(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
|
1319 |
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
|
1320 |
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
|
1321 |
... 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
|
1322 |
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
|
1323 |
|
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1324 |
|a1 a2 an| |
701 | 1325 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1326 |
a1 := 1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1327 |
a2 := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1328 |
an := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1329 |
self timesRepeat:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1330 |
an := a1 + a2. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1331 |
a2 := a1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1332 |
a1 := an |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1333 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1334 |
^ an |
701 | 1335 |
|
1336 |
" |
|
1422 | 1337 |
Transcript showCR:(Time millisecondsToRun:[30 fib]) |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1338 |
Transcript showCR:(Time millisecondsToRun:[30 recursiveFib]) |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1339 |
Transcript showCR:(Time millisecondsToRun:[60 fib]) |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1340 |
Transcript showCR:(Time millisecondsToRun:[200 fib]) |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1341 |
Transcript showCR:(Time millisecondsToRun:[1500 fib]) |
701 | 1342 |
" |
1343 |
! |
|
1344 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1345 |
gcd:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1346 |
"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
|
1347 |
Euclids & Knuths algorithm." |
1 | 1348 |
|
3060 | 1349 |
|selfAbs argAbs t| |
1 | 1350 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1351 |
selfAbs := self abs. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1352 |
argAbs := anInteger abs. |
1888 | 1353 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1354 |
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
|
1355 |
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
|
1356 |
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
|
1357 |
argAbs := t. |
1 | 1358 |
]. |
52 | 1359 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1360 |
argAbs = 0 ifTrue: [^ selfAbs]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1361 |
selfAbs := selfAbs \\ argAbs. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1362 |
selfAbs = 0 ifTrue:[^ argAbs]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1363 |
^ argAbs gcd_helper:selfAbs |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1364 |
|
3060 | 1365 |
" |
1366 |
3141589999999999 gcd:1000000000000000 |
|
1367 |
" |
|
1368 |
||
1369 |
"Modified: / 25.10.1997 / 16:08:45 / cg" |
|
1 | 1370 |
! |
1371 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1372 |
integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1373 |
"return an integer representing 1/self * 2**n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1374 |
Where an integer is one bit longer than self." |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1375 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1376 |
|b rem result| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1377 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1378 |
b := self highBit. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1379 |
rem := 1 bitShift:b. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1380 |
result := 0 asLargeInteger numberOfDigits:(b // 8)+1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1381 |
b := b+1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1382 |
[b > 0] whileTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1383 |
rem >= self ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1384 |
rem := rem -= self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1385 |
result digits bitSetAt:b. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1386 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1387 |
rem := rem mul2. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1388 |
b := b - 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1389 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1390 |
^ result compressed. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1391 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1392 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1393 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1394 |
333 integerReciprocal (2 raisedTo:18) // 333 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1395 |
393 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1396 |
8 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1397 |
15 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1398 |
15112233445566 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1399 |
10239552311579 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1400 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1401 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1402 |
"Modified: / 3.5.1999 / 14:27:18 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1403 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1404 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1405 |
inverseMod:n |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1406 |
"find the modular inverse for myself to n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1407 |
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
|
1408 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1409 |
|e ret| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1410 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1411 |
e := self extendedEuclid:n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1412 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1413 |
(e at:3) == 1 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1414 |
ret := e at:1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1415 |
ret negative ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1416 |
^ ret + n |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1417 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1418 |
^ ret. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1419 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1420 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1421 |
^ 0 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1422 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1423 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1424 |
14 inverseMod:5 -> 4 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1425 |
5 inverseMod:14 -> 3 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1426 |
14 inverseMod:11 -> 4 (4 * 14) \\ 11 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1427 |
11 inverseMod:14 -> 9 (9 * 11) \\ 14 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1428 |
79 inverseMod:3220 -> 1019 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1429 |
3220 inverseMod:79 -> 54 (54 * 3220) \\ 79 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1430 |
1234567891 inverseMod:1111111111119 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1431 |
-> 148726663534 (148726663534*1234567891) \\ 1111111111119 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1432 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1433 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1434 |
14 extendedEuclid:11 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1435 |
5 extendedEuclid:14 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1436 |
14 extendedEuclid:2 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1437 |
3220 extendedEuclid:79 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1438 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1439 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1440 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1441 |
"Modified: / 30.4.1999 / 19:08:01 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1442 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1443 |
|
1 | 1444 |
lcm:anInteger |
52 | 1445 |
"return the least common multiple (using gcd:)" |
1446 |
||
1447 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
1448 |
||
92 | 1449 |
" |
1450 |
65 lcm:15 |
|
1451 |
3 lcm:15 |
|
1452 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1453 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1454 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1455 |
raisedTo:exp mod:mod |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1456 |
"return the receiver raised to exp (an Integer)" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1457 |
|
4151 | 1458 |
|result m t bits "{Class: SmallInteger}"| |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1459 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1460 |
"use the addition chaining algorithm" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1461 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1462 |
exp negative ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1463 |
^ self class |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1464 |
raise:#domainErrorSignal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1465 |
receiver:self |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1466 |
selector:#raisedTo:mod: |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1467 |
errorString:'modulo arithmethic with negative exponent' |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1468 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1469 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1470 |
m := mod asModuloNumber. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1471 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1472 |
result := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1473 |
t := self \\ mod. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1474 |
bits := exp highBit. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1475 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1476 |
1 to:bits do:[:i| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1477 |
(exp bitAt:i) == 1 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1478 |
result := m modulusOf:(result * t). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1479 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1480 |
t := m modulusOf:(t * t). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1481 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1482 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1483 |
^ result |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1484 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1485 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1486 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1487 |
2 raisedTo:2 mod:3 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1488 |
20000000000000 raisedTo:200 mod:190 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1489 |
(20000000000000 raisedTo:200) \\ 190 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1490 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1491 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1492 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
4151 | 1493 |
"Modified: / 5.5.1999 / 11:18:20 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1494 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1495 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1496 |
raisedToCrtModP:p q:q ep:ep eq:eq u:u |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1497 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1498 |
This is a faster modexp for moduli with a known factorisation into two |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1499 |
relatively prime factors p and q, and an input relatively prime to the |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1500 |
modulus, the Chinese Remainder Theorem to do the computation mod p and |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1501 |
mod q, and then combine the results. This relies on a number of |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1502 |
precomputed values, but does not actually require the modulus n or the |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1503 |
exponent e. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1504 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1505 |
expout = expin ^ e mod (p*q). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1506 |
We form this by evaluating |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1507 |
p2 = (expin ^ e) mod p and |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1508 |
q2 = (expin ^ e) mod q |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1509 |
and then combining the two by the CRT. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1510 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1511 |
Two optimisations of this are possible. First, we can reduce expin |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1512 |
modulo p and q before starting. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1513 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1514 |
Second, since we know the factorisation of p and q (trivially derived |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1515 |
from the factorisation of n = p*q), and expin is relatively prime to |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1516 |
both p and q, we can use Euler's theorem, expin^phi(m) = 1 (mod m), |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1517 |
to throw away multiples of phi(p) or phi(q) in e. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1518 |
Letting ep = e mod phi(p) and |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1519 |
eq = e mod phi(q) |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1520 |
then combining these two speedups, we only need to evaluate |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1521 |
p2 = ((expin mod p) ^ ep) mod p and |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1522 |
q2 = ((expin mod q) ^ eq) mod q. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1523 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1524 |
Now we need to apply the CRT. Starting with |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1525 |
expout = p2 (mod p) and |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1526 |
expout = q2 (mod q) |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1527 |
we can say that expout = p2 + p * k, and if we assume that 0 <= p2 < p, |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1528 |
then 0 <= expout < p*q for some 0 <= k < q. Since we want expout = q2 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1529 |
(mod q), then p*k = q2-p2 (mod q). Since p and q are relatively prime, |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1530 |
p has a multiplicative inverse u mod q. In other words, u = 1/p (mod q). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1531 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1532 |
Multiplying by u on both sides gives k = u*(q2-p2) (mod q). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1533 |
Since we want 0 <= k < q, we can thus find k as |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1534 |
k = (u * (q2-p2)) mod q. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1535 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1536 |
Once we have k, evaluating p2 + p * k is easy, and |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1537 |
that gives us the result |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1538 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1539 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1540 |
|result t| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1541 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1542 |
t := self \\ p. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1543 |
result := t raisedTo:ep mod:p. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1544 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1545 |
t := self \\ q. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1546 |
t := t raisedTo:eq mod:q. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1547 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1548 |
"now p2 is in result, q2 in t" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1549 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1550 |
t := t -= result. |
4141 | 1551 |
t := t \\ q. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1552 |
t := t *= u. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1553 |
t := t \\ q. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1554 |
t := t *= p. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1555 |
result := result += t. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1556 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1557 |
^ result. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1558 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1559 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1560 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1561 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1562 |
2 raisedTo:2 mod:3 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1563 |
20000000000000 raisedTo:200 mod:190 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1564 |
(20000000000000 raisedTo:200) \\ 190 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1565 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1566 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1567 |
"Created: / 30.4.1999 / 15:53:15 / stefan" |
4151 | 1568 |
"Modified: / 5.5.1999 / 11:01:15 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1569 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1570 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1571 |
raisedToInteger:exp |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1572 |
"return the receiver raised to exp" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1573 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1574 |
|result e t bits "{SmallInteger}"| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1575 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1576 |
"use the addition chaining algorithm" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1577 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1578 |
result := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1579 |
t := self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1580 |
exp < 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1581 |
e := exp negated. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1582 |
] ifFalse:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1583 |
e := exp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1584 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1585 |
bits := e highBit. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1586 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1587 |
1 to:bits do:[:i| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1588 |
(e bitAt:i) == 1 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1589 |
result := result * t. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1590 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1591 |
t := t * t. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1592 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1593 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1594 |
(exp < 0) ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1595 |
^ 1 / result |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1596 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1597 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1598 |
^ result |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1599 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1600 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1601 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1602 |
(2 raisedTo:216) asFloat |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1603 |
2 raisedTo:10 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1604 |
-2 raisedTo:10 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1605 |
2 raisedTo:9 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1606 |
-2 raisedTo:9 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1607 |
10 raisedTo:-10 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1608 |
2 raisedTo:0 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1609 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1610 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1611 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1612 |
"Modified: / 27.4.1999 / 16:16:11 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1613 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1614 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1615 |
recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1616 |
"compute the fibionacci number for the receiver. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1617 |
WARNING: |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1618 |
Dont use this method if you need fibionacci numbers - |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1619 |
this method is for benchmarking purposes only. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1620 |
(use #fib instead and dont ever try 60 recursiveFib ...)" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1621 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1622 |
(self > 1) ifTrue:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1623 |
^ (self - 1) recursiveFib + (self - 2) recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1624 |
]. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1625 |
^ 1 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1626 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1627 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1628 |
30 fib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1629 |
30 recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1630 |
Transcript showCR:(Time millisecondsToRun:[30 recursiveFib]) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1631 |
Transcript showCR:(Time millisecondsToRun:[30 fib]) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1632 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1633 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1634 |
"Modified: 18.7.1996 / 12:47:19 / cg" |
1 | 1635 |
! ! |
1636 |
||
1637 |
!Integer methodsFor:'printing & storing'! |
|
1638 |
||
49 | 1639 |
displayString |
1640 |
"return a string for displaying in a view (as in inspector). |
|
1641 |
The output radix is usually 10, but can be changed by setting |
|
1642 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
1643 |
||
54 | 1644 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
159 | 1645 |
^ self printString |
54 | 1646 |
]. |
49 | 1647 |
^ self radixPrintStringRadix:DefaultDisplayRadix |
1648 |
||
92 | 1649 |
" |
1650 |
Integer displayRadix:16. 12345 |
|
1651 |
Integer displayRadix:2. 12345 |
|
1652 |
Integer displayRadix:10. 12345 |
|
1653 |
" |
|
49 | 1654 |
! |
1655 |
||
701 | 1656 |
errorPrintHex |
1657 |
"print the receiver as a hex number on the standard error stream" |
|
1658 |
||
1659 |
(self printStringRadix:16) errorPrint |
|
1660 |
! |
|
1 | 1661 |
|
701 | 1662 |
hexPrintString |
1663 |
"return a hex string representation of the receiver" |
|
1664 |
||
1665 |
^ self printStringRadix:16 |
|
3874 | 1666 |
|
1667 |
" |
|
1668 |
127 hexPrintString |
|
1669 |
-1 hexPrintString |
|
1670 |
" |
|
1671 |
||
1672 |
"Modified: / 11.10.1998 / 01:15:43 / cg" |
|
701 | 1673 |
! |
1674 |
||
2408 | 1675 |
hexPrintString:size |
1676 |
"return a hex string representation of the receiver, |
|
1677 |
padded to size characters" |
|
1678 |
||
1679 |
^ (self printStringRadix:16) leftPaddedTo:size with:$0 |
|
1680 |
||
1681 |
"Created: 18.2.1997 / 13:32:33 / cg" |
|
1682 |
! |
|
1683 |
||
701 | 1684 |
printHex |
1685 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 1686 |
|
701 | 1687 |
(self printStringRadix:16) print |
1688 |
! |
|
1689 |
||
1690 |
printOn:aStream |
|
1691 |
"append a printed description of the receiver to aStream" |
|
1692 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1693 |
self printOn:aStream base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1694 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1695 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
701 | 1696 |
! |
1697 |
||
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
1698 |
printOn:aStream base:b |
1 | 1699 |
"return a string representation of the receiver in the specified |
1700 |
radix (without the initial XXr)" |
|
1701 |
||
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
1702 |
|base num s divMod mod r r2 r4 nD numN| |
1 | 1703 |
|
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
1704 |
base := b. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1705 |
(base between:2 and:36) ifFalse:[ |
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
1706 |
self error:'invalid base' mayProceed:true. |
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
1707 |
base := 10. |
54 | 1708 |
]. |
1709 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1710 |
(self = 0) ifTrue:[aStream nextPut:$0. ^ self]. |
1 | 1711 |
(self < 0) ifTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1712 |
aStream nextPut:$- . |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1713 |
num := self negated. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1714 |
] ifFalse:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1715 |
num := self. |
1 | 1716 |
]. |
92 | 1717 |
|
1718 |
" |
|
1719 |
claus: changed it from a recursive algorithm; |
|
1720 |
(it used to trigger stack-overflow exceptions when printing |
|
1721 |
3000 factorial ...) |
|
1722 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1723 |
"/ leftPart := num // base. |
92 | 1724 |
"/ (leftPart ~= 0) ifTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1725 |
"/ leftPart printOn:aStream base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1726 |
"/ aStream nextPut:(Character digitValue:(num \\ base). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1727 |
"/ ^ self |
92 | 1728 |
"/ ]. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1729 |
"/ aStream nextPut:(Character digitValue:num). |
2951 | 1730 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1731 |
"/ 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
|
1732 |
"/ 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
|
1733 |
"/ 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
|
1734 |
"/ 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
|
1735 |
"/ 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
|
1736 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1737 |
r2 := base*base. "/ radix^2 |
3138 | 1738 |
r4 := r2*r2. "/ radix^4 |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1739 |
base <= 10 ifTrue:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1740 |
r := r4*r2. "/ radix^6 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1741 |
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
|
1742 |
] ifFalse:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1743 |
r := r4*base. "/ radix^5 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1744 |
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
|
1745 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1746 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1747 |
"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
|
1748 |
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
|
1749 |
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
|
1750 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1751 |
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
|
1752 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1753 |
[num >= r] whileTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1754 |
"/ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1755 |
"/ chop off nD digits. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1756 |
"/ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1757 |
divMod := num divMod:r. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1758 |
num := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1759 |
numN := divMod at:2. |
2951 | 1760 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1761 |
"/ process them |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1762 |
nD timesRepeat:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1763 |
divMod := numN divMod:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1764 |
numN := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1765 |
mod := divMod at:2. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1766 |
s nextPut:(Character digitValue:mod). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1767 |
]. |
2951 | 1768 |
]. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1769 |
|
92 | 1770 |
[num ~= 0] whileTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1771 |
divMod := num divMod:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1772 |
num := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1773 |
mod := divMod at:2. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1774 |
s nextPut:(Character digitValue:mod). |
1 | 1775 |
]. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1776 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1777 |
aStream nextPutAll:(s contents reverse). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1778 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1779 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1780 |
3000 factorial printOn:Transcript base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1781 |
10 printOn:Transcript base:3 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1782 |
31 printOn:Transcript base:3 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1783 |
-20 printOn:Transcript base:16 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1784 |
-20 printOn:Transcript base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1785 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1786 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1787 |
"Modified: / 17.11.1997 / 16:27:45 / cg" |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1788 |
"Modified: / 20.1.1998 / 18:05:02 / stefan" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1789 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1790 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1791 |
printOn:aStream radix:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1792 |
"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
|
1793 |
The receiver is printed in radix base (instead of the default, 10). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1794 |
This method is obsoleted by #printOn:base:, which is ST-80 compatible." |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1795 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1796 |
self printOn:aStream base:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1797 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1798 |
"Modified: / 20.5.1996 / 11:54:10 / cg" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1799 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1800 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1801 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1802 |
printStringRadix:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1803 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1804 |
|s| |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1805 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1806 |
s := WriteStream on:(String new:20). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1807 |
self printOn:s base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1808 |
^ s contents |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1809 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1810 |
"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
|
1811 |
"Modified: / 20.1.1998 / 14:10:54 / stefan" |
1 | 1812 |
! |
1813 |
||
1814 |
printStringRadix:aRadix size:sz fill:fillCharacter |
|
1815 |
"return a string representation of the receiver in the specified |
|
1816 |
radix. The string is padded on the left with fillCharacter to make |
|
1817 |
its size as specified in sz." |
|
1818 |
||
2951 | 1819 |
|s actualSize| |
1 | 1820 |
|
1821 |
s := self printStringRadix:aRadix. |
|
2951 | 1822 |
actualSize := s size. |
1823 |
actualSize < sz ifTrue:[ |
|
1824 |
s := ((String new:(sz - actualSize)) atAllPut:fillCharacter) , s |
|
1 | 1825 |
]. |
1826 |
^ s |
|
1827 |
||
92 | 1828 |
" |
1829 |
1024 printStringRadix:16 size:4 fill:$0 |
|
1830 |
1024 printStringRadix:2 size:16 fill:$. |
|
1831 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
1832 |
" |
|
216 | 1833 |
! |
1834 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1835 |
radixPrintStringRadix:base |
701 | 1836 |
"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
|
1837 |
base; prepend XXr to the string" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1838 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1839 |
|s| |
701 | 1840 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1841 |
s := WriteStream on:(String new:20). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1842 |
base printOn:s. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1843 |
s nextPut:$r. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1844 |
self printOn:s base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1845 |
^ s contents |
216 | 1846 |
|
701 | 1847 |
" |
1848 |
31 radixPrintStringRadix:2 |
|
1849 |
31 radixPrintStringRadix:3 |
|
1850 |
31 radixPrintStringRadix:36 |
|
1851 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1852 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1853 |
"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
|
1854 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
1 | 1855 |
! ! |
1856 |
||
701 | 1857 |
!Integer methodsFor:'queries'! |
202 | 1858 |
|
701 | 1859 |
digitAt:n |
1860 |
"return the n-th byte of the binary representation. |
|
1861 |
This is a very stupid implementation, and should be redefined in |
|
1862 |
concrete subclasses." |
|
202 | 1863 |
|
701 | 1864 |
|num count| |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1865 |
|
701 | 1866 |
num := self. |
1867 |
count := n. |
|
1868 |
[count > 1] whileTrue:[ |
|
1869 |
num := num // 256. |
|
1870 |
count := count - 1 |
|
202 | 1871 |
]. |
701 | 1872 |
^ num \\ 256 |
159 | 1873 |
|
1874 |
" |
|
701 | 1875 |
16r44332211 digitAt:1 |
1876 |
16r44332211 digitAt:2 |
|
1877 |
16r44332211 digitAt:3 |
|
1878 |
16r44332211 digitAt:4 |
|
1879 |
16r44332211 digitAt:5 |
|
1880 |
16r00332211 digitAt:4 |
|
1881 |
16r00332211 digitAt:5 |
|
159 | 1882 |
" |
1 | 1883 |
! |
1884 |
||
701 | 1885 |
digitLength |
1886 |
"return the number of bytes needed for the binary representation |
|
2816 | 1887 |
of the receiver. |
1888 |
This method is redefined in concrete classes - the fallback here is |
|
1889 |
never really used." |
|
701 | 1890 |
|
1891 |
^ (self log:256) ceiling asInteger |
|
2816 | 1892 |
|
1893 |
"Modified: 31.7.1997 / 13:19:06 / cg" |
|
701 | 1894 |
! |
1895 |
||
1896 |
isInteger |
|
1897 |
"return true, if the receiver is some kind of integer number" |
|
1898 |
||
1899 |
^ true |
|
3036 | 1900 |
! |
1901 |
||
1902 |
isPowerOfTwo |
|
1903 |
"return true, if the receiver is a power of 2" |
|
1904 |
||
1905 |
^ (self bitAnd:(self - 1)) == 0 |
|
1906 |
||
1907 |
" |
|
1908 |
0 isPowerOfTwo |
|
1909 |
1 isPowerOfTwo |
|
1910 |
2 isPowerOfTwo |
|
1911 |
3 isPowerOfTwo |
|
1912 |
4 isPowerOfTwo |
|
1913 |
16r8000000000000000 isPowerOfTwo |
|
1914 |
16r8000000000000001 isPowerOfTwo |
|
1915 |
" |
|
1916 |
||
1917 |
"Modified: 15.10.1997 / 18:43:49 / cg" |
|
701 | 1918 |
! ! |
202 | 1919 |
|
2458 | 1920 |
!Integer methodsFor:'special bit operators'! |
1921 |
||
1922 |
bitAnd_32:anInteger |
|
1923 |
"return a C-semantic 32bit locical-and of the receiver and |
|
1924 |
the argument. Both must be either Small- or LargeIntegers. |
|
1925 |
This (nonstandard) specialized method is provided to allow simulation of |
|
1926 |
and operations with C semantics." |
|
1927 |
||
1928 |
%{ /* NOCONTEXT */ |
|
1929 |
int val1, val2, rslt; |
|
1930 |
||
1931 |
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
|
1932 |
val1 = __intVal(self); |
2458 | 1933 |
} 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
|
1934 |
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
|
1935 |
if (!val1) goto bad; |
2458 | 1936 |
} 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
|
1937 |
goto bad; |
2458 | 1938 |
} |
1939 |
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
|
1940 |
val2 = __intVal(anInteger); |
2458 | 1941 |
} 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
|
1942 |
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
|
1943 |
if (!val2) goto bad; |
2458 | 1944 |
} 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
|
1945 |
goto bad; |
2458 | 1946 |
} |
1947 |
rslt = val1 & val2; |
|
1948 |
RETURN(__MKINT(rslt)); |
|
1949 |
||
1950 |
bad: ; |
|
1951 |
%}. |
|
1952 |
self primitiveFailed. |
|
1953 |
! |
|
1954 |
||
1955 |
bitOr_32:anInteger |
|
1956 |
"return a C-semantic 32bit locical-or of the receiver and |
|
1957 |
the argument. Both must be either Small- or LargeIntegers. |
|
1958 |
This (nonstandard) specialized method is provided to allow simulation of |
|
1959 |
and operations with C semantics." |
|
1960 |
||
1961 |
%{ /* NOCONTEXT */ |
|
1962 |
int val1, val2, rslt; |
|
1963 |
||
1964 |
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
|
1965 |
val1 = __intVal(self); |
2458 | 1966 |
} 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
|
1967 |
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
|
1968 |
if (!val1) goto bad; |
2458 | 1969 |
} 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
|
1970 |
goto bad; |
2458 | 1971 |
} |
1972 |
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
|
1973 |
val2 = __intVal(anInteger); |
2458 | 1974 |
} 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
|
1975 |
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
|
1976 |
if (!val2) goto bad; |
2458 | 1977 |
} 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
|
1978 |
goto bad; |
2458 | 1979 |
} |
1980 |
rslt = val1 | val2; |
|
1981 |
RETURN(__MKINT(rslt)); |
|
1982 |
||
1983 |
bad: ; |
|
1984 |
%}. |
|
1985 |
self primitiveFailed. |
|
1986 |
! ! |
|
1987 |
||
701 | 1988 |
!Integer methodsFor:'truncation & rounding'! |
1989 |
||
1990 |
ceiling |
|
1558 | 1991 |
"return the smallest integer which is larger or equal to the receiver. |
1992 |
For integers, this is the receiver itself." |
|
345 | 1993 |
|
701 | 1994 |
^ self |
1558 | 1995 |
|
1996 |
"Modified: 18.7.1996 / 12:44:06 / cg" |
|
701 | 1997 |
! |
345 | 1998 |
|
701 | 1999 |
compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2000 |
"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
|
2001 |
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
|
2002 |
zeros removed. This method is redefined in LargeInteger." |
701 | 2003 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2004 |
^ self |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2005 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2006 |
"Modified: 5.11.1996 / 14:07:41 / cg" |
202 | 2007 |
! |
2008 |
||
701 | 2009 |
floor |
1558 | 2010 |
"return the largest integer which is smaller or equal to the receiver. |
2011 |
For integers, this is the receiver itself." |
|
701 | 2012 |
|
2013 |
^ self |
|
1558 | 2014 |
|
2015 |
"Modified: 18.7.1996 / 12:44:00 / cg" |
|
701 | 2016 |
! |
202 | 2017 |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2018 |
fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2019 |
"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
|
2020 |
(i.e. the receiver minus its truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2021 |
Since integers have no fraction, return 0 here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2022 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2023 |
^ 0 |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2024 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2025 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2026 |
1234.56789 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2027 |
1.2345e6 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2028 |
1000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2029 |
10000000000000000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2030 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2031 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2032 |
"Modified: 4.11.1996 / 20:27:44 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2033 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2034 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2035 |
integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2036 |
"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
|
2037 |
(i.e. the receivers truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2038 |
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
|
2039 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2040 |
^ self |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2041 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2042 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2043 |
1234.56789 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2044 |
1.2345e6 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2045 |
1000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2046 |
10000000000000000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2047 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2048 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2049 |
"Modified: 4.11.1996 / 20:28:22 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2050 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2051 |
|
701 | 2052 |
normalize |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2053 |
"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
|
2054 |
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
|
2055 |
zeros removed. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2056 |
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
|
2057 |
renamed to #compressed for ST-80 compatibility." |
1 | 2058 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2059 |
^ self compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2060 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2061 |
"Modified: 5.11.1996 / 14:08:24 / cg" |
701 | 2062 |
! |
1 | 2063 |
|
701 | 2064 |
rounded |
2065 |
"return the receiver rounded toward the next Integer - |
|
1558 | 2066 |
for integers this is the receiver itself." |
701 | 2067 |
|
2068 |
^ self |
|
1558 | 2069 |
|
2070 |
"Modified: 18.7.1996 / 12:44:24 / cg" |
|
701 | 2071 |
! |
69 | 2072 |
|
701 | 2073 |
truncated |
2074 |
"return the receiver truncated towards zero - |
|
1558 | 2075 |
for integers this is the receiver itself." |
159 | 2076 |
|
701 | 2077 |
^ self |
1558 | 2078 |
|
2079 |
"Modified: 18.7.1996 / 12:44:33 / cg" |
|
701 | 2080 |
! ! |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
2081 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2082 |
!Integer::ModuloNumber class methodsFor:'documentation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2083 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2084 |
documentation |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2085 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2086 |
This is a helper class to perform fast computation of the modulus. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2087 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2088 |
[author:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2089 |
Stefan Vogel |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2090 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2091 |
[see also:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2092 |
Integer SmallInteger LargeInsteger |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2093 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2094 |
[instance variables:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2095 |
modulus the modulus |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2096 |
reciprocal reciprocal of the modulus |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2097 |
shift shift count to cut off some bits |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2098 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2099 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2100 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2101 |
examples |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2102 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2103 |
[exBegin] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2104 |
17 asModuloNumber modulusOf:38 |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2105 |
[exEnd] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2106 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2107 |
[exBegin] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2108 |
38 \\ 17 |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2109 |
[exEnd] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2110 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2111 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2112 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2113 |
!Integer::ModuloNumber class methodsFor:'instance creation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2114 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2115 |
mod:anInteger |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2116 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2117 |
^ self new mod:anInteger |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2118 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2119 |
"Created: / 3.5.1999 / 11:13:15 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2120 |
"Modified: / 3.5.1999 / 11:18:37 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2121 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2122 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2123 |
!Integer::ModuloNumber methodsFor:'accessing'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2124 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2125 |
mod:n |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2126 |
"set the modulus" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2127 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2128 |
modulus := n. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2129 |
reciprocal := n integerReciprocal. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2130 |
shift := (n highBit ) negated. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2131 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2132 |
"Created: / 3.5.1999 / 10:02:39 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2133 |
"Modified: / 3.5.1999 / 14:30:49 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2134 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2135 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2136 |
!Integer::ModuloNumber methodsFor:'arithmethic'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2137 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2138 |
modulusOf:aNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2139 |
|d e| |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2140 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2141 |
d := aNumber. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2142 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2143 |
"throw off low nbits(modulus)" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2144 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2145 |
e := (d bitShift:shift). |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2146 |
e := e * reciprocal. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2147 |
e := (e bitShift:shift). |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2148 |
e := e * modulus. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2149 |
d := d - e. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2150 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2151 |
"this subtract is done max 2 times" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2152 |
[d > modulus] whileTrue:[ |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2153 |
d := d - modulus. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2154 |
]. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2155 |
^ d. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2156 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2157 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2158 |
|m| |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2159 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2160 |
m := self new mod:7. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2161 |
m modulusOf:55. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2162 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2163 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2164 |
"Modified: / 3.5.1999 / 14:30:32 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2165 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2166 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2167 |
!Integer::ModuloNumber methodsFor:'converting'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2168 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2169 |
asModuloNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2170 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2171 |
^ self |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2172 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2173 |
"Created: / 3.5.1999 / 14:48:27 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2174 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2175 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2176 |
!Integer class methodsFor:'documentation'! |
701 | 2177 |
|
2178 |
version |
|
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
2179 |
^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.102 1999-08-04 14:07:41 cg Exp $' |
1 | 2180 |
! ! |