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