author | Claus Gittinger <cg@exept.de> |
Sat, 12 Oct 1996 20:45:52 +0200 | |
changeset 1717 | 0db1dbfc057d |
parent 1556 | 134d96466f5a |
child 1794 | c8f7736d1c12 |
permissions | -rw-r--r-- |
1 | 1 |
" |
68 | 2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
345 | 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 |
Integer subclass:#LargeInteger |
|
1210 | 14 |
instanceVariableNames:'sign digitByteArray' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
20 |
!LargeInteger class methodsFor:'documentation'! |
1 | 21 |
|
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
345 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
77 | 36 |
documentation |
37 |
" |
|
38 |
This class provides arbitrary precision integers. These are represented as: |
|
39 |
sign (-1/0/+1) and, if sign ~~ 0 |
|
40 |
a ByteArray of digits with 8 bits per element; |
|
88 | 41 |
least significant 8 bits at index 1 ... |
1 | 42 |
|
88 | 43 |
The implementation is definitely not tuned for high performance |
44 |
(some key-methods should be rewritten as primitives), |
|
77 | 45 |
but ok for now, since LargeIntegers are not used very often. |
46 |
It will be reimplemented when everything else runs fine and a need arises |
|
47 |
(or someone rewrites it and sends me the code :-). |
|
48 |
||
49 |
For real speed, its implementation should be mapped onto a tuned arbitrary |
|
88 | 50 |
precision math package (maybe even special-cased for 32 and 64 bit |
51 |
large-ints, since those are the most common). |
|
52 |
||
53 |
LargeIntegers are usually not created explicitely, but result from |
|
54 |
SmallInteger arithmetic overflowing the SmallInteger range. |
|
357 | 55 |
Also, results of LargeInteger operations are converted to back to |
56 |
SmallIntegers, when possible (see normalize). |
|
92 | 57 |
|
58 |
In contrast to ST-80, there is only one class for LargeIntegers, keeping |
|
59 |
the sign as an instance variable (ST-80 has LargePositiveInteger and |
|
60 |
LargeNegativeInteger). This may change. |
|
1295 | 61 |
|
62 |
[author:] |
|
63 |
Claus Gittinger |
|
1556 | 64 |
|
65 |
[see also:] |
|
66 |
Number |
|
67 |
Float Fraction |
|
68 |
SmallInteger |
|
77 | 69 |
" |
70 |
! ! |
|
1 | 71 |
|
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
72 |
!LargeInteger class methodsFor:'instance creation'! |
1 | 73 |
|
74 |
new |
|
75 |
"catch creation message" |
|
76 |
||
77 |
self error:'LargeIntegers cannot be created with new' |
|
78 |
! |
|
79 |
||
701 | 80 |
new:numberOfDigits |
81 |
"catch creation message" |
|
2 | 82 |
|
701 | 83 |
self error:'LargeIntegers cannot be created with new' |
1 | 84 |
! |
85 |
||
701 | 86 |
value:aSmallInteger |
87 |
"create and return a new LargeInteger with value taken from |
|
88 |
the argument, aSmallInteger. |
|
89 |
Notice: this should be only used internally, since such small |
|
90 |
largeIntegers do not normally occur in the system. |
|
91 |
May change without notice." |
|
92 |
||
93 |
^ self basicNew value:aSmallInteger |
|
94 |
||
95 |
"LargeInteger value:3689" |
|
96 |
! ! |
|
97 |
||
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
98 |
!LargeInteger class methodsFor:'queries'! |
701 | 99 |
|
100 |
isBuiltInClass |
|
1264 | 101 |
"return true if this class is known by the run-time-system. |
102 |
Here, true is returned." |
|
701 | 103 |
|
104 |
^ true |
|
1264 | 105 |
|
106 |
"Modified: 23.4.1996 / 15:59:21 / cg" |
|
1 | 107 |
! ! |
108 |
||
68 | 109 |
!LargeInteger methodsFor:'arithmetic'! |
1 | 110 |
|
701 | 111 |
* aNumber |
112 |
"return the product of the receiver and the argument, aNumber" |
|
113 |
||
114 |
|otherSign| |
|
115 |
||
116 |
(sign == 0) ifTrue:[^ 0]. "cannot happen if correctly normalized" |
|
117 |
||
118 |
" |
|
119 |
this is the common case, multiplying with SmallInteger. |
|
120 |
Use a special method for this case ... |
|
121 |
" |
|
122 |
(aNumber class == SmallInteger) ifTrue:[ |
|
123 |
^ self productFromInteger:aNumber |
|
124 |
]. |
|
125 |
||
126 |
" |
|
127 |
if the argument is not a largeInteger, coerce |
|
128 |
" |
|
129 |
(aNumber class == self class) ifFalse:[ |
|
130 |
^ self retry:#* coercing:aNumber |
|
131 |
]. |
|
132 |
||
133 |
otherSign := aNumber sign. |
|
134 |
(sign == otherSign) ifTrue:[^ self absMul:aNumber]. |
|
135 |
(otherSign == 0) ifTrue:[^ 0]. |
|
136 |
^ (self absMul:aNumber) sign:-1 |
|
137 |
! |
|
138 |
||
92 | 139 |
+ aNumber |
140 |
"return the sum of the receiver and the argument, aNumber" |
|
141 |
||
142 |
|otherSign| |
|
143 |
||
144 |
(sign == 0) ifTrue:[^ aNumber]. "cannot happen if correctly normalized" |
|
145 |
||
146 |
" |
|
147 |
this is the common case, adding a SmallInteger. |
|
148 |
Use a special method for this case ... |
|
149 |
" |
|
150 |
(aNumber class == SmallInteger) ifTrue:[ |
|
345 | 151 |
^ self sumFromInteger:aNumber |
92 | 152 |
]. |
153 |
||
154 |
" |
|
155 |
if the argument is not a largeInteger, coerce |
|
156 |
" |
|
157 |
(aNumber class == self class) ifFalse:[ |
|
345 | 158 |
^ self retry:#+ coercing:aNumber |
92 | 159 |
]. |
160 |
||
161 |
otherSign := aNumber sign. |
|
162 |
(sign > 0) ifTrue:[ |
|
345 | 163 |
"I am positive" |
164 |
(otherSign > 0) ifTrue:[^ self absPlus:aNumber]. |
|
165 |
(otherSign < 0) ifTrue:[^ self absMinus:aNumber]. |
|
166 |
^ self |
|
92 | 167 |
]. |
168 |
"I am negative" |
|
169 |
(otherSign > 0) ifTrue:[^ aNumber absMinus:self]. |
|
170 |
(otherSign < 0) ifTrue:[^ (self absPlus:aNumber) sign:-1]. |
|
171 |
^ self |
|
357 | 172 |
|
173 |
" |
|
174 |
SmallInteger maxVal |
|
175 |
SmallInteger maxVal + 1 |
|
176 |
SmallInteger maxVal + 2 |
|
177 |
SmallInteger minVal |
|
178 |
SmallInteger minVal - 1 |
|
179 |
" |
|
92 | 180 |
! |
181 |
||
182 |
- aNumber |
|
183 |
"return the difference of the receiver and the argument, aNumber" |
|
184 |
||
185 |
|otherSign result| |
|
186 |
||
187 |
(sign == 0) ifTrue:[^ aNumber negated]. "cannot happen if correctly normalized" |
|
188 |
||
189 |
" |
|
190 |
this is the common case, subtracting a SmallInteger. |
|
191 |
Use a special method for this case ... |
|
192 |
" |
|
193 |
(aNumber class == SmallInteger) ifTrue:[ |
|
345 | 194 |
sign < 0 ifTrue:[ |
195 |
aNumber > 0 ifTrue:[ |
|
196 |
result := (self absFastPlus:aNumber) sign:-1 |
|
197 |
] ifFalse:[ |
|
198 |
result := (self absFastMinus:aNumber) sign:-1 |
|
199 |
]. |
|
200 |
] ifFalse:[ |
|
201 |
aNumber > 0 ifTrue:[ |
|
202 |
result := self absFastMinus:aNumber |
|
203 |
] ifFalse:[ |
|
204 |
result := self absFastPlus:aNumber |
|
205 |
] |
|
206 |
]. |
|
207 |
^ result normalize. |
|
92 | 208 |
]. |
209 |
||
210 |
" |
|
211 |
if the argument is not a largeInteger, coerce |
|
212 |
" |
|
213 |
(aNumber class == self class) ifFalse:[ |
|
345 | 214 |
^ self retry:#- coercing:aNumber |
92 | 215 |
]. |
216 |
||
217 |
otherSign := aNumber sign. |
|
218 |
(sign > 0) ifTrue:[ |
|
345 | 219 |
"I am positive" |
220 |
(otherSign > 0) ifTrue:[^ self absMinus:aNumber]. |
|
221 |
(otherSign < 0) ifTrue:[^ self absPlus:aNumber]. |
|
222 |
^ self |
|
92 | 223 |
]. |
224 |
"I am negative" |
|
225 |
(otherSign > 0) ifTrue:[^ (self absPlus:aNumber) negated]. |
|
226 |
(otherSign < 0) ifTrue:[^ (self absMinus:aNumber) negated]. |
|
227 |
^ self |
|
228 |
||
229 |
" |
|
230 |
12345678901234567890 - 0 |
|
357 | 231 |
12345678901234567890 - 1 |
92 | 232 |
12345678901234567890 - -1 |
233 |
-12345678901234567890 - 1 |
|
234 |
-12345678901234567890 - -1 |
|
235 |
||
236 |
12345678901234567890 - 12345678901234567880 |
|
237 |
12345678901234567890 - 12345000000000000000 |
|
238 |
12345678901234567890 - -87654321098765432110 |
|
239 |
-12345678901234567890 - 87654321098765432110 |
|
240 |
-12345678901234567890 - -12345678901234567880 |
|
241 |
-12345678901234567890 - -12345678901234567980 |
|
242 |
" |
|
243 |
! |
|
244 |
||
701 | 245 |
/ aNumber |
1065 | 246 |
"return the quotient of the receiver and the argument, aNumber" |
92 | 247 |
|
701 | 248 |
aNumber isInteger ifTrue:[ |
249 |
^ (Fraction numerator:self |
|
250 |
denominator:aNumber) reduced |
|
92 | 251 |
]. |
252 |
||
701 | 253 |
"this is a q&d hack - we loose lots of precision here ..." |
254 |
^ (self asFloat / aNumber asFloat) |
|
92 | 255 |
! |
256 |
||
68 | 257 |
// aNumber |
258 |
"return the quotient of the receiver and the argument, aNumber" |
|
1 | 259 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
260 |
|otherSign divMod d| |
1 | 261 |
|
262 |
otherSign := aNumber sign. |
|
263 |
||
92 | 264 |
" |
265 |
this is the common case, dividing by a SmallInteger. |
|
266 |
Use a special method for this case ... |
|
267 |
" |
|
68 | 268 |
(aNumber class == SmallInteger) ifTrue:[ |
345 | 269 |
(aNumber abs between:1 and:16r003fffff) ifTrue:[ |
270 |
sign < 0 ifTrue:[ |
|
271 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber negated) at:1]. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
272 |
d := aNumber. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
273 |
] ifFalse:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
274 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber) at:1]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
275 |
d := aNumber negated |
345 | 276 |
]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
277 |
divMod := self absFastDiv:d. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
278 |
"/ stupid adjust ... |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
279 |
(divMod at:2) == 0 ifFalse:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
280 |
^ ((divMod at:1) sign:-1) - 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
281 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
282 |
^ (divMod at:1) sign:-1 |
345 | 283 |
] |
1 | 284 |
]. |
68 | 285 |
|
92 | 286 |
" |
287 |
if the argument is not a largeInteger, coerce |
|
288 |
" |
|
289 |
(aNumber class == self class) ifFalse:[ |
|
345 | 290 |
^ self retry:#// coercing:aNumber |
1 | 291 |
]. |
292 |
||
92 | 293 |
sign < 0 ifTrue:[ |
345 | 294 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber negated) at:1]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
295 |
] ifFalse:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
296 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber) at:1]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
297 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
298 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
299 |
divMod := self absDiv:aNumber. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
300 |
"/ stupid adjust ... |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
301 |
(divMod at:2) == 0 ifFalse:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
302 |
^ ((divMod at:1) sign:-1) - 1 |
1 | 303 |
]. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
304 |
^ (divMod at:1) sign:-1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
305 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
306 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
307 |
9000000000 // 4000000000 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
308 |
-9000000000 // 4000000000 => -3 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
309 |
9000000000 // -4000000000 => -3 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
310 |
-9000000000 // -4000000000 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
311 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
312 |
9000000000 quo: 4000000000 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
313 |
-9000000000 quo: 4000000000 => -2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
314 |
9000000000 quo: -4000000000 => -2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
315 |
-9000000000 quo: -4000000000 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
316 |
" |
68 | 317 |
! |
318 |
||
319 |
\\ aNumber |
|
320 |
"return the remainder of division of the receiver by the argument, aNumber" |
|
321 |
||
322 |
|otherSign| |
|
323 |
||
324 |
otherSign := aNumber sign. |
|
92 | 325 |
|
326 |
" |
|
327 |
this is the common case, dividing by a SmallInteger. |
|
328 |
Use a special method for this case ... |
|
329 |
" |
|
68 | 330 |
(aNumber class == SmallInteger) ifTrue:[ |
345 | 331 |
(aNumber abs between:1 and:16r003fffff) ifTrue:[ |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
332 |
^ ((self absFastDiv:aNumber) at:2) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
333 |
] |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
334 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
335 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
336 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
337 |
if the argument is not a largeInteger, coerce |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
338 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
339 |
(aNumber class == self class) ifFalse:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
340 |
^ self retry:#\\ coercing:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
341 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
342 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
343 |
^ ((self absDiv:aNumber) at:2) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
344 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
345 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
346 |
9000000000 \\ 4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
347 |
-9000000000 \\ 4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
348 |
9000000000 \\ -4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
349 |
-9000000000 \\ -4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
350 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
351 |
9000000000 rem: 4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
352 |
-9000000000 rem: 4000000000 => -1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
353 |
9000000000 rem: -4000000000 => -1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
354 |
-9000000000 rem: -4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
355 |
" |
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 |
|
1210 | 358 |
negated |
359 |
"return an integer with value negated from the receivers value." |
|
360 |
||
361 |
|newNumber| |
|
362 |
||
363 |
(sign == 0) ifTrue:[^ 0]. |
|
364 |
||
365 |
" |
|
366 |
special case for SmallInteger minVal |
|
367 |
" |
|
368 |
sign == 1 ifTrue:[ |
|
369 |
digitByteArray size == 4 ifTrue:[ |
|
370 |
(digitByteArray at:1) == 0 ifTrue:[ |
|
371 |
(digitByteArray at:2) == 0 ifTrue:[ |
|
372 |
(digitByteArray at:3) == 0 ifTrue:[ |
|
373 |
(digitByteArray at:4) == 16r40 ifTrue:[ |
|
374 |
^ SmallInteger minVal |
|
375 |
]. |
|
376 |
] |
|
377 |
] |
|
378 |
] |
|
379 |
] |
|
380 |
]. |
|
381 |
newNumber := self shallowCopy. |
|
382 |
newNumber sign:(sign negated). |
|
383 |
^ newNumber |
|
384 |
! |
|
385 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
386 |
quo:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
387 |
"return the quotient of the receiver and the argument, aNumber" |
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 |
|otherSign d| |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
390 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
391 |
otherSign := aNumber sign. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
392 |
|
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 |
this is the common case, dividing by a SmallInteger. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
395 |
Use a special method for this case ... |
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 |
(aNumber class == SmallInteger) ifTrue:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
398 |
(aNumber abs between:1 and:16r003fffff) ifTrue:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
399 |
sign < 0 ifTrue:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
400 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber negated) at:1]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
401 |
d := aNumber. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
402 |
] ifFalse:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
403 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber) at:1]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
404 |
d := aNumber negated. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
405 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
406 |
^ ((self absFastDiv:d ) at:1) sign:-1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
407 |
] |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
408 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
409 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
410 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
411 |
if the argument is not a largeInteger, coerce |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
412 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
413 |
(aNumber class == self class) ifFalse:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
414 |
^ self retry:#quo: coercing:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
415 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
416 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
417 |
sign < 0 ifTrue:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
418 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber negated) at:1]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
419 |
] ifFalse:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
420 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber) at:1]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
421 |
]. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
422 |
^ ((self absDiv:aNumber) at:1) sign:-1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
423 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
424 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
425 |
9000000000 // 4000000000 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
426 |
-9000000000 // 4000000000 => -3 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
427 |
9000000000 // -4000000000 => -3 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
428 |
-9000000000 // -4000000000 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
429 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
430 |
9000000000 quo: 4000000000 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
431 |
-9000000000 quo: 4000000000 => -2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
432 |
9000000000 quo: -4000000000 => -2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
433 |
-9000000000 quo: -4000000000 => 2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
434 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
435 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
436 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
437 |
rem:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
438 |
"return the remainder of division of the receiver by the argument, aNumber" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
439 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
440 |
|otherSign| |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
441 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
442 |
otherSign := aNumber sign. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
443 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
444 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
445 |
this is the common case, dividing by a SmallInteger. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
446 |
Use a special method for this case ... |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
447 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
448 |
(aNumber class == SmallInteger) ifTrue:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
449 |
(aNumber abs between:1 and:16r003fffff) ifTrue:[ |
345 | 450 |
sign < 0 ifTrue:[ |
451 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber negated) at:2]. |
|
452 |
^ ((self absFastDiv:aNumber) at:2) sign:-1 |
|
453 |
]. |
|
454 |
(sign == otherSign) ifTrue:[^ (self absFastDiv:aNumber) at:2]. |
|
455 |
^ ((self absFastDiv:aNumber negated) at:2) sign:-1 |
|
456 |
] |
|
68 | 457 |
]. |
458 |
||
92 | 459 |
" |
460 |
if the argument is not a largeInteger, coerce |
|
461 |
" |
|
68 | 462 |
(aNumber class == self class) ifFalse:[ |
345 | 463 |
^ self retry:#\\ coercing:aNumber |
1 | 464 |
]. |
92 | 465 |
|
466 |
sign < 0 ifTrue:[ |
|
345 | 467 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber negated) at:2]. |
468 |
^ ((self absDiv:aNumber) at:2) sign:-1 |
|
1 | 469 |
]. |
92 | 470 |
(sign == otherSign) ifTrue:[^ (self absDiv:aNumber) at:2]. |
471 |
^ ((self absDiv:aNumber negated) at:2) sign:-1 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
472 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
473 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
474 |
9000000000 \\ 4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
475 |
-9000000000 \\ 4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
476 |
9000000000 \\ -4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
477 |
-9000000000 \\ -4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
478 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
479 |
9000000000 rem: 4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
480 |
-9000000000 rem: 4000000000 => -1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
481 |
9000000000 rem: -4000000000 => -1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
482 |
-9000000000 rem: -4000000000 => 1000000000 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
483 |
" |
1 | 484 |
! ! |
485 |
||
701 | 486 |
!LargeInteger methodsFor:'byte access'! |
487 |
||
488 |
digitAt:index |
|
489 |
"return 8 bits of value, starting at byte index" |
|
490 |
||
491 |
index > digitByteArray size ifTrue:[^ 0]. |
|
492 |
^ digitByteArray at:index |
|
493 |
! |
|
494 |
||
495 |
digitAt:index put:aByte |
|
496 |
"set the 8 bits, index is a byte index" |
|
497 |
||
498 |
digitByteArray at:index put:aByte |
|
499 |
! |
|
500 |
||
501 |
digitLength |
|
502 |
"return the number bytes used by this Integer" |
|
503 |
||
504 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
505 |
check if there is a 0-byte ... |
701 | 506 |
this allows to ask unnormalized LargeIntegers |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
507 |
for their digitLength |
701 | 508 |
" |
509 |
|l "{ Class: SmallInteger }" | |
|
92 | 510 |
|
701 | 511 |
l := digitByteArray size. |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
512 |
[l ~~ 0 and:[(digitByteArray at:l) == 0]] whileTrue:[ |
701 | 513 |
l := l - 1. |
514 |
]. |
|
515 |
^ l |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
516 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
517 |
"Modified: 2.2.1996 / 16:47:21 / cg" |
701 | 518 |
! |
519 |
||
520 |
digits |
|
521 |
"return a byteArray fille with the receivers bits |
|
522 |
(8 bits of the absolute value per element)" |
|
523 |
||
524 |
^ digitByteArray |
|
525 |
! ! |
|
526 |
||
527 |
!LargeInteger methodsFor:'coercing & converting'! |
|
68 | 528 |
|
701 | 529 |
asFloat |
530 |
"return a Float with same value as myself. |
|
531 |
Since floats have a limited precision, you usually loose bits when |
|
532 |
doing this." |
|
533 |
||
534 |
|newFloat| |
|
535 |
||
536 |
newFloat := 0.0. |
|
537 |
(sign == 0) ifFalse:[ |
|
538 |
digitByteArray reverseDo:[:aDigit | |
|
539 |
newFloat := (newFloat * 256.0) + aDigit asFloat |
|
540 |
]. |
|
541 |
(sign < 0) ifTrue:[ |
|
542 |
newFloat := newFloat negated |
|
345 | 543 |
] |
701 | 544 |
]. |
545 |
^ newFloat |
|
546 |
||
547 |
" |
|
548 |
1234567890 asFloat |
|
549 |
12345678901234567890 asFloat |
|
550 |
12345678901234567890 asFloat asInteger |
|
551 |
" |
|
552 |
! |
|
553 |
||
554 |
asLargeInteger |
|
555 |
"return a LargeInteger with same value as myself - thats me" |
|
556 |
||
557 |
^ self |
|
558 |
! |
|
559 |
||
560 |
asSmallInteger |
|
561 |
"return a SmallInteger with same value as myself - |
|
562 |
the result is invalid if the receivers value cannot |
|
563 |
be represented as a SmallInteger. |
|
564 |
Q: should we raise an exception if this happens ?" |
|
565 |
||
566 |
|value| |
|
567 |
||
568 |
value := 0. |
|
569 |
(sign == 0) ifFalse:[ |
|
570 |
digitByteArray reverseDo:[:aDigit | |
|
571 |
value := (value times:256) + aDigit |
|
345 | 572 |
]. |
701 | 573 |
(sign < 0) ifTrue:[ |
574 |
value := value negated |
|
345 | 575 |
] |
68 | 576 |
]. |
701 | 577 |
^ value |
578 |
! |
|
579 |
||
580 |
coerce:aNumber |
|
581 |
"return the argument as a LargeInteger" |
|
582 |
||
583 |
^ aNumber asLargeInteger |
|
584 |
! |
|
585 |
||
586 |
normalize |
|
587 |
"if the receiver can be represented as a SmallInteger, return |
|
588 |
a SmallInteger with my value; otherwise return self with leading |
|
589 |
zeros removed" |
|
590 |
||
591 |
|index "{ Class: SmallInteger }" |
|
592 |
val "{ Class: SmallInteger }" | |
|
593 |
||
594 |
%{ /* NOCONTEXT */ |
|
595 |
OBJ t; |
|
596 |
||
1133 | 597 |
if (__INST(sign) == __MKSMALLINT(0)) { |
598 |
RETURN (__MKSMALLINT(0)); |
|
701 | 599 |
} |
600 |
||
1133 | 601 |
t = __INST(digitByteArray); |
701 | 602 |
if (__isByteArray(t)) { |
1133 | 603 |
unsigned char *_digitBytes = __ByteArrayInstPtr(t)->ba_element; |
701 | 604 |
int _idx = _byteArraySize(t); |
605 |
int _val; |
|
606 |
||
607 |
while ((_idx > 0) && (_digitBytes[_idx - 1] == 0)) { |
|
608 |
_idx--; |
|
609 |
} |
|
610 |
switch (_idx) { |
|
611 |
case 0: |
|
1133 | 612 |
RETURN (__MKSMALLINT(0)); |
701 | 613 |
break; |
614 |
||
615 |
case 1: |
|
616 |
_val = _digitBytes[0]; |
|
1133 | 617 |
if (__INST(sign) == __MKSMALLINT(-1)) |
701 | 618 |
_val = -_val; |
1133 | 619 |
RETURN (__MKSMALLINT(_val)); |
701 | 620 |
|
621 |
case 2: |
|
622 |
_val = (_digitBytes[1]<<8) + _digitBytes[0]; |
|
1133 | 623 |
if (__INST(sign) == __MKSMALLINT(-1)) |
701 | 624 |
_val = -_val; |
1133 | 625 |
RETURN (__MKSMALLINT(_val)); |
701 | 626 |
|
627 |
case 3: |
|
628 |
_val = (((_digitBytes[2]<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
|
1133 | 629 |
if (__INST(sign) == __MKSMALLINT(-1)) |
701 | 630 |
_val = -_val; |
1133 | 631 |
RETURN (__MKSMALLINT(_val)); |
701 | 632 |
|
633 |
case 4: |
|
634 |
_val = _digitBytes[3]; |
|
635 |
if (_val <= 0x40) { |
|
636 |
_val = (((((_val<<8) + _digitBytes[2])<<8) + _digitBytes[1])<<8) + _digitBytes[0]; |
|
1133 | 637 |
if (__INST(sign) == __MKSMALLINT(-1)) |
701 | 638 |
_val = -_val; |
639 |
if ((_val >= _MIN_INT) && (_val <= _MAX_INT)) { |
|
1133 | 640 |
RETURN (__MKSMALLINT(_val)); |
701 | 641 |
} |
642 |
} |
|
643 |
break; |
|
644 |
||
645 |
default: |
|
646 |
break; |
|
647 |
} |
|
648 |
} |
|
649 |
%}. |
|
650 |
index := digitByteArray size. |
|
651 |
[(index > 0) and:[(digitByteArray at:index) == 0]] whileTrue:[ |
|
652 |
index := index - 1 |
|
653 |
]. |
|
654 |
||
655 |
(index ~~ digitByteArray size) ifTrue:[ |
|
656 |
digitByteArray := digitByteArray copyFrom:1 to:index |
|
657 |
]. |
|
658 |
^ self |
|
659 |
! |
|
660 |
||
661 |
value:aSmallInteger |
|
662 |
"return a new LargeInteger with value taken from a SmallInteger. |
|
663 |
This method will fail, if the argument is not a smallInteger." |
|
664 |
||
665 |
|absValue |
|
666 |
b1 "{ Class: SmallInteger }" |
|
667 |
b2 "{ Class: SmallInteger }" |
|
668 |
b3 "{ Class: SmallInteger }"| |
|
92 | 669 |
|
670 |
" |
|
701 | 671 |
could have simply created a 4-byte largeinteger and normalize |
672 |
it; the code below does the normalize right away, avoiding the |
|
673 |
overhead of producing any intermediate byte-arrays (and the scanning) |
|
92 | 674 |
" |
701 | 675 |
(aSmallInteger == 0) ifTrue: [ |
676 |
digitByteArray := #[0]. |
|
677 |
sign := 0. |
|
678 |
^ self |
|
679 |
]. |
|
680 |
(aSmallInteger < 0) ifTrue: [ |
|
681 |
sign := -1. |
|
682 |
absValue := aSmallInteger negated |
|
683 |
] ifFalse: [ |
|
684 |
sign := 1. |
|
685 |
absValue := aSmallInteger |
|
686 |
]. |
|
687 |
b1 := absValue bitAnd:16rFF. |
|
688 |
absValue := absValue bitShift:-8. |
|
689 |
absValue == 0 ifTrue:[ |
|
690 |
digitByteArray := ByteArray with:b1 |
|
691 |
] ifFalse:[ |
|
692 |
b2 := absValue bitAnd:16rFF. |
|
693 |
absValue := absValue bitShift:-8. |
|
694 |
absValue == 0 ifTrue:[ |
|
695 |
digitByteArray := ByteArray with:b1 with:b2 |
|
696 |
] ifFalse:[ |
|
697 |
b3 := absValue bitAnd:16rFF. |
|
698 |
absValue := absValue bitShift:-8. |
|
699 |
absValue == 0 ifTrue:[ |
|
700 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 |
|
701 |
] ifFalse:[ |
|
702 |
digitByteArray := ByteArray with:b1 with:b2 with:b3 with:absValue |
|
703 |
] |
|
704 |
] |
|
705 |
] |
|
706 |
! ! |
|
707 |
||
708 |
!LargeInteger methodsFor:'comparing'! |
|
709 |
||
710 |
< aNumber |
|
711 |
"return true, if the argument, aNumber is greater than the receiver" |
|
712 |
||
713 |
|otherSign| |
|
714 |
||
715 |
(aNumber class == self class) ifFalse:[ |
|
716 |
^ self retry:#< coercing:aNumber |
|
717 |
]. |
|
718 |
otherSign := aNumber sign. |
|
719 |
||
720 |
(sign > 0) ifTrue:[ |
|
721 |
"I am positive" |
|
722 |
(otherSign > 0) ifTrue:[^ self absLess:aNumber]. |
|
723 |
^ false "aNumber is <= 0" |
|
724 |
]. |
|
725 |
(sign == 0) ifTrue:[ |
|
726 |
(otherSign > 0) ifTrue:[^ true]. |
|
727 |
^ false |
|
728 |
]. |
|
729 |
"I am negative" |
|
730 |
(otherSign > 0) ifTrue:[^ true]. |
|
731 |
(otherSign == 0) ifTrue:[^ true]. |
|
732 |
^ (self absLess:aNumber) not |
|
92 | 733 |
! |
734 |
||
701 | 735 |
= aNumber |
736 |
"return true, if the argument, aNumber has the same value as |
|
737 |
the receiver" |
|
738 |
||
739 |
(aNumber class == self class) ifFalse:[ |
|
740 |
aNumber respondsToArithmetic ifFalse:[ ^ false ]. |
|
741 |
^ self retry:#= coercing:aNumber |
|
742 |
]. |
|
743 |
(aNumber sign == sign) ifFalse:[^ false]. |
|
744 |
^ self absEq:aNumber |
|
745 |
! |
|
746 |
||
747 |
> aNumber |
|
748 |
"return true, if the argument, aNumber is less than the receiver" |
|
749 |
||
750 |
|otherSign| |
|
751 |
||
752 |
(aNumber class == self class) ifFalse:[ |
|
753 |
^ self retry:#> coercing:aNumber |
|
754 |
]. |
|
755 |
otherSign := aNumber sign. |
|
756 |
||
757 |
(sign > 0) ifTrue:[ |
|
758 |
"I am positive" |
|
759 |
(otherSign > 0) ifTrue:[^ aNumber absLess:self]. |
|
760 |
^ true "aNumber is <= 0" |
|
761 |
]. |
|
762 |
(sign == 0) ifTrue:[ |
|
763 |
"I am zero" |
|
764 |
(otherSign > 0) ifTrue:[^ false]. |
|
765 |
^ true |
|
766 |
]. |
|
767 |
"I am negative" |
|
768 |
(otherSign > 0) ifTrue:[^ false]. |
|
769 |
(otherSign == 0) ifTrue:[^ false]. |
|
770 |
^ (self absLess:aNumber) not |
|
771 |
! ! |
|
772 |
||
773 |
!LargeInteger methodsFor:'double dispatching'! |
|
774 |
||
92 | 775 |
differenceFromInteger:anInteger |
776 |
"sent, when anInteger does not know how to subtract the receiver. |
|
777 |
Return the result of 'anInteger - self'. The argument must be a SmallInteger." |
|
778 |
||
779 |
|result| |
|
780 |
||
781 |
anInteger > 0 ifTrue:[ |
|
345 | 782 |
sign > 0 ifTrue:[ |
783 |
result := (self absFastMinus:anInteger) sign:-1 |
|
784 |
] ifFalse:[ |
|
785 |
result := self absFastPlus:anInteger |
|
786 |
] |
|
92 | 787 |
] ifFalse:[ |
345 | 788 |
anInteger == 0 ifTrue:[ |
789 |
^ self negated |
|
790 |
]. |
|
791 |
sign > 0 ifTrue:[ |
|
792 |
result := (self absFastPlus:anInteger negated) sign:-1 |
|
793 |
] ifFalse:[ |
|
794 |
result := (self absFastMinus:anInteger) sign:-1 |
|
795 |
] |
|
92 | 796 |
]. |
797 |
^ result normalize |
|
798 |
||
799 |
" |
|
800 |
12345678901234567890 |
|
801 |
-12345678901234567890 |
|
802 |
12345678901234567890 differenceFromInteger:0 |
|
803 |
12345678901234567890 differenceFromInteger:1 |
|
804 |
12345678901234567890 differenceFromInteger:-1 |
|
805 |
-12345678901234567890 differenceFromInteger:1 |
|
806 |
-12345678901234567890 differenceFromInteger:-1 |
|
807 |
" |
|
68 | 808 |
! |
809 |
||
810 |
productFromInteger:anInteger |
|
811 |
"sent, when anInteger does not know how to multiply the receiver. |
|
812 |
Return the product of the receiver and the argument, aSmallInteger" |
|
813 |
||
814 |
|num result |
|
815 |
resultDigitByteArray |
|
816 |
val "{ Class: SmallInteger }" |
|
817 |
len "{ Class: SmallInteger }" |
|
818 |
carry "{ Class: SmallInteger }" |
|
85 | 819 |
prod "{ Class: SmallInteger }" |
820 |
ok| |
|
68 | 821 |
|
822 |
"multiplying by a small integer is done here" |
|
823 |
||
824 |
"trivial cases" |
|
825 |
anInteger == 0 ifTrue:[^ 0]. |
|
826 |
anInteger == 1 ifTrue:[^ self]. |
|
827 |
||
828 |
num := anInteger abs. |
|
829 |
(num > 16r3FFFFF) ifTrue:[ |
|
345 | 830 |
"if num is too big (so that multiplying by a byte could create a Large)" |
68 | 831 |
|
345 | 832 |
^ anInteger retry:#* coercing:self |
68 | 833 |
]. |
834 |
||
835 |
len := digitByteArray size. |
|
836 |
||
837 |
result := self class basicNew numberOfDigits:(len + 4). |
|
1 | 838 |
|
85 | 839 |
"used to be the following. replaced, to avoid another multiplication" |
68 | 840 |
" |
841 |
result sign:(sign * anInteger sign). |
|
842 |
" |
|
843 |
anInteger < 0 ifTrue:[ |
|
345 | 844 |
sign > 0 ifTrue:[ |
845 |
result sign:-1 |
|
846 |
]. |
|
68 | 847 |
] ifFalse:[ |
345 | 848 |
sign < 0 ifTrue:[ |
849 |
result sign:sign |
|
850 |
] |
|
68 | 851 |
]. |
852 |
||
853 |
resultDigitByteArray := result digits. |
|
854 |
||
855 |
carry := 0. |
|
856 |
val := num. |
|
857 |
||
85 | 858 |
ok := false. |
859 |
%{ |
|
250 | 860 |
if (__bothSmallInteger(len, val) |
1133 | 861 |
&& __isByteArray(__INST(digitByteArray)) |
85 | 862 |
&& __isByteArray(resultDigitByteArray)) { |
1133 | 863 |
int _l = __intVal(len); |
864 |
int _v = __intVal(val); |
|
345 | 865 |
unsigned _carry = 0; |
866 |
unsigned _prod; |
|
1133 | 867 |
unsigned char *digitP = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
868 |
unsigned char *resultP = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
85 | 869 |
|
345 | 870 |
while (_l-- > 0) { |
871 |
_prod = *digitP++ * _v + _carry; |
|
872 |
*resultP++ = _prod & 0xFF; |
|
873 |
_carry = _prod >> 8; |
|
874 |
} |
|
875 |
while (_carry) { |
|
876 |
*resultP++ = _carry & 0xFF; |
|
877 |
_carry >>= 8; |
|
878 |
} |
|
879 |
ok = true; |
|
85 | 880 |
} |
881 |
%}. |
|
92 | 882 |
" |
883 |
fall back - normally not reached |
|
884 |
(could make it a primitive-failure as well) |
|
885 |
" |
|
85 | 886 |
ok ifFalse:[ |
345 | 887 |
1 to:len do:[:i | |
888 |
prod := (digitByteArray basicAt:i) * val + carry. |
|
889 |
resultDigitByteArray basicAt:i put:(prod bitAnd:16rFF). |
|
890 |
carry := prod bitShift:-8. |
|
891 |
]. |
|
892 |
[carry ~~ 0] whileTrue:[ |
|
893 |
len := len + 1. |
|
894 |
resultDigitByteArray basicAt:len put:(carry bitAnd:16rFF). |
|
895 |
carry := carry bitShift:-8 |
|
896 |
]. |
|
68 | 897 |
]. |
898 |
^ result normalize |
|
92 | 899 |
! |
900 |
||
701 | 901 |
sumFromInteger:anInteger |
902 |
"sent, when anInteger does not know how to add the receiver. |
|
903 |
Return the sum of the receiver and the argument, (which must be a SmallInteger)" |
|
92 | 904 |
|
701 | 905 |
|result| |
92 | 906 |
|
701 | 907 |
anInteger > 0 ifTrue:[ |
908 |
sign < 0 ifTrue:[ |
|
909 |
result := (self absFastMinus:anInteger) sign:-1 |
|
910 |
] ifFalse:[ |
|
911 |
result := self absFastPlus:anInteger |
|
912 |
] |
|
913 |
] ifFalse:[ |
|
914 |
anInteger == 0 ifTrue:[ |
|
915 |
^ self |
|
916 |
]. |
|
917 |
sign < 0 ifTrue:[ |
|
918 |
result := (self absFastPlus:anInteger abs) sign:-1 |
|
919 |
] ifFalse:[ |
|
920 |
result := self absFastMinus:anInteger |
|
921 |
] |
|
922 |
]. |
|
923 |
^ result normalize |
|
92 | 924 |
|
701 | 925 |
" |
926 |
12345678901234567890 |
|
927 |
-12345678901234567890 |
|
928 |
12345678901234567890 sumFromInteger:0 |
|
929 |
-12345678901234567890 sumFromInteger:0 |
|
930 |
12345678901234567890 sumFromInteger:1 |
|
931 |
12345678901234567890 sumFromInteger:-1 |
|
932 |
-12345678901234567890 sumFromInteger:1 |
|
933 |
-12345678901234567890 sumFromInteger:-1 |
|
934 |
" |
|
68 | 935 |
! ! |
936 |
||
701 | 937 |
!LargeInteger methodsFor:'printing & storing'! |
345 | 938 |
|
701 | 939 |
storeOn:aStream |
940 |
"append a representation of the receiver to aStream, which can |
|
941 |
be used to reconstruct the receiver." |
|
68 | 942 |
|
701 | 943 |
self printOn:aStream. |
944 |
aStream nextPutAll:' asLargeInteger' |
|
68 | 945 |
! ! |
946 |
||
947 |
!LargeInteger methodsFor:'private'! |
|
948 |
||
701 | 949 |
absDiv:anInteger |
950 |
"return an array with two LargeIntegers representing |
|
951 |
abs(self) // abs(theArgument) and abs(self) \\ abs(theArgument). |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
952 |
Used as a helper for \\, //, rem: and quo:. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
953 |
This method needs a rewrite." |
68 | 954 |
|
701 | 955 |
|tmp1 tmp2 |
956 |
rem |
|
957 |
count "{ Class: SmallInteger }" |
|
958 |
digit "{ Class: SmallInteger }" | |
|
92 | 959 |
|
701 | 960 |
anInteger == 0 ifTrue:[ |
961 |
^ DivisionByZeroSignal raise |
|
962 |
]. |
|
92 | 963 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
964 |
tmp1 := self simpleDeepCopy. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
965 |
tmp1 sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
966 |
tmp2 := anInteger simpleDeepCopy. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
967 |
tmp2 sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
968 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
969 |
(tmp1 < tmp2) ifTrue:[ |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
970 |
^ Array with:0 with:tmp1 |
68 | 971 |
]. |
92 | 972 |
|
701 | 973 |
count := 0. |
974 |
[tmp2 < tmp1] whileTrue:[ |
|
975 |
tmp2 mul256. |
|
976 |
count := count + 1 |
|
977 |
]. |
|
978 |
||
979 |
tmp2 div256. |
|
980 |
||
981 |
rem := 0 asLargeInteger. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
982 |
rem sign:1. |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
983 |
|
701 | 984 |
[count == 0] whileFalse:[ |
985 |
digit := 0. |
|
986 |
[tmp1 >= tmp2] whileTrue:[ |
|
987 |
digit := digit + 1. |
|
988 |
tmp1 := tmp1 - tmp2 |
|
989 |
]. |
|
990 |
rem := rem * 256 + digit. |
|
991 |
tmp2 div256. |
|
992 |
count := count - 1 |
|
993 |
]. |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
994 |
^ Array with:rem with:tmp1 |
92 | 995 |
! |
996 |
||
701 | 997 |
absEq:aLargeInteger |
998 |
"return true, if abs(self) = abs(theArgument)" |
|
92 | 999 |
|
701 | 1000 |
|len1 "{ Class: SmallInteger }" |
1001 |
len2 "{ Class: SmallInteger }" |
|
1002 |
d1 "{ Class: SmallInteger }" |
|
1003 |
d2 "{ Class: SmallInteger }" |
|
1004 |
otherDigitByteArray | |
|
92 | 1005 |
|
701 | 1006 |
len1 := digitByteArray size. |
1007 |
otherDigitByteArray := aLargeInteger digits. |
|
1008 |
len2 := otherDigitByteArray size. |
|
92 | 1009 |
|
701 | 1010 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
1011 |
len1 := len1 - 1 |
|
1012 |
]. |
|
1013 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
1014 |
len2 := len2 - 1 |
|
68 | 1015 |
]. |
701 | 1016 |
(len1 ~~ len2) ifTrue:[^ false]. |
1017 |
[len1 > 0] whileTrue:[ |
|
1018 |
d1 := digitByteArray basicAt:len1. |
|
1019 |
d2 := otherDigitByteArray basicAt:len1. |
|
1020 |
(d1 ~~ d2) ifTrue:[^ false]. |
|
1021 |
len1 := len1 - 1 |
|
68 | 1022 |
]. |
701 | 1023 |
^ true |
68 | 1024 |
! |
1025 |
||
1026 |
absFastDiv:aSmallInteger |
|
1027 |
"return an array with two LargeIntegers representing |
|
1028 |
abs(self) // aSmallInteger and abs(self) \\ aSmallInteger" |
|
1029 |
||
77 | 1030 |
|tmp1 "{ Class: SmallInteger }" |
1031 |
prevRest "{ Class: SmallInteger }" |
|
1032 |
count "{ Class: SmallInteger }" |
|
85 | 1033 |
newDigitByteArray result |
1034 |
ok| |
|
68 | 1035 |
|
1036 |
aSmallInteger == 0 ifTrue:[ |
|
345 | 1037 |
^ DivisionByZeroSignal raise |
68 | 1038 |
]. |
1039 |
||
1040 |
"This cannot happen (if always normalized) |
|
1041 |
self < aSmallInteger ifTrue:[ |
|
345 | 1042 |
^ Array with:0 with:self |
68 | 1043 |
]. |
1044 |
" |
|
1045 |
count := digitByteArray size. |
|
1046 |
result := self class basicNew numberOfDigits:count. |
|
1047 |
newDigitByteArray := result digits. |
|
85 | 1048 |
ok := false. |
1049 |
%{ |
|
1133 | 1050 |
if (__isByteArray(__INST(digitByteArray)) |
85 | 1051 |
&& __isByteArray(newDigitByteArray) |
250 | 1052 |
&& __bothSmallInteger(count, aSmallInteger)) { |
345 | 1053 |
unsigned int rest = 0; |
1133 | 1054 |
int index = __intVal(count); |
1055 |
int divisor = __intVal(aSmallInteger); |
|
1056 |
unsigned char *digitBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
|
1057 |
unsigned char *resultBytes = __ByteArrayInstPtr(newDigitByteArray)->ba_element; |
|
68 | 1058 |
|
345 | 1059 |
while (index > 0) { |
1060 |
unsigned int t; |
|
85 | 1061 |
|
345 | 1062 |
index--; |
1063 |
t = digitBytes[index]; |
|
1064 |
t = t | (rest << 8); |
|
1065 |
resultBytes[index] = t / divisor; |
|
1066 |
rest = t % divisor; |
|
1067 |
} |
|
1133 | 1068 |
prevRest = __MKSMALLINT(rest); |
345 | 1069 |
ok = true; |
85 | 1070 |
} |
1071 |
%}. |
|
1072 |
" |
|
1073 |
slow code - not normally reached |
|
1074 |
(could also do a primitiveFailure here) |
|
1075 |
" |
|
92 | 1076 |
ok ifFalse:[ |
345 | 1077 |
prevRest := 0. |
1078 |
count to:1 by:-1 do:[:i | |
|
1079 |
tmp1 := digitByteArray at:i. |
|
1080 |
tmp1 := (tmp1 + (prevRest * 256)). |
|
1081 |
newDigitByteArray at:i put:tmp1 // aSmallInteger. |
|
1082 |
prevRest := (tmp1 \\ aSmallInteger). |
|
1083 |
] |
|
68 | 1084 |
]. |
1085 |
||
1086 |
^ Array with:(result normalize) with:prevRest |
|
1087 |
! |
|
1088 |
||
701 | 1089 |
absFastMinus:aSmallInteger |
1090 |
"return a LargeInteger representing abs(self) - abs(theArgument). |
|
1091 |
The result is not normalized." |
|
1092 |
||
1093 |
|result resultDigitByteArray |
|
1094 |
len "{ Class: SmallInteger }" |
|
1095 |
index "{ Class: SmallInteger }" |
|
1096 |
borrow "{ Class: SmallInteger }" |
|
1097 |
diff "{ Class: SmallInteger }" | |
|
1098 |
||
1099 |
len := digitByteArray size. |
|
1100 |
||
1101 |
result := self class basicNew numberOfDigits:(len + 1). |
|
1102 |
resultDigitByteArray := result digits. |
|
1103 |
||
1104 |
index := 1. |
|
1105 |
borrow := aSmallInteger abs. |
|
1106 |
||
1107 |
[borrow ~~ 0] whileTrue:[ |
|
1108 |
(index <= len) ifTrue:[ |
|
1109 |
diff := (digitByteArray basicAt:index) - (borrow bitAnd:16rFF). |
|
1110 |
borrow := borrow bitShift:-8. |
|
1111 |
diff < 0 ifTrue:[ |
|
1112 |
diff := diff + 256. |
|
1113 |
borrow := borrow + 1. |
|
1114 |
] |
|
1115 |
] ifFalse:[ |
|
1116 |
diff := borrow bitAnd:255. |
|
1117 |
borrow := borrow bitShift:-8. |
|
1118 |
]. |
|
1119 |
resultDigitByteArray basicAt:index put:diff. |
|
1120 |
index := index + 1 |
|
1121 |
]. |
|
1122 |
[index <= len] whileTrue:[ |
|
1123 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
|
1124 |
index := index + 1 |
|
1125 |
]. |
|
1126 |
||
1127 |
^ result |
|
1128 |
||
1129 |
" |
|
1130 |
12345678900000000000 absFastMinus:1 |
|
1131 |
12345678900000000000 absFastMinus:1000000 |
|
1132 |
12345678900000000000 absFastMinus:255 |
|
1133 |
(SmallInteger maxVal + 1) absFastMinus:1 |
|
1134 |
(SmallInteger minVal - 1) absFastMinus:1 |
|
1135 |
" |
|
1136 |
! |
|
1137 |
||
1138 |
absFastPlus:aSmallInteger |
|
1139 |
"return a LargeInteger representing abs(self) + abs(theArgument). |
|
1140 |
The result is not normalized." |
|
1141 |
||
1142 |
|result resultDigitByteArray |
|
1143 |
len "{ Class: SmallInteger }" |
|
1144 |
index "{ Class: SmallInteger }" |
|
1145 |
carry "{ Class: SmallInteger }" | |
|
1146 |
||
1147 |
len := digitByteArray size. |
|
1148 |
||
1149 |
result := self class basicNew numberOfDigits:(len + 1). |
|
1150 |
resultDigitByteArray := result digits. |
|
1151 |
||
1152 |
index := 1. |
|
1153 |
carry := aSmallInteger abs. |
|
1154 |
||
1155 |
[carry ~~ 0] whileTrue:[ |
|
1156 |
(index <= len) ifTrue:[ |
|
1157 |
carry := (digitByteArray basicAt:index) + carry. |
|
1158 |
]. |
|
1159 |
resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF). |
|
1160 |
carry := carry bitShift:-8. |
|
1161 |
index := index + 1 |
|
1162 |
]. |
|
1163 |
[index <= len] whileTrue:[ |
|
1164 |
resultDigitByteArray basicAt:index put:(digitByteArray basicAt:index). |
|
1165 |
index := index + 1 |
|
1166 |
]. |
|
1167 |
||
1168 |
^ result |
|
1169 |
! |
|
1170 |
||
1171 |
absLess:aLargeInteger |
|
1172 |
"return true, if abs(self) < abs(theArgument)" |
|
1173 |
||
1174 |
|len1 "{ Class: SmallInteger }" |
|
1175 |
len2 "{ Class: SmallInteger }" |
|
1176 |
d1 "{ Class: SmallInteger }" |
|
1177 |
d2 "{ Class: SmallInteger }" |
|
1178 |
otherDigitByteArray | |
|
1179 |
||
1180 |
len1 := digitByteArray size. |
|
1181 |
otherDigitByteArray := aLargeInteger digits. |
|
1182 |
len2 := otherDigitByteArray size. |
|
1183 |
||
1184 |
[(digitByteArray basicAt:len1) == 0] whileTrue:[ |
|
1185 |
len1 := len1 - 1 |
|
1186 |
]. |
|
1187 |
[(otherDigitByteArray basicAt:len2) == 0] whileTrue:[ |
|
1188 |
len2 := len2 - 1 |
|
1189 |
]. |
|
1190 |
(len1 < len2) ifTrue:[^ true]. |
|
1191 |
(len1 > len2) ifTrue:[^ false]. |
|
1192 |
||
1193 |
[len1 > 0] whileTrue:[ |
|
1194 |
d1 := digitByteArray basicAt:len1. |
|
1195 |
d2 := otherDigitByteArray basicAt:len1. |
|
1196 |
(d1 < d2) ifTrue:[^ true]. |
|
1197 |
(d1 > d2) ifTrue:[^ false]. |
|
1198 |
len1 := len1 - 1 |
|
1199 |
]. |
|
1200 |
^ false |
|
1201 |
! |
|
1202 |
||
1203 |
absMinus:aLargeInteger |
|
1204 |
"return a LargeInteger representing abs(self) - abs(theArgument)" |
|
1205 |
||
1206 |
|result done |
|
1207 |
otherDigitByteArray resultDigitByteArray |
|
1208 |
len1 "{ Class: SmallInteger }" |
|
1209 |
len2 "{ Class: SmallInteger }" |
|
1210 |
index "{ Class: SmallInteger }" |
|
1211 |
borrow "{ Class: SmallInteger }" |
|
1212 |
diff "{ Class: SmallInteger }" |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1213 |
carry "{ Class: SmallInteger }" |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1214 |
lResult| |
701 | 1215 |
|
1216 |
len1 := digitByteArray size. |
|
1217 |
otherDigitByteArray := aLargeInteger digits. |
|
1218 |
len2 := otherDigitByteArray size. |
|
1219 |
||
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1220 |
lResult := (len1 max: len2) + 1. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1221 |
result := self class basicNew numberOfDigits:lResult. |
701 | 1222 |
resultDigitByteArray := result digits. |
1223 |
||
1224 |
index := 1. |
|
1225 |
borrow := 0. |
|
1226 |
||
1227 |
done := false. |
|
1228 |
[done] whileFalse:[ |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1229 |
diff := borrow. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1230 |
(index <= len1) ifTrue:[ |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1231 |
diff := diff + (digitByteArray basicAt:index). |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1232 |
(index <= len2) ifTrue:[ |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1233 |
diff := diff - (otherDigitByteArray basicAt:index) |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1234 |
] |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1235 |
] ifFalse:[ |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1236 |
(index <= len2) ifTrue:[ |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1237 |
diff := diff - (otherDigitByteArray basicAt:index) |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1238 |
] ifFalse:[ |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1239 |
"end reached" |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1240 |
done := true |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1241 |
] |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1242 |
]. |
701 | 1243 |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1244 |
"/ workaround for |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1245 |
"/ gcc code generator bug |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1246 |
|
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1247 |
(diff >= 0) ifTrue:[ |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1248 |
borrow := 0 |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1249 |
] ifFalse:[ |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1250 |
borrow := -1. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1251 |
diff := diff + 16r100 |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1252 |
]. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1253 |
|
701 | 1254 |
"/ (diff < 0) ifTrue:[ |
1255 |
"/ borrow := -1. |
|
1256 |
"/ diff := diff + 16r100 |
|
1257 |
"/ ] ifFalse:[ |
|
1258 |
"/ borrow := 0 |
|
1259 |
"/ ]. |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1260 |
|
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1261 |
resultDigitByteArray basicAt:index put:diff. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1262 |
index := index + 1 |
701 | 1263 |
]. |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1264 |
|
701 | 1265 |
(borrow ~~ 0) ifTrue:[ |
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1266 |
"/ must generate 255's complement |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1267 |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1268 |
result sign:-1. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1269 |
[index <= lResult] whileTrue:[ |
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1270 |
resultDigitByteArray basicAt:index put:16rFF. |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1271 |
index := index + 1. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1272 |
]. |
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1273 |
index := resultDigitByteArray size. |
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1274 |
[index > 0] whileTrue:[ |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1275 |
resultDigitByteArray basicAt:index put:(255 - (resultDigitByteArray at:index)). |
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1276 |
index := index -1. |
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1277 |
]. |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1278 |
|
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1279 |
index := 1. |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1280 |
carry := 1. |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1281 |
[carry ~~ 0] whileTrue:[ |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1282 |
(index <= lResult) ifTrue:[ |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1283 |
carry := (resultDigitByteArray basicAt:index) + carry. |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1284 |
]. |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1285 |
resultDigitByteArray basicAt:index put:(carry bitAnd:16rFF). |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1286 |
carry := carry bitShift:-8. |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1287 |
index := index + 1 |
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1288 |
]. |
701 | 1289 |
]. |
1290 |
^ result normalize |
|
1408
e9db184b34ee
oops - large-minus was wrong when sign changes
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
1291 |
|
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1292 |
"Modified: 1.7.1996 / 21:07:17 / cg" |
701 | 1293 |
! |
1294 |
||
68 | 1295 |
absMul:aLargeInteger |
1296 |
"return a LargeInteger representing abs(self) * abs(theArgument)" |
|
1297 |
||
92 | 1298 |
|result otherDigitByteArray resultDigitByteArray ok |
68 | 1299 |
idx "{ Class: SmallInteger }" |
1300 |
carry "{ Class: SmallInteger }" |
|
1301 |
len1 "{ Class: SmallInteger }" |
|
1302 |
len2 "{ Class: SmallInteger }" |
|
1303 |
dstIndex "{ Class: SmallInteger }" |
|
77 | 1304 |
prod "{ Class: SmallInteger }" |
1305 |
v "{ Class: SmallInteger }"| |
|
68 | 1306 |
|
1307 |
len1 := digitByteArray size. |
|
1308 |
otherDigitByteArray := aLargeInteger digits. |
|
1309 |
len2 := otherDigitByteArray size. |
|
1310 |
||
1311 |
result := LargeInteger basicNew numberOfDigits:(len1 + len2 + 1). |
|
1312 |
resultDigitByteArray := result digits. |
|
92 | 1313 |
ok := false. |
1314 |
%{ |
|
1133 | 1315 |
if (__isByteArray(__INST(digitByteArray)) |
92 | 1316 |
&& __isByteArray(otherDigitByteArray) |
1317 |
&& __isByteArray(resultDigitByteArray) |
|
250 | 1318 |
&& __bothSmallInteger(len1, len2)) { |
1133 | 1319 |
unsigned char *myBytes = __ByteArrayInstPtr(__INST(digitByteArray))->ba_element; |
1320 |
unsigned char *otherBytes = __ByteArrayInstPtr(otherDigitByteArray)->ba_element; |
|
1321 |
unsigned char *resultBytes = __ByteArrayInstPtr(resultDigitByteArray)->ba_element; |
|
345 | 1322 |
int _index1, _index2, _dstIndex, _idx; |
1323 |
unsigned _prod, _carry, _v; |
|
68 | 1324 |
|
1133 | 1325 |
for (_index1 = 0; _index1 < __intVal(len1); _index1++) { |
1326 |
for (_index2 = 0; _index2 < __intVal(len2); _index2++) { |
|
345 | 1327 |
_dstIndex = _index1 + _index2; |
1328 |
_prod = myBytes[_index1] * otherBytes[_index2]; |
|
1329 |
_prod += resultBytes[_dstIndex]; |
|
1330 |
resultBytes[_dstIndex] = _prod & 0xFF; |
|
1331 |
_carry = _prod >> 8; |
|
1332 |
if (_carry) { |
|
1333 |
_idx = _dstIndex + 1; |
|
1334 |
while (_carry) { |
|
1335 |
_v = resultBytes[_idx] + _carry; |
|
1336 |
resultBytes[_idx] = _v & 0xFF; |
|
1337 |
_carry = _v >> 8; |
|
1338 |
_idx = _idx + 1; |
|
1339 |
} |
|
1340 |
} |
|
1341 |
} |
|
1342 |
} |
|
1343 |
ok = true; |
|
92 | 1344 |
} |
1345 |
%}. |
|
1346 |
ok ifFalse:[ |
|
345 | 1347 |
1 to:len1 do:[:index1 | |
1348 |
1 to:len2 do:[:index2 | |
|
1349 |
dstIndex := index1 + index2 - 1. |
|
1350 |
prod := (digitByteArray basicAt:index1) * (otherDigitByteArray basicAt:index2). |
|
1351 |
prod := prod + (resultDigitByteArray basicAt:dstIndex). |
|
1352 |
resultDigitByteArray basicAt:dstIndex put:(prod bitAnd:16rFF). |
|
1353 |
carry := prod bitShift:-8. |
|
1354 |
carry ~~ 0 ifTrue:[ |
|
1355 |
idx := dstIndex + 1. |
|
1356 |
[carry ~~ 0] whileTrue:[ |
|
1357 |
v := (resultDigitByteArray basicAt:idx) + carry. |
|
1358 |
resultDigitByteArray basicAt:idx put:(v bitAnd:255). |
|
1359 |
carry := v bitShift:-8. |
|
1360 |
idx := idx + 1 |
|
1361 |
] |
|
1362 |
] |
|
1363 |
] |
|
1364 |
]. |
|
68 | 1365 |
]. |
1366 |
^ result normalize |
|
1367 |
! |
|
1368 |
||
1369 |
absPlus:aLargeInteger |
|
1370 |
"return a LargeInteger representing abs(self) + abs(theArgument)" |
|
1371 |
||
1372 |
|result done otherDigitByteArray resultDigitByteArray |
|
1373 |
len1 "{ Class: SmallInteger }" |
|
1374 |
len2 "{ Class: SmallInteger }" |
|
1375 |
index "{ Class: SmallInteger }" |
|
1376 |
carry "{ Class: SmallInteger }" |
|
1377 |
sum "{ Class: SmallInteger }" | |
|
1378 |
||
1379 |
len1 := digitByteArray size. |
|
1380 |
otherDigitByteArray := aLargeInteger digits. |
|
1381 |
len2 := otherDigitByteArray size. |
|
1382 |
||
92 | 1383 |
result := self class basicNew numberOfDigits:((len1 max: len2) + 1). |
68 | 1384 |
resultDigitByteArray := result digits. |
1385 |
||
1386 |
index := 1. |
|
1387 |
carry := 0. |
|
1388 |
||
1389 |
done := false. |
|
1390 |
[done] whileFalse:[ |
|
345 | 1391 |
sum := carry. |
1392 |
(index <= len1) ifTrue:[ |
|
1393 |
sum := sum + (digitByteArray basicAt:index). |
|
1394 |
(index <= len2) ifTrue:[ |
|
1395 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
1396 |
] |
|
1397 |
] ifFalse:[ |
|
1398 |
(index <= len2) ifTrue:[ |
|
1399 |
sum := sum + (otherDigitByteArray basicAt:index) |
|
1400 |
] ifFalse:[ |
|
1401 |
"end reached" |
|
1402 |
done := true |
|
1403 |
] |
|
1404 |
]. |
|
1405 |
(sum >= 16r100) ifTrue:[ |
|
1406 |
carry := 1. |
|
1407 |
sum := sum - 16r100 |
|
1408 |
] ifFalse:[ |
|
1409 |
carry := 0 |
|
1410 |
]. |
|
1411 |
resultDigitByteArray basicAt:index put:sum. |
|
1412 |
index := index + 1 |
|
68 | 1413 |
]. |
1414 |
^ result normalize |
|
1415 |
! |
|
1416 |
||
1417 |
div256 |
|
1418 |
"destructively divide the receiver by 256. |
|
1419 |
private - used for division only" |
|
1420 |
||
1421 |
digitByteArray replaceFrom:1 with:digitByteArray startingAt:2. |
|
77 | 1422 |
digitByteArray at:(digitByteArray size) put:0 |
68 | 1423 |
! |
1424 |
||
1425 |
mul256 |
|
1426 |
"destructively multiply the receiver by 256. |
|
1427 |
private - used for division only" |
|
1428 |
||
1429 |
|newDigits| |
|
1430 |
||
359 | 1431 |
newDigits := ByteArray uninitializedNew:(digitByteArray size + 1). |
68 | 1432 |
newDigits replaceFrom:2 with:digitByteArray startingAt:1. |
1433 |
newDigits at:1 put:0. |
|
1434 |
digitByteArray := newDigits |
|
701 | 1435 |
! |
68 | 1436 |
|
701 | 1437 |
numberOfDigits:nDigits |
1438 |
digitByteArray := ByteArray new:nDigits. |
|
1439 |
sign := 1. |
|
68 | 1440 |
! |
1441 |
||
701 | 1442 |
setDigits:digits |
1443 |
digitByteArray := digits. |
|
1444 |
sign := 1. |
|
68 | 1445 |
! |
1446 |
||
701 | 1447 |
sign:aNumber |
1448 |
sign := aNumber |
|
1 | 1449 |
! ! |
1450 |
||
1451 |
!LargeInteger methodsFor:'testing'! |
|
1452 |
||
701 | 1453 |
even |
1454 |
"return true if the receiver is even" |
|
1 | 1455 |
|
701 | 1456 |
^ (digitByteArray at:1) even |
1 | 1457 |
! |
1458 |
||
68 | 1459 |
negative |
1460 |
"return true, if the receiver is < 0" |
|
1461 |
||
1462 |
^ (sign < 0) |
|
1463 |
! |
|
1464 |
||
1 | 1465 |
odd |
1466 |
"return true if the receiver is odd" |
|
1467 |
||
68 | 1468 |
^ (digitByteArray at:1) even |
1 | 1469 |
! |
1470 |
||
1471 |
positive |
|
1472 |
"return true, if the receiver is >= 0" |
|
1473 |
||
1474 |
^ (sign >= 0) |
|
1475 |
! |
|
1476 |
||
701 | 1477 |
sign |
1478 |
"return the sign of the receiver" |
|
1479 |
||
1480 |
^ sign |
|
1481 |
! |
|
1482 |
||
1 | 1483 |
strictlyPositive |
1484 |
"return true, if the receiver is > 0" |
|
1485 |
||
1486 |
^ (sign > 0) |
|
1487 |
! ! |
|
1488 |
||
1504
9e5ae33dbfe1
oops - bug when subtracting with borrow:
Claus Gittinger <cg@exept.de>
parents:
1408
diff
changeset
|
1489 |
!LargeInteger class methodsFor:'documentation'! |
1 | 1490 |
|
701 | 1491 |
version |
1717
0db1dbfc057d
code cleanup - removed unused/unneeded methods
Claus Gittinger <cg@exept.de>
parents:
1556
diff
changeset
|
1492 |
^ '$Header: /cvs/stx/stx/libbasic/LargeInteger.st,v 1.31 1996-10-12 18:45:52 cg Exp $' |
1 | 1493 |
! ! |