author | Claus Gittinger <cg@exept.de> |
Tue, 13 Oct 1998 14:33:01 +0200 | |
changeset 3874 | a04715e25340 |
parent 3683 | 2eac599d04c1 |
child 3884 | ddda4144e331 |
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 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
20 |
!Integer class methodsFor:'documentation'! |
88 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 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 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
abstract superclass for all integer numbers. |
|
39 |
See details in concrete subclasses LargeInteger and SmallInteger. |
|
212 | 40 |
|
1892 | 41 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
int op float -> float |
1892 | 46 |
|
1295 | 47 |
[Class variables:] |
212 | 48 |
|
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 |
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
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
(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
|
54 |
- I know - I am lazy ;-). Default is 10. |
212 | 55 |
|
1295 | 56 |
|
57 |
[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
|
58 |
Claus Gittinger |
1556 | 59 |
|
60 |
[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
|
61 |
Number |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
62 |
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
|
63 |
Float ShortFloat Fraction FixedPoint |
88 | 64 |
" |
65 |
! ! |
|
1 | 66 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
67 |
!Integer class methodsFor:'instance creation'! |
92 | 68 |
|
3044 | 69 |
byte1:b1 byte2:b2 byte3:b3 byte4:b4 |
70 |
"Squeak compatibility: |
|
71 |
Return an Integer given four value bytes. |
|
72 |
The returned integer is either a Small- or a LargeInteger" |
|
73 |
||
74 |
|t| |
|
75 |
||
76 |
t := b4. |
|
77 |
t := (t bitShift:8) + b3. |
|
78 |
t := (t bitShift:8) + b2. |
|
79 |
^ (t bitShift:8) + b1. |
|
80 |
||
81 |
" |
|
82 |
(Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString |
|
83 |
(Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString |
|
84 |
" |
|
85 |
||
86 |
"Created: 19.10.1997 / 18:08:52 / cg" |
|
87 |
"Modified: 19.10.1997 / 18:09:04 / cg" |
|
88 |
! |
|
89 |
||
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
90 |
fastFromString:aString at:startIndex |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
91 |
"return the next unsigned Integer from the string |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
92 |
as a decimal number, starting at startIndex. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
93 |
No spaces are skipped. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
94 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
numbers." |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
99 |
%{ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
100 |
if (__isString(aString) && __isSmallInteger(startIndex)) { |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
101 |
char *cp = (char *)(__stringVal(aString)); |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
102 |
int idx = __intVal(startIndex) - 1; |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
103 |
INT val; |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
104 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
105 |
if ((unsigned)idx < __stringSize(aString)) { |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
106 |
val = atoi(cp + idx); |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
107 |
RETURN (__MKINT(val)); |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
108 |
} |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
109 |
} |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
110 |
%}. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
111 |
self primitiveFailed. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
112 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
113 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
114 |
Integer fastFromString:'12345' at:1 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
115 |
Integer fastFromString:'12345' at:2 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
116 |
Integer fastFromString:'12345' at:3 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
117 |
Integer fastFromString:'12345' at:4 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
118 |
Integer fastFromString:'12345' at:5 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
119 |
Integer fastFromString:'12345' at:6 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
120 |
Integer fastFromString:'12345' at:0 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
121 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
122 |
Time millisecondsToRun:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
123 |
100000 timesRepeat:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
124 |
Integer readFrom:'12345' |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
125 |
] |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
126 |
] |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
127 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
128 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
129 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
130 |
Time millisecondsToRun:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
131 |
100000 timesRepeat:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
132 |
Integer fastFromString:'12345' at:1 |
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 |
|
92 | 138 |
new:numberOfBytes neg:negative |
139 |
"for ST-80 compatibility: |
|
140 |
Return an empty Integer (uninitialized value) with space for |
|
141 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 142 |
negative specifies if the result should be a negative number. |
143 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 144 |
|
132 | 145 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
159 | 146 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
701 | 147 |
! |
148 |
||
149 |
readFrom:aStringOrStream |
|
150 |
"return the next unsigned Integer from the (character-)stream aStream |
|
151 |
as decimal number. |
|
152 |
||
153 |
NOTICE: |
|
154 |
This behaves different from the default readFrom:, in returning |
|
155 |
0 (instead of raising an error) in case no number can be read. |
|
156 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
157 |
- depending on the upcoming ANSI standard, this may change." |
|
158 |
||
159 |
^ self readFrom:aStringOrStream onError:0 |
|
160 |
||
161 |
" |
|
162 |
Integer readFrom:(ReadStream on:'foobar') |
|
163 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
|
164 |
" |
|
165 |
! |
|
166 |
||
167 |
readFrom:aStringOrStream onError:exceptionBlock |
|
168 |
"return the next Integer from the (character-)stream aStream, |
|
169 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
170 |
Also, all initial whitespace is skipped. |
|
171 |
If the string does not represent a valid integer number, |
|
172 |
return the value of exceptionBlock." |
|
173 |
||
1700 | 174 |
ErrorSignal handle:[:ex | |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
175 |
^ exceptionBlock value |
1700 | 176 |
] do:[ |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
177 |
|str nextChar value negative| |
701 | 178 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
179 |
str := aStringOrStream readStream. |
701 | 180 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
181 |
nextChar := str skipSeparators. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
182 |
(nextChar == $-) ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
183 |
negative := true. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
184 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
185 |
nextChar := str peekOrNil |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
186 |
] ifFalse:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
187 |
negative := false |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
188 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
189 |
(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
|
190 |
" |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
191 |
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
|
192 |
" |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
193 |
^ exceptionBlock value |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
194 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
195 |
value := self readFrom:str radix:10 onError:[^ exceptionBlock value]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
196 |
nextChar := str peekOrNil. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
197 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
198 |
"-xxr<number> is invalid; should be xxr-<val>" |
1700 | 199 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
200 |
negative ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
201 |
'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
|
202 |
negative := false |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
203 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
204 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
205 |
value := self readFrom:str radix:value onError:[^ exceptionBlock value] |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
206 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
207 |
negative ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
208 |
^ value negated |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
209 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
210 |
^ value |
701 | 211 |
]. |
212 |
||
213 |
" |
|
214 |
Integer readFrom:'12345' |
|
215 |
Integer readFrom:'-12345' |
|
216 |
Integer readFrom:'+12345' |
|
217 |
Integer readFrom:'16rFFFF' |
|
218 |
Integer readFrom:'12345.1234' |
|
219 |
Integer readFrom:'foo' |
|
220 |
Integer readFrom:'foo' onError:[0] |
|
221 |
" |
|
222 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
223 |
"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
|
224 |
"Modified: / 14.4.1998 / 19:17:28 / cg" |
701 | 225 |
! |
226 |
||
227 |
readFrom:aStringOrStream radix:radix |
|
228 |
"return the next unsigned Integer from the (character-)stream aStream |
|
229 |
in radix; (assumes that the initial XXr has already been read). |
|
230 |
No whitespace-skipping is done. |
|
231 |
Returns 0 if no number available. |
|
232 |
||
233 |
NOTICE: |
|
234 |
This behaves different from the default readFrom:, in returning |
|
235 |
0 (instead of raising an error) in case no number can be read. |
|
236 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
237 |
- depending on the upcoming ANSI standard, this may change." |
|
238 |
||
239 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
240 |
! |
|
241 |
||
242 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
243 |
"return the next unsigned Integer from the (character-)stream aStream |
|
244 |
in radix; (assumes that the initial XXr has already been read). |
|
245 |
No whitespace-skipping is done. |
|
246 |
Returns the value of exceptionBlock, if no number is available." |
|
247 |
||
248 |
|str nextChar value| |
|
249 |
||
250 |
str := aStringOrStream readStream. |
|
251 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
252 |
nextChar := str peekOrNil. |
701 | 253 |
(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
|
254 |
^ exceptionBlock value |
701 | 255 |
]. |
256 |
||
257 |
value := nextChar digitValue. |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
258 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
259 |
nextChar := str peekOrNil. |
701 | 260 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
261 |
value := value * radix + nextChar digitValue. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
262 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
263 |
nextChar := str peekOrNil. |
701 | 264 |
]. |
265 |
^ value |
|
266 |
||
267 |
" |
|
268 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
|
269 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
270 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
271 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
272 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
273 |
" |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
274 |
|
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
275 |
"Modified: / 14.4.1998 / 19:16:46 / cg" |
701 | 276 |
! ! |
277 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
278 |
!Integer class methodsFor:'constants'! |
701 | 279 |
|
280 |
unity |
|
1555 | 281 |
"return the neutral element for multiplication (1)" |
701 | 282 |
|
283 |
^ 1 |
|
1555 | 284 |
|
285 |
"Modified: 18.7.1996 / 12:26:43 / cg" |
|
701 | 286 |
! |
287 |
||
288 |
zero |
|
1555 | 289 |
"return the neutral element for addition (0)" |
701 | 290 |
|
291 |
^ 0 |
|
1555 | 292 |
|
293 |
"Modified: 18.7.1996 / 12:26:38 / cg" |
|
92 | 294 |
! ! |
295 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
296 |
!Integer class methodsFor:'misc'! |
49 | 297 |
|
298 |
displayRadix:aNumber |
|
54 | 299 |
"being tired of always sending #printStringRadix: in the inspectors, |
49 | 300 |
this allows you to change the default print radix for the displayString |
301 |
method." |
|
302 |
||
303 |
DefaultDisplayRadix := aNumber |
|
132 | 304 |
|
305 |
" |
|
306 |
Integer displayRadix:16. 123456 inspect |
|
307 |
Integer displayRadix:10. 123456 inspect |
|
308 |
" |
|
49 | 309 |
! ! |
310 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
311 |
!Integer methodsFor:'VisualAge compatibility'! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
312 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
313 |
<< aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
314 |
"left shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
315 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
316 |
^ self bitShift:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
317 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
318 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
319 |
1 << 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
320 |
64 << -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
321 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
322 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
323 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
324 |
>> aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
325 |
"right shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
326 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
327 |
^ self bitShift:(aNumber negated) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
328 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
329 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
330 |
1 >> -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
331 |
64 >> 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
332 |
" |
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 |
isBitSet:index |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
336 |
"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
|
337 |
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
|
338 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
339 |
^ (self bitAt:index) ~~ 0 |
1 | 340 |
|
927
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 |
5 isBitSet:3 => true |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
343 |
2r0101 isBitSet:2 => false |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
344 |
2r0101 isBitSet:1 => true |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
345 |
2r0101 isBitSet:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
346 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
347 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
348 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
349 |
setBit:index |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
350 |
"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
|
351 |
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
|
352 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
353 |
^ self bitOr:(1 bitShift:index-1) |
1 | 354 |
|
927
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 |
0 setBit:3 => 4 (2r100) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
357 |
0 setBit:48 => 140737488355328 (2r1000.....000) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
358 |
((0 setBit:99) setBit:100) printStringRadix:2 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
359 |
" |
1 | 360 |
! ! |
361 |
||
69 | 362 |
!Integer methodsFor:'bit operators'! |
363 |
||
364 |
bitAnd:anInteger |
|
357 | 365 |
"return the bitwise-and of the receiver and the argument, anInteger. |
366 |
This is a general and slow implementation, walking over the bytes of |
|
367 |
the receiver and the argument." |
|
69 | 368 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
369 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
370 |
result byte| |
69 | 371 |
|
92 | 372 |
n := (anInteger digitLength) min:(self digitLength). |
373 |
result := self class basicNew numberOfDigits:n. |
|
69 | 374 |
|
375 |
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
|
376 |
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
|
377 |
result digitAt:index put:byte. |
69 | 378 |
]. |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
379 |
(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
|
380 |
^ result compressed |
92 | 381 |
]. |
382 |
^ result |
|
357 | 383 |
|
384 |
" |
|
385 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
|
386 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
387 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
388 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
389 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
390 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
391 |
"Modified: 5.11.1996 / 14:06:26 / cg" |
69 | 392 |
! |
393 |
||
85 | 394 |
bitAt:index |
92 | 395 |
"return the value of the index's bit (index starts at 1). |
396 |
Notice: the result of bitShift: on negative receivers is not |
|
159 | 397 |
defined in the language standard (since the implementation |
398 |
is free to choose any internal representation for integers)" |
|
85 | 399 |
|
400 |
|i "{Class: SmallInteger}"| |
|
401 |
||
402 |
i := index - 1. |
|
403 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
|
92 | 404 |
|
405 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
406 |
1 bitAt:1 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
407 |
1 bitAt:2 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
408 |
1 bitAt:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
409 |
2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
410 |
2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
411 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
412 |
(1 bitShift:1000) bitAt:1000 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
413 |
(1 bitShift:1000) bitAt:1001 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
414 |
(1 bitShift:1000) bitAt:1002 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
415 |
|
92 | 416 |
(1 bitShift:30) bitAt:30 |
417 |
(1 bitShift:30) bitAt:31 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
418 |
(1 bitShift:30) bitAt:32 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
419 |
(1 bitShift:31) bitAt:31 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
420 |
(1 bitShift:31) bitAt:32 |
92 | 421 |
(1 bitShift:31) bitAt:33 |
422 |
(1 bitShift:32) bitAt:32 |
|
423 |
(1 bitShift:32) bitAt:33 |
|
424 |
(1 bitShift:32) bitAt:34 |
|
425 |
(1 bitShift:64) bitAt:64 |
|
426 |
(1 bitShift:64) bitAt:65 |
|
427 |
(1 bitShift:64) bitAt:66 |
|
428 |
" |
|
85 | 429 |
! |
430 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
431 |
bitClear:anInteger |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
432 |
"return the bitwise-and of the receiver and the complement of the argument, anInteger, |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
433 |
returning the receiver with bits of the argument cleared." |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
434 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
435 |
^self bitAnd:(anInteger bitInvert) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
436 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
437 |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
438 |
bitInvert |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
439 |
"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
|
440 |
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
|
441 |
since the digits are stored as absolute value." |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
442 |
|
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
443 |
|n "{ Class: SmallInteger }" |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
444 |
result byte| |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
445 |
|
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
446 |
n := self digitLength. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
447 |
result := self class basicNew numberOfDigits:n. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
448 |
|
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
449 |
1 to:n do:[:index | |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
450 |
byte := self digitAt:index. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
451 |
result digitAt:index put:(byte bitInvert bitAnd:16rFF). |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
452 |
]. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
453 |
(byte == 255 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
454 |
^ result compressed |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
455 |
]. |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
456 |
^ result |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
457 |
! |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
458 |
|
701 | 459 |
bitOr:anInteger |
460 |
"return the bitwise-or 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." |
|
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| |
701 | 466 |
|
467 |
n := (anInteger digitLength) max:(self digitLength). |
|
468 |
result := self class basicNew numberOfDigits:n. |
|
469 |
||
470 |
1 to:n do:[:index | |
|
471 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
|
472 |
result digitAt:index put:byte. |
|
473 |
]. |
|
474 |
"/ no need to normalize - if the operands were correct |
|
475 |
"/ byte == 0 ifTrue:[ |
|
476 |
"/ ^ result normalize |
|
477 |
"/ ]. |
|
478 |
^ result |
|
479 |
! |
|
480 |
||
85 | 481 |
bitShift:shiftCount |
482 |
"return the value of the receiver shifted by shiftCount bits; |
|
483 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 484 |
|
485 |
Notice: the result of bitShift: on negative receivers is not |
|
3638 | 486 |
defined in the language standard (since the implementation |
487 |
is free to choose any internal representation for integers)" |
|
85 | 488 |
|
92 | 489 |
|result |
490 |
prev "{ Class: SmallInteger }" |
|
491 |
next "{ Class: SmallInteger }" |
|
492 |
byte "{ Class: SmallInteger }" |
|
359 | 493 |
byte2 "{ Class: SmallInteger }" |
92 | 494 |
bitShift "{ Class: SmallInteger }" |
495 |
revShift "{ Class: SmallInteger }" |
|
496 |
digitShift "{ Class: SmallInteger }" |
|
497 |
n "{ Class: SmallInteger }" |
|
3044 | 498 |
nn "{ Class: SmallInteger }" |
499 |
nDigits "{ Class: SmallInteger }" | |
|
85 | 500 |
|
501 |
shiftCount > 0 ifTrue:[ |
|
3638 | 502 |
"left shift" |
85 | 503 |
|
3638 | 504 |
digitShift := shiftCount // 8. |
505 |
bitShift := shiftCount \\ 8. |
|
506 |
n := self digitLength. |
|
92 | 507 |
|
3638 | 508 |
" |
509 |
modulo 8 shifts can be done faster ... |
|
510 |
" |
|
511 |
bitShift == 0 ifTrue:[ |
|
512 |
result := self class basicNew numberOfDigits:n + digitShift. |
|
513 |
result sign:self sign. |
|
514 |
result digits replaceFrom:(digitShift + 1) with:self digits. |
|
515 |
" |
|
516 |
no normalize needed, since receiver was already normalized |
|
517 |
" |
|
518 |
^ result |
|
519 |
]. |
|
92 | 520 |
|
3638 | 521 |
" |
522 |
less-than-8 shifts can be done faster ... |
|
523 |
" |
|
524 |
digitShift == 0 ifTrue:[ |
|
525 |
n := n + 1. |
|
526 |
result := self class basicNew numberOfDigits:n. |
|
527 |
result sign:self sign. |
|
528 |
prev := 0. |
|
529 |
1 to:n-1 do:[:index | |
|
530 |
byte := self digitAt:index. |
|
531 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
532 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
533 |
prev := byte bitShift:-8. |
|
534 |
]. |
|
535 |
result digitAt:n put:prev. |
|
536 |
" |
|
537 |
might have stored a 0-byte ... |
|
538 |
" |
|
539 |
prev == 0 ifTrue:[ |
|
540 |
^ result compressed |
|
541 |
]. |
|
542 |
^ result. |
|
543 |
]. |
|
92 | 544 |
|
3638 | 545 |
" |
546 |
slow case ... |
|
547 |
" |
|
548 |
n := n + digitShift + 1. |
|
549 |
result := self class basicNew numberOfDigits:n. |
|
550 |
result sign:self sign. |
|
551 |
byte := self digitAt:1. |
|
552 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
|
553 |
result digitAt:(digitShift + 1) put:byte. |
|
554 |
revShift := -8 + bitShift. |
|
555 |
nDigits := self digitLength. |
|
556 |
2 to:nDigits do:[:index | |
|
557 |
byte := self digitAt:index. |
|
558 |
byte2 := self digitAt:index-1. |
|
559 |
byte := byte bitShift:bitShift. |
|
560 |
byte2 := byte2 bitShift:revShift. |
|
561 |
byte := (byte bitOr:byte2) bitAnd:16rFF. |
|
562 |
result digitAt:(index + digitShift) put:byte. |
|
563 |
]. |
|
564 |
byte2 := self digitAt:nDigits. |
|
565 |
byte2 := (byte2 bitShift:revShift) bitAnd:16rFF. |
|
566 |
result digitAt:(nDigits + digitShift + 1) put:byte2. |
|
567 |
" |
|
568 |
might have stored a 0-byte ... |
|
569 |
" |
|
570 |
byte2 == 0 ifTrue:[ |
|
571 |
^ result compressed |
|
572 |
]. |
|
573 |
^ result |
|
92 | 574 |
]. |
575 |
||
576 |
shiftCount < 0 ifTrue:[ |
|
3638 | 577 |
"right shift" |
85 | 578 |
|
3638 | 579 |
digitShift := shiftCount negated // 8. |
580 |
bitShift := shiftCount negated \\ 8. |
|
581 |
n := self digitLength. |
|
92 | 582 |
|
3638 | 583 |
digitShift >= n ifTrue:[ |
584 |
^ 0 |
|
585 |
]. |
|
92 | 586 |
|
3638 | 587 |
" |
588 |
modulo 8 shifts can be done faster ... |
|
589 |
" |
|
590 |
bitShift == 0 ifTrue:[ |
|
591 |
n := n-digitShift. |
|
592 |
result := self class basicNew numberOfDigits:n. |
|
593 |
result sign:self sign. |
|
594 |
result digits replaceFrom:1 to:n with:self digits startingAt:(digitShift + 1) . |
|
595 |
n <= SmallInteger maxBytes ifTrue:[ |
|
596 |
^ result compressed |
|
597 |
]. |
|
598 |
^ result |
|
599 |
]. |
|
85 | 600 |
|
3638 | 601 |
" |
602 |
less-than-8 shifts can be done faster ... |
|
603 |
" |
|
604 |
digitShift == 0 ifTrue:[ |
|
605 |
result := self class basicNew numberOfDigits:n. |
|
606 |
result sign:self sign. |
|
607 |
prev := 0. |
|
608 |
bitShift := bitShift negated. |
|
609 |
revShift := 8 + bitShift. |
|
610 |
n to:1 by:-1 do:[:index | |
|
611 |
byte := self digitAt:index. |
|
612 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
613 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
614 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
615 |
prev := next. |
|
616 |
]. |
|
617 |
(n <= 5) ifTrue:[ |
|
618 |
^ result compressed |
|
619 |
]. |
|
620 |
^ result |
|
621 |
]. |
|
92 | 622 |
|
3638 | 623 |
" |
624 |
slow case ... |
|
625 |
" |
|
626 |
nn := n-digitShift. |
|
627 |
result := self class basicNew numberOfDigits:nn. |
|
628 |
result sign:self sign. |
|
85 | 629 |
|
3638 | 630 |
prev := 0. |
631 |
bitShift := bitShift negated. |
|
632 |
revShift := 8 + bitShift. |
|
633 |
n to:(1 + digitShift) by:-1 do:[:index | |
|
634 |
byte := self digitAt:index. |
|
635 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
636 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
637 |
result digitAt:(index - digitShift) put:byte. |
|
638 |
prev := next. |
|
639 |
]. |
|
640 |
"the last stored byte ..." |
|
641 |
^ result compressed |
|
85 | 642 |
]. |
92 | 643 |
|
644 |
^ self "no shift" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
645 |
|
3135 | 646 |
"Modified: / 18.12.1997 / 17:17:29 / stefan" |
3638 | 647 |
"Modified: / 8.7.1998 / 12:45:24 / cg" |
701 | 648 |
! |
1 | 649 |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
650 |
bitTest:anInteger |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
651 |
"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
|
652 |
is non-0, false otherwise. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
653 |
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
|
654 |
the receiver and the argument." |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
655 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
656 |
|n "{ Class: SmallInteger }" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
657 |
result byte| |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
658 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
659 |
n := (anInteger digitLength) min:(self digitLength). |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
660 |
result := self class basicNew numberOfDigits:n. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
661 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
662 |
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
|
663 |
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
|
664 |
byte ~~ 0 ifTrue:[^ true]. |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
665 |
]. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
666 |
^ false |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
667 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
668 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
669 |
16r112233445566778899 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
670 |
16r112233445566778800 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
671 |
16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
672 |
16r112233445566778899 bitTest:16rFF0000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
673 |
16r112233445566778899 bitTest:16r00000000000000FFFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
674 |
16r1234567800000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
675 |
16r8765432100000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
676 |
16r12345678 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
677 |
16r87654321 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
678 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
679 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
680 |
"Modified: 16.5.1996 / 16:29:50 / cg" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
681 |
! |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
682 |
|
701 | 683 |
bitXor:anInteger |
684 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
685 |
This is a general and slow implementation, walking over the bytes of |
|
686 |
the receiver and the argument." |
|
687 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
688 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
689 |
result byte| |
701 | 690 |
|
691 |
n := (anInteger digitLength) max:(self digitLength). |
|
692 |
result := self class basicNew numberOfDigits:n. |
|
1 | 693 |
|
701 | 694 |
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
|
695 |
byte := (anInteger digitAt:index) bitXor:(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
|
696 |
result digitAt:index put:byte. |
701 | 697 |
]. |
698 |
byte == 0 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
|
699 |
^ result compressed |
701 | 700 |
]. |
701 |
^ result |
|
1 | 702 |
|
701 | 703 |
" |
704 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
|
705 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
|
706 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
707 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
|
708 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
709 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
710 |
"Modified: 5.11.1996 / 14:06:40 / cg" |
1 | 711 |
! |
712 |
||
3683 | 713 |
clearBit:index |
714 |
"return a new number where the specified bit is off. |
|
715 |
Bits are counted from 1 starting with the least significant." |
|
716 |
||
717 |
|n "{ Class: SmallInteger }" |
|
718 |
byteIndex "{ Class: SmallInteger }" |
|
719 |
bitIndex "{ Class: SmallInteger }" |
|
720 |
result byte| |
|
721 |
||
722 |
byteIndex := ((index - 1) // 8) + 1. |
|
723 |
n := self digitLength. |
|
724 |
byteIndex > n ifTrue:[ |
|
725 |
^ self |
|
726 |
]. |
|
727 |
||
728 |
result := self copy. |
|
729 |
bitIndex := ((index - 1) \\ 8) + 1. |
|
730 |
byte := (result digitAt:byteIndex) clearBit:bitIndex. |
|
731 |
result digitAt:byteIndex put:byte. |
|
732 |
(byte == 0 or:[n == byteIndex and:[n <= SmallInteger maxBytes]]) ifTrue:[ |
|
733 |
^ result compressed |
|
734 |
]. |
|
735 |
^ result |
|
736 |
||
737 |
" |
|
738 |
3111111111 clearBit:1 |
|
739 |
" |
|
740 |
||
741 |
"Modified: / 28.7.1998 / 18:35:50 / cg" |
|
742 |
! |
|
743 |
||
701 | 744 |
highBit |
745 |
"return the bitIndex of the highest bit set. The returned bitIndex |
|
746 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
747 |
||
748 |
|byteNr highByte| |
|
749 |
||
750 |
byteNr := self digitLength. |
|
751 |
highByte := self digitAt:byteNr. |
|
752 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 753 |
|
701 | 754 |
" |
755 |
1 highBit |
|
756 |
(1 bitShift:1) highBit |
|
757 |
(1 bitShift:30) highBit |
|
758 |
(1 bitShift:31) highBit |
|
759 |
(1 bitShift:32) highBit |
|
760 |
(1 bitShift:33) highBit |
|
761 |
(1 bitShift:64) highBit |
|
762 |
(1 bitShift:1000) highBit |
|
763 |
((1 bitShift:64)-1) highBit |
|
764 |
" |
|
2429 | 765 |
! |
766 |
||
767 |
lowBit |
|
768 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
769 |
starts at 1 for the least significant bit. Returns -1 if no bit is set." |
|
770 |
||
771 |
|maxBytes "{ Class: SmallInteger }" |
|
772 |
bitIndex "{ Class: SmallInteger }" |
|
773 |
byte| |
|
774 |
||
775 |
maxBytes := self digitLength. |
|
776 |
bitIndex := 0. |
|
777 |
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
|
778 |
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
|
779 |
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
|
780 |
^ 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
|
781 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
782 |
bitIndex := bitIndex + 8 |
2429 | 783 |
]. |
784 |
^ -1 |
|
785 |
||
786 |
" |
|
787 |
0 lowBit |
|
788 |
1 lowBit |
|
789 |
(1 bitShift:1) lowBit |
|
790 |
(1 bitShift:1) highBit |
|
791 |
(1 bitShift:30) lowBit |
|
792 |
(1 bitShift:30) highBit |
|
793 |
(1 bitShift:31) lowBit |
|
794 |
(1 bitShift:31) highBit |
|
795 |
(1 bitShift:32) lowBit |
|
796 |
(1 bitShift:32) highBit |
|
797 |
(1 bitShift:33) lowBit |
|
798 |
(1 bitShift:33) highBit |
|
799 |
(1 bitShift:64) lowBit |
|
800 |
(1 bitShift:64) highBit |
|
801 |
(1 bitShift:1000) lowBit |
|
802 |
(1 bitShift:1000) highBit |
|
803 |
((1 bitShift:64)-1) lowBit |
|
804 |
((1 bitShift:64)-1) highBit |
|
805 |
" |
|
806 |
||
807 |
"Modified: 1.3.1997 / 16:54:23 / cg" |
|
701 | 808 |
! ! |
809 |
||
810 |
!Integer methodsFor:'coercing and converting'! |
|
811 |
||
1888 | 812 |
asFixedPoint |
813 |
"return the receiver as a fixedPoint number" |
|
814 |
||
815 |
^ FixedPoint numerator:self denominator:1 scale:1 |
|
816 |
||
817 |
" |
|
818 |
100 asFixedPoint |
|
819 |
100 asFixedPoint + 0.1 asFixedPoint |
|
820 |
" |
|
821 |
||
822 |
"Modified: 5.11.1996 / 15:13:17 / cg" |
|
823 |
! |
|
824 |
||
2141 | 825 |
asFixedPoint:scale |
826 |
"return the receiver as fixedPoint number, with the given number |
|
827 |
of post-decimal-point digits." |
|
828 |
||
829 |
^ FixedPoint numerator:self denominator:1 scale:scale |
|
830 |
||
831 |
" |
|
832 |
100 asFixedPoint:2 |
|
833 |
100 asFixedPoint + (0.1 asFixedPoint:2) |
|
834 |
" |
|
835 |
||
836 |
"Modified: 10.1.1997 / 20:00:08 / cg" |
|
837 |
! |
|
838 |
||
701 | 839 |
asFraction |
840 |
"return a Fraction with same value as receiver" |
|
841 |
||
2794
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
842 |
^ 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
|
843 |
|
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
844 |
"Modified: 28.7.1997 / 19:26:06 / cg" |
1 | 845 |
! |
846 |
||
701 | 847 |
asInteger |
1 | 848 |
"return the receiver truncated towards zero - |
849 |
for integers this is self" |
|
850 |
||
851 |
^ self |
|
1337 | 852 |
! |
853 |
||
854 |
signExtendedByteValue |
|
855 |
"return a smallInteger from sign-extending the 8'th bit. |
|
856 |
May be useful for communication interfaces" |
|
857 |
||
858 |
^ (self bitAnd:16rFF) signExtendedByteValue |
|
859 |
||
860 |
"Created: 7.5.1996 / 09:31:52 / cg" |
|
861 |
! |
|
862 |
||
863 |
signExtendedShortValue |
|
864 |
"return a smallInteger from sign-extending the 16'th bit. |
|
865 |
May be useful for communication interfaces" |
|
866 |
||
867 |
^ (self bitAnd:16rFFFF) signExtendedShortValue |
|
868 |
||
869 |
"Modified: 7.5.1996 / 09:31:57 / cg" |
|
1 | 870 |
! ! |
871 |
||
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
872 |
!Integer methodsFor:'comparing'! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
873 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
874 |
hash |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
875 |
"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
|
876 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
877 |
^ self bitAnd:16r3FFFFFFF. |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
878 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
879 |
" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
880 |
-20000000000000 hash |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
881 |
20000000000000 hash |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
882 |
" |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
883 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
884 |
"Created: / 14.11.1996 / 12:12:27 / cg" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
885 |
"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
|
886 |
! ! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
887 |
|
701 | 888 |
!Integer methodsFor:'double dispatching'! |
889 |
||
890 |
differenceFromFraction:aFraction |
|
891 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 892 |
|
701 | 893 |
|d| |
1 | 894 |
|
701 | 895 |
d := aFraction denominator. |
896 |
^ (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
|
897 |
denominator:d) |
2793 | 898 |
|
899 |
"Modified: 28.7.1997 / 19:08:30 / cg" |
|
1 | 900 |
! |
901 |
||
701 | 902 |
productFromFraction:aFraction |
903 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 904 |
|
701 | 905 |
^ (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
|
906 |
denominator:aFraction denominator) |
2793 | 907 |
|
908 |
"Modified: 28.7.1997 / 19:08:27 / cg" |
|
1 | 909 |
! |
910 |
||
701 | 911 |
quotientFromFraction:aFraction |
912 |
"sent when a fraction does not know how to divide the receiver, an integer" |
|
913 |
||
2793 | 914 |
^ 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
|
915 |
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
|
916 |
denominator:(self * aFraction denominator) |
2793 | 917 |
|
918 |
"Modified: 28.7.1997 / 19:08:23 / cg" |
|
701 | 919 |
! |
1 | 920 |
|
701 | 921 |
sumFromFraction:aFraction |
922 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
923 |
||
924 |
|d| |
|
925 |
||
926 |
d := aFraction denominator. |
|
2793 | 927 |
^ 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
|
928 |
denominator:d |
2793 | 929 |
|
930 |
"Modified: 28.7.1997 / 19:08:11 / cg" |
|
1 | 931 |
! ! |
932 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
933 |
!Integer methodsFor:'helpers'! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
934 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
935 |
gcd_helper:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
936 |
"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
|
937 |
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
|
938 |
larger than the arg." |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
939 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
940 |
| selfAbs argAbs selfLowBit argLowBit shift t | |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
941 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
942 |
selfAbs := self. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
943 |
argAbs := anInteger. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
944 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
945 |
selfLowBit := selfAbs lowBit - 1. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
946 |
argLowBit := argAbs lowBit - 1. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
947 |
shift := selfLowBit min:argLowBit. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
948 |
argAbs := argAbs bitShift:(argLowBit negated). |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
949 |
[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
|
950 |
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
|
951 |
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
|
952 |
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
|
953 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
954 |
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
|
955 |
selfLowBit := selfAbs lowBit - 1. |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
956 |
]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
957 |
^ argAbs bitShift:shift |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
958 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
959 |
"Created: 1.3.1997 / 16:38:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
960 |
"Modified: 1.3.1997 / 16:45:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
961 |
! ! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
962 |
|
1 | 963 |
!Integer methodsFor:'misc math'! |
964 |
||
701 | 965 |
acker:n |
1560 | 966 |
"return the value of acker(self, n). |
967 |
;-) Do not try with receivers > 3" |
|
701 | 968 |
|
969 |
(self == 0) ifTrue:[^ n + 1]. |
|
970 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
971 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
972 |
||
973 |
" |
|
974 |
3 acker:2 |
|
975 |
" |
|
1560 | 976 |
|
977 |
"Modified: 18.7.1996 / 13:08:16 / cg" |
|
701 | 978 |
! |
979 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
980 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
981 |
"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
|
982 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
983 |
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
|
984 |
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
|
985 |
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
|
986 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
987 |
^ 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
|
988 |
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
|
989 |
with:(self \\ aNumber) |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
990 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
991 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
992 |
10 divMod:3 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
993 |
100 factorial divMod:103 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
994 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
995 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
996 |
"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
|
997 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
998 |
|
1 | 999 |
factorial |
1560 | 1000 |
"return fac(self) (i.e. 1*2*3...*self) using an iterative algorithm. |
1001 |
This is slightly faster than the recursive algorithm, and does not |
|
1002 |
suffer from stack overflow problems (with big receivers)" |
|
1003 |
||
2745 | 1004 |
|p i| |
1560 | 1005 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1006 |
(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
|
1007 |
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
|
1008 |
"/ |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1009 |
"/ 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
|
1010 |
"/ |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1011 |
^ self class |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1012 |
raise:#domainErrorSignal |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1013 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1014 |
selector:#factorial |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1015 |
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
|
1016 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1017 |
^ 1 |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1018 |
]. |
1560 | 1019 |
p := 2. |
2745 | 1020 |
i := 3. |
1021 |
[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
|
1022 |
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
|
1023 |
i := i + 1. |
1560 | 1024 |
]. |
1025 |
^ p |
|
1026 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1027 |
" |
1560 | 1028 |
10 factorial |
1029 |
1000 factorial |
|
1030 |
10000 factorial |
|
1031 |
10000 factorialR |
|
1032 |
||
1033 |
Time millisecondsToRun:[2000 factorial] |
|
1034 |
Time millisecondsToRun:[2000 factorialR] |
|
2745 | 1035 |
-1 factorial |
1560 | 1036 |
" |
1037 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1038 |
"Modified: 29.10.1996 / 20:58:43 / cg" |
1560 | 1039 |
! |
1040 |
||
1041 |
factorialR |
|
1042 |
"return fac(self) (i.e. 1*2*3...*self) using a recursive algorithm." |
|
1 | 1043 |
|
356 | 1044 |
(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
|
1045 |
^ self * (self - 1) factorialR |
1 | 1046 |
]. |
356 | 1047 |
^ 1 |
212 | 1048 |
|
1049 |
" |
|
1560 | 1050 |
10 factorialR |
1051 |
1000 factorialR |
|
212 | 1052 |
" |
1560 | 1053 |
|
1054 |
"Created: 18.7.1996 / 12:48:36 / cg" |
|
1 | 1055 |
! |
1056 |
||
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1057 |
fib |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1058 |
"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
|
1059 |
(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
|
1060 |
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
|
1061 |
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
|
1062 |
... 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
|
1063 |
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
|
1064 |
|
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1065 |
|a1 a2 an| |
701 | 1066 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1067 |
a1 := 1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1068 |
a2 := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1069 |
an := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1070 |
self timesRepeat:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1071 |
an := a1 + a2. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1072 |
a2 := a1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1073 |
a1 := an |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1074 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1075 |
^ an |
701 | 1076 |
|
1077 |
" |
|
1422 | 1078 |
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
|
1079 |
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
|
1080 |
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
|
1081 |
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
|
1082 |
Transcript showCR:(Time millisecondsToRun:[1500 fib]) |
701 | 1083 |
" |
1084 |
! |
|
1085 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1086 |
gcd:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1087 |
"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
|
1088 |
Euclids & Knuths algorithm." |
1 | 1089 |
|
3060 | 1090 |
|selfAbs argAbs t| |
1 | 1091 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1092 |
selfAbs := self abs. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1093 |
argAbs := anInteger abs. |
1888 | 1094 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1095 |
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
|
1096 |
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
|
1097 |
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
|
1098 |
argAbs := t. |
1 | 1099 |
]. |
52 | 1100 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1101 |
argAbs = 0 ifTrue: [^ selfAbs]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1102 |
selfAbs := selfAbs \\ argAbs. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1103 |
selfAbs = 0 ifTrue:[^ argAbs]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1104 |
^ argAbs gcd_helper:selfAbs |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1105 |
|
3060 | 1106 |
" |
1107 |
3141589999999999 gcd:1000000000000000 |
|
1108 |
" |
|
1109 |
||
1110 |
"Modified: / 25.10.1997 / 16:08:45 / cg" |
|
1 | 1111 |
! |
1112 |
||
1113 |
lcm:anInteger |
|
52 | 1114 |
"return the least common multiple (using gcd:)" |
1115 |
||
1116 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
1117 |
||
92 | 1118 |
" |
1119 |
65 lcm:15 |
|
1120 |
3 lcm:15 |
|
1121 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1122 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1123 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1124 |
recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1125 |
"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
|
1126 |
WARNING: |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1127 |
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
|
1128 |
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
|
1129 |
(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
|
1130 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1131 |
(self > 1) ifTrue:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1132 |
^ (self - 1) recursiveFib + (self - 2) recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1133 |
]. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1134 |
^ 1 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1135 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1136 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1137 |
30 fib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1138 |
30 recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1139 |
Transcript showCR:(Time millisecondsToRun:[30 recursiveFib]) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1140 |
Transcript showCR:(Time millisecondsToRun:[30 fib]) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1141 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1142 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1143 |
"Modified: 18.7.1996 / 12:47:19 / cg" |
1 | 1144 |
! ! |
1145 |
||
1146 |
!Integer methodsFor:'printing & storing'! |
|
1147 |
||
49 | 1148 |
displayString |
1149 |
"return a string for displaying in a view (as in inspector). |
|
1150 |
The output radix is usually 10, but can be changed by setting |
|
1151 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
1152 |
||
54 | 1153 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
159 | 1154 |
^ self printString |
54 | 1155 |
]. |
49 | 1156 |
^ self radixPrintStringRadix:DefaultDisplayRadix |
1157 |
||
92 | 1158 |
" |
1159 |
Integer displayRadix:16. 12345 |
|
1160 |
Integer displayRadix:2. 12345 |
|
1161 |
Integer displayRadix:10. 12345 |
|
1162 |
" |
|
49 | 1163 |
! |
1164 |
||
701 | 1165 |
errorPrintHex |
1166 |
"print the receiver as a hex number on the standard error stream" |
|
1167 |
||
1168 |
(self printStringRadix:16) errorPrint |
|
1169 |
! |
|
1 | 1170 |
|
701 | 1171 |
hexPrintString |
1172 |
"return a hex string representation of the receiver" |
|
1173 |
||
1174 |
^ self printStringRadix:16 |
|
3874 | 1175 |
|
1176 |
" |
|
1177 |
127 hexPrintString |
|
1178 |
-1 hexPrintString |
|
1179 |
" |
|
1180 |
||
1181 |
"Modified: / 11.10.1998 / 01:15:43 / cg" |
|
701 | 1182 |
! |
1183 |
||
2408 | 1184 |
hexPrintString:size |
1185 |
"return a hex string representation of the receiver, |
|
1186 |
padded to size characters" |
|
1187 |
||
1188 |
^ (self printStringRadix:16) leftPaddedTo:size with:$0 |
|
1189 |
||
1190 |
"Created: 18.2.1997 / 13:32:33 / cg" |
|
1191 |
! |
|
1192 |
||
701 | 1193 |
printHex |
1194 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 1195 |
|
701 | 1196 |
(self printStringRadix:16) print |
1197 |
! |
|
1198 |
||
1199 |
printOn:aStream |
|
1200 |
"append a printed description of the receiver to aStream" |
|
1201 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1202 |
self printOn:aStream base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1203 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1204 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
701 | 1205 |
! |
1206 |
||
1431
243d233bffa5
printOn:radix: -> printOn:base: (ST-80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1422
diff
changeset
|
1207 |
printOn:aStream base:base |
1 | 1208 |
"return a string representation of the receiver in the specified |
1209 |
radix (without the initial XXr)" |
|
1210 |
||
3136
91dbf1ea13a6
print in junks of 6 digits (instead of 8 digits)
Claus Gittinger <cg@exept.de>
parents:
3135
diff
changeset
|
1211 |
|num s divMod mod r r2 r4 nD numN| |
1 | 1212 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1213 |
(base between:2 and:36) ifFalse:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1214 |
self error:'invalid base'. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1215 |
^ self printOn:aStream base:10 |
54 | 1216 |
]. |
1217 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1218 |
(self = 0) ifTrue:[aStream nextPut:$0. ^ self]. |
1 | 1219 |
(self < 0) ifTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1220 |
aStream nextPut:$- . |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1221 |
num := self negated. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1222 |
] ifFalse:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1223 |
num := self. |
1 | 1224 |
]. |
92 | 1225 |
|
1226 |
" |
|
1227 |
claus: changed it from a recursive algorithm; |
|
1228 |
(it used to trigger stack-overflow exceptions when printing |
|
1229 |
3000 factorial ...) |
|
1230 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1231 |
"/ leftPart := num // base. |
92 | 1232 |
"/ (leftPart ~= 0) ifTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1233 |
"/ leftPart printOn:aStream base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1234 |
"/ aStream nextPut:(Character digitValue:(num \\ base). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1235 |
"/ ^ self |
92 | 1236 |
"/ ]. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1237 |
"/ aStream nextPut:(Character digitValue:num). |
2951 | 1238 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1239 |
"/ 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
|
1240 |
"/ 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
|
1241 |
"/ 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
|
1242 |
"/ 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
|
1243 |
"/ 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
|
1244 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1245 |
r2 := base*base. "/ radix^2 |
3138 | 1246 |
r4 := r2*r2. "/ radix^4 |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1247 |
base <= 10 ifTrue:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1248 |
r := r4*r2. "/ radix^6 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1249 |
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
|
1250 |
] ifFalse:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1251 |
r := r4*base. "/ radix^5 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1252 |
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
|
1253 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1254 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1255 |
"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
|
1256 |
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
|
1257 |
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
|
1258 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1259 |
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
|
1260 |
|
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 |
[num >= r] whileTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1262 |
"/ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1263 |
"/ chop off nD digits. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1264 |
"/ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1265 |
divMod := num divMod:r. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1266 |
num := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1267 |
numN := divMod at:2. |
2951 | 1268 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1269 |
"/ process them |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1270 |
nD timesRepeat:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1271 |
divMod := numN divMod:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1272 |
numN := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1273 |
mod := divMod at:2. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1274 |
s nextPut:(Character digitValue:mod). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1275 |
]. |
2951 | 1276 |
]. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1277 |
|
92 | 1278 |
[num ~= 0] whileTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1279 |
divMod := num divMod:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1280 |
num := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1281 |
mod := divMod at:2. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1282 |
s nextPut:(Character digitValue:mod). |
1 | 1283 |
]. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1284 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1285 |
aStream nextPutAll:(s contents reverse). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1286 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1287 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1288 |
3000 factorial printOn:Transcript base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1289 |
10 printOn:Transcript base:3 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1290 |
31 printOn:Transcript base:3 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1291 |
-20 printOn:Transcript base:16 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1292 |
-20 printOn:Transcript base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1293 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1294 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1295 |
"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
|
1296 |
"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
|
1297 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1298 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1299 |
printOn:aStream radix:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1300 |
"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
|
1301 |
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
|
1302 |
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
|
1303 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1304 |
self printOn:aStream base:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1305 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1306 |
"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
|
1307 |
"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
|
1308 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1309 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1310 |
printStringRadix:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1311 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1312 |
|s| |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1313 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1314 |
s := WriteStream on:(String new:20). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1315 |
self printOn:s base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1316 |
^ s contents |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1317 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1318 |
"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
|
1319 |
"Modified: / 20.1.1998 / 14:10:54 / stefan" |
1 | 1320 |
! |
1321 |
||
1322 |
printStringRadix:aRadix size:sz fill:fillCharacter |
|
1323 |
"return a string representation of the receiver in the specified |
|
1324 |
radix. The string is padded on the left with fillCharacter to make |
|
1325 |
its size as specified in sz." |
|
1326 |
||
2951 | 1327 |
|s actualSize| |
1 | 1328 |
|
1329 |
s := self printStringRadix:aRadix. |
|
2951 | 1330 |
actualSize := s size. |
1331 |
actualSize < sz ifTrue:[ |
|
1332 |
s := ((String new:(sz - actualSize)) atAllPut:fillCharacter) , s |
|
1 | 1333 |
]. |
1334 |
^ s |
|
1335 |
||
92 | 1336 |
" |
1337 |
1024 printStringRadix:16 size:4 fill:$0 |
|
1338 |
1024 printStringRadix:2 size:16 fill:$. |
|
1339 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
1340 |
" |
|
216 | 1341 |
! |
1342 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1343 |
radixPrintStringRadix:base |
701 | 1344 |
"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
|
1345 |
base; prepend XXr to the string" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1346 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1347 |
|s| |
701 | 1348 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1349 |
s := WriteStream on:(String new:20). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1350 |
base printOn:s. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1351 |
s nextPut:$r. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1352 |
self printOn:s base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1353 |
^ s contents |
216 | 1354 |
|
701 | 1355 |
" |
1356 |
31 radixPrintStringRadix:2 |
|
1357 |
31 radixPrintStringRadix:3 |
|
1358 |
31 radixPrintStringRadix:36 |
|
1359 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1360 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1361 |
"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
|
1362 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
1 | 1363 |
! ! |
1364 |
||
701 | 1365 |
!Integer methodsFor:'queries'! |
202 | 1366 |
|
701 | 1367 |
digitAt:n |
1368 |
"return the n-th byte of the binary representation. |
|
1369 |
This is a very stupid implementation, and should be redefined in |
|
1370 |
concrete subclasses." |
|
202 | 1371 |
|
701 | 1372 |
|num count| |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1373 |
|
701 | 1374 |
num := self. |
1375 |
count := n. |
|
1376 |
[count > 1] whileTrue:[ |
|
1377 |
num := num // 256. |
|
1378 |
count := count - 1 |
|
202 | 1379 |
]. |
701 | 1380 |
^ num \\ 256 |
159 | 1381 |
|
1382 |
" |
|
701 | 1383 |
16r44332211 digitAt:1 |
1384 |
16r44332211 digitAt:2 |
|
1385 |
16r44332211 digitAt:3 |
|
1386 |
16r44332211 digitAt:4 |
|
1387 |
16r44332211 digitAt:5 |
|
1388 |
16r00332211 digitAt:4 |
|
1389 |
16r00332211 digitAt:5 |
|
159 | 1390 |
" |
1 | 1391 |
! |
1392 |
||
701 | 1393 |
digitLength |
1394 |
"return the number of bytes needed for the binary representation |
|
2816 | 1395 |
of the receiver. |
1396 |
This method is redefined in concrete classes - the fallback here is |
|
1397 |
never really used." |
|
701 | 1398 |
|
1399 |
^ (self log:256) ceiling asInteger |
|
2816 | 1400 |
|
1401 |
"Modified: 31.7.1997 / 13:19:06 / cg" |
|
701 | 1402 |
! |
1403 |
||
1404 |
isInteger |
|
1405 |
"return true, if the receiver is some kind of integer number" |
|
1406 |
||
1407 |
^ true |
|
3036 | 1408 |
! |
1409 |
||
1410 |
isPowerOfTwo |
|
1411 |
"return true, if the receiver is a power of 2" |
|
1412 |
||
1413 |
^ (self bitAnd:(self - 1)) == 0 |
|
1414 |
||
1415 |
" |
|
1416 |
0 isPowerOfTwo |
|
1417 |
1 isPowerOfTwo |
|
1418 |
2 isPowerOfTwo |
|
1419 |
3 isPowerOfTwo |
|
1420 |
4 isPowerOfTwo |
|
1421 |
16r8000000000000000 isPowerOfTwo |
|
1422 |
16r8000000000000001 isPowerOfTwo |
|
1423 |
" |
|
1424 |
||
1425 |
"Modified: 15.10.1997 / 18:43:49 / cg" |
|
701 | 1426 |
! ! |
202 | 1427 |
|
2458 | 1428 |
!Integer methodsFor:'special bit operators'! |
1429 |
||
1430 |
bitAnd_32:anInteger |
|
1431 |
"return a C-semantic 32bit locical-and of the receiver and |
|
1432 |
the argument. Both must be either Small- or LargeIntegers. |
|
1433 |
This (nonstandard) specialized method is provided to allow simulation of |
|
1434 |
and operations with C semantics." |
|
1435 |
||
1436 |
%{ /* NOCONTEXT */ |
|
1437 |
int val1, val2, rslt; |
|
1438 |
||
1439 |
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
|
1440 |
val1 = __intVal(self); |
2458 | 1441 |
} 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
|
1442 |
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
|
1443 |
if (!val1) goto bad; |
2458 | 1444 |
} 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
|
1445 |
goto bad; |
2458 | 1446 |
} |
1447 |
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
|
1448 |
val2 = __intVal(anInteger); |
2458 | 1449 |
} 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
|
1450 |
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
|
1451 |
if (!val2) goto bad; |
2458 | 1452 |
} 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
|
1453 |
goto bad; |
2458 | 1454 |
} |
1455 |
rslt = val1 & val2; |
|
1456 |
RETURN(__MKINT(rslt)); |
|
1457 |
||
1458 |
bad: ; |
|
1459 |
%}. |
|
1460 |
self primitiveFailed. |
|
1461 |
! |
|
1462 |
||
1463 |
bitOr_32:anInteger |
|
1464 |
"return a C-semantic 32bit locical-or of the receiver and |
|
1465 |
the argument. Both must be either Small- or LargeIntegers. |
|
1466 |
This (nonstandard) specialized method is provided to allow simulation of |
|
1467 |
and operations with C semantics." |
|
1468 |
||
1469 |
%{ /* NOCONTEXT */ |
|
1470 |
int val1, val2, rslt; |
|
1471 |
||
1472 |
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
|
1473 |
val1 = __intVal(self); |
2458 | 1474 |
} 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
|
1475 |
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
|
1476 |
if (!val1) goto bad; |
2458 | 1477 |
} 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
|
1478 |
goto bad; |
2458 | 1479 |
} |
1480 |
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
|
1481 |
val2 = __intVal(anInteger); |
2458 | 1482 |
} 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
|
1483 |
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
|
1484 |
if (!val2) goto bad; |
2458 | 1485 |
} 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
|
1486 |
goto bad; |
2458 | 1487 |
} |
1488 |
rslt = val1 | val2; |
|
1489 |
RETURN(__MKINT(rslt)); |
|
1490 |
||
1491 |
bad: ; |
|
1492 |
%}. |
|
1493 |
self primitiveFailed. |
|
1494 |
! ! |
|
1495 |
||
701 | 1496 |
!Integer methodsFor:'truncation & rounding'! |
1497 |
||
1498 |
ceiling |
|
1558 | 1499 |
"return the smallest integer which is larger or equal to the receiver. |
1500 |
For integers, this is the receiver itself." |
|
345 | 1501 |
|
701 | 1502 |
^ self |
1558 | 1503 |
|
1504 |
"Modified: 18.7.1996 / 12:44:06 / cg" |
|
701 | 1505 |
! |
345 | 1506 |
|
701 | 1507 |
compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1508 |
"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
|
1509 |
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
|
1510 |
zeros removed. This method is redefined in LargeInteger." |
701 | 1511 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1512 |
^ self |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1513 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1514 |
"Modified: 5.11.1996 / 14:07:41 / cg" |
202 | 1515 |
! |
1516 |
||
701 | 1517 |
floor |
1558 | 1518 |
"return the largest integer which is smaller or equal to the receiver. |
1519 |
For integers, this is the receiver itself." |
|
701 | 1520 |
|
1521 |
^ self |
|
1558 | 1522 |
|
1523 |
"Modified: 18.7.1996 / 12:44:00 / cg" |
|
701 | 1524 |
! |
202 | 1525 |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1526 |
fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1527 |
"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
|
1528 |
(i.e. the receiver minus its truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1529 |
Since integers have no fraction, return 0 here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1530 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1531 |
^ 0 |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1532 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1533 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1534 |
1234.56789 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1535 |
1.2345e6 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1536 |
1000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1537 |
10000000000000000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1538 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1539 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1540 |
"Modified: 4.11.1996 / 20:27:44 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1541 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1542 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1543 |
integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1544 |
"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
|
1545 |
(i.e. the receivers truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1546 |
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
|
1547 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1548 |
^ self |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1549 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1550 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1551 |
1234.56789 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1552 |
1.2345e6 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1553 |
1000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1554 |
10000000000000000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1555 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1556 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1557 |
"Modified: 4.11.1996 / 20:28:22 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1558 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
1559 |
|
701 | 1560 |
normalize |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1561 |
"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
|
1562 |
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
|
1563 |
zeros removed. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1564 |
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
|
1565 |
renamed to #compressed for ST-80 compatibility." |
1 | 1566 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1567 |
^ self compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1568 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1569 |
"Modified: 5.11.1996 / 14:08:24 / cg" |
701 | 1570 |
! |
1 | 1571 |
|
701 | 1572 |
rounded |
1573 |
"return the receiver rounded toward the next Integer - |
|
1558 | 1574 |
for integers this is the receiver itself." |
701 | 1575 |
|
1576 |
^ self |
|
1558 | 1577 |
|
1578 |
"Modified: 18.7.1996 / 12:44:24 / cg" |
|
701 | 1579 |
! |
69 | 1580 |
|
701 | 1581 |
truncated |
1582 |
"return the receiver truncated towards zero - |
|
1558 | 1583 |
for integers this is the receiver itself." |
159 | 1584 |
|
701 | 1585 |
^ self |
1558 | 1586 |
|
1587 |
"Modified: 18.7.1996 / 12:44:33 / cg" |
|
701 | 1588 |
! ! |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
1589 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1590 |
!Integer class methodsFor:'documentation'! |
701 | 1591 |
|
1592 |
version |
|
3874 | 1593 |
^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.85 1998-10-13 12:33:01 cg Exp $' |
1 | 1594 |
! ! |