author | Claus Gittinger <cg@exept.de> |
Thu, 28 Mar 2002 12:02:25 +0100 | |
changeset 6481 | d5c94e93067c |
parent 6480 | 4ff7f2af25fc |
child 6515 | f35f8c519c30 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
14 |
|
1 | 15 |
Number subclass:#Integer |
1210 | 16 |
instanceVariableNames:'' |
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
17 |
classVariableNames:'DefaultDisplayRadix BCDConversionErrorSignal' |
1210 | 18 |
poolDictionaries:'' |
19 |
category:'Magnitude-Numbers' |
|
1 | 20 |
! |
21 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
22 |
Object subclass:#ModuloNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
23 |
instanceVariableNames:'modulus reciprocal shift' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
24 |
classVariableNames:'' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
25 |
poolDictionaries:'' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
26 |
privateIn:Integer |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
27 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
28 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
29 |
!Integer class methodsFor:'documentation'! |
88 | 30 |
|
31 |
copyright |
|
32 |
" |
|
33 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 34 |
All Rights Reserved |
1 | 35 |
|
88 | 36 |
This software is furnished under a license and may be used |
37 |
only in accordance with the terms of that license and with the |
|
38 |
inclusion of the above copyright notice. This software may not |
|
39 |
be provided or otherwise made available to, or used by, any |
|
40 |
other person. No title to or ownership of the software is |
|
41 |
hereby transferred. |
|
42 |
" |
|
43 |
! |
|
1 | 44 |
|
88 | 45 |
documentation |
46 |
" |
|
47 |
abstract superclass for all integer numbers. |
|
48 |
See details in concrete subclasses LargeInteger and SmallInteger. |
|
212 | 49 |
|
1892 | 50 |
Mixed mode arithmetic: |
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
51 |
int op int -> int |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
52 |
int op fix -> fix; scale is fix's scale |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
53 |
int op fraction -> fraction |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
54 |
int op float -> float |
1892 | 55 |
|
1295 | 56 |
[Class variables:] |
212 | 57 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
58 |
DefaultDisplayRadix the radix in which integers present their |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
59 |
displayString (which is used in inspectors) |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
60 |
If you are to look at many hex numbers, bitmasks |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
61 |
etc. you may set this to 2 or 16. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
62 |
(avoids typing printStringRadix:.. all the time |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
63 |
- I know - I am lazy ;-). Default is 10. |
212 | 64 |
|
1295 | 65 |
|
66 |
[author:] |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
67 |
Claus Gittinger |
1556 | 68 |
|
69 |
[see also:] |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
70 |
Number |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
71 |
LargeInteger SmallInteger |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
72 |
Float ShortFloat Fraction FixedPoint |
88 | 73 |
" |
74 |
! ! |
|
1 | 75 |
|
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
76 |
!Integer class methodsFor:'initialization'! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
77 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
78 |
initialize |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
79 |
BCDConversionErrorSignal isNil ifTrue:[ |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
80 |
BCDConversionErrorSignal := ConversionErrorSignal newSignal. |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
81 |
BCDConversionErrorSignal nameClass:self message:#bcdConversionErrorSignal. |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
82 |
BCDConversionErrorSignal notifierString:'bcd conversion error'. |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
83 |
]. |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
84 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
85 |
"Modified: / 15.11.1999 / 20:36:04 / cg" |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
86 |
! ! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
87 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
88 |
!Integer class methodsFor:'instance creation'! |
92 | 89 |
|
3044 | 90 |
byte1:b1 byte2:b2 byte3:b3 byte4:b4 |
91 |
"Squeak compatibility: |
|
92 |
Return an Integer given four value bytes. |
|
93 |
The returned integer is either a Small- or a LargeInteger" |
|
94 |
||
95 |
|t| |
|
96 |
||
97 |
t := b4. |
|
98 |
t := (t bitShift:8) + b3. |
|
99 |
t := (t bitShift:8) + b2. |
|
100 |
^ (t bitShift:8) + b1. |
|
101 |
||
102 |
" |
|
103 |
(Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString |
|
104 |
(Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString |
|
105 |
" |
|
106 |
||
107 |
"Created: 19.10.1997 / 18:08:52 / cg" |
|
108 |
"Modified: 19.10.1997 / 18:09:04 / cg" |
|
109 |
! |
|
110 |
||
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
111 |
fastFromString:aString at:startIndex |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
112 |
"return the next unsigned Integer from the string |
4610 | 113 |
as a decimal number, starting at startIndex. |
114 |
The number must be in the native machines int range |
|
115 |
(i.e. 63bit on alpha / 31 on all others); |
|
116 |
However, for portability, only use it for 31bit numbers. |
|
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
117 |
No spaces are skipped. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
118 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
numbers." |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
123 |
%{ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
124 |
if (__isString(aString) && __isSmallInteger(startIndex)) { |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
125 |
char *cp = (char *)(__stringVal(aString)); |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
126 |
int idx = __intVal(startIndex) - 1; |
4610 | 127 |
unsigned INT val; |
3639
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 |
if ((unsigned)idx < __stringSize(aString)) { |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
130 |
val = atoi(cp + idx); |
4610 | 131 |
if (val <= _MAX_INT) { |
132 |
RETURN(__MKSMALLINT(val)); |
|
133 |
} |
|
134 |
RETURN (__MKUINT(val)); |
|
3639
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 |
%}. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
138 |
self primitiveFailed. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
139 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
140 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
141 |
Integer fastFromString:'12345' at:1 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
142 |
Integer fastFromString:'12345' at:2 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
143 |
Integer fastFromString:'12345' at:3 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
144 |
Integer fastFromString:'12345' at:4 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
145 |
Integer fastFromString:'12345' at:5 |
4610 | 146 |
Integer fastFromString:'1234512345' at:1 |
147 |
Integer fastFromString:'2147483647' at:1 |
|
4611 | 148 |
|
4610 | 149 |
Integer fastFromString:'4294967295' at:1 |
4611 | 150 |
Integer fastFromString:'12345' at:6 |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
151 |
Integer fastFromString:'12345' at:0 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
152 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
153 |
Time millisecondsToRun:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
154 |
100000 timesRepeat:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
155 |
Integer readFrom:'12345' |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
156 |
] |
4610 | 157 |
] |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
158 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
159 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
160 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
161 |
Time millisecondsToRun:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
162 |
100000 timesRepeat:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
163 |
Integer fastFromString:'12345' at:1 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
164 |
] |
4610 | 165 |
] |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
166 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
167 |
! |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
168 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
169 |
fromBCDBytes:aByteArray |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
170 |
"given a byteArray in BCD format, return an appropriate integer. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
171 |
The byteArray must contain the BCD encoded decimal string, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
172 |
starting with the most significant digits. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
173 |
This conversion is useful for some communication protocols, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
174 |
or control systems, which represent big numbers this way... |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
175 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
176 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
177 |
|val| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
178 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
179 |
val := 0. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
180 |
aByteArray do:[:twoDigits | |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
181 |
|hi lo| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
182 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
183 |
hi := (twoDigits bitShift:-4) bitAnd:16r0F. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
184 |
lo := twoDigits bitAnd:16r0F. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
185 |
val := (val * 100) + (hi * 10) + lo |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
186 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
187 |
^ val |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
188 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
189 |
" |
4921 | 190 |
Integer fromBCDBytes:#[16r12 16r34 16r56] |
191 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78] |
|
192 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90] |
|
193 |
Integer fromBCDBytes:#[16r98 16r76 16r54] |
|
194 |
Integer fromBCDBytes:#[16r98 16r76 16r54 16r32] |
|
195 |
Integer fromBCDBytes:#[16r98 16r76 16r54 16r32 16r10] |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
196 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
197 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
198 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
199 |
|
5171 | 200 |
fromSwappedBCDBytes:aByteArray |
201 |
"given a byteArray in BCD format, return an appropriate integer. |
|
202 |
The byteArray must contain the BCD encoded decimal string, |
|
203 |
starting with the LEAST significant digits. |
|
204 |
This conversion is useful for some communication protocols, |
|
205 |
or control systems (e.g. SMC), which represent big numbers this way... |
|
206 |
" |
|
207 |
||
208 |
|val| |
|
209 |
||
210 |
val := 0. |
|
211 |
aByteArray do:[:twoDigits | |
|
212 |
|hi lo| |
|
213 |
||
214 |
lo := (twoDigits bitShift:-4) bitAnd:16r0F. |
|
215 |
hi := twoDigits bitAnd:16r0F. |
|
5882
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
216 |
lo <= 9 ifTrue:[ |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
217 |
val := (val * 100) + (hi * 10) + lo |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
218 |
] ifFalse:[ |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
219 |
"16rF is used to encode an odd number of digits" |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
220 |
val := (val * 10) + hi. |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
221 |
]. |
5171 | 222 |
]. |
223 |
^ val |
|
224 |
||
225 |
" |
|
226 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56] |
|
5882
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
227 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16rF6] |
5171 | 228 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78] |
229 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90] |
|
230 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54] |
|
231 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32] |
|
232 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32 16r10] |
|
233 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90] |
|
234 |
" |
|
235 |
! |
|
236 |
||
92 | 237 |
new:numberOfBytes neg:negative |
238 |
"for ST-80 compatibility: |
|
239 |
Return an empty Integer (uninitialized value) with space for |
|
240 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 241 |
negative specifies if the result should be a negative number. |
242 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 243 |
|
132 | 244 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
159 | 245 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
701 | 246 |
! |
247 |
||
248 |
readFrom:aStringOrStream |
|
249 |
"return the next unsigned Integer from the (character-)stream aStream |
|
250 |
as decimal number. |
|
251 |
||
252 |
NOTICE: |
|
253 |
This behaves different from the default readFrom:, in returning |
|
254 |
0 (instead of raising an error) in case no number can be read. |
|
255 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
256 |
- depending on the upcoming ANSI standard, this may change." |
|
257 |
||
258 |
^ self readFrom:aStringOrStream onError:0 |
|
259 |
||
260 |
" |
|
261 |
Integer readFrom:(ReadStream on:'foobar') |
|
262 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
|
263 |
" |
|
264 |
! |
|
265 |
||
266 |
readFrom:aStringOrStream onError:exceptionBlock |
|
267 |
"return the next Integer from the (character-)stream aStream, |
|
268 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
269 |
Also, all initial whitespace is skipped. |
|
270 |
If the string does not represent a valid integer number, |
|
271 |
return the value of exceptionBlock." |
|
272 |
||
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
273 |
|value| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
274 |
|
1700 | 275 |
ErrorSignal handle:[:ex | |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
276 |
^ exceptionBlock value |
1700 | 277 |
] do:[ |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
278 |
|str nextChar negative| |
701 | 279 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
280 |
str := aStringOrStream readStream. |
701 | 281 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
282 |
nextChar := str skipSeparators. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
283 |
(nextChar == $-) ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
284 |
negative := true. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
285 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
286 |
nextChar := str peekOrNil |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
287 |
] ifFalse:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
288 |
negative := false |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
289 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
290 |
(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
|
291 |
" |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
292 |
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
|
293 |
" |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
294 |
^ exceptionBlock value |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
295 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
296 |
value := self readFrom:str radix:10. |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
297 |
nextChar := str peekOrNil. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
298 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
299 |
"-xxr<number> is invalid; should be xxr-<val>" |
1700 | 300 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
301 |
negative ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
302 |
'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
|
303 |
negative := false |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
304 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
305 |
str next. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
306 |
value := self readFrom:str radix:value |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
307 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
308 |
negative ifTrue:[ |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
309 |
value := value negated |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
310 |
]. |
701 | 311 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
312 |
^ value |
701 | 313 |
|
314 |
" |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
315 |
Integer readFrom:'12345' onError:['wrong'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
316 |
Integer readFrom:'-12345' onError:['wrong'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
317 |
Integer readFrom:'+12345' onError:['wrong'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
318 |
Integer readFrom:'16rFFFF' onError:['wrong'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
319 |
Integer readFrom:'12345.1234' onError:['wrong'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
320 |
Integer readFrom:'foo' onError:['wrong'] |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
321 |
Integer readFrom:'foo' |
701 | 322 |
" |
323 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
324 |
"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
|
325 |
"Modified: / 14.4.1998 / 19:17:28 / cg" |
701 | 326 |
! |
327 |
||
328 |
readFrom:aStringOrStream radix:radix |
|
329 |
"return the next unsigned Integer from the (character-)stream aStream |
|
330 |
in radix; (assumes that the initial XXr has already been read). |
|
331 |
No whitespace-skipping is done. |
|
332 |
Returns 0 if no number available. |
|
333 |
||
334 |
NOTICE: |
|
335 |
This behaves different from the default readFrom:, in returning |
|
336 |
0 (instead of raising an error) in case no number can be read. |
|
337 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
338 |
- depending on the upcoming ANSI standard, this may change." |
|
339 |
||
340 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
341 |
! |
|
342 |
||
343 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
344 |
"return the next unsigned Integer from the (character-)stream aStream |
|
345 |
in radix; (assumes that the initial XXr has already been read). |
|
346 |
No whitespace-skipping is done. |
|
347 |
Returns the value of exceptionBlock, if no number is available." |
|
348 |
||
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
349 |
|str nextChar nextChar2 value |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
350 |
r "{ Class: SmallInteger }" |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
351 |
r2 "{ Class: SmallInteger }"| |
701 | 352 |
|
353 |
str := aStringOrStream readStream. |
|
354 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
355 |
nextChar := str peekOrNil. |
701 | 356 |
(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
|
357 |
^ exceptionBlock value |
701 | 358 |
]. |
359 |
||
360 |
value := nextChar digitValue. |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
361 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
362 |
nextChar := str peekOrNil. |
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
363 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
364 |
"/ OLD code |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
365 |
"/ [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
366 |
"/ str next. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
367 |
"/ value := value * radix + nextChar digitValue. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
368 |
"/ nextChar := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
369 |
"/ ]. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
370 |
"/ ^ value. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
371 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
372 |
"/ the code below does the same, but is much faster, if the |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
373 |
"/ converted number is large |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
374 |
"/ (requires only half as many LargeInt multiplications and additions) |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
375 |
"/ It should not be slower for smallIntegers. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
376 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
377 |
r := radix. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
378 |
r2 := r * r. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
379 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
380 |
[nextChar notNil and:[nextChar isDigitRadix:r]] whileTrue:[ |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
381 |
str next. |
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
382 |
nextChar2 := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
383 |
(nextChar2 notNil and:[nextChar2 isDigitRadix:r]) ifTrue:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
384 |
str next. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
385 |
value := (value * r2) + ((nextChar digitValue * r) + nextChar2 digitValue). |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
386 |
nextChar := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
387 |
] ifFalse:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
388 |
^ (value * r) + nextChar digitValue. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
389 |
] |
701 | 390 |
]. |
391 |
^ value |
|
392 |
||
393 |
" |
|
394 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
|
395 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
396 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
397 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
398 |
Integer readFrom:(ReadStream on:'foobar') radix:10 onError:nil |
|
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
399 |
Integer readFrom:'gg' radix:10 onError:0 |
701 | 400 |
" |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
401 |
|
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
402 |
"Modified: / 14.4.1998 / 19:16:46 / cg" |
701 | 403 |
! ! |
404 |
||
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
405 |
!Integer class methodsFor:'Signal constants'! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
406 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
407 |
bcdConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
408 |
"return the signal which is raised when bcd conversion fails |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
409 |
(i.e. when trying to decode an invalid BCD number)" |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
410 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
411 |
^ BCDConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
412 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
413 |
"Modified: / 15.11.1999 / 20:35:20 / cg" |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
414 |
! ! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
415 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
416 |
!Integer class methodsFor:'constants'! |
701 | 417 |
|
418 |
unity |
|
1555 | 419 |
"return the neutral element for multiplication (1)" |
701 | 420 |
|
421 |
^ 1 |
|
1555 | 422 |
|
423 |
"Modified: 18.7.1996 / 12:26:43 / cg" |
|
701 | 424 |
! |
425 |
||
426 |
zero |
|
1555 | 427 |
"return the neutral element for addition (0)" |
701 | 428 |
|
429 |
^ 0 |
|
1555 | 430 |
|
431 |
"Modified: 18.7.1996 / 12:26:38 / cg" |
|
92 | 432 |
! ! |
433 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
434 |
!Integer class methodsFor:'misc'! |
49 | 435 |
|
436 |
displayRadix:aNumber |
|
54 | 437 |
"being tired of always sending #printStringRadix: in the inspectors, |
49 | 438 |
this allows you to change the default print radix for the displayString |
439 |
method." |
|
440 |
||
441 |
DefaultDisplayRadix := aNumber |
|
132 | 442 |
|
443 |
" |
|
444 |
Integer displayRadix:16. 123456 inspect |
|
445 |
Integer displayRadix:10. 123456 inspect |
|
446 |
" |
|
49 | 447 |
! ! |
448 |
||
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
449 |
!Integer class methodsFor:'queries'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
450 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
451 |
hasSharedInstances |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
452 |
"return true if this class has shared instances, that is, instances |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
453 |
with the same value are identical. |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
454 |
Although not always shared (LargeIntegers), these should be treated |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
455 |
so, to be independent of the number of bits in a SmallInt" |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
456 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
457 |
^ true |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
458 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
459 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
460 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
461 |
|
6249 | 462 |
!Integer methodsFor:'Compatibility - Dolphin'! |
463 |
||
464 |
highWord |
|
465 |
"return the high 16 bits of a 32 bit value" |
|
466 |
||
467 |
^ self bitShift:-16 |
|
468 |
||
469 |
" |
|
470 |
(16r12345678 highWord) hexPrintString |
|
471 |
(16r12345678 lowWord) hexPrintString |
|
472 |
" |
|
473 |
! |
|
474 |
||
475 |
lowWord |
|
476 |
"return the low 16 bits of a 32 bit value" |
|
477 |
||
478 |
^ self bitAnd:16rFFFF |
|
479 |
||
480 |
" |
|
481 |
(16r12345678 lowWord) hexPrintString |
|
482 |
(16r12345678 highWord) hexPrintString |
|
483 |
" |
|
484 |
! |
|
485 |
||
486 |
mask:integerMask set:aBoolean |
|
487 |
"Answer the result of setting/resetting the specified mask in the receiver." |
|
488 |
||
489 |
^ aBoolean |
|
490 |
ifTrue: [self maskSet:integerMask] |
|
491 |
ifFalse: [self maskClear:integerMask] |
|
492 |
||
493 |
"turn on the 1-bit: |
|
494 |
|v| |
|
495 |
||
496 |
v := 2r0100. |
|
497 |
v mask:1 set:true |
|
498 |
||
499 |
turn off the 1-bit: |
|
500 |
|v| |
|
501 |
||
502 |
v := 2r0101. |
|
503 |
v mask:1 set:false |
|
504 |
" |
|
505 |
! ! |
|
506 |
||
4867 | 507 |
!Integer methodsFor:'Compatibility - Squeak'! |
508 |
||
509 |
atRandom |
|
510 |
"return a random number between 1 amd myself" |
|
511 |
||
512 |
self < 1 ifTrue:[^ self]. |
|
513 |
^ (1 to:self) atRandom |
|
514 |
! |
|
4376 | 515 |
|
516 |
printStringBase:base |
|
4867 | 517 |
"return my printString in a base; |
518 |
same as printStringRadix:" |
|
519 |
||
4376 | 520 |
^ self printStringRadix:base |
521 |
! ! |
|
522 |
||
5186 | 523 |
!Integer methodsFor:'Compatibility - V''Age'! |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
524 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
525 |
<< aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
526 |
"left shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
527 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
528 |
^ self bitShift:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
529 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
530 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
531 |
1 << 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
532 |
64 << -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
533 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
534 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
535 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
536 |
>> aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
537 |
"right shift" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
538 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
539 |
^ self bitShift:(aNumber negated) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
540 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
541 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
542 |
1 >> -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
543 |
64 >> 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
544 |
" |
1 | 545 |
! ! |
546 |
||
4423 | 547 |
!Integer methodsFor:'bcd conversion'! |
548 |
||
549 |
decodeFromBCD |
|
550 |
"return a number representing the value of the BCD encoded receiver." |
|
551 |
||
4424 | 552 |
|v rslt multiplier nibble| |
4423 | 553 |
|
554 |
v := self. |
|
555 |
rslt := 0. |
|
556 |
multiplier := 1. |
|
557 |
[v > 0] whileTrue:[ |
|
4424 | 558 |
nibble := v bitAnd:16r0F. |
559 |
nibble > 9 ifTrue:[ |
|
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
560 |
^ BCDConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
561 |
raiseRequestWith:self |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
562 |
errorString:'bad BCD coded value' |
4424 | 563 |
]. |
564 |
rslt := rslt + (nibble * multiplier). |
|
4423 | 565 |
multiplier := multiplier * 10. |
566 |
v := v bitShift:-4. |
|
567 |
]. |
|
568 |
^ rslt |
|
569 |
||
570 |
" |
|
4949 | 571 |
16r1234567890123 decodeFromBCD |
4921 | 572 |
16r1073741823 decodeFromBCD |
573 |
16r1073741824 decodeFromBCD |
|
574 |
16r1073741825 decodeFromBCD |
|
575 |
||
4423 | 576 |
16r55 decodeFromBCD |
577 |
16r127 decodeFromBCD |
|
4921 | 578 |
16r800000 decodeFromBCD |
579 |
16r8000000 decodeFromBCD |
|
580 |
16r80000000 decodeFromBCD |
|
581 |
16r800000000 decodeFromBCD |
|
582 |
16r127567890 decodeFromBCD |
|
4423 | 583 |
16r1234567890 decodeFromBCD |
4424 | 584 |
|
585 |
16r5A decodeFromBCD |
|
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
586 |
16rFF decodeFromBCD |
4423 | 587 |
" |
588 |
||
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
589 |
"Modified: / 15.11.1999 / 20:37:20 / cg" |
4423 | 590 |
! |
591 |
||
592 |
encodeAsBCD |
|
593 |
"return a BCD encoded number representing the same value as the |
|
594 |
receiver." |
|
595 |
||
596 |
|v rslt shift| |
|
597 |
||
598 |
v := self. |
|
599 |
rslt := shift := 0. |
|
600 |
[v > 0] whileTrue:[ |
|
601 |
rslt := rslt + ((v \\ 10) bitShift:shift). |
|
602 |
shift := shift + 4. |
|
603 |
v := v // 10. |
|
604 |
]. |
|
605 |
^ rslt |
|
606 |
||
607 |
" |
|
608 |
55 encodeAsBCD hexPrintString |
|
4921 | 609 |
127 encodeAsBCD hexPrintString |
610 |
127 encodeAsBCD hexPrintString |
|
611 |
8912345 encodeAsBCD hexPrintString |
|
612 |
89123456 encodeAsBCD hexPrintString |
|
613 |
891234567 encodeAsBCD hexPrintString |
|
614 |
900000000 encodeAsBCD hexPrintString |
|
615 |
1073741823 encodeAsBCD hexPrintString |
|
616 |
1073741824 encodeAsBCD hexPrintString |
|
617 |
1073741825 encodeAsBCD hexPrintString |
|
618 |
1891234567 encodeAsBCD hexPrintString |
|
619 |
8912345678 encodeAsBCD hexPrintString |
|
4423 | 620 |
1234567890 encodeAsBCD hexPrintString |
621 |
" |
|
622 |
||
623 |
! ! |
|
624 |
||
69 | 625 |
!Integer methodsFor:'bit operators'! |
626 |
||
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
627 |
allMask:anInteger |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
628 |
"return true if all 1-bits in anInteger are also 1 in the receiver" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
629 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
630 |
^ (self bitAnd:anInteger) == anInteger |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
631 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
632 |
"2r00001111 allMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
633 |
"2r00001111 allMask:2r00011110" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
634 |
"2r00001111 allMask:2r00000000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
635 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
636 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
637 |
anyMask:anInteger |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
638 |
"return true if any 1-bits in anInteger is also 1 in the receiver. |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
639 |
(somewhat incorrect, if the mask is zero)" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
640 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
641 |
^ (self bitAnd:anInteger) ~~ 0 |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
642 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
643 |
"2r00001111 anyMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
644 |
"2r00001111 anyMask:2r11110000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
645 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
646 |
|
69 | 647 |
bitAnd:anInteger |
357 | 648 |
"return the bitwise-and of the receiver and the argument, anInteger. |
649 |
This is a general and slow implementation, walking over the bytes of |
|
650 |
the receiver and the argument." |
|
69 | 651 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
652 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
653 |
result byte| |
69 | 654 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
655 |
anInteger isInteger ifFalse:[ |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
656 |
^ anInteger bitAndFromInteger:self. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
657 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
658 |
|
92 | 659 |
n := (anInteger digitLength) min:(self digitLength). |
660 |
result := self class basicNew numberOfDigits:n. |
|
69 | 661 |
|
662 |
1 to:n do:[:index | |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
663 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
664 |
result digitAt:index put:byte. |
69 | 665 |
]. |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
666 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
667 |
^ result compressed |
92 | 668 |
]. |
669 |
^ result |
|
357 | 670 |
|
671 |
" |
|
672 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
|
673 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
674 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
675 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
676 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
677 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
678 |
"Modified: 5.11.1996 / 14:06:26 / cg" |
69 | 679 |
! |
680 |
||
85 | 681 |
bitAt:index |
92 | 682 |
"return the value of the index's bit (index starts at 1). |
683 |
Notice: the result of bitShift: on negative receivers is not |
|
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
684 |
defined in the language standard (since the implementation |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
685 |
is free to choose any internal representation for integers)" |
85 | 686 |
|
687 |
|i "{Class: SmallInteger}"| |
|
688 |
||
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
689 |
index <= 0 ifTrue:[ |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
690 |
^ SubscriptOutOfBoundsSignal |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
691 |
raiseRequestWith:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
692 |
errorString:'index out of bounds' |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
693 |
]. |
85 | 694 |
i := index - 1. |
695 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
|
92 | 696 |
|
697 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
698 |
1 bitAt:1 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
699 |
1 bitAt:2 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
700 |
1 bitAt:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
701 |
2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
702 |
2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
703 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
704 |
(1 bitShift:1000) bitAt:1000 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
705 |
(1 bitShift:1000) bitAt:1001 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
706 |
(1 bitShift:1000) bitAt:1002 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
707 |
|
92 | 708 |
(1 bitShift:30) bitAt:30 |
709 |
(1 bitShift:30) bitAt:31 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
710 |
(1 bitShift:30) bitAt:32 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
711 |
(1 bitShift:31) bitAt:31 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
712 |
(1 bitShift:31) bitAt:32 |
92 | 713 |
(1 bitShift:31) bitAt:33 |
714 |
(1 bitShift:32) bitAt:32 |
|
715 |
(1 bitShift:32) bitAt:33 |
|
716 |
(1 bitShift:32) bitAt:34 |
|
717 |
(1 bitShift:64) bitAt:64 |
|
718 |
(1 bitShift:64) bitAt:65 |
|
719 |
(1 bitShift:64) bitAt:66 |
|
720 |
" |
|
85 | 721 |
! |
722 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
723 |
bitClear:anInteger |
3884 | 724 |
"return the bitwise-and of the receiver and the complement of argument, anInteger. |
725 |
This is a general and slow implementation, walking over the bytes of |
|
726 |
the receiver and the argument." |
|
727 |
||
728 |
|n "{ Class: SmallInteger }" |
|
729 |
result byte| |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
730 |
|
3884 | 731 |
n := (anInteger digitLength) max:(self digitLength). |
732 |
result := self class basicNew numberOfDigits:n. |
|
733 |
||
734 |
1 to:n do:[:index | |
|
735 |
byte := (self digitAt:index) bitClear:(anInteger digitAt:index). |
|
736 |
result digitAt:index put:byte. |
|
737 |
]. |
|
738 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
|
739 |
^ result compressed |
|
740 |
]. |
|
741 |
^ result |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
742 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
743 |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
744 |
bitInvert |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
745 |
"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
|
746 |
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
|
747 |
since the digits are stored as absolute value." |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
748 |
|
4614 | 749 |
"/ ^ -1 - self |
750 |
||
751 |
|n "{ Class: SmallInteger }" |
|
752 |
result byte| |
|
753 |
||
754 |
n := self digitLength. |
|
755 |
result := self class basicNew numberOfDigits:n. |
|
756 |
||
757 |
1 to:n do:[:index | |
|
758 |
byte := self digitAt:index. |
|
759 |
result digitAt:index put:(byte bitInvert bitAnd:16rFF). |
|
760 |
]. |
|
761 |
(byte == 255 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
|
762 |
^ result compressed |
|
763 |
]. |
|
764 |
^ result |
|
765 |
||
766 |
" |
|
767 |
16rffffffff bitInvert |
|
768 |
16rff00ff00 bitInvert hexPrintString |
|
769 |
" |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
770 |
! |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
771 |
|
4376 | 772 |
bitInvert32 |
773 |
"return a new integer, where the low32 bits are complemented." |
|
774 |
||
775 |
^ self bitXor: 16rFFFFFFFF |
|
776 |
||
777 |
" |
|
4609 | 778 |
16r80000000 bitInvert32 hexPrintString |
779 |
16r7FFFFFFF bitInvert32 hexPrintString |
|
4614 | 780 |
16rFFFFFFFF bitInvert32 hexPrintString |
781 |
0 bitInvert32 hexPrintString |
|
4376 | 782 |
" |
783 |
! |
|
784 |
||
701 | 785 |
bitOr:anInteger |
786 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
787 |
This is a general and slow implementation, walking over the bytes of |
|
788 |
the receiver and the argument." |
|
789 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
790 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
791 |
result byte| |
701 | 792 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
793 |
anInteger isInteger ifFalse:[ |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
794 |
^ anInteger bitOrFromInteger:self. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
795 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
796 |
|
701 | 797 |
n := (anInteger digitLength) max:(self digitLength). |
798 |
result := self class basicNew numberOfDigits:n. |
|
799 |
||
800 |
1 to:n do:[:index | |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
801 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
802 |
result digitAt:index put:byte. |
701 | 803 |
]. |
804 |
"/ no need to normalize - if the operands were correct |
|
805 |
"/ byte == 0 ifTrue:[ |
|
806 |
"/ ^ result normalize |
|
807 |
"/ ]. |
|
808 |
^ result |
|
809 |
! |
|
810 |
||
85 | 811 |
bitShift:shiftCount |
812 |
"return the value of the receiver shifted by shiftCount bits; |
|
813 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 814 |
|
815 |
Notice: the result of bitShift: on negative receivers is not |
|
3638 | 816 |
defined in the language standard (since the implementation |
817 |
is free to choose any internal representation for integers)" |
|
85 | 818 |
|
92 | 819 |
|result |
820 |
prev "{ Class: SmallInteger }" |
|
821 |
next "{ Class: SmallInteger }" |
|
822 |
byte "{ Class: SmallInteger }" |
|
359 | 823 |
byte2 "{ Class: SmallInteger }" |
92 | 824 |
bitShift "{ Class: SmallInteger }" |
825 |
revShift "{ Class: SmallInteger }" |
|
826 |
digitShift "{ Class: SmallInteger }" |
|
827 |
n "{ Class: SmallInteger }" |
|
3044 | 828 |
nn "{ Class: SmallInteger }" |
829 |
nDigits "{ Class: SmallInteger }" | |
|
85 | 830 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
831 |
shiftCount isInteger ifFalse:[ |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
832 |
^ shiftCount bitShiftFromInteger:self. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
833 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
834 |
|
85 | 835 |
shiftCount > 0 ifTrue:[ |
3638 | 836 |
"left shift" |
85 | 837 |
|
3638 | 838 |
digitShift := shiftCount // 8. |
839 |
bitShift := shiftCount \\ 8. |
|
840 |
n := self digitLength. |
|
92 | 841 |
|
3638 | 842 |
" |
843 |
modulo 8 shifts can be done faster ... |
|
844 |
" |
|
845 |
bitShift == 0 ifTrue:[ |
|
5017
09d6baf12523
dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents:
5003
diff
changeset
|
846 |
n := n + digitShift. |
09d6baf12523
dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents:
5003
diff
changeset
|
847 |
result := self class basicNew numberOfDigits:n. |
3638 | 848 |
result sign:self sign. |
5017
09d6baf12523
dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents:
5003
diff
changeset
|
849 |
result digitBytes replaceFrom:(digitShift + 1) to:n with:self digitBytes. |
3638 | 850 |
" |
851 |
no normalize needed, since receiver was already normalized |
|
852 |
" |
|
853 |
^ result |
|
854 |
]. |
|
92 | 855 |
|
3638 | 856 |
" |
857 |
less-than-8 shifts can be done faster ... |
|
858 |
" |
|
859 |
digitShift == 0 ifTrue:[ |
|
4151 | 860 |
nn := n+1. |
861 |
result := self class basicNew numberOfDigits:nn. |
|
3638 | 862 |
result sign:self sign. |
863 |
prev := 0. |
|
4151 | 864 |
1 to:n do:[:index | |
3638 | 865 |
byte := self digitAt:index. |
866 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
867 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
868 |
prev := byte bitShift:-8. |
|
869 |
]. |
|
4151 | 870 |
result digitAt:nn put:prev. |
3638 | 871 |
" |
872 |
might have stored a 0-byte ... |
|
873 |
" |
|
874 |
prev == 0 ifTrue:[ |
|
875 |
^ result compressed |
|
876 |
]. |
|
877 |
^ result. |
|
878 |
]. |
|
92 | 879 |
|
3638 | 880 |
" |
881 |
slow case ... |
|
882 |
" |
|
883 |
n := n + digitShift + 1. |
|
884 |
result := self class basicNew numberOfDigits:n. |
|
885 |
result sign:self sign. |
|
886 |
byte := self digitAt:1. |
|
887 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
|
888 |
result digitAt:(digitShift + 1) put:byte. |
|
889 |
revShift := -8 + bitShift. |
|
890 |
nDigits := self digitLength. |
|
891 |
2 to:nDigits do:[:index | |
|
892 |
byte := self digitAt:index. |
|
893 |
byte2 := self digitAt:index-1. |
|
894 |
byte := byte bitShift:bitShift. |
|
895 |
byte2 := byte2 bitShift:revShift. |
|
896 |
byte := (byte bitOr:byte2) bitAnd:16rFF. |
|
897 |
result digitAt:(index + digitShift) put:byte. |
|
898 |
]. |
|
899 |
byte2 := self digitAt:nDigits. |
|
900 |
byte2 := (byte2 bitShift:revShift) bitAnd:16rFF. |
|
901 |
result digitAt:(nDigits + digitShift + 1) put:byte2. |
|
902 |
" |
|
903 |
might have stored a 0-byte ... |
|
904 |
" |
|
905 |
byte2 == 0 ifTrue:[ |
|
906 |
^ result compressed |
|
907 |
]. |
|
908 |
^ result |
|
92 | 909 |
]. |
910 |
||
911 |
shiftCount < 0 ifTrue:[ |
|
3638 | 912 |
"right shift" |
85 | 913 |
|
3638 | 914 |
digitShift := shiftCount negated // 8. |
915 |
bitShift := shiftCount negated \\ 8. |
|
916 |
n := self digitLength. |
|
92 | 917 |
|
3638 | 918 |
digitShift >= n ifTrue:[ |
919 |
^ 0 |
|
920 |
]. |
|
92 | 921 |
|
3638 | 922 |
" |
923 |
modulo 8 shifts can be done faster ... |
|
924 |
" |
|
925 |
bitShift == 0 ifTrue:[ |
|
926 |
n := n-digitShift. |
|
927 |
result := self class basicNew numberOfDigits:n. |
|
928 |
result sign:self sign. |
|
4830
879f09ec443a
Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents:
4803
diff
changeset
|
929 |
result digitBytes replaceFrom:1 to:n with:self digitBytes startingAt:(digitShift + 1) . |
3638 | 930 |
n <= SmallInteger maxBytes ifTrue:[ |
931 |
^ result compressed |
|
932 |
]. |
|
933 |
^ result |
|
934 |
]. |
|
85 | 935 |
|
3638 | 936 |
" |
937 |
less-than-8 shifts can be done faster ... |
|
938 |
" |
|
939 |
digitShift == 0 ifTrue:[ |
|
940 |
result := self class basicNew numberOfDigits:n. |
|
941 |
result sign:self sign. |
|
942 |
prev := 0. |
|
943 |
bitShift := bitShift negated. |
|
944 |
revShift := 8 + bitShift. |
|
945 |
n to:1 by:-1 do:[:index | |
|
946 |
byte := self digitAt:index. |
|
947 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
948 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
949 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
950 |
prev := next. |
|
951 |
]. |
|
952 |
(n <= 5) ifTrue:[ |
|
953 |
^ result compressed |
|
954 |
]. |
|
955 |
^ result |
|
956 |
]. |
|
92 | 957 |
|
3638 | 958 |
" |
959 |
slow case ... |
|
960 |
" |
|
961 |
nn := n-digitShift. |
|
962 |
result := self class basicNew numberOfDigits:nn. |
|
963 |
result sign:self sign. |
|
85 | 964 |
|
3638 | 965 |
prev := 0. |
966 |
bitShift := bitShift negated. |
|
967 |
revShift := 8 + bitShift. |
|
4151 | 968 |
nn := digitShift + 1. |
969 |
n to:nn by:-1 do:[:index | |
|
3638 | 970 |
byte := self digitAt:index. |
971 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
972 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
973 |
result digitAt:(index - digitShift) put:byte. |
|
974 |
prev := next. |
|
975 |
]. |
|
976 |
"the last stored byte ..." |
|
977 |
^ result compressed |
|
85 | 978 |
]. |
92 | 979 |
|
980 |
^ self "no shift" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
981 |
|
3638 | 982 |
"Modified: / 8.7.1998 / 12:45:24 / cg" |
4151 | 983 |
"Modified: / 5.5.1999 / 16:05:05 / stefan" |
701 | 984 |
! |
1 | 985 |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
986 |
bitTest:anInteger |
5782 | 987 |
"return true, if any bit from aMask is set in the receiver. |
988 |
I.e. true, if the bitwise-AND of the receiver and the argument, anInteger |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
989 |
is non-0, false otherwise. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
990 |
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
|
991 |
the receiver and the argument." |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
992 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
993 |
|n "{ Class: SmallInteger }" |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
994 |
byte| |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
995 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
996 |
n := (anInteger digitLength) min:(self digitLength). |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
997 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
998 |
1 to:n do:[:index | |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
999 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1000 |
byte ~~ 0 ifTrue:[^ true]. |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1001 |
]. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1002 |
^ false |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1003 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1004 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1005 |
16r112233445566778899 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1006 |
16r112233445566778800 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1007 |
16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1008 |
16r112233445566778899 bitTest:16rFF0000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1009 |
16r112233445566778899 bitTest:16r00000000000000FFFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1010 |
16r1234567800000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1011 |
16r8765432100000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1012 |
16r12345678 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1013 |
16r87654321 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1014 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1015 |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1016 |
"Modified: / 6.6.1999 / 15:10:33 / cg" |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1017 |
! |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1018 |
|
701 | 1019 |
bitXor:anInteger |
1020 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
1021 |
This is a general and slow implementation, walking over the bytes of |
|
1022 |
the receiver and the argument." |
|
1023 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1024 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1025 |
result byte| |
701 | 1026 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1027 |
anInteger isInteger ifFalse:[ |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1028 |
^ anInteger bitXorFromInteger:self. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1029 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1030 |
|
701 | 1031 |
n := (anInteger digitLength) max:(self digitLength). |
1032 |
result := self class basicNew numberOfDigits:n. |
|
1 | 1033 |
|
701 | 1034 |
1 to:n do:[:index | |
3993
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
1035 |
byte := (anInteger digitAt:index) bitXor:(self digitAt:index). |
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
1036 |
result digitAt:index put:byte. |
701 | 1037 |
]. |
3993
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
1038 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
1039 |
^ result compressed |
701 | 1040 |
]. |
1041 |
^ result |
|
1 | 1042 |
|
701 | 1043 |
" |
1044 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
|
1045 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
|
1046 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
1047 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
|
1048 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1049 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1050 |
"Modified: 5.11.1996 / 14:06:40 / cg" |
1 | 1051 |
! |
1052 |
||
5210 | 1053 |
changeBit:index to:aBooleanOrNumber |
1054 |
"return a new number where the specified bit is on or off, |
|
1055 |
sepending on aBooleanOrNumber. |
|
1056 |
Bits are counted from 1 starting with the least significant. |
|
1057 |
The methods name may be missleading: the receiver is not changed, |
|
1058 |
but a new number is returned. Should be named #withBitChanged:to:" |
|
1059 |
||
1060 |
(aBooleanOrNumber == 0 or:[aBooleanOrNumber == false]) ifTrue:[ |
|
1061 |
^ self bitClear:index |
|
1062 |
]. |
|
1063 |
^ self setBit:index |
|
1064 |
||
1065 |
" |
|
1066 |
0 changeBit:3 to:1 => 4 (2r100) |
|
1067 |
" |
|
1068 |
! |
|
1069 |
||
3683 | 1070 |
clearBit:index |
1071 |
"return a new number where the specified bit is off. |
|
5209 | 1072 |
Bits are counted from 1 starting with the least significant. |
1073 |
The methods name may be missleading: the receiver is not changed, |
|
1074 |
but a new number is returned. Should be named #withBitCleared:" |
|
3683 | 1075 |
|
1076 |
|n "{ Class: SmallInteger }" |
|
1077 |
byteIndex "{ Class: SmallInteger }" |
|
1078 |
bitIndex "{ Class: SmallInteger }" |
|
1079 |
result byte| |
|
1080 |
||
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1081 |
index <= 0 ifTrue:[ |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1082 |
^ SubscriptOutOfBoundsSignal |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1083 |
raiseRequestWith:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1084 |
errorString:'index out of bounds' |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1085 |
]. |
3683 | 1086 |
byteIndex := ((index - 1) // 8) + 1. |
1087 |
n := self digitLength. |
|
1088 |
byteIndex > n ifTrue:[ |
|
1089 |
^ self |
|
1090 |
]. |
|
1091 |
||
1092 |
result := self copy. |
|
1093 |
bitIndex := ((index - 1) \\ 8) + 1. |
|
1094 |
byte := (result digitAt:byteIndex) clearBit:bitIndex. |
|
1095 |
result digitAt:byteIndex put:byte. |
|
1096 |
(byte == 0 or:[n == byteIndex and:[n <= SmallInteger maxBytes]]) ifTrue:[ |
|
1097 |
^ result compressed |
|
1098 |
]. |
|
1099 |
^ result |
|
1100 |
||
1101 |
" |
|
1102 |
3111111111 clearBit:1 |
|
1103 |
" |
|
1104 |
||
1105 |
"Modified: / 28.7.1998 / 18:35:50 / cg" |
|
1106 |
! |
|
1107 |
||
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1108 |
even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1109 |
"return true if the receiver is even" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1110 |
|
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1111 |
^ (self bitTest:1) not |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1112 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1113 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1114 |
16r112233445566778899 even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1115 |
16r112233445566778800 even |
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1116 |
1 even |
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1117 |
2 even |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1118 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1119 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1120 |
"Created: / 6.6.1999 / 15:00:40 / cg" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1121 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1122 |
|
701 | 1123 |
highBit |
1124 |
"return the bitIndex of the highest bit set. The returned bitIndex |
|
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1125 |
starts at 1 for the least significant bit. |
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1126 |
Returns 0 if no bit is set." |
701 | 1127 |
|
1128 |
|byteNr highByte| |
|
1129 |
||
1130 |
byteNr := self digitLength. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1131 |
byteNr == 0 ifTrue:[ |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1132 |
^ 0 |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1133 |
]. |
701 | 1134 |
highByte := self digitAt:byteNr. |
1135 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 1136 |
|
701 | 1137 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1138 |
0 highBit |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1139 |
-1 highBit |
701 | 1140 |
(1 bitShift:1) highBit |
1141 |
(1 bitShift:30) highBit |
|
1142 |
(1 bitShift:31) highBit |
|
1143 |
(1 bitShift:32) highBit |
|
1144 |
(1 bitShift:33) highBit |
|
1145 |
(1 bitShift:64) highBit |
|
1146 |
(1 bitShift:1000) highBit |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1147 |
(1 bitShift:1000) negated highBit |
701 | 1148 |
((1 bitShift:64)-1) highBit |
1149 |
" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1150 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1151 |
"Modified: / 3.5.1999 / 09:20:57 / stefan" |
2429 | 1152 |
! |
1153 |
||
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1154 |
invertBit:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1155 |
"return a new number where the specified bit is inverted. |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1156 |
Bits are counted from 1 starting with the least significant. |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1157 |
The methods name may be missleading: the receiver is not changed, |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1158 |
but a new number is returned. Should be named #withBitInverted:" |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1159 |
|
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1160 |
index <= 0 ifTrue:[ |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1161 |
^ SubscriptOutOfBoundsSignal |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1162 |
raiseRequestWith:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1163 |
errorString:'index out of bounds' |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1164 |
]. |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1165 |
^ self bitXor:(1 bitShift:index-1) |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1166 |
|
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1167 |
" |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1168 |
0 invertBit:3 => 4 (2r100) |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1169 |
0 invertBit:48 => 140737488355328 (2r1000.....000) |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1170 |
((0 invertBit:99) invertBit:100) printStringRadix:2 |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1171 |
" |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1172 |
! |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1173 |
|
5209 | 1174 |
isBitSet:index |
1175 |
"return true if the index' bit is set; false otherwise. |
|
1176 |
Bits are counted from 1 starting with the least significant." |
|
1177 |
||
1178 |
^ (self bitAt:index) ~~ 0 |
|
1179 |
||
1180 |
" |
|
1181 |
5 isBitSet:3 => true |
|
1182 |
2r0101 isBitSet:2 => false |
|
1183 |
2r0101 isBitSet:1 => true |
|
1184 |
2r0101 isBitSet:0 index error |
|
1185 |
" |
|
1186 |
! |
|
1187 |
||
2429 | 1188 |
lowBit |
1189 |
"return the bitIndex of the lowest bit set. The returned bitIndex |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1190 |
starts at 1 for the least significant bit. |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1191 |
Returns 0 if no bit is set." |
2429 | 1192 |
|
1193 |
|maxBytes "{ Class: SmallInteger }" |
|
1194 |
bitIndex "{ Class: SmallInteger }" |
|
1195 |
byte| |
|
1196 |
||
1197 |
maxBytes := self digitLength. |
|
1198 |
bitIndex := 0. |
|
1199 |
1 to:maxBytes do:[:byteIndex | |
|
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1200 |
byte := self digitAt:byteIndex. |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1201 |
byte ~~ 0 ifTrue:[ |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1202 |
^ bitIndex + byte lowBit |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1203 |
]. |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1204 |
bitIndex := bitIndex + 8 |
2429 | 1205 |
]. |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1206 |
^ 0 "/ should not happen |
2429 | 1207 |
|
1208 |
" |
|
1209 |
0 lowBit |
|
1210 |
1 lowBit |
|
1211 |
(1 bitShift:1) lowBit |
|
1212 |
(1 bitShift:1) highBit |
|
1213 |
(1 bitShift:30) lowBit |
|
1214 |
(1 bitShift:30) highBit |
|
1215 |
(1 bitShift:31) lowBit |
|
1216 |
(1 bitShift:31) highBit |
|
1217 |
(1 bitShift:32) lowBit |
|
1218 |
(1 bitShift:32) highBit |
|
1219 |
(1 bitShift:33) lowBit |
|
1220 |
(1 bitShift:33) highBit |
|
1221 |
(1 bitShift:64) lowBit |
|
1222 |
(1 bitShift:64) highBit |
|
1223 |
(1 bitShift:1000) lowBit |
|
1224 |
(1 bitShift:1000) highBit |
|
1225 |
((1 bitShift:64)-1) lowBit |
|
1226 |
((1 bitShift:64)-1) highBit |
|
1227 |
" |
|
1228 |
||
1229 |
"Modified: 1.3.1997 / 16:54:23 / cg" |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1230 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1231 |
|
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1232 |
maskClear:aMask |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1233 |
^ self bitAnd:aMask bitInvert |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1234 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1235 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1236 |
maskSet:aMask |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1237 |
^ self bitOr:aMask |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1238 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1239 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1240 |
noMask:anInteger |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1241 |
"return true if no 1-bit in anInteger is 1 in the receiver" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1242 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1243 |
^ (self bitAnd:anInteger) == 0 |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1244 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1245 |
"2r00001111 noMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1246 |
"2r00001111 noMask:2r11110000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1247 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1248 |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1249 |
odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1250 |
"return true if the receiver is odd" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1251 |
|
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1252 |
^ self bitTest:1 |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1253 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1254 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1255 |
16r112233445566778899 odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1256 |
16r112233445566778800 odd |
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1257 |
1 odd |
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1258 |
2 odd |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1259 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1260 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1261 |
"Created: / 6.6.1999 / 15:00:55 / cg" |
5209 | 1262 |
! |
1263 |
||
1264 |
setBit:index |
|
1265 |
"return a new number where the specified bit is on. |
|
1266 |
Bits are counted from 1 starting with the least significant. |
|
1267 |
The methods name may be missleading: the receiver is not changed, |
|
1268 |
but a new number is returned. Should be named #withBitCleared:" |
|
1269 |
||
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1270 |
index <= 0 ifTrue:[ |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1271 |
^ SubscriptOutOfBoundsSignal |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1272 |
raiseRequestWith:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1273 |
errorString:'index out of bounds' |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1274 |
]. |
5209 | 1275 |
^ self bitOr:(1 bitShift:index-1) |
1276 |
||
1277 |
" |
|
1278 |
0 setBit:3 => 4 (2r100) |
|
1279 |
0 setBit:48 => 140737488355328 (2r1000.....000) |
|
1280 |
((0 setBit:99) setBit:100) printStringRadix:2 |
|
1281 |
" |
|
701 | 1282 |
! ! |
1283 |
||
3890 | 1284 |
!Integer methodsFor:'byte access'! |
1285 |
||
1286 |
digitByteLength |
|
1287 |
"return the number bytes required for a 2's complement |
|
1288 |
binary representation of this Integer. |
|
1289 |
For positive receivers, thats the same as the digitLength." |
|
1290 |
||
1291 |
" |
|
1292 |
check if there is a 0-byte ... |
|
1293 |
this allows to ask unnormalized LargeIntegers |
|
1294 |
for their digitLength |
|
1295 |
" |
|
1296 |
|l "{ Class: SmallInteger }" | |
|
1297 |
||
1298 |
self >= 0 ifTrue:[^ self digitLength]. |
|
1299 |
||
1300 |
l := self negated digitLength. |
|
1301 |
(self digitByteAt:l) == 16rFF ifTrue:[ |
|
1302 |
^ (l - 1) max:1 |
|
1303 |
]. |
|
1304 |
^ l |
|
1305 |
||
1306 |
" |
|
1307 |
-129 digitByteLength |
|
1308 |
" |
|
1309 |
||
1310 |
"Created: / 25.10.1998 / 14:58:09 / cg" |
|
1311 |
"Modified: / 25.10.1998 / 19:20:33 / cg" |
|
1312 |
! ! |
|
1313 |
||
5238 | 1314 |
!Integer methodsFor:'coercing & converting'! |
701 | 1315 |
|
1888 | 1316 |
asFixedPoint |
1317 |
"return the receiver as a fixedPoint number" |
|
1318 |
||
1319 |
^ FixedPoint numerator:self denominator:1 scale:1 |
|
1320 |
||
1321 |
" |
|
1322 |
100 asFixedPoint |
|
1323 |
100 asFixedPoint + 0.1 asFixedPoint |
|
1324 |
" |
|
1325 |
||
1326 |
"Modified: 5.11.1996 / 15:13:17 / cg" |
|
1327 |
! |
|
1328 |
||
2141 | 1329 |
asFixedPoint:scale |
1330 |
"return the receiver as fixedPoint number, with the given number |
|
1331 |
of post-decimal-point digits." |
|
1332 |
||
1333 |
^ FixedPoint numerator:self denominator:1 scale:scale |
|
1334 |
||
1335 |
" |
|
1336 |
100 asFixedPoint:2 |
|
1337 |
100 asFixedPoint + (0.1 asFixedPoint:2) |
|
1338 |
" |
|
1339 |
||
1340 |
"Modified: 10.1.1997 / 20:00:08 / cg" |
|
1341 |
! |
|
1342 |
||
701 | 1343 |
asFraction |
1344 |
"return a Fraction with same value as receiver" |
|
1345 |
||
2794
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
1346 |
^ 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
|
1347 |
|
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
1348 |
"Modified: 28.7.1997 / 19:26:06 / cg" |
1 | 1349 |
! |
1350 |
||
701 | 1351 |
asInteger |
1 | 1352 |
"return the receiver truncated towards zero - |
1353 |
for integers this is self" |
|
1354 |
||
1355 |
^ self |
|
1337 | 1356 |
! |
1357 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1358 |
asModuloNumber |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1359 |
"return a precomputed modulo number" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1360 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
1361 |
^ ModuloNumber modulus:self. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1362 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1363 |
"Created: / 3.5.1999 / 14:48:03 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1364 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1365 |
|
1337 | 1366 |
signExtendedByteValue |
1367 |
"return a smallInteger from sign-extending the 8'th bit. |
|
1368 |
May be useful for communication interfaces" |
|
1369 |
||
1370 |
^ (self bitAnd:16rFF) signExtendedByteValue |
|
1371 |
||
1372 |
"Created: 7.5.1996 / 09:31:52 / cg" |
|
1373 |
! |
|
1374 |
||
1375 |
signExtendedShortValue |
|
1376 |
"return a smallInteger from sign-extending the 16'th bit. |
|
1377 |
May be useful for communication interfaces" |
|
1378 |
||
1379 |
^ (self bitAnd:16rFFFF) signExtendedShortValue |
|
1380 |
||
1381 |
"Modified: 7.5.1996 / 09:31:57 / cg" |
|
1 | 1382 |
! ! |
1383 |
||
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1384 |
!Integer methodsFor:'comparing'! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1385 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1386 |
hash |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1387 |
"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
|
1388 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1389 |
^ self bitAnd:16r3FFFFFFF. |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1390 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1391 |
" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1392 |
-20000000000000 hash |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1393 |
20000000000000 hash |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1394 |
" |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1395 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1396 |
"Created: / 14.11.1996 / 12:12:27 / cg" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
1397 |
"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
|
1398 |
! ! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
1399 |
|
701 | 1400 |
!Integer methodsFor:'double dispatching'! |
1401 |
||
1402 |
differenceFromFraction:aFraction |
|
1403 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 1404 |
|
701 | 1405 |
|d| |
1 | 1406 |
|
701 | 1407 |
d := aFraction denominator. |
1408 |
^ (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
|
1409 |
denominator:d) |
2793 | 1410 |
|
1411 |
"Modified: 28.7.1997 / 19:08:30 / cg" |
|
1 | 1412 |
! |
1413 |
||
701 | 1414 |
productFromFraction:aFraction |
1415 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 1416 |
|
701 | 1417 |
^ (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
|
1418 |
denominator:aFraction denominator) |
2793 | 1419 |
|
1420 |
"Modified: 28.7.1997 / 19:08:27 / cg" |
|
1 | 1421 |
! |
1422 |
||
701 | 1423 |
quotientFromFraction:aFraction |
1424 |
"sent when a fraction does not know how to divide the receiver, an integer" |
|
1425 |
||
2793 | 1426 |
^ 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
|
1427 |
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
|
1428 |
denominator:(self * aFraction denominator) |
2793 | 1429 |
|
1430 |
"Modified: 28.7.1997 / 19:08:23 / cg" |
|
701 | 1431 |
! |
1 | 1432 |
|
701 | 1433 |
sumFromFraction:aFraction |
1434 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
1435 |
||
1436 |
|d| |
|
1437 |
||
1438 |
d := aFraction denominator. |
|
2793 | 1439 |
^ 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
|
1440 |
denominator:d |
2793 | 1441 |
|
1442 |
"Modified: 28.7.1997 / 19:08:11 / cg" |
|
1 | 1443 |
! ! |
1444 |
||
5469
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1445 |
!Integer methodsFor:'encoding'! |
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1446 |
|
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1447 |
encodeOn:anEncoder with:aParameter |
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1448 |
|
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1449 |
anEncoder encodeInteger:self with:aParameter |
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1450 |
|
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1451 |
|
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1452 |
! ! |
8aca7ce5626f
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5435
diff
changeset
|
1453 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1454 |
!Integer methodsFor:'helpers'! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1455 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1456 |
gcd_helper:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1457 |
"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
|
1458 |
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
|
1459 |
larger than the arg." |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1460 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1461 |
| selfAbs argAbs selfLowBit argLowBit shift t | |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1462 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1463 |
selfAbs := self. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1464 |
argAbs := anInteger. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1465 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1466 |
selfLowBit := selfAbs lowBit - 1. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1467 |
argLowBit := argAbs lowBit - 1. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1468 |
shift := selfLowBit min:argLowBit. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1469 |
argAbs := argAbs bitShift:(argLowBit negated). |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1470 |
[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
|
1471 |
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
|
1472 |
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
|
1473 |
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
|
1474 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
1475 |
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
|
1476 |
selfLowBit := selfAbs lowBit - 1. |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1477 |
]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1478 |
^ argAbs bitShift:shift |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1479 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1480 |
"Created: 1.3.1997 / 16:38:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1481 |
"Modified: 1.3.1997 / 16:45:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1482 |
! ! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1483 |
|
1 | 1484 |
!Integer methodsFor:'misc math'! |
1485 |
||
701 | 1486 |
acker:n |
1560 | 1487 |
"return the value of acker(self, n). |
1488 |
;-) Do not try with receivers > 3" |
|
701 | 1489 |
|
1490 |
(self == 0) ifTrue:[^ n + 1]. |
|
1491 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
1492 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
1493 |
||
1494 |
" |
|
1495 |
3 acker:2 |
|
1496 |
" |
|
1560 | 1497 |
|
1498 |
"Modified: 18.7.1996 / 13:08:16 / cg" |
|
701 | 1499 |
! |
1500 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1501 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1502 |
"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
|
1503 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1504 |
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
|
1505 |
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
|
1506 |
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
|
1507 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1508 |
^ 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
|
1509 |
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
|
1510 |
with:(self \\ aNumber) |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1511 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1512 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1513 |
10 divMod:3 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1514 |
100 factorial divMod:103 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1515 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1516 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1517 |
"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
|
1518 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1519 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1520 |
extendedEuclid:tb |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1521 |
"return the solution of 'ax + by = gcd(a,b)'. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1522 |
An array conatining x, y and gcd(a,b) is returned." |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1523 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1524 |
|a b gcd gcd1 u u1 v v1 tmp t swap shift "{SmallInteger}"| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1525 |
|
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
1526 |
self < tb ifTrue:[ |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1527 |
a := self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1528 |
b := tb. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1529 |
swap := false. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1530 |
] ifFalse:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1531 |
a := tb. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1532 |
b := self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1533 |
swap := true. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1534 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1535 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1536 |
shift := ((a lowBit) min:(b lowBit))-1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1537 |
shift > 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1538 |
tmp := shift negated. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1539 |
a := a bitShift:tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1540 |
b := b bitShift:tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1541 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1542 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1543 |
gcd := a copy. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1544 |
gcd1 := b copy. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1545 |
u := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1546 |
u1 := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1547 |
v := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1548 |
v1 := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1549 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1550 |
[ |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
1551 |
"/ The following condition is true: |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
1552 |
"/ (a * u1) + (b * v1) ~= gcd1 ifTrue:[self halt]. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1553 |
t := gcd1 divMod:gcd. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1554 |
gcd1 := gcd. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1555 |
gcd := t at:2. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1556 |
t := t at:1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1557 |
tmp := v. |
5003
304be9a3a226
Add some debug in #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
5001
diff
changeset
|
1558 |
"/v1 - (v * t) - v1 + (v * t) ~= 0 ifTrue:[self halt]. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1559 |
v := v1 - (v * t). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1560 |
v1 := tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1561 |
tmp := u. |
5003
304be9a3a226
Add some debug in #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
5001
diff
changeset
|
1562 |
"/u1 - (u * t) - u1 + (u * t) ~= 0 ifTrue:[self halt]. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1563 |
u := u1 - (u * t). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1564 |
u1 := tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1565 |
] doWhile:[gcd > 0]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1566 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1567 |
shift > 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1568 |
gcd1 := gcd1 bitShift:shift. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1569 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1570 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1571 |
swap ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1572 |
^ Array with:v1 with:u1 with:gcd1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1573 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1574 |
^ Array with:u1 with:v1 with:gcd1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1575 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1576 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1577 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1578 |
14 extendedEuclid:5 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1579 |
14 extendedEuclid:2 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1580 |
25 extendedEuclid:15 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1581 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1582 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1583 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
1584 |
"Modified: / 18.11.1999 / 16:19:24 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1585 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1586 |
|
1 | 1587 |
factorial |
1560 | 1588 |
"return fac(self) (i.e. 1*2*3...*self) using an iterative algorithm. |
1589 |
This is slightly faster than the recursive algorithm, and does not |
|
1590 |
suffer from stack overflow problems (with big receivers)" |
|
1591 |
||
2745 | 1592 |
|p i| |
1560 | 1593 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1594 |
(self < 2) ifTrue:[ |
6195 | 1595 |
self < 0 ifTrue:[ |
1596 |
"/ |
|
1597 |
"/ requested factorial of a negative number |
|
1598 |
"/ |
|
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1599 |
^ self class |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1600 |
raise:#domainErrorSignal |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1601 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1602 |
selector:#factorial |
6195 | 1603 |
arguments:#() |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
1604 |
errorString:'factorial of negative number' |
6195 | 1605 |
]. |
1606 |
^ 1 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1607 |
]. |
1560 | 1608 |
p := 2. |
2745 | 1609 |
i := 3. |
1610 |
[i <= self] whileTrue:[ |
|
6195 | 1611 |
p := p * i. |
1612 |
i := i + 1. |
|
1560 | 1613 |
]. |
1614 |
^ p |
|
1615 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
1616 |
" |
1560 | 1617 |
10 factorial |
1618 |
1000 factorial |
|
1619 |
10000 factorial |
|
1620 |
10000 factorialR |
|
1621 |
||
1622 |
Time millisecondsToRun:[2000 factorial] |
|
1623 |
Time millisecondsToRun:[2000 factorialR] |
|
2745 | 1624 |
-1 factorial |
1560 | 1625 |
" |
1626 |
||
6195 | 1627 |
"Modified: / 16.11.2001 / 14:14:55 / cg" |
1560 | 1628 |
! |
1629 |
||
1630 |
factorialR |
|
3908 | 1631 |
"return fac(self) (i.e. 1*2*3...*self) using a recursive algorithm. |
1632 |
This is included to demonstration purposes - if you really need |
|
1633 |
factorial numbers, use the iterative #factorial, which is slightly |
|
1634 |
faster and does not suffer from stack overflow problems (with big receivers)." |
|
1 | 1635 |
|
356 | 1636 |
(self >= 2) ifTrue:[ |
3908 | 1637 |
^ self * (self - 1) factorialR |
1 | 1638 |
]. |
356 | 1639 |
^ 1 |
212 | 1640 |
|
1641 |
" |
|
1560 | 1642 |
10 factorialR |
1643 |
1000 factorialR |
|
4158 | 1644 |
Time millisecondsToRun:[10000 factorial] |
212 | 1645 |
" |
1560 | 1646 |
|
3908 | 1647 |
"Created: / 18.7.1996 / 12:48:36 / cg" |
4158 | 1648 |
"Modified: / 8.5.1999 / 18:40:13 / cg" |
1 | 1649 |
! |
1650 |
||
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1651 |
fib |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1652 |
"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
|
1653 |
(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
|
1654 |
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
|
1655 |
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
|
1656 |
... 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
|
1657 |
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
|
1658 |
|
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1659 |
|a1 a2 an| |
701 | 1660 |
|
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1661 |
a1 := 1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1662 |
a2 := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1663 |
an := 0. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1664 |
self timesRepeat:[ |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1665 |
an := a1 + a2. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1666 |
a2 := a1. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1667 |
a1 := an |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1668 |
]. |
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1669 |
^ an |
701 | 1670 |
|
1671 |
" |
|
1422 | 1672 |
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
|
1673 |
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
|
1674 |
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
|
1675 |
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
|
1676 |
Transcript showCR:(Time millisecondsToRun:[1500 fib]) |
701 | 1677 |
" |
1678 |
! |
|
1679 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1680 |
gcd:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1681 |
"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
|
1682 |
Euclids & Knuths algorithm." |
1 | 1683 |
|
3060 | 1684 |
|selfAbs argAbs t| |
1 | 1685 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1686 |
selfAbs := self abs. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1687 |
argAbs := anInteger abs. |
1888 | 1688 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1689 |
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
|
1690 |
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
|
1691 |
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
|
1692 |
argAbs := t. |
1 | 1693 |
]. |
52 | 1694 |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1695 |
argAbs = 0 ifTrue: [^ selfAbs]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1696 |
selfAbs := selfAbs \\ argAbs. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1697 |
selfAbs = 0 ifTrue:[^ argAbs]. |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
1698 |
^ argAbs gcd_helper:selfAbs |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1699 |
|
3060 | 1700 |
" |
1701 |
3141589999999999 gcd:1000000000000000 |
|
1702 |
" |
|
1703 |
||
1704 |
"Modified: / 25.10.1997 / 16:08:45 / cg" |
|
1 | 1705 |
! |
1706 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1707 |
integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1708 |
"return an integer representing 1/self * 2**n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1709 |
Where an integer is one bit longer than self." |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1710 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1711 |
|b rem result| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1712 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1713 |
b := self highBit. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1714 |
rem := 1 bitShift:b. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1715 |
result := 0 asLargeInteger numberOfDigits:(b // 8)+1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1716 |
b := b+1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1717 |
[b > 0] whileTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1718 |
rem >= self ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1719 |
rem := rem -= self. |
4830
879f09ec443a
Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents:
4803
diff
changeset
|
1720 |
result digitBytes bitSetAt:b. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1721 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1722 |
rem := rem mul2. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1723 |
b := b - 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1724 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1725 |
^ result compressed. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1726 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1727 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1728 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1729 |
333 integerReciprocal (2 raisedTo:18) // 333 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1730 |
393 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1731 |
8 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1732 |
15 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1733 |
15112233445566 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1734 |
10239552311579 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1735 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1736 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1737 |
"Modified: / 3.5.1999 / 14:27:18 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1738 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1739 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1740 |
inverseMod:n |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1741 |
"find the modular inverse for myself to n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1742 |
This is defined as the solution of: '1 = (self * x) mod n" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1743 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1744 |
|e ret| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1745 |
|
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
1746 |
"the following expression returns #(x y (self gcd:n)), the solution of the equation |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
1747 |
(self * x) + (n * y) = self gcd:n" |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
1748 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1749 |
e := self extendedEuclid:n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1750 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1751 |
(e at:3) == 1 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1752 |
ret := e at:1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1753 |
ret negative ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1754 |
^ ret + n |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1755 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1756 |
^ ret. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1757 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1758 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1759 |
^ 0 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1760 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1761 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1762 |
14 inverseMod:5 -> 4 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1763 |
5 inverseMod:14 -> 3 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1764 |
14 inverseMod:11 -> 4 (4 * 14) \\ 11 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1765 |
11 inverseMod:14 -> 9 (9 * 11) \\ 14 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1766 |
79 inverseMod:3220 -> 1019 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1767 |
3220 inverseMod:79 -> 54 (54 * 3220) \\ 79 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1768 |
1234567891 inverseMod:1111111111119 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1769 |
-> 148726663534 (148726663534*1234567891) \\ 1111111111119 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1770 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1771 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1772 |
14 extendedEuclid:11 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1773 |
5 extendedEuclid:14 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1774 |
14 extendedEuclid:2 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1775 |
3220 extendedEuclid:79 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1776 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1777 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1778 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
1779 |
"Modified: / 18.11.1999 / 16:21:37 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1780 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1781 |
|
1 | 1782 |
lcm:anInteger |
52 | 1783 |
"return the least common multiple (using gcd:)" |
1784 |
||
1785 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
1786 |
||
92 | 1787 |
" |
1788 |
65 lcm:15 |
|
1789 |
3 lcm:15 |
|
1790 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1791 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1792 |
|
4973 | 1793 |
raisedTo:exp mod:mod |
1794 |
"return the modulu (remainder) of |
|
1795 |
the receiver raised to exp (an Integer) and mod (another Integer)" |
|
1796 |
||
1797 |
|result m t |
|
5816 | 1798 |
"/ eI "{Class: SmallInteger}" |
1799 |
bits "{Class: SmallInteger}"| |
|
4973 | 1800 |
|
1801 |
"use the addition chaining algorithm" |
|
1802 |
||
1803 |
exp negative ifTrue:[ |
|
1804 |
^ self class |
|
1805 |
raise:#domainErrorSignal |
|
1806 |
receiver:self |
|
1807 |
selector:#raisedTo:mod: |
|
6195 | 1808 |
arguments:(Array with:exp with:mod) |
4973 | 1809 |
errorString:'modulo arithmethic with negative exponent' |
1810 |
]. |
|
1811 |
||
1812 |
m := mod asModuloNumber. |
|
1813 |
||
1814 |
result := 1. |
|
1815 |
t := m modulusOf:self. |
|
1816 |
||
1817 |
"/ to be validated ... |
|
1818 |
"/ (exp class == SmallInteger) ifTrue:[ |
|
1819 |
"/ eI := exp. |
|
1820 |
"/ [eI ~~ 0] whileTrue:[ |
|
1821 |
"/ [(eI bitAnd:1) == 0] whileTrue:[ |
|
1822 |
"/ eI := eI bitShift:-1. |
|
1823 |
"/ t := m modulusOf:(t * t). |
|
1824 |
"/ ]. |
|
1825 |
"/ eI := eI - 1. |
|
1826 |
"/ result := m modulusOf:(result * t). |
|
1827 |
"/ ]. |
|
1828 |
"/ ] ifFalse:[ |
|
1829 |
bits := exp highBit. |
|
1830 |
||
1831 |
1 to:bits do:[:i| |
|
1832 |
(exp bitAt:i) == 1 ifTrue:[ |
|
1833 |
result := m modulusOf:(result * t). |
|
1834 |
]. |
|
1835 |
t := m modulusOf:(t * t). |
|
1836 |
]. |
|
1837 |
"/ ]. |
|
1838 |
||
1839 |
^ result |
|
1840 |
||
1841 |
" |
|
1842 |
2 raisedTo:2 mod:3 |
|
1843 |
20000000000000 raisedTo:200 mod:190 -> 30 |
|
1844 |
(20000000000000 raisedTo:200) \\ 190 |
|
1845 |
||
1846 |
Time millisecondsToRun:[100 timesRepeat:[ |
|
1847 |
20000000000000 raisedTo:200 mod:190 |
|
1848 |
] |
|
1849 |
] |
|
1850 |
||
1851 |
Time millisecondsToRun:[100 timesRepeat:[ |
|
1852 |
(20000000000000 raisedTo:200) \\ 190 |
|
1853 |
] |
|
1854 |
] |
|
1855 |
" |
|
1856 |
||
1857 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
|
1858 |
"Modified: / 5.5.1999 / 11:18:20 / stefan" |
|
6195 | 1859 |
"Modified: / 16.11.2001 / 14:15:21 / cg" |
4973 | 1860 |
! |
1861 |
||
1862 |
raisedToCrtModP:p q:q ep:ep eq:eq u:u |
|
1863 |
"Application of the Chinese Remainder Theorem (CRT). |
|
1864 |
||
1865 |
This is a faster modexp for moduli with a known factorisation into two |
|
1866 |
relatively prime factors p and q, and an input relatively prime to the |
|
1867 |
modulus, the Chinese Remainder Theorem to do the computation mod p and |
|
1868 |
mod q, and then combine the results. This relies on a number of |
|
1869 |
precomputed values, but does not actually require the modulus n or the |
|
1870 |
exponent e. |
|
1871 |
||
1872 |
expout = expin ^ e mod (p*q). |
|
1873 |
We form this by evaluating |
|
1874 |
p2 = (expin ^ e) mod p and |
|
1875 |
q2 = (expin ^ e) mod q |
|
1876 |
and then combining the two by the CRT. |
|
1877 |
||
1878 |
Two optimisations of this are possible. First, we can reduce expin |
|
1879 |
modulo p and q before starting. |
|
1880 |
||
1881 |
Second, since we know the factorisation of p and q (trivially derived |
|
1882 |
from the factorisation of n = p*q), and expin is relatively prime to |
|
1883 |
both p and q, we can use Euler's theorem, expin^phi(m) = 1 (mod m), |
|
1884 |
to throw away multiples of phi(p) or phi(q) in e. |
|
1885 |
Letting ep = e mod phi(p) and |
|
1886 |
eq = e mod phi(q) |
|
1887 |
then combining these two speedups, we only need to evaluate |
|
1888 |
p2 = ((expin mod p) ^ ep) mod p and |
|
1889 |
q2 = ((expin mod q) ^ eq) mod q. |
|
1890 |
||
1891 |
Now we need to apply the CRT. Starting with |
|
1892 |
expout = p2 (mod p) and |
|
1893 |
expout = q2 (mod q) |
|
1894 |
we can say that expout = p2 + p * k, and if we assume that 0 <= p2 < p, |
|
1895 |
then 0 <= expout < p*q for some 0 <= k < q. Since we want expout = q2 |
|
1896 |
(mod q), then p*k = q2-p2 (mod q). Since p and q are relatively prime, |
|
1897 |
p has a multiplicative inverse u mod q. In other words, u = 1/p (mod q). |
|
1898 |
||
1899 |
Multiplying by u on both sides gives k = u*(q2-p2) (mod q). |
|
1900 |
Since we want 0 <= k < q, we can thus find k as |
|
1901 |
k = (u * (q2-p2)) mod q. |
|
1902 |
||
1903 |
Once we have k, evaluating p2 + p * k is easy, and |
|
1904 |
that gives us the result |
|
1905 |
" |
|
1906 |
||
1907 |
|result t mp mq| |
|
1908 |
||
1909 |
||
1910 |
mp := p asModuloNumber. |
|
1911 |
t := mp modulusOf:self. |
|
1912 |
result := t raisedTo:ep mod:mp. |
|
1913 |
||
1914 |
mq := q asModuloNumber. |
|
1915 |
t := mq modulusOf:self. |
|
1916 |
t := t raisedTo:eq mod:mq. |
|
1917 |
||
1918 |
"now p2 is in result, q2 in t" |
|
1919 |
||
1920 |
t := t -= result. |
|
1921 |
t < 0 ifTrue:[ |
|
1922 |
t := t + q. |
|
1923 |
]. |
|
1924 |
t := t *= u. |
|
1925 |
t := mq modulusOf:t. |
|
1926 |
t := t *= p. |
|
1927 |
result := result += t. |
|
1928 |
||
1929 |
^ result. |
|
1930 |
||
1931 |
||
1932 |
||
1933 |
" |
|
1934 |
2 raisedTo:2 mod:3 |
|
1935 |
20000000000000 raisedTo:200 mod:190 |
|
1936 |
(20000000000000 raisedTo:200) \\ 190 |
|
1937 |
" |
|
1938 |
||
1939 |
"Created: / 30.4.1999 / 15:53:15 / stefan" |
|
1940 |
"Modified: / 5.5.1999 / 11:01:15 / stefan" |
|
1941 |
! |
|
1942 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1943 |
raisedToInteger:exp |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1944 |
"return the receiver raised to exp" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1945 |
|
5816 | 1946 |
|result e t |
1947 |
"/ bits "{SmallInteger}" |
|
1948 |
| |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1949 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1950 |
"use the addition chaining algorithm" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1951 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1952 |
result := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1953 |
t := self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1954 |
exp < 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1955 |
e := exp negated. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1956 |
] ifFalse:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1957 |
e := exp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1958 |
]. |
4918 | 1959 |
|
4952 | 1960 |
[e ~~ 0] whileTrue:[ |
1961 |
[(e bitAnd:1) == 0] whileTrue:[ |
|
1962 |
e := e bitShift:-1. |
|
1963 |
t := t * t. |
|
1964 |
]. |
|
1965 |
e := e - 1. |
|
1966 |
result := result * t. |
|
1967 |
]. |
|
1968 |
||
1969 |
"/ the new algorithm above is somewhat faster (15%) |
|
1970 |
"/ |
|
1971 |
"/ bits := e highBit. |
|
1972 |
"/ |
|
1973 |
"/ 1 to:bits do:[:i| |
|
1974 |
"/ (e bitAt:i) == 1 ifTrue:[ |
|
4918 | 1975 |
"/ result := result * t. |
1976 |
"/ ]. |
|
4952 | 1977 |
"/ t := t * t. |
4918 | 1978 |
"/ ]. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1979 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1980 |
(exp < 0) ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1981 |
^ 1 / result |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1982 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1983 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1984 |
^ result |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1985 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1986 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1987 |
" |
4952 | 1988 |
(2 raisedToInteger:216) |
4918 | 1989 |
(2 raisedTo:216) |
1990 |
-> 105312291668557186697918027683670432318895095400549111254310977536 |
|
4952 | 1991 |
|
1992 |
(2 raisedToInteger:216) asFloat |
|
4917 | 1993 |
(2 raisedTo:216) asFloat |
4918 | 1994 |
-> 1.05312E+65 |
4952 | 1995 |
|
1996 |
(2 raisedToInteger:500) |
|
4918 | 1997 |
(2 raisedTo:500) |
1998 |
-> 3273390607896141870013189696827599152216642046043064789483291368096133796404674554883270092325904157150886684127560071009217256545885393053328527589376 |
|
4952 | 1999 |
2 raisedToInteger:10 |
4918 | 2000 |
-> 1024 |
4952 | 2001 |
-2 raisedToInteger:10 |
4918 | 2002 |
-> 1024 |
4952 | 2003 |
-2 raisedToInteger:9 |
4918 | 2004 |
-> -512 |
4952 | 2005 |
10 raisedToInteger:-10 |
4918 | 2006 |
-> (1/10000000000) |
4952 | 2007 |
2 raisedToInteger:0 |
4918 | 2008 |
-> 1 |
4952 | 2009 |
2 raisedToInteger:-1 |
4918 | 2010 |
-> (1/2) |
2011 |
||
2012 |
Time millisecondsToRun:[ |
|
2013 |
10000 timesRepeat:[ |
|
2014 |
(2 raisedToInteger:500) |
|
2015 |
] |
|
4952 | 2016 |
] |
4918 | 2017 |
|
2018 |
Time millisecondsToRun:[ |
|
2019 |
|bigNum| |
|
2020 |
bigNum := 2 raisedToInteger:500. |
|
2021 |
10 timesRepeat:[ |
|
2022 |
(bigNum raisedToInteger:500) |
|
2023 |
] |
|
2024 |
] |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2025 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2026 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2027 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2028 |
"Modified: / 27.4.1999 / 16:16:11 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2029 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2030 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2031 |
recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2032 |
"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
|
2033 |
WARNING: |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2034 |
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
|
2035 |
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
|
2036 |
(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
|
2037 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2038 |
(self > 1) ifTrue:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2039 |
^ (self - 1) recursiveFib + (self - 2) recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2040 |
]. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2041 |
^ 1 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2042 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2043 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2044 |
30 fib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2045 |
30 recursiveFib |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2046 |
Transcript showCR:(Time millisecondsToRun:[30 recursiveFib]) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2047 |
Transcript showCR:(Time millisecondsToRun:[30 fib]) |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2048 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2049 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2050 |
"Modified: 18.7.1996 / 12:47:19 / cg" |
1 | 2051 |
! ! |
2052 |
||
2053 |
!Integer methodsFor:'printing & storing'! |
|
2054 |
||
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2055 |
asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2056 |
"return a byteArray containing the receiver in BCD encoding. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2057 |
The byteArray will contain the BCD encoded decimal string, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2058 |
starting with the most significant digits first. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2059 |
This conversion is useful for some communication protocols, |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2060 |
or control systems, which represent big numbers this way... |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2061 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2062 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2063 |
|s rest twoDigits hi lo| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2064 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2065 |
self == 0 ifTrue:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2066 |
^ #[ 16r00 ] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2067 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2068 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2069 |
"/ a very rough estimate on the final size ... |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2070 |
s := (ByteArray new:(self digitLength * 2)) writeStream. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2071 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2072 |
rest := self. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2073 |
[rest > 0] whileTrue:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2074 |
twoDigits := rest \\ 100. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2075 |
rest := rest // 100. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2076 |
hi := twoDigits \\ 10. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2077 |
lo := twoDigits // 10. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2078 |
s nextPut:(lo bitShift:4)+hi |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2079 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2080 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2081 |
^ s contents reverse |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2082 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2083 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2084 |
12345678 asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2085 |
12345678901234567890 asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2086 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2087 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2088 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2089 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2090 |
|
49 | 2091 |
displayString |
2092 |
"return a string for displaying in a view (as in inspector). |
|
2093 |
The output radix is usually 10, but can be changed by setting |
|
2094 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
2095 |
||
54 | 2096 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
159 | 2097 |
^ self printString |
54 | 2098 |
]. |
49 | 2099 |
^ self radixPrintStringRadix:DefaultDisplayRadix |
2100 |
||
92 | 2101 |
" |
2102 |
Integer displayRadix:16. 12345 |
|
2103 |
Integer displayRadix:2. 12345 |
|
2104 |
Integer displayRadix:10. 12345 |
|
2105 |
" |
|
49 | 2106 |
! |
2107 |
||
701 | 2108 |
errorPrintHex |
2109 |
"print the receiver as a hex number on the standard error stream" |
|
2110 |
||
2111 |
(self printStringRadix:16) errorPrint |
|
2112 |
! |
|
1 | 2113 |
|
701 | 2114 |
hexPrintString |
2115 |
"return a hex string representation of the receiver" |
|
2116 |
||
2117 |
^ self printStringRadix:16 |
|
3874 | 2118 |
|
2119 |
" |
|
2120 |
127 hexPrintString |
|
2121 |
-1 hexPrintString |
|
2122 |
" |
|
2123 |
||
2124 |
"Modified: / 11.10.1998 / 01:15:43 / cg" |
|
701 | 2125 |
! |
2126 |
||
2408 | 2127 |
hexPrintString:size |
2128 |
"return a hex string representation of the receiver, |
|
2129 |
padded to size characters" |
|
2130 |
||
5799 | 2131 |
"/ ^ (self printStringRadix:16) leftPaddedTo:size with:$0 |
2132 |
^ self printStringRadix:16 size:size fill:$0 |
|
2133 |
||
2134 |
" |
|
2135 |
12345 hexPrintString:4 |
|
2136 |
" |
|
2408 | 2137 |
|
2138 |
"Created: 18.2.1997 / 13:32:33 / cg" |
|
2139 |
! |
|
2140 |
||
701 | 2141 |
printHex |
2142 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 2143 |
|
701 | 2144 |
(self printStringRadix:16) print |
2145 |
! |
|
2146 |
||
2147 |
printOn:aStream |
|
2148 |
"append a printed description of the receiver to aStream" |
|
2149 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2150 |
self printOn:aStream base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2151 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2152 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
701 | 2153 |
! |
2154 |
||
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
2155 |
printOn:aStream base:b |
1 | 2156 |
"return a string representation of the receiver in the specified |
2157 |
radix (without the initial XXr)" |
|
2158 |
||
5988 | 2159 |
^ self printOn:aStream base:b showRadix:false |
2160 |
||
2161 |
" |
|
2162 |
10 printOn:Transcript base:3 |
|
2163 |
31 printOn:Transcript base:3 |
|
2164 |
-20 printOn:Transcript base:16 |
|
2165 |
-20 printOn:Transcript base:10 |
|
2166 |
3000 factorial printOn:Transcript base:10 |
|
2167 |
" |
|
2168 |
||
2169 |
"Modified: / 20.1.1998 / 18:05:02 / stefan" |
|
2170 |
"Modified: / 7.9.2001 / 13:52:17 / cg" |
|
2171 |
! |
|
2172 |
||
2173 |
printOn:aStream base:b showRadix:showRadix |
|
2174 |
"append a string representation of the receiver in the specified numberBase to aStream |
|
2175 |
(if showRadix is true, with initial XXr) |
|
2176 |
The radix argument should be between 2 and 36." |
|
2177 |
||
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
2178 |
|base num s divMod mod r r2 r4 nD numN| |
1 | 2179 |
|
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
2180 |
base := b. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2181 |
(base between:2 and:36) ifFalse:[ |
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
2182 |
self error:'invalid base' mayProceed:true. |
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
2183 |
base := 10. |
54 | 2184 |
]. |
2185 |
||
5988 | 2186 |
showRadix ifTrue:[ |
2187 |
base printOn:aStream. |
|
2188 |
aStream nextPutAll:'r'. |
|
2189 |
]. |
|
2190 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2191 |
(self = 0) ifTrue:[aStream nextPut:$0. ^ self]. |
1 | 2192 |
(self < 0) ifTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2193 |
aStream nextPut:$- . |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2194 |
num := self negated. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2195 |
] ifFalse:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2196 |
num := self. |
1 | 2197 |
]. |
92 | 2198 |
|
2199 |
" |
|
2200 |
claus: changed it from a recursive algorithm; |
|
2201 |
(it used to trigger stack-overflow exceptions when printing |
|
2202 |
3000 factorial ...) |
|
2203 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2204 |
"/ leftPart := num // base. |
92 | 2205 |
"/ (leftPart ~= 0) ifTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2206 |
"/ leftPart printOn:aStream base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2207 |
"/ aStream nextPut:(Character digitValue:(num \\ base). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2208 |
"/ ^ self |
92 | 2209 |
"/ ]. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2210 |
"/ aStream nextPut:(Character digitValue:num). |
2951 | 2211 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
2212 |
"/ 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
|
2213 |
"/ 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
|
2214 |
"/ 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
|
2215 |
"/ 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
|
2216 |
"/ 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
|
2217 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2218 |
r2 := base*base. "/ radix^2 |
3138 | 2219 |
r4 := r2*r2. "/ radix^4 |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2220 |
base <= 10 ifTrue:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2221 |
r := r4*r2. "/ radix^6 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2222 |
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
|
2223 |
] ifFalse:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2224 |
r := r4*base. "/ radix^5 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2225 |
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
|
2226 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
2227 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2228 |
"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
|
2229 |
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
|
2230 |
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
|
2231 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2232 |
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
|
2233 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
2234 |
[num >= r] whileTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2235 |
"/ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2236 |
"/ chop off nD digits. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2237 |
"/ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2238 |
divMod := num divMod:r. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2239 |
num := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2240 |
numN := divMod at:2. |
2951 | 2241 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2242 |
"/ process them |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2243 |
nD timesRepeat:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2244 |
divMod := numN divMod:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2245 |
numN := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2246 |
mod := divMod at:2. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2247 |
s nextPut:(Character digitValue:mod). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2248 |
]. |
2951 | 2249 |
]. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2250 |
|
92 | 2251 |
[num ~= 0] whileTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2252 |
divMod := num divMod:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2253 |
num := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2254 |
mod := divMod at:2. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2255 |
s nextPut:(Character digitValue:mod). |
1 | 2256 |
]. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2257 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2258 |
aStream nextPutAll:(s contents reverse). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2259 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2260 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2261 |
3000 factorial printOn:Transcript base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2262 |
10 printOn:Transcript base:3 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2263 |
31 printOn:Transcript base:3 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2264 |
-20 printOn:Transcript base:16 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2265 |
-20 printOn:Transcript base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2266 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2267 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2268 |
"Modified: / 20.1.1998 / 18:05:02 / stefan" |
5988 | 2269 |
"Created: / 7.9.2001 / 13:51:33 / cg" |
2270 |
"Modified: / 7.9.2001 / 13:54:50 / cg" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2271 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2272 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2273 |
printOn:aStream radix:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2274 |
"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
|
2275 |
The receiver is printed in radix base (instead of the default, 10). |
5988 | 2276 |
This method is obsoleted by #printOn:base:, which is ST-80 & ANSI compatible." |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2277 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2278 |
self printOn:aStream base:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2279 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2280 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
5988 | 2281 |
"Modified: / 7.9.2001 / 13:58:29 / cg" |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2282 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2283 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2284 |
printStringRadix:base |
5988 | 2285 |
"return a string representation of the receiver in the specified |
2286 |
base; does NOT prepend XXr to the string. |
|
2287 |
See also: radixPrintStringRadix: |
|
2288 |
printOn:base:showRadix:" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2289 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2290 |
|s| |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2291 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2292 |
s := WriteStream on:(String new:20). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2293 |
self printOn:s base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2294 |
^ s contents |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2295 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2296 |
"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
|
2297 |
"Modified: / 20.1.1998 / 14:10:54 / stefan" |
5988 | 2298 |
"Modified: / 7.9.2001 / 13:58:13 / cg" |
1 | 2299 |
! |
2300 |
||
2301 |
printStringRadix:aRadix size:sz fill:fillCharacter |
|
2302 |
"return a string representation of the receiver in the specified |
|
2303 |
radix. The string is padded on the left with fillCharacter to make |
|
2304 |
its size as specified in sz." |
|
2305 |
||
2951 | 2306 |
|s actualSize| |
1 | 2307 |
|
2308 |
s := self printStringRadix:aRadix. |
|
2951 | 2309 |
actualSize := s size. |
2310 |
actualSize < sz ifTrue:[ |
|
2311 |
s := ((String new:(sz - actualSize)) atAllPut:fillCharacter) , s |
|
1 | 2312 |
]. |
2313 |
^ s |
|
2314 |
||
92 | 2315 |
" |
2316 |
1024 printStringRadix:16 size:4 fill:$0 |
|
2317 |
1024 printStringRadix:2 size:16 fill:$. |
|
2318 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
2319 |
" |
|
216 | 2320 |
! |
2321 |
||
5988 | 2322 |
radixPrintStringRadix:radix |
701 | 2323 |
"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
|
2324 |
base; prepend XXr to the string" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2325 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2326 |
|s| |
701 | 2327 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2328 |
s := WriteStream on:(String new:20). |
5988 | 2329 |
self printOn:s base:radix showRadix:true. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2330 |
^ s contents |
216 | 2331 |
|
701 | 2332 |
" |
5988 | 2333 |
31 radixPrintStringRadix:2 |
2334 |
31 radixPrintStringRadix:3 |
|
2335 |
31 radixPrintStringRadix:10 |
|
2336 |
31 radixPrintStringRadix:16 |
|
2337 |
31 radixPrintStringRadix:36 |
|
701 | 2338 |
" |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2339 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2340 |
"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
|
2341 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
5988 | 2342 |
"Modified: / 7.9.2001 / 13:57:00 / cg" |
1 | 2343 |
! ! |
2344 |
||
701 | 2345 |
!Integer methodsFor:'queries'! |
202 | 2346 |
|
701 | 2347 |
digitAt:n |
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2348 |
"return the n-th byte of the binary representation." |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2349 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2350 |
self subclassResponsibility |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2351 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2352 |
"This is a very stupid implementation, and should be redefined in |
701 | 2353 |
concrete subclasses." |
202 | 2354 |
|
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2355 |
"/ |num count| |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2356 |
"/ |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2357 |
"/ num := self. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2358 |
"/ count := n. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2359 |
"/ [count > 1] whileTrue:[ |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2360 |
"/ num := num // 256. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2361 |
"/ count := count - 1 |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2362 |
"/ ]. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2363 |
"/ ^ num \\ 256 |
159 | 2364 |
|
2365 |
" |
|
701 | 2366 |
16r44332211 digitAt:1 |
2367 |
16r44332211 digitAt:2 |
|
2368 |
16r44332211 digitAt:3 |
|
2369 |
16r44332211 digitAt:4 |
|
2370 |
16r44332211 digitAt:5 |
|
2371 |
16r00332211 digitAt:4 |
|
2372 |
16r00332211 digitAt:5 |
|
159 | 2373 |
" |
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2374 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2375 |
"Modified: / 26.9.2001 / 21:21:21 / cg" |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2376 |
! |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2377 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2378 |
digitByteAt:n |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2379 |
"return 8 bits of my signed value, starting at byte index. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2380 |
For positive receivers, this is the same as #digitAt:; |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2381 |
for negative ones, the actual bit representation is returned." |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2382 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2383 |
self subclassResponsibility |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2384 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2385 |
"Created: / 26.9.2001 / 21:18:43 / cg" |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
2386 |
"Modified: / 26.9.2001 / 21:20:19 / cg" |
1 | 2387 |
! |
2388 |
||
701 | 2389 |
digitLength |
2390 |
"return the number of bytes needed for the binary representation |
|
2816 | 2391 |
of the receiver. |
2392 |
This method is redefined in concrete classes - the fallback here is |
|
2393 |
never really used." |
|
701 | 2394 |
|
2395 |
^ (self log:256) ceiling asInteger |
|
2816 | 2396 |
|
2397 |
"Modified: 31.7.1997 / 13:19:06 / cg" |
|
701 | 2398 |
! |
2399 |
||
2400 |
isInteger |
|
2401 |
"return true, if the receiver is some kind of integer number" |
|
2402 |
||
2403 |
^ true |
|
3036 | 2404 |
! |
2405 |
||
2406 |
isPowerOfTwo |
|
2407 |
"return true, if the receiver is a power of 2" |
|
2408 |
||
2409 |
^ (self bitAnd:(self - 1)) == 0 |
|
2410 |
||
2411 |
" |
|
2412 |
0 isPowerOfTwo |
|
2413 |
1 isPowerOfTwo |
|
2414 |
2 isPowerOfTwo |
|
2415 |
3 isPowerOfTwo |
|
2416 |
4 isPowerOfTwo |
|
2417 |
16r8000000000000000 isPowerOfTwo |
|
2418 |
16r8000000000000001 isPowerOfTwo |
|
2419 |
" |
|
2420 |
||
2421 |
"Modified: 15.10.1997 / 18:43:49 / cg" |
|
701 | 2422 |
! ! |
202 | 2423 |
|
2458 | 2424 |
!Integer methodsFor:'special bit operators'! |
2425 |
||
2426 |
bitAnd_32:anInteger |
|
2427 |
"return a C-semantic 32bit locical-and of the receiver and |
|
2428 |
the argument. Both must be either Small- or LargeIntegers. |
|
2429 |
This (nonstandard) specialized method is provided to allow simulation of |
|
2430 |
and operations with C semantics." |
|
2431 |
||
2432 |
%{ /* NOCONTEXT */ |
|
2433 |
int val1, val2, rslt; |
|
2434 |
||
2435 |
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
|
2436 |
val1 = __intVal(self); |
2458 | 2437 |
} 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
|
2438 |
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
|
2439 |
if (!val1) goto bad; |
2458 | 2440 |
} 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
|
2441 |
goto bad; |
2458 | 2442 |
} |
2443 |
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
|
2444 |
val2 = __intVal(anInteger); |
2458 | 2445 |
} 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
|
2446 |
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
|
2447 |
if (!val2) goto bad; |
2458 | 2448 |
} 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
|
2449 |
goto bad; |
2458 | 2450 |
} |
2451 |
rslt = val1 & val2; |
|
2452 |
RETURN(__MKINT(rslt)); |
|
2453 |
||
2454 |
bad: ; |
|
2455 |
%}. |
|
2456 |
self primitiveFailed. |
|
2457 |
! |
|
2458 |
||
2459 |
bitOr_32:anInteger |
|
2460 |
"return a C-semantic 32bit locical-or of the receiver and |
|
2461 |
the argument. Both must be either Small- or LargeIntegers. |
|
2462 |
This (nonstandard) specialized method is provided to allow simulation of |
|
2463 |
and operations with C semantics." |
|
2464 |
||
2465 |
%{ /* NOCONTEXT */ |
|
2466 |
int val1, val2, rslt; |
|
2467 |
||
2468 |
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
|
2469 |
val1 = __intVal(self); |
2458 | 2470 |
} 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
|
2471 |
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
|
2472 |
if (!val1) goto bad; |
2458 | 2473 |
} 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
|
2474 |
goto bad; |
2458 | 2475 |
} |
2476 |
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
|
2477 |
val2 = __intVal(anInteger); |
2458 | 2478 |
} 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
|
2479 |
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
|
2480 |
if (!val2) goto bad; |
2458 | 2481 |
} 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
|
2482 |
goto bad; |
2458 | 2483 |
} |
2484 |
rslt = val1 | val2; |
|
2485 |
RETURN(__MKINT(rslt)); |
|
2486 |
||
2487 |
bad: ; |
|
2488 |
%}. |
|
2489 |
self primitiveFailed. |
|
2490 |
! ! |
|
2491 |
||
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2492 |
!Integer methodsFor:'testing'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2493 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2494 |
isLiteral |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2495 |
"return true, if the receiver can be used as a literal constant in ST syntax |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2496 |
(i.e. can be used in constant arrays)" |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2497 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2498 |
^ true |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2499 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2500 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2501 |
!Integer methodsFor:'tracing'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2502 |
|
4682 | 2503 |
traceInto:aRequestor level:level from:referrer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2504 |
"double dispatch into tracer, passing my type implicitely in the selector" |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2505 |
|
4682 | 2506 |
^ aRequestor traceInteger:self level:level from:referrer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2507 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2508 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2509 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
2510 |
|
701 | 2511 |
!Integer methodsFor:'truncation & rounding'! |
2512 |
||
2513 |
ceiling |
|
1558 | 2514 |
"return the smallest integer which is larger or equal to the receiver. |
2515 |
For integers, this is the receiver itself." |
|
345 | 2516 |
|
701 | 2517 |
^ self |
1558 | 2518 |
|
2519 |
"Modified: 18.7.1996 / 12:44:06 / cg" |
|
701 | 2520 |
! |
345 | 2521 |
|
701 | 2522 |
compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2523 |
"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
|
2524 |
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
|
2525 |
zeros removed. This method is redefined in LargeInteger." |
701 | 2526 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2527 |
^ self |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2528 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2529 |
"Modified: 5.11.1996 / 14:07:41 / cg" |
202 | 2530 |
! |
2531 |
||
701 | 2532 |
floor |
1558 | 2533 |
"return the largest integer which is smaller or equal to the receiver. |
2534 |
For integers, this is the receiver itself." |
|
701 | 2535 |
|
2536 |
^ self |
|
1558 | 2537 |
|
2538 |
"Modified: 18.7.1996 / 12:44:00 / cg" |
|
701 | 2539 |
! |
202 | 2540 |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2541 |
fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2542 |
"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
|
2543 |
(i.e. the receiver minus its truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2544 |
Since integers have no fraction, return 0 here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2545 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2546 |
^ 0 |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2547 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2548 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2549 |
1234.56789 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2550 |
1.2345e6 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2551 |
1000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2552 |
10000000000000000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2553 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2554 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2555 |
"Modified: 4.11.1996 / 20:27:44 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2556 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2557 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2558 |
integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2559 |
"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
|
2560 |
(i.e. the receivers truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2561 |
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
|
2562 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2563 |
^ self |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2564 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2565 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2566 |
1234.56789 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2567 |
1.2345e6 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2568 |
1000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2569 |
10000000000000000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2570 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2571 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2572 |
"Modified: 4.11.1996 / 20:28:22 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2573 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
2574 |
|
701 | 2575 |
normalize |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2576 |
"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
|
2577 |
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
|
2578 |
zeros removed. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2579 |
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
|
2580 |
renamed to #compressed for ST-80 compatibility." |
1 | 2581 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2582 |
^ self compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2583 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2584 |
"Modified: 5.11.1996 / 14:08:24 / cg" |
701 | 2585 |
! |
1 | 2586 |
|
701 | 2587 |
rounded |
2588 |
"return the receiver rounded toward the next Integer - |
|
1558 | 2589 |
for integers this is the receiver itself." |
701 | 2590 |
|
2591 |
^ self |
|
1558 | 2592 |
|
2593 |
"Modified: 18.7.1996 / 12:44:24 / cg" |
|
701 | 2594 |
! |
69 | 2595 |
|
701 | 2596 |
truncated |
2597 |
"return the receiver truncated towards zero - |
|
1558 | 2598 |
for integers this is the receiver itself." |
159 | 2599 |
|
701 | 2600 |
^ self |
1558 | 2601 |
|
2602 |
"Modified: 18.7.1996 / 12:44:33 / cg" |
|
701 | 2603 |
! ! |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
2604 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2605 |
!Integer::ModuloNumber class methodsFor:'documentation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2606 |
|
4803 | 2607 |
copyright |
2608 |
" |
|
2609 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
2610 |
All Rights Reserved |
|
2611 |
||
2612 |
This software is furnished under a license and may be used |
|
2613 |
only in accordance with the terms of that license and with the |
|
2614 |
inclusion of the above copyright notice. This software may not |
|
2615 |
be provided or otherwise made available to, or used by, any |
|
2616 |
other person. No title to or ownership of the software is |
|
2617 |
hereby transferred. |
|
2618 |
" |
|
2619 |
||
2620 |
! |
|
2621 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2622 |
documentation |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2623 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2624 |
This is a helper class to perform fast computation of the modulus. |
4609 | 2625 |
(with big numbers, this does make a difference) |
4803 | 2626 |
|
2627 |
WARNING: this does only work with numbers which have no common |
|
2628 |
divisor (which is true for cryptographic applications). |
|
2629 |
So, use this only if you know what you are doing ... |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2630 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2631 |
[author:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2632 |
Stefan Vogel |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2633 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2634 |
[see also:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2635 |
Integer SmallInteger LargeInsteger |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2636 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2637 |
[instance variables:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2638 |
modulus the modulus |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2639 |
reciprocal reciprocal of the modulus |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2640 |
shift shift count to cut off some bits |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2641 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2642 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2643 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2644 |
examples |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2645 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2646 |
[exBegin] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2647 |
17 asModuloNumber modulusOf:38 |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2648 |
[exEnd] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2649 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2650 |
[exBegin] |
4609 | 2651 |
38 \\ 17 |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2652 |
[exEnd] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2653 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2654 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2655 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2656 |
!Integer::ModuloNumber class methodsFor:'instance creation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2657 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2658 |
modulus:anInteger |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2659 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2660 |
^ self new modulus:anInteger |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2661 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2662 |
"Created: / 3.5.1999 / 11:13:15 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2663 |
"Modified: / 3.5.1999 / 11:18:37 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2664 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2665 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2666 |
!Integer::ModuloNumber methodsFor:'accessing'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2667 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2668 |
modulus |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2669 |
"return the modulus" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2670 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2671 |
^ modulus |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2672 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2673 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2674 |
modulus:n |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2675 |
"set the modulus" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2676 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2677 |
modulus := n. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2678 |
reciprocal := n integerReciprocal. |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2679 |
shift := n highBit negated. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2680 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2681 |
"Created: / 3.5.1999 / 10:02:39 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2682 |
"Modified: / 3.5.1999 / 14:30:49 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2683 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2684 |
|
5238 | 2685 |
!Integer::ModuloNumber methodsFor:'arithmetic'! |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2686 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2687 |
modulusOf:aNumber |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2688 |
"compute the aNumber modulo myself. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2689 |
The shortcut works only, if aNumber is < modulo * modulo |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2690 |
(When doing arithmethic modulo something). |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2691 |
Otherwise do it the long way" |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2692 |
|
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2693 |
|e t cnt| |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2694 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2695 |
"throw off low nbits(modulus)" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2696 |
|
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2697 |
e := (aNumber bitShift:shift). |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2698 |
e := e * reciprocal. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2699 |
e := (e bitShift:shift). |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2700 |
e := e * modulus. |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2701 |
e := aNumber - e. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2702 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2703 |
"this subtract is done max 2 times" |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2704 |
cnt := 2. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2705 |
[(t := e - modulus) >= 0] whileTrue:[ |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2706 |
e := t. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2707 |
cnt == 0 ifTrue:[ |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2708 |
^ e \\ modulus. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2709 |
]. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2710 |
cnt := cnt - 1. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2711 |
]. |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2712 |
^ e. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2713 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2714 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2715 |
|m| |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2716 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2717 |
m := self new modulus:7. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2718 |
m modulusOf:55. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2719 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2720 |
|
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
2721 |
"Shortcut does not work: (size of divisor vs. dividend): |
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2722 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2723 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2724 |
m := self new modulus:7. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2725 |
m modulusOf:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2726 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2727 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2728 |
"SLOW (using standard \\ operation): |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2729 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2730 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2731 |
m := 123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2732 |
Time millisecondsToRun:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2733 |
1000 timesRepeat:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2734 |
874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658 \\ m |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2735 |
] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2736 |
] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2737 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2738 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2739 |
"fast (using moduloNumber with almost same-sized dividend and divisor): |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2740 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2741 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2742 |
m := self new modulus:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2743 |
Time millisecondsToRun:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2744 |
1000 timesRepeat:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2745 |
m modulusOf:874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2746 |
] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2747 |
] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2748 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2749 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2750 |
"Modified: / 3.5.1999 / 14:30:32 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2751 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2752 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2753 |
!Integer::ModuloNumber methodsFor:'converting'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2754 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2755 |
asModuloNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2756 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2757 |
^ self |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2758 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2759 |
"Created: / 3.5.1999 / 14:48:27 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2760 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
2761 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2762 |
!Integer class methodsFor:'documentation'! |
701 | 2763 |
|
2764 |
version |
|
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
2765 |
^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.144 2002-03-28 11:02:13 cg Exp $' |
1 | 2766 |
! ! |
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
2767 |
Integer initialize! |