author | Claus Gittinger <cg@exept.de> |
Mon, 26 Jan 2009 09:53:00 +0100 | |
changeset 11496 | 74c8c4d468d7 |
parent 11495 | 833b96ce156d |
child 11499 | fa02ab9a83f0 |
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 |
" |
|
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
13 |
|
1 | 14 |
Number subclass:#Integer |
1210 | 15 |
instanceVariableNames:'' |
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
16 |
classVariableNames:'DefaultDisplayRadix BCDConversionErrorSignal' |
1210 | 17 |
poolDictionaries:'' |
18 |
category:'Magnitude-Numbers' |
|
1 | 19 |
! |
20 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
21 |
Object subclass:#ModuloNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
22 |
instanceVariableNames:'modulus reciprocal shift' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
23 |
classVariableNames:'' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
24 |
poolDictionaries:'' |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
25 |
privateIn:Integer |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
26 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
27 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
28 |
!Integer class methodsFor:'documentation'! |
88 | 29 |
|
30 |
copyright |
|
31 |
" |
|
32 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
159 | 33 |
All Rights Reserved |
1 | 34 |
|
88 | 35 |
This software is furnished under a license and may be used |
36 |
only in accordance with the terms of that license and with the |
|
37 |
inclusion of the above copyright notice. This software may not |
|
38 |
be provided or otherwise made available to, or used by, any |
|
39 |
other person. No title to or ownership of the software is |
|
40 |
hereby transferred. |
|
41 |
" |
|
42 |
! |
|
1 | 43 |
|
88 | 44 |
documentation |
45 |
" |
|
46 |
abstract superclass for all integer numbers. |
|
47 |
See details in concrete subclasses LargeInteger and SmallInteger. |
|
212 | 48 |
|
1892 | 49 |
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
|
50 |
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
|
51 |
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
|
52 |
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
|
53 |
int op float -> float |
1892 | 54 |
|
1295 | 55 |
[Class variables:] |
212 | 56 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
(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
|
62 |
- I know - I am lazy ;-). Default is 10. |
212 | 63 |
|
1295 | 64 |
|
65 |
[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
|
66 |
Claus Gittinger |
1556 | 67 |
|
68 |
[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
|
69 |
Number |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
70 |
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
|
71 |
Float ShortFloat Fraction FixedPoint |
88 | 72 |
" |
73 |
! ! |
|
1 | 74 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
75 |
!Integer class methodsFor:'instance creation'! |
92 | 76 |
|
3044 | 77 |
byte1:b1 byte2:b2 byte3:b3 byte4:b4 |
78 |
"Squeak compatibility: |
|
79 |
Return an Integer given four value bytes. |
|
80 |
The returned integer is either a Small- or a LargeInteger" |
|
81 |
||
82 |
|t| |
|
83 |
||
84 |
t := b4. |
|
85 |
t := (t bitShift:8) + b3. |
|
86 |
t := (t bitShift:8) + b2. |
|
87 |
^ (t bitShift:8) + b1. |
|
88 |
||
89 |
" |
|
90 |
(Integer byte1:16r10 byte2:16r32 byte3:16r54 byte4:16r76) hexPrintString |
|
91 |
(Integer byte1:16r00 byte2:16r11 byte3:16r22 byte4:16r33) hexPrintString |
|
92 |
" |
|
93 |
||
94 |
"Created: 19.10.1997 / 18:08:52 / cg" |
|
95 |
"Modified: 19.10.1997 / 18:09:04 / cg" |
|
96 |
! |
|
97 |
||
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
98 |
fastFromString:aString at:startIndex |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
99 |
"return the next unsigned Integer from the string |
4610 | 100 |
as a decimal number, starting at startIndex. |
101 |
The number must be in the native machines int range |
|
102 |
(i.e. 63bit on alpha / 31 on all others); |
|
103 |
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
|
104 |
No spaces are skipped. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
105 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
numbers." |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
110 |
%{ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
111 |
if (__isString(aString) && __isSmallInteger(startIndex)) { |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
112 |
char *cp = (char *)(__stringVal(aString)); |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
113 |
int idx = __intVal(startIndex) - 1; |
4610 | 114 |
unsigned INT val; |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
115 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
116 |
if ((unsigned)idx < __stringSize(aString)) { |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
117 |
val = atoi(cp + idx); |
4610 | 118 |
if (val <= _MAX_INT) { |
8913 | 119 |
RETURN(__mkSmallInteger(val)); |
4610 | 120 |
} |
121 |
RETURN (__MKUINT(val)); |
|
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
122 |
} |
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 |
%}. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
125 |
self primitiveFailed. |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
126 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
127 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
128 |
Integer fastFromString:'12345' at:1 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
129 |
Integer fastFromString:'12345' at:2 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
130 |
Integer fastFromString:'12345' at:3 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
131 |
Integer fastFromString:'12345' at:4 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
132 |
Integer fastFromString:'12345' at:5 |
4610 | 133 |
Integer fastFromString:'1234512345' at:1 |
134 |
Integer fastFromString:'2147483647' at:1 |
|
4611 | 135 |
|
4610 | 136 |
Integer fastFromString:'4294967295' at:1 |
4611 | 137 |
Integer fastFromString:'12345' at:6 |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
138 |
Integer fastFromString:'12345' at:0 |
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 |
Time millisecondsToRun:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
141 |
100000 timesRepeat:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
142 |
Integer readFrom:'12345' |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
143 |
] |
4610 | 144 |
] |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
145 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
146 |
|
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
147 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
148 |
Time millisecondsToRun:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
149 |
100000 timesRepeat:[ |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
150 |
Integer fastFromString:'12345' at:1 |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
151 |
] |
4610 | 152 |
] |
3639
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
153 |
" |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
154 |
! |
c4fbb700a569
added #fastFromString: (for Andis IEC-SocketInterface)
Claus Gittinger <cg@exept.de>
parents:
3638
diff
changeset
|
155 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
156 |
fromBCDBytes:aByteArray |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
157 |
"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
|
158 |
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
|
159 |
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
|
160 |
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
|
161 |
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
|
162 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
163 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
164 |
|val| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
165 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
166 |
val := 0. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
167 |
aByteArray do:[:twoDigits | |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
168 |
|hi lo| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
169 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
170 |
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
|
171 |
lo := twoDigits bitAnd:16r0F. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
172 |
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
|
173 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
174 |
^ val |
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 |
" |
4921 | 177 |
Integer fromBCDBytes:#[16r12 16r34 16r56] |
178 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78] |
|
179 |
Integer fromBCDBytes:#[16r12 16r34 16r56 16r78 16r90] |
|
180 |
Integer fromBCDBytes:#[16r98 16r76 16r54] |
|
181 |
Integer fromBCDBytes:#[16r98 16r76 16r54 16r32] |
|
182 |
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
|
183 |
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
|
184 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
185 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
186 |
|
5171 | 187 |
fromSwappedBCDBytes:aByteArray |
188 |
"given a byteArray in BCD format, return an appropriate integer. |
|
189 |
The byteArray must contain the BCD encoded decimal string, |
|
190 |
starting with the LEAST significant digits. |
|
191 |
This conversion is useful for some communication protocols, |
|
192 |
or control systems (e.g. SMC), which represent big numbers this way... |
|
193 |
" |
|
194 |
||
195 |
|val| |
|
196 |
||
197 |
val := 0. |
|
198 |
aByteArray do:[:twoDigits | |
|
199 |
|hi lo| |
|
200 |
||
201 |
lo := (twoDigits bitShift:-4) bitAnd:16r0F. |
|
202 |
hi := twoDigits bitAnd:16r0F. |
|
5882
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
203 |
lo <= 9 ifTrue:[ |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
204 |
val := (val * 100) + (hi * 10) + lo |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
205 |
] ifFalse:[ |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
206 |
"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
|
207 |
val := (val * 10) + hi. |
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
208 |
]. |
5171 | 209 |
]. |
210 |
^ val |
|
211 |
||
212 |
" |
|
213 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56] |
|
5882
8574ffbe45fb
Fix #fromSwappedBCDBytes: for an odd number of digits
Stefan Vogel <sv@exept.de>
parents:
5816
diff
changeset
|
214 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16rF6] |
5171 | 215 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78] |
216 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90] |
|
217 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54] |
|
218 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32] |
|
219 |
Integer fromSwappedBCDBytes:#[16r98 16r76 16r54 16r32 16r10] |
|
220 |
Integer fromSwappedBCDBytes:#[16r12 16r34 16r56 16r78 16r90 16r12 16r34 16r56 16r78 16r90] |
|
221 |
" |
|
222 |
! |
|
223 |
||
92 | 224 |
new:numberOfBytes neg:negative |
225 |
"for ST-80 compatibility: |
|
226 |
Return an empty Integer (uninitialized value) with space for |
|
227 |
numberOfBytes bytes (= digitLength). The additional argument |
|
132 | 228 |
negative specifies if the result should be a negative number. |
229 |
The digits can be stored byte-wise into the result, using digitAt:put:" |
|
92 | 230 |
|
132 | 231 |
^ (LargeInteger basicNew numberOfDigits:numberOfBytes) |
159 | 232 |
sign:(negative ifTrue:[-1] ifFalse:[1]) |
701 | 233 |
! |
234 |
||
235 |
readFrom:aStringOrStream |
|
236 |
"return the next unsigned Integer from the (character-)stream aStream |
|
237 |
as decimal number. |
|
238 |
||
239 |
NOTICE: |
|
240 |
This behaves different from the default readFrom:, in returning |
|
241 |
0 (instead of raising an error) in case no number can be read. |
|
242 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
243 |
- depending on the upcoming ANSI standard, this may change." |
|
244 |
||
245 |
^ self readFrom:aStringOrStream onError:0 |
|
246 |
||
247 |
" |
|
248 |
Integer readFrom:(ReadStream on:'foobar') |
|
249 |
Integer readFrom:(ReadStream on:'foobar') onError:nil |
|
250 |
" |
|
251 |
! |
|
252 |
||
253 |
readFrom:aStringOrStream onError:exceptionBlock |
|
254 |
"return the next Integer from the (character-)stream aStream, |
|
255 |
handling initial XXr for arbitrary radix numbers and initial sign. |
|
256 |
Also, all initial whitespace is skipped. |
|
257 |
If the string does not represent a valid integer number, |
|
258 |
return the value of exceptionBlock." |
|
259 |
||
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
260 |
|value| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
261 |
|
7093
60bad14d38fc
Convert Object>>errorSignal -> Error
Stefan Vogel <sv@exept.de>
parents:
6907
diff
changeset
|
262 |
Error handle:[:ex | |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
263 |
^ exceptionBlock value |
1700 | 264 |
] do:[ |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
265 |
|str nextChar negative| |
701 | 266 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
267 |
str := aStringOrStream readStream. |
701 | 268 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
269 |
nextChar := str skipSeparators. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
270 |
(nextChar == $-) ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
271 |
negative := true. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
272 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
273 |
nextChar := str peekOrNil |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
274 |
] ifFalse:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
275 |
negative := false |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
276 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
277 |
(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
|
278 |
" |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
279 |
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
|
280 |
" |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
281 |
^ exceptionBlock value |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
282 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
283 |
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
|
284 |
nextChar := str peekOrNil. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
285 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
286 |
"-xxr<number> is invalid; should be xxr-<val>" |
1700 | 287 |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
288 |
negative ifTrue:[ |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
289 |
'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
|
290 |
negative := false |
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 |
str next. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
293 |
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
|
294 |
]. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
295 |
negative ifTrue:[ |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
296 |
value := value negated |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
297 |
]. |
701 | 298 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
299 |
^ value |
701 | 300 |
|
301 |
" |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3993
diff
changeset
|
302 |
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
|
303 |
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
|
304 |
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
|
305 |
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
|
306 |
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
|
307 |
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
|
308 |
Integer readFrom:'foo' |
701 | 309 |
" |
310 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
311 |
"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
|
312 |
"Modified: / 14.4.1998 / 19:17:28 / cg" |
701 | 313 |
! |
314 |
||
315 |
readFrom:aStringOrStream radix:radix |
|
316 |
"return the next unsigned Integer from the (character-)stream aStream |
|
317 |
in radix; (assumes that the initial XXr has already been read). |
|
318 |
No whitespace-skipping is done. |
|
319 |
Returns 0 if no number available. |
|
320 |
||
321 |
NOTICE: |
|
322 |
This behaves different from the default readFrom:, in returning |
|
323 |
0 (instead of raising an error) in case no number can be read. |
|
324 |
It is unclear, if this is the correct behavior (ST-80 does this) |
|
325 |
- depending on the upcoming ANSI standard, this may change." |
|
326 |
||
327 |
^ self readFrom:aStringOrStream radix:radix onError:0 |
|
328 |
! |
|
329 |
||
330 |
readFrom:aStringOrStream radix:radix onError:exceptionBlock |
|
331 |
"return the next unsigned Integer from the (character-)stream aStream |
|
332 |
in radix; (assumes that the initial XXr has already been read). |
|
333 |
No whitespace-skipping is done. |
|
334 |
Returns the value of exceptionBlock, if no number is available." |
|
335 |
||
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
336 |
|str nextChar nextChar2 value |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
337 |
r "{ Class: SmallInteger }" |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
338 |
r2 "{ Class: SmallInteger }"| |
701 | 339 |
|
340 |
str := aStringOrStream readStream. |
|
341 |
||
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
342 |
nextChar := str peekOrNil. |
701 | 343 |
(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
|
344 |
^ exceptionBlock value |
701 | 345 |
]. |
346 |
||
347 |
value := nextChar digitValue. |
|
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
348 |
str next. |
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
349 |
nextChar := str peekOrNil. |
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
350 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
351 |
"/ OLD code |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
352 |
"/ [nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
353 |
"/ str next. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
354 |
"/ value := value * radix + nextChar digitValue. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
355 |
"/ nextChar := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
356 |
"/ ]. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
357 |
"/ ^ value. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
358 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
359 |
"/ 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
|
360 |
"/ converted number is large |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
361 |
"/ (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
|
362 |
"/ It should not be slower for smallIntegers. |
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 |
r := radix. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
365 |
r2 := r * r. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
366 |
|
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
367 |
[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
|
368 |
str next. |
4169
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
369 |
nextChar2 := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
370 |
(nextChar2 notNil and:[nextChar2 isDigitRadix:r]) ifTrue:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
371 |
str next. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
372 |
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
|
373 |
nextChar := str peekOrNil. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
374 |
] ifFalse:[ |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
375 |
^ (value * r) + nextChar digitValue. |
7079cb06963b
tuned string -> integer conversion for Large numbers.
Claus Gittinger <cg@exept.de>
parents:
4158
diff
changeset
|
376 |
] |
701 | 377 |
]. |
378 |
^ value |
|
379 |
||
380 |
" |
|
381 |
Integer readFrom:(ReadStream on:'12345') radix:10 |
|
382 |
Integer readFrom:(ReadStream on:'FFFF') radix:16 |
|
383 |
Integer readFrom:(ReadStream on:'1010') radix:2 |
|
384 |
Integer readFrom:(ReadStream on:'foobar') radix:10 |
|
385 |
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
|
386 |
Integer readFrom:'gg' radix:10 onError:0 |
701 | 387 |
" |
3374
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
388 |
|
6897f584accb
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3298
diff
changeset
|
389 |
"Modified: / 14.4.1998 / 19:16:46 / cg" |
6635 | 390 |
! |
391 |
||
6673 | 392 |
readFromRomanString:aStringOrStream |
393 |
"convert a string or stream containing a roman representation into an integer. |
|
11125 | 394 |
Raises a RomanNumberFormatError, if the inputs format is completely wrong. |
6682 | 395 |
Raises BadRomanNumberFormatError if its wrong, but could be parsed. |
396 |
Notifies via NaiveRomanNumberFormatNotification, if its a bit wrong (naive format). |
|
397 |
Will read both real and naive roman numbers (see printRomanOn: vs. printRomanOn:naive:), |
|
398 |
however, a notification is raised for for naive numbers (catch it if you are interested in it)." |
|
6635 | 399 |
|
400 |
|romanValues s c val digitVal prevDigitVal countSame delta |
|
401 |
stopOnSeparator finish| |
|
402 |
||
403 |
romanValues := Dictionary |
|
404 |
withKeys:#($M $D $C $L $X $V $I) |
|
405 |
andValues:#(1000 500 100 50 10 5 1). |
|
406 |
||
407 |
(stopOnSeparator := aStringOrStream isStream) ifFalse:[ |
|
408 |
s := aStringOrStream readStream. |
|
409 |
]. |
|
410 |
s atEnd ifTrue:[ |
|
6673 | 411 |
^ RomanNumberFormatError raiseErrorString:'empty string' |
6635 | 412 |
]. |
413 |
val := 0. |
|
414 |
prevDigitVal := 99999. |
|
415 |
countSame := 1. |
|
416 |
finish := false. |
|
417 |
||
418 |
[s atEnd or:[finish]] whileFalse:[ |
|
419 |
c := s next asUppercase. |
|
420 |
c isSeparator ifTrue:[ |
|
421 |
stopOnSeparator ifFalse:[ |
|
6673 | 422 |
^ RomanNumberFormatError raiseErrorString:'garbage at the end' |
6635 | 423 |
]. |
424 |
finish := true. |
|
425 |
] ifFalse:[ |
|
426 |
digitVal := romanValues at:c ifAbsent:nil. |
|
427 |
digitVal isNil ifTrue:[ |
|
6673 | 428 |
^ RomanNumberFormatError raiseErrorString:'invalid character' |
6635 | 429 |
]. |
430 |
||
431 |
digitVal = prevDigitVal ifTrue:[ |
|
432 |
( #( 1 10 100 1000) includes:digitVal) ifFalse:[ |
|
6673 | 433 |
^ RomanNumberFormatError raiseErrorString:'character may not be repeated' |
6635 | 434 |
]. |
435 |
val := val + digitVal. |
|
436 |
countSame := countSame + 1. |
|
6673 | 437 |
countSame >= 4 ifTrue:[ |
6635 | 438 |
digitVal ~= 1000 ifTrue:[ |
6673 | 439 |
countSame > 4 ifTrue:[ |
6682 | 440 |
"/ this is a bad roman number (such as MCCCCCCCCXXXXXXII); |
441 |
"/ Its not correct, but sometimes encountered on buildings. |
|
442 |
"/ If you do not want to be too picky, |
|
443 |
"/ provide a proceeding handler in order to proceed the conversion. |
|
444 |
BadRomanNumberFormatError raiseRequestErrorString:'more than 4 occurrences of same character' |
|
445 |
] ifFalse:[ |
|
446 |
"/ this is a naive roman number (such as VIIII); |
|
447 |
"/ Its not correct, but very often encountered (especially as page numbers). |
|
448 |
"/ The notification below normally goes unnoticed, unless some input validator |
|
449 |
"/ wants to be very picky, and treat this as an error. |
|
450 |
"/ To do so, provide a handler for NaiveRomanNumberFormatNotification. |
|
451 |
NaiveRomanNumberFormatNotification raiseRequestErrorString:'more than 3 occurrences of same character'. |
|
452 |
] |
|
6635 | 453 |
] |
454 |
]. |
|
455 |
] ifFalse:[ |
|
456 |
digitVal < prevDigitVal ifTrue:[ |
|
457 |
val := val + digitVal. |
|
458 |
] ifFalse:[ |
|
459 |
countSame == 1 ifFalse:[ |
|
6673 | 460 |
^ RomanNumberFormatError raiseErrorString:'invalid character combination' |
6635 | 461 |
]. |
462 |
delta := digitVal - prevDigitVal. |
|
463 |
( #( 4 9 40 90 400 900) includes:delta) ifFalse:[ |
|
6673 | 464 |
^ RomanNumberFormatError raiseErrorString:'invalid character combination' |
6635 | 465 |
]. |
466 |
val := val - prevDigitVal. |
|
467 |
val := val + delta. |
|
468 |
digitVal := prevDigitVal - 0.1. "/ trick: prevent prevDigit from arriving again. |
|
469 |
]. |
|
470 |
countSame := 1. |
|
471 |
]. |
|
472 |
prevDigitVal := digitVal. |
|
473 |
]. |
|
474 |
]. |
|
475 |
"/ val > 5000 ifTrue:[ |
|
6673 | 476 |
"/ ^ RomanNumberFormatError raiseErrorStirng:'number out of range (1..5000)' |
6635 | 477 |
"/ ]. |
478 |
^ val. |
|
479 |
||
480 |
" |
|
6673 | 481 |
Integer readFromRomanString:'I' |
6682 | 482 |
Integer readFromRomanString:'II' |
6673 | 483 |
Integer readFromRomanString:'III' |
484 |
Integer readFromRomanString:'IV' |
|
485 |
Integer readFromRomanString:'clix' |
|
486 |
Integer readFromRomanString:'MIX' |
|
487 |
Integer readFromRomanString:'MCMXCIX' |
|
488 |
||
6682 | 489 |
Naive cases (which are accepted): |
490 |
Integer readFromRomanString:'IIII' |
|
491 |
Integer readFromRomanString:'VIIII' |
|
492 |
Integer readFromRomanString:'CLXXXXVIIII' |
|
493 |
||
494 |
Error case (not proceedable): |
|
6673 | 495 |
Integer readFromRomanString:'LC' |
6682 | 496 |
|
497 |
Error case (proceedable): |
|
498 |
Integer readFromRomanString:'MCCCCCCCCXXXXXXIIIIII' |
|
499 |
||
500 |
BadRomanNumberFormatError ignoreIn:[ |
|
501 |
Integer readFromRomanString:'MCCCCCCCCXXXXXXIIIIII' |
|
502 |
] |
|
503 |
" |
|
504 |
||
505 |
||
506 |
"naive cases: |
|
507 |
#( |
|
508 |
'MCMXCIX' 1999 |
|
509 |
'MCMXCVIIII' 1999 |
|
510 |
'MCMLXXXXIX' 1999 |
|
511 |
'MDCCCCXCIX' 1999 |
|
512 |
'MDCCCCXCVIIII' 1999 |
|
513 |
'MDCCCCLXXXXIX' 1999 |
|
514 |
'MDCCCCLXXXXVIIII' 1999 |
|
515 |
) pairWiseDo:[:goodString :expectedValue | |
|
516 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
|
517 |
] |
|
6635 | 518 |
" |
519 |
||
6673 | 520 |
|
6635 | 521 |
"error cases: |
522 |
#( |
|
523 |
'XIIX' |
|
524 |
'VV' |
|
525 |
'VVV' |
|
526 |
'XXL' |
|
527 |
'XLX' |
|
528 |
'LC' |
|
529 |
'LL' |
|
530 |
'DD' |
|
531 |
) do:[:badString | |
|
6673 | 532 |
(Integer readFromRomanString:badString onError:nil) notNil ifTrue:[self halt]. |
6635 | 533 |
] |
534 |
" |
|
535 |
||
536 |
"good cases: |
|
537 |
#( 'I' 1 |
|
538 |
'II' 2 |
|
539 |
'III' 3 |
|
540 |
'IV' 4 |
|
541 |
'V' 5 |
|
542 |
'VI' 6 |
|
543 |
'VII' 7 |
|
544 |
'VIII' 8 |
|
545 |
'IX' 9 |
|
546 |
'X' 10 |
|
547 |
'XI' 11 |
|
548 |
'XII' 12 |
|
549 |
'XIII' 13 |
|
550 |
'XIV' 14 |
|
551 |
'XV' 15 |
|
552 |
'XVI' 16 |
|
553 |
'XVII' 17 |
|
554 |
'XVIII' 18 |
|
555 |
'XIX' 19 |
|
556 |
'XX' 20 |
|
557 |
'XXX' 30 |
|
558 |
'L' 50 |
|
559 |
'XL' 40 |
|
560 |
'LX' 60 |
|
561 |
'LXX' 70 |
|
562 |
'LXXX' 80 |
|
563 |
'CXL' 140 |
|
564 |
'CL' 150 |
|
565 |
'CLX' 160 |
|
566 |
'MMM' 3000 |
|
567 |
'MMMM' 4000 |
|
568 |
'MMMMCMXCIX' 4999 |
|
569 |
'MMMMMMMMMCMXCIX' 9999 |
|
570 |
) pairWiseDo:[:goodString :expectedValue | |
|
6673 | 571 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
6635 | 572 |
] |
573 |
" |
|
574 |
||
575 |
" |
|
576 |
1 to:9999 do:[:n | |
|
577 |
|romanString| |
|
578 |
||
579 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
6673 | 580 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
581 |
] |
|
582 |
" |
|
583 |
! |
|
584 |
||
585 |
readFromRomanString:aStringOrStream onError:exceptionalValue |
|
586 |
"convert a string or stream containing a roman representation into an integer. |
|
6682 | 587 |
Raises an exception, if the inputs format is wrong. |
588 |
Does allow reading of naive (more than 3 in a row) and |
|
589 |
bad (not using L and D) roman numbers. |
|
590 |
(Such numbers can be seen on some medevial buildings. " |
|
6673 | 591 |
|
592 |
|val| |
|
593 |
||
594 |
RomanNumberFormatError |
|
595 |
handle:[:ex | |
|
596 |
val := exceptionalValue value |
|
597 |
] |
|
598 |
do:[ |
|
599 |
val := self readFromRomanString:aStringOrStream |
|
600 |
]. |
|
601 |
^ val |
|
602 |
||
603 |
||
604 |
" |
|
605 |
Integer readFromRomanString:'I' onError:nil |
|
606 |
Integer readFromRomanString:'II' onError:nil |
|
607 |
Integer readFromRomanString:'III' onError:nil |
|
608 |
Integer readFromRomanString:'IV' onError:nil |
|
609 |
Integer readFromRomanString:'clix' onError:nil |
|
610 |
Integer readFromRomanString:'MCMXCIX' onError:nil |
|
611 |
||
6682 | 612 |
Naive cases (which are accepted): |
6673 | 613 |
Integer readFromRomanString:'IIII' onError:nil |
6682 | 614 |
Integer readFromRomanString:'VIIII' onError:nil |
615 |
Integer readFromRomanString:'CLXXXXVIIII' onError:nil |
|
616 |
||
617 |
Error cases: |
|
618 |
\ Integer readFromRomanString:'LC' onError:nil |
|
6673 | 619 |
" |
620 |
||
621 |
"error cases: |
|
622 |
#( |
|
623 |
'XIIX' |
|
624 |
'VV' |
|
625 |
'VVV' |
|
626 |
'XXL' |
|
627 |
'XLX' |
|
628 |
'LC' |
|
629 |
'LL' |
|
630 |
'DD' |
|
631 |
) do:[:badString | |
|
632 |
(Integer readFromRomanString:badString onError:nil) notNil ifTrue:[self halt]. |
|
633 |
] |
|
634 |
" |
|
635 |
||
6682 | 636 |
"naive (but handled) cases: |
637 |
#( |
|
638 |
'IIII' 4 |
|
639 |
'VIIII' 9 |
|
640 |
'XIIII' 14 |
|
641 |
'XVIIII' 19 |
|
642 |
) pairWiseDo:[:goodString :expectedValue | |
|
643 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
|
644 |
] |
|
645 |
" |
|
646 |
||
6673 | 647 |
"good cases: |
648 |
#( 'I' 1 |
|
649 |
'II' 2 |
|
650 |
'III' 3 |
|
651 |
'IV' 4 |
|
652 |
'V' 5 |
|
653 |
'VI' 6 |
|
654 |
'VII' 7 |
|
655 |
'VIII' 8 |
|
656 |
'IX' 9 |
|
657 |
'X' 10 |
|
658 |
'XI' 11 |
|
659 |
'XII' 12 |
|
660 |
'XIII' 13 |
|
661 |
'XIV' 14 |
|
662 |
'XV' 15 |
|
663 |
'XVI' 16 |
|
664 |
'XVII' 17 |
|
665 |
'XVIII' 18 |
|
666 |
'XIX' 19 |
|
667 |
'XX' 20 |
|
668 |
'XXX' 30 |
|
669 |
'L' 50 |
|
670 |
'XL' 40 |
|
671 |
'LX' 60 |
|
672 |
'LXX' 70 |
|
673 |
'LXXX' 80 |
|
674 |
'CXL' 140 |
|
675 |
'CL' 150 |
|
676 |
'CLX' 160 |
|
677 |
'MMM' 3000 |
|
678 |
'MMMM' 4000 |
|
679 |
'MMMMCMXCIX' 4999 |
|
680 |
'MMMMMMMMMCMXCIX' 9999 |
|
681 |
) pairWiseDo:[:goodString :expectedValue | |
|
682 |
(Integer readFromRomanString:goodString onError:nil) ~= expectedValue ifTrue:[self halt]. |
|
683 |
] |
|
684 |
" |
|
685 |
||
686 |
" |
|
687 |
1 to:9999 do:[:n | |
|
688 |
|romanString| |
|
689 |
||
690 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
691 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
692 |
] |
|
693 |
" |
|
694 |
||
695 |
"reading naive numbers: |
|
696 |
||
697 |
1 to:9999 do:[:n | |
|
698 |
|romanString| |
|
699 |
||
700 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:true]. |
|
6682 | 701 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
6635 | 702 |
] |
703 |
" |
|
8897
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
704 |
! |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
705 |
|
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
706 |
readFromString:aString radix:base onError:exceptionBlock |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
707 |
|str val| |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
708 |
|
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
709 |
str := ReadStream on:aString. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
710 |
val := self readFrom:str radix:base onError:[^ exceptionBlock value]. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
711 |
str atEnd ifFalse:[ ^ exceptionBlock value]. |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
712 |
^ val |
701 | 713 |
! ! |
714 |
||
9141 | 715 |
!Integer class methodsFor:'Compatibility-Squeak'! |
716 |
||
717 |
readFrom:aStringOrStream base:aBase |
|
718 |
"for squeak compatibility" |
|
719 |
||
720 |
^ self readFrom:aStringOrStream radix:aBase |
|
721 |
! ! |
|
722 |
||
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
723 |
!Integer class methodsFor:'Signal constants'! |
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 |
"return the signal which is raised when bcd conversion fails |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
727 |
(i.e. when trying to decode an invalid BCD number)" |
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 |
^ BCDConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
730 |
|
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
731 |
"Modified: / 15.11.1999 / 20:35:20 / cg" |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
732 |
! ! |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
733 |
|
7444 | 734 |
!Integer class methodsFor:'class initialization'! |
735 |
||
736 |
initialize |
|
737 |
BCDConversionErrorSignal isNil ifTrue:[ |
|
738 |
BCDConversionErrorSignal := ConversionError newSignal. |
|
739 |
BCDConversionErrorSignal nameClass:self message:#bcdConversionErrorSignal. |
|
740 |
BCDConversionErrorSignal notifierString:'bcd conversion error'. |
|
741 |
]. |
|
742 |
||
743 |
"Modified: / 15.11.1999 / 20:36:04 / cg" |
|
744 |
! ! |
|
745 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
746 |
!Integer class methodsFor:'constants'! |
701 | 747 |
|
7727 | 748 |
epsilon |
749 |
"return the maximum relative spacing" |
|
750 |
||
751 |
"don't know, what to really return here. |
|
8635 | 752 |
Returning 1 gives stupid values when doing some taylor series approximations |
753 |
(although it is correct)" |
|
7727 | 754 |
|
755 |
^ Float epsilon. |
|
8635 | 756 |
|
757 |
" |
|
758 |
2 sqrt_withAccuracy:(Integer epsilon) |
|
759 |
2 sqrt_withAccuracy:1 |
|
760 |
" |
|
7727 | 761 |
! |
762 |
||
701 | 763 |
unity |
1555 | 764 |
"return the neutral element for multiplication (1)" |
701 | 765 |
|
766 |
^ 1 |
|
1555 | 767 |
|
768 |
"Modified: 18.7.1996 / 12:26:43 / cg" |
|
701 | 769 |
! |
770 |
||
771 |
zero |
|
1555 | 772 |
"return the neutral element for addition (0)" |
701 | 773 |
|
774 |
^ 0 |
|
1555 | 775 |
|
776 |
"Modified: 18.7.1996 / 12:26:38 / cg" |
|
92 | 777 |
! ! |
778 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
779 |
!Integer class methodsFor:'misc'! |
49 | 780 |
|
781 |
displayRadix:aNumber |
|
54 | 782 |
"being tired of always sending #printStringRadix: in the inspectors, |
49 | 783 |
this allows you to change the default print radix for the displayString |
784 |
method." |
|
785 |
||
786 |
DefaultDisplayRadix := aNumber |
|
132 | 787 |
|
788 |
" |
|
789 |
Integer displayRadix:16. 123456 inspect |
|
790 |
Integer displayRadix:10. 123456 inspect |
|
791 |
" |
|
49 | 792 |
! ! |
793 |
||
9125 | 794 |
!Integer class methodsFor:'prime numbers'! |
795 |
||
796 |
largePrimesUpTo: max do: aBlock |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
797 |
"Evaluate aBlock with all primes up and including maxValue. |
9125 | 798 |
The Algorithm is adapted from http://www.rsok.com/~jrm/printprimes.html |
799 |
It encodes prime numbers much more compactly than #primesUpTo: |
|
800 |
38.5 integer per byte (2310 numbers per 60 byte) allow for some fun large primes. |
|
801 |
(all primes up to SmallInteger maxVal can be computed within ~27MB of memory; |
|
802 |
the regular #primesUpTo: would require 4 *GIGA*bytes). |
|
803 |
Note: The algorithm could be re-written to produce the first primes (which require |
|
804 |
the longest time to sieve) faster but only at the cost of clarity." |
|
805 |
||
806 |
| limit flags maskBitIndex bitIndex maskBit byteIndex index primesUpTo2310 indexLimit | |
|
807 |
||
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
808 |
limit := max asInteger. |
9125 | 809 |
indexLimit := max sqrt truncated + 1. |
810 |
"Create the array of flags." |
|
811 |
flags := ByteArray new: (limit + 2309) // 2310 * 60 + 60. |
|
812 |
flags atAllPut: 16rFF. "set all to true" |
|
813 |
||
814 |
"Compute the primes up to 2310" |
|
815 |
primesUpTo2310 := self primesUpTo: 2310. |
|
816 |
||
817 |
"Create a mapping from 2310 integers to 480 bits (60 byte)" |
|
818 |
maskBitIndex := Array new: 2310. |
|
819 |
bitIndex := -1. "for pre-increment" |
|
820 |
maskBitIndex at: 1 put: (bitIndex := bitIndex + 1). |
|
821 |
maskBitIndex at: 2 put: (bitIndex := bitIndex + 1). |
|
822 |
||
823 |
1 to: 5 do:[:i| aBlock value: (primesUpTo2310 at: i)]. |
|
824 |
||
825 |
index := 6. |
|
826 |
2 to: 2309 do:[:n| |
|
827 |
[(primesUpTo2310 at: index) < n] |
|
828 |
whileTrue:[index := index + 1]. |
|
829 |
n = (primesUpTo2310 at: index) ifTrue:[ |
|
830 |
maskBitIndex at: n+1 put: (bitIndex := bitIndex + 1). |
|
831 |
] ifFalse:[ |
|
832 |
"if modulo any of the prime factors of 2310, then could not be prime" |
|
833 |
(n \\ 2 = 0 or:[n \\ 3 = 0 or:[n \\ 5 = 0 or:[n \\ 7 = 0 or:[n \\ 11 = 0]]]]) |
|
834 |
ifTrue:[maskBitIndex at: n+1 put: 0] |
|
835 |
ifFalse:[maskBitIndex at: n+1 put: (bitIndex := bitIndex + 1)]. |
|
836 |
]. |
|
837 |
]. |
|
838 |
||
839 |
"Now the real work begins... |
|
840 |
Start with 13 since multiples of 2,3,5,7,11 are handled by the storage method; |
|
841 |
increment by 2 for odd numbers only." |
|
842 |
13 to: limit by: 2 do:[:n| |
|
843 |
(maskBit := maskBitIndex at: (n \\ 2310 + 1)) = 0 ifFalse:["not a multiple of 2,3,5,7,11" |
|
844 |
byteIndex := n // 2310 * 60 + (maskBit-1 bitShift: -3) + 1. |
|
845 |
bitIndex := 1 bitShift: (maskBit bitAnd: 7). |
|
846 |
((flags at: byteIndex) bitAnd: bitIndex) = 0 ifFalse:["not marked -- n is prime" |
|
847 |
aBlock value: n. |
|
848 |
"Start with n*n since any integer < n has already been sieved |
|
849 |
(e.g., any multiple of n with a number k < n has been cleared |
|
850 |
when k was sieved); add 2 * i to avoid even numbers and |
|
851 |
mark all multiples of this prime. Note: n < indexLimit below |
|
852 |
limits running into LargeInts -- nothing more." |
|
853 |
n < indexLimit ifTrue:[ |
|
854 |
index := n * n. |
|
855 |
(index bitAnd: 1) = 0 ifTrue:[index := index + n]. |
|
856 |
[index <= limit] whileTrue:[ |
|
857 |
(maskBit := maskBitIndex at: (index \\ 2310 + 1)) = 0 ifFalse:[ |
|
858 |
byteIndex := (index // 2310 * 60) + (maskBit-1 bitShift: -3) + 1. |
|
859 |
maskBit := 255 - (1 bitShift: (maskBit bitAnd: 7)). |
|
860 |
flags at: byteIndex put: ((flags at: byteIndex) bitAnd: maskBit). |
|
861 |
]. |
|
862 |
index := index + (2 * n)]. |
|
863 |
]. |
|
864 |
]. |
|
865 |
]. |
|
866 |
]. |
|
867 |
||
868 |
" |
|
869 |
Integer largePrimesUpTo:1000000 do:[:i | i > 900000 ifTrue:[self halt] ] |
|
870 |
(Integer primesUpTo:1000000) inspect |
|
871 |
" |
|
872 |
! |
|
873 |
||
874 |
primesUpTo: max |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
875 |
"Return a list of prime integers up to abd including the given integer." |
9125 | 876 |
|
877 |
^Array streamContents:[:s| self primesUpTo: max do:[:prime| s nextPut: prime]] |
|
878 |
||
879 |
" |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
880 |
Integer primesUpTo: 100 |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
881 |
Integer primesUpTo: 13 |
9316 | 882 |
(Integer primesUpTo: 100) select:[:p | (p >= 10) & (p <= 99)] |
9125 | 883 |
" |
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
884 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
885 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
886 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
887 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
888 |
N := 1000. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
889 |
p := 1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
890 |
a := (1 to:1000) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
891 |
collect:[:i | p := p nextPrime. p ] |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
892 |
thenSelect:[:p | p <= N]. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
893 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
894 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
895 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
896 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
897 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
898 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
899 |
N := 1000 nextPrime. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
900 |
p := 1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
901 |
a := (1 to:1000) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
902 |
collect:[:i | p := p nextPrime. p ] |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
903 |
thenSelect:[:p | p <= N]. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
904 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
905 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
906 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
907 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
908 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
909 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
910 |
N := 1000 nextPrime-1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
911 |
p := 1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
912 |
a := (1 to:1000) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
913 |
collect:[:i | p := p nextPrime. p ] |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
914 |
thenSelect:[:p | p <= N]. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
915 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
916 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
917 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
918 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
919 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
920 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
921 |
N := 100000. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
922 |
p := 1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
923 |
a := (1 to:N) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
924 |
collect:[:i | p := p nextPrime. p ] |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
925 |
thenSelect:[:p | p <= N]. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
926 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
927 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
928 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
929 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
930 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
931 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
932 |
N := 100000 nextPrime. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
933 |
p := 1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
934 |
a := (1 to:N) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
935 |
collect:[:i | p := p nextPrime. p ] |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
936 |
thenSelect:[:p | p <= N]. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
937 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
938 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
939 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
940 |
" |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
941 |
|p N a b| |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
942 |
|
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
943 |
N := 100000 nextPrime-1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
944 |
p := 1. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
945 |
a := (1 to:N) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
946 |
collect:[:i | p := p nextPrime. p ] |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
947 |
thenSelect:[:p | p <= N]. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
948 |
b := Integer primesUpTo:N. |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
949 |
self assert:(a = b) |
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
950 |
" |
9125 | 951 |
! |
952 |
||
953 |
primesUpTo: max do: aBlock |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
954 |
"Compute aBlock with all prime integers up to and including the given integer." |
9125 | 955 |
|
956 |
| limit flags prime k | |
|
957 |
||
958 |
limit := max asInteger - 1. |
|
959 |
"Fall back into #largePrimesUpTo:do: if we'd require more than 100k of memory; |
|
960 |
the alternative will only requre 1/154th of the amount we need here and is almost as fast." |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
961 |
limit > 25000 ifTrue:[^ self largePrimesUpTo: max do: aBlock]. |
11492 | 962 |
|
963 |
"/ sieve, on the fly |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
964 |
flags := (ByteArray new: limit) atAllPut: 1. |
9125 | 965 |
1 to: limit do: [:i | |
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
966 |
(flags at: i) == 1 ifTrue: [ |
9125 | 967 |
prime := i + 1. |
968 |
k := i + prime. |
|
969 |
[k <= limit] whileTrue: [ |
|
11495
833b96ce156d
subtile bug in primesUpTo: - did not include the limit,
Claus Gittinger <cg@exept.de>
parents:
11494
diff
changeset
|
970 |
flags at: k put: 0. |
9125 | 971 |
k := k + prime |
972 |
]. |
|
973 |
aBlock value: prime |
|
974 |
] |
|
975 |
]. |
|
976 |
||
977 |
" |
|
978 |
Integer primesUpTo: 100 |
|
979 |
" |
|
980 |
! ! |
|
981 |
||
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
982 |
!Integer class methodsFor:'queries'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
983 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
984 |
hasSharedInstances |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
985 |
"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
|
986 |
with the same value are identical. |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
987 |
Although not always shared (LargeIntegers), these should be treated |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
988 |
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
|
989 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
990 |
^ true |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
991 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
992 |
|
8892 | 993 |
! |
994 |
||
995 |
isAbstract |
|
11223 | 996 |
"Return if this class is an abstract class. |
997 |
True is returned for Integer here; false for subclasses. |
|
998 |
Abstract subclasses must redefine again." |
|
999 |
||
8892 | 1000 |
^ self == Integer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1001 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
1002 |
|
7261 | 1003 |
!Integer methodsFor:'Compatibility-Dolphin'! |
6249 | 1004 |
|
6907 | 1005 |
& aNumber |
1006 |
^ self bitAnd:aNumber |
|
1007 |
||
1008 |
" |
|
1009 |
14 | 1 |
|
1010 |
9 & 8 |
|
1011 |
" |
|
1012 |
! |
|
1013 |
||
6249 | 1014 |
highWord |
1015 |
"return the high 16 bits of a 32 bit value" |
|
1016 |
||
1017 |
^ self bitShift:-16 |
|
1018 |
||
1019 |
" |
|
1020 |
(16r12345678 highWord) hexPrintString |
|
1021 |
(16r12345678 lowWord) hexPrintString |
|
1022 |
" |
|
1023 |
! |
|
1024 |
||
1025 |
lowWord |
|
1026 |
"return the low 16 bits of a 32 bit value" |
|
1027 |
||
1028 |
^ self bitAnd:16rFFFF |
|
1029 |
||
1030 |
" |
|
1031 |
(16r12345678 lowWord) hexPrintString |
|
1032 |
(16r12345678 highWord) hexPrintString |
|
1033 |
" |
|
1034 |
! |
|
1035 |
||
1036 |
mask:integerMask set:aBoolean |
|
1037 |
"Answer the result of setting/resetting the specified mask in the receiver." |
|
1038 |
||
1039 |
^ aBoolean |
|
1040 |
ifTrue: [self maskSet:integerMask] |
|
1041 |
ifFalse: [self maskClear:integerMask] |
|
1042 |
||
1043 |
"turn on the 1-bit: |
|
1044 |
|v| |
|
1045 |
||
1046 |
v := 2r0100. |
|
1047 |
v mask:1 set:true |
|
1048 |
||
1049 |
turn off the 1-bit: |
|
1050 |
|v| |
|
1051 |
||
1052 |
v := 2r0101. |
|
1053 |
v mask:1 set:false |
|
1054 |
" |
|
6875 | 1055 |
! |
1056 |
||
1057 |
printStringRadix:aRadix padTo:sz |
|
1058 |
^ self printStringRadix:aRadix size:sz fill:$0 |
|
1059 |
||
1060 |
" |
|
1061 |
1024 printStringRadix:16 size:4 fill:$0 |
|
1062 |
1024 printStringRadix:2 size:16 fill:$. |
|
1063 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
1064 |
" |
|
6907 | 1065 |
! |
1066 |
||
1067 |
| aNumber |
|
1068 |
^ self bitOr:aNumber |
|
1069 |
||
1070 |
" |
|
1071 |
14 | 1 |
|
1072 |
9 & 8 |
|
1073 |
" |
|
6249 | 1074 |
! ! |
1075 |
||
7261 | 1076 |
!Integer methodsFor:'Compatibility-Squeak'! |
4867 | 1077 |
|
1078 |
atRandom |
|
1079 |
"return a random number between 1 amd myself" |
|
1080 |
||
8573 | 1081 |
^ self atRandom:Random. |
8572 | 1082 |
|
1083 |
" |
|
1084 |
100 atRandom |
|
1085 |
1000 atRandom |
|
1086 |
" |
|
4867 | 1087 |
! |
4376 | 1088 |
|
8573 | 1089 |
atRandom:aRandomGenerator |
1090 |
"return a random number between 1 amd myself" |
|
1091 |
||
1092 |
self < 1 ifTrue:[^ self]. |
|
1093 |
^ aRandomGenerator nextIntegerBetween:1 and:self |
|
1094 |
||
1095 |
" |
|
1096 |
100 atRandom:(Random new) |
|
1097 |
1000 atRandom:(Random new) |
|
1098 |
" |
|
1099 |
! |
|
1100 |
||
4376 | 1101 |
printStringBase:base |
4867 | 1102 |
"return my printString in a base; |
1103 |
same as printStringRadix:" |
|
1104 |
||
4376 | 1105 |
^ self printStringRadix:base |
9068 | 1106 |
! |
1107 |
||
9125 | 1108 |
printStringHex |
1109 |
"return my printString in base 16; |
|
1110 |
same as printStringRadix:" |
|
1111 |
||
1112 |
^ self printStringRadix:16 |
|
1113 |
||
1114 |
" |
|
1115 |
4096 printStringHex |
|
1116 |
" |
|
1117 |
! |
|
1118 |
||
9068 | 1119 |
printStringRoman |
9069 | 1120 |
"return my roman printString; |
1121 |
almost the same as romanPrintString:" |
|
1122 |
||
9068 | 1123 |
"funny - although the romans did not have negative numbers - squeak has" |
1124 |
self negative ifTrue:[ |
|
1125 |
^ '-' , self negated romanPrintString |
|
1126 |
]. |
|
1127 |
^ self romanPrintString |
|
4376 | 1128 |
! ! |
1129 |
||
7261 | 1130 |
!Integer methodsFor:'Compatibility-V''Age'! |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1131 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1132 |
<< aNumber |
7155 | 1133 |
"V'Age compatibility: left shift" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1134 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1135 |
^ self bitShift:aNumber |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1136 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1137 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1138 |
1 << 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1139 |
64 << -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1140 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1141 |
! |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1142 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1143 |
>> aNumber |
7155 | 1144 |
"V'Age compatibility: right shift" |
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1145 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1146 |
^ self bitShift:(aNumber negated) |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1147 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1148 |
" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1149 |
1 >> -5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1150 |
64 >> 5 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1151 |
" |
1 | 1152 |
! ! |
1153 |
||
4423 | 1154 |
!Integer methodsFor:'bcd conversion'! |
1155 |
||
1156 |
decodeFromBCD |
|
1157 |
"return a number representing the value of the BCD encoded receiver." |
|
1158 |
||
4424 | 1159 |
|v rslt multiplier nibble| |
4423 | 1160 |
|
1161 |
v := self. |
|
1162 |
rslt := 0. |
|
1163 |
multiplier := 1. |
|
1164 |
[v > 0] whileTrue:[ |
|
4424 | 1165 |
nibble := v bitAnd:16r0F. |
1166 |
nibble > 9 ifTrue:[ |
|
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
1167 |
^ BCDConversionErrorSignal |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
1168 |
raiseRequestWith:self |
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
1169 |
errorString:'bad BCD coded value' |
4424 | 1170 |
]. |
1171 |
rslt := rslt + (nibble * multiplier). |
|
4423 | 1172 |
multiplier := multiplier * 10. |
1173 |
v := v bitShift:-4. |
|
1174 |
]. |
|
1175 |
^ rslt |
|
1176 |
||
1177 |
" |
|
4949 | 1178 |
16r1234567890123 decodeFromBCD |
4921 | 1179 |
16r1073741823 decodeFromBCD |
1180 |
16r1073741824 decodeFromBCD |
|
1181 |
16r1073741825 decodeFromBCD |
|
1182 |
||
4423 | 1183 |
16r55 decodeFromBCD |
1184 |
16r127 decodeFromBCD |
|
4921 | 1185 |
16r800000 decodeFromBCD |
1186 |
16r8000000 decodeFromBCD |
|
1187 |
16r80000000 decodeFromBCD |
|
1188 |
16r800000000 decodeFromBCD |
|
1189 |
16r127567890 decodeFromBCD |
|
4423 | 1190 |
16r1234567890 decodeFromBCD |
4424 | 1191 |
|
1192 |
16r5A decodeFromBCD |
|
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
1193 |
16rFF decodeFromBCD |
4423 | 1194 |
" |
1195 |
||
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
1196 |
"Modified: / 15.11.1999 / 20:37:20 / cg" |
4423 | 1197 |
! |
1198 |
||
1199 |
encodeAsBCD |
|
1200 |
"return a BCD encoded number representing the same value as the |
|
1201 |
receiver." |
|
1202 |
||
1203 |
|v rslt shift| |
|
1204 |
||
1205 |
v := self. |
|
1206 |
rslt := shift := 0. |
|
1207 |
[v > 0] whileTrue:[ |
|
1208 |
rslt := rslt + ((v \\ 10) bitShift:shift). |
|
1209 |
shift := shift + 4. |
|
1210 |
v := v // 10. |
|
1211 |
]. |
|
1212 |
^ rslt |
|
1213 |
||
1214 |
" |
|
1215 |
55 encodeAsBCD hexPrintString |
|
4921 | 1216 |
127 encodeAsBCD hexPrintString |
1217 |
127 encodeAsBCD hexPrintString |
|
1218 |
8912345 encodeAsBCD hexPrintString |
|
1219 |
89123456 encodeAsBCD hexPrintString |
|
1220 |
891234567 encodeAsBCD hexPrintString |
|
1221 |
900000000 encodeAsBCD hexPrintString |
|
1222 |
1073741823 encodeAsBCD hexPrintString |
|
1223 |
1073741824 encodeAsBCD hexPrintString |
|
1224 |
1073741825 encodeAsBCD hexPrintString |
|
1225 |
1891234567 encodeAsBCD hexPrintString |
|
1226 |
8912345678 encodeAsBCD hexPrintString |
|
4423 | 1227 |
1234567890 encodeAsBCD hexPrintString |
1228 |
" |
|
1229 |
||
1230 |
! ! |
|
1231 |
||
69 | 1232 |
!Integer methodsFor:'bit operators'! |
1233 |
||
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1234 |
allMask:anInteger |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1235 |
"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
|
1236 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1237 |
^ (self bitAnd:anInteger) == anInteger |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1238 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1239 |
"2r00001111 allMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1240 |
"2r00001111 allMask:2r00011110" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1241 |
"2r00001111 allMask:2r00000000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1242 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1243 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1244 |
anyMask:anInteger |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1245 |
"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
|
1246 |
(somewhat incorrect, if the mask is zero)" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1247 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1248 |
^ (self bitAnd:anInteger) ~~ 0 |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1249 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1250 |
"2r00001111 anyMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1251 |
"2r00001111 anyMask:2r11110000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1252 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1253 |
|
69 | 1254 |
bitAnd:anInteger |
357 | 1255 |
"return the bitwise-and of the receiver and the argument, anInteger. |
1256 |
This is a general and slow implementation, walking over the bytes of |
|
1257 |
the receiver and the argument." |
|
69 | 1258 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1259 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1260 |
result byte| |
69 | 1261 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1262 |
anInteger isInteger ifFalse:[ |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1263 |
^ anInteger bitAndFromInteger:self. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1264 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1265 |
|
92 | 1266 |
n := (anInteger digitLength) min:(self digitLength). |
1267 |
result := self class basicNew numberOfDigits:n. |
|
69 | 1268 |
|
1269 |
1 to:n do:[:index | |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1270 |
byte := (anInteger digitAt:index) bitAnd:(self digitAt:index). |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1271 |
result digitAt:index put:byte. |
69 | 1272 |
]. |
3162
c7c8af356ff2
dont hardcode the numberof bytes (use maxBytes for alpha);
Claus Gittinger <cg@exept.de>
parents:
3138
diff
changeset
|
1273 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1274 |
^ result compressed |
92 | 1275 |
]. |
1276 |
^ result |
|
357 | 1277 |
|
1278 |
" |
|
1279 |
(16r112233445566778899 bitAnd:16rFF ) printStringRadix:16 |
|
1280 |
(16r112233445566778899 bitAnd:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 |
|
1281 |
(16r112233445566778899 bitAnd:16rFF0000000000000000) printStringRadix:16 |
|
1282 |
(16r112233445566778899 bitAnd:16r00000000000000FFFF) printStringRadix:16 |
|
1283 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1284 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1285 |
"Modified: 5.11.1996 / 14:06:26 / cg" |
69 | 1286 |
! |
1287 |
||
85 | 1288 |
bitAt:index |
92 | 1289 |
"return the value of the index's bit (index starts at 1). |
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
1290 |
Notice: the result of bitAt: on negative receivers is not |
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1291 |
defined in the language standard (since the implementation |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1292 |
is free to choose any internal representation for integers)" |
85 | 1293 |
|
1294 |
|i "{Class: SmallInteger}"| |
|
1295 |
||
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
1296 |
i := index - 1. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
1297 |
i < 0 ifTrue:[ |
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1298 |
^ SubscriptOutOfBoundsSignal |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1299 |
raiseRequestWith:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1300 |
errorString:'index out of bounds' |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1301 |
]. |
85 | 1302 |
^ (self digitAt:(i // 8 + 1)) bitAt:(i \\ 8 + 1) |
92 | 1303 |
|
1304 |
" |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1305 |
1 bitAt:1 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1306 |
1 bitAt:2 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1307 |
1 bitAt:0 index error |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1308 |
2r1000100010001000100010001000100010001000100010001000 bitAt:48 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1309 |
2r1000100010001000100010001000100010001000100010001000 bitAt:47 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1310 |
|
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1311 |
(1 bitShift:1000) bitAt:1000 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1312 |
(1 bitShift:1000) bitAt:1001 => 1 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1313 |
(1 bitShift:1000) bitAt:1002 => 0 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1314 |
|
92 | 1315 |
(1 bitShift:30) bitAt:30 |
1316 |
(1 bitShift:30) bitAt:31 |
|
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1317 |
(1 bitShift:30) bitAt:32 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1318 |
(1 bitShift:31) bitAt:31 |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1319 |
(1 bitShift:31) bitAt:32 |
92 | 1320 |
(1 bitShift:31) bitAt:33 |
1321 |
(1 bitShift:32) bitAt:32 |
|
1322 |
(1 bitShift:32) bitAt:33 |
|
1323 |
(1 bitShift:32) bitAt:34 |
|
1324 |
(1 bitShift:64) bitAt:64 |
|
1325 |
(1 bitShift:64) bitAt:65 |
|
1326 |
(1 bitShift:64) bitAt:66 |
|
1327 |
" |
|
85 | 1328 |
! |
1329 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1330 |
bitClear:anInteger |
3884 | 1331 |
"return the bitwise-and of the receiver and the complement of argument, anInteger. |
1332 |
This is a general and slow implementation, walking over the bytes of |
|
1333 |
the receiver and the argument." |
|
1334 |
||
1335 |
|n "{ Class: SmallInteger }" |
|
1336 |
result byte| |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1337 |
|
3884 | 1338 |
n := (anInteger digitLength) max:(self digitLength). |
1339 |
result := self class basicNew numberOfDigits:n. |
|
1340 |
||
1341 |
1 to:n do:[:index | |
|
1342 |
byte := (self digitAt:index) bitClear:(anInteger digitAt:index). |
|
1343 |
result digitAt:index put:byte. |
|
1344 |
]. |
|
1345 |
(byte == 0 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
|
1346 |
^ result compressed |
|
1347 |
]. |
|
1348 |
^ result |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1349 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
1350 |
|
11492 | 1351 |
bitCount |
1352 |
"return the number of 1-bits in the receiver" |
|
1353 |
||
1354 |
|n "{ Class: SmallInteger }" |
|
1355 |
cnt byte| |
|
1356 |
||
1357 |
n := self digitLength. |
|
1358 |
cnt := 0. |
|
1359 |
||
1360 |
1 to:n do:[:index | |
|
1361 |
byte := self digitAt:index. |
|
1362 |
cnt := cnt + (byte bitCount) |
|
1363 |
]. |
|
1364 |
^ cnt |
|
1365 |
||
1366 |
" |
|
1367 |
2r100000000000000000000000000000000000000000000000000000000001 bitCount |
|
1368 |
2r111111111111111111111111111111111111111111111111111111111111111111 bitCount |
|
1369 |
" |
|
1370 |
! |
|
1371 |
||
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1372 |
bitInvert |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1373 |
"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
|
1374 |
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
|
1375 |
since the digits are stored as absolute value." |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1376 |
|
4614 | 1377 |
"/ ^ -1 - self |
1378 |
||
1379 |
|n "{ Class: SmallInteger }" |
|
1380 |
result byte| |
|
1381 |
||
1382 |
n := self digitLength. |
|
1383 |
result := self class basicNew numberOfDigits:n. |
|
1384 |
||
1385 |
1 to:n do:[:index | |
|
1386 |
byte := self digitAt:index. |
|
1387 |
result digitAt:index put:(byte bitInvert bitAnd:16rFF). |
|
1388 |
]. |
|
1389 |
(byte == 255 or:[n <= SmallInteger maxBytes]) ifTrue:[ |
|
1390 |
^ result compressed |
|
1391 |
]. |
|
1392 |
^ result |
|
1393 |
||
1394 |
" |
|
1395 |
16rffffffff bitInvert |
|
1396 |
16rff00ff00 bitInvert hexPrintString |
|
1397 |
" |
|
3455
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1398 |
! |
2b19e1e70adc
added bitInvert for LargeIntegers (positive only)
Claus Gittinger <cg@exept.de>
parents:
3395
diff
changeset
|
1399 |
|
4376 | 1400 |
bitInvert32 |
1401 |
"return a new integer, where the low32 bits are complemented." |
|
1402 |
||
1403 |
^ self bitXor: 16rFFFFFFFF |
|
1404 |
||
1405 |
" |
|
4609 | 1406 |
16r80000000 bitInvert32 hexPrintString |
1407 |
16r7FFFFFFF bitInvert32 hexPrintString |
|
4614 | 1408 |
16rFFFFFFFF bitInvert32 hexPrintString |
1409 |
0 bitInvert32 hexPrintString |
|
4376 | 1410 |
" |
1411 |
! |
|
1412 |
||
701 | 1413 |
bitOr:anInteger |
1414 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
1415 |
This is a general and slow implementation, walking over the bytes of |
|
1416 |
the receiver and the argument." |
|
1417 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1418 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1419 |
result byte| |
701 | 1420 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1421 |
anInteger isInteger ifFalse:[ |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1422 |
^ anInteger bitOrFromInteger:self. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1423 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1424 |
|
701 | 1425 |
n := (anInteger digitLength) max:(self digitLength). |
1426 |
result := self class basicNew numberOfDigits:n. |
|
1427 |
||
1428 |
1 to:n do:[:index | |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1429 |
byte := (anInteger digitAt:index) bitOr:(self digitAt:index). |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1430 |
result digitAt:index put:byte. |
701 | 1431 |
]. |
1432 |
"/ no need to normalize - if the operands were correct |
|
1433 |
"/ byte == 0 ifTrue:[ |
|
1434 |
"/ ^ result normalize |
|
1435 |
"/ ]. |
|
1436 |
^ result |
|
1437 |
! |
|
1438 |
||
85 | 1439 |
bitShift:shiftCount |
1440 |
"return the value of the receiver shifted by shiftCount bits; |
|
1441 |
leftShift if shiftCount > 0; rightShift otherwise. |
|
92 | 1442 |
|
1443 |
Notice: the result of bitShift: on negative receivers is not |
|
3638 | 1444 |
defined in the language standard (since the implementation |
7155 | 1445 |
is free to choose any internal representation for integers) |
1446 |
However, ST/X preserves the sign." |
|
85 | 1447 |
|
92 | 1448 |
|result |
1449 |
prev "{ Class: SmallInteger }" |
|
1450 |
next "{ Class: SmallInteger }" |
|
1451 |
byte "{ Class: SmallInteger }" |
|
359 | 1452 |
byte2 "{ Class: SmallInteger }" |
92 | 1453 |
bitShift "{ Class: SmallInteger }" |
1454 |
revShift "{ Class: SmallInteger }" |
|
1455 |
digitShift "{ Class: SmallInteger }" |
|
1456 |
n "{ Class: SmallInteger }" |
|
3044 | 1457 |
nn "{ Class: SmallInteger }" |
1458 |
nDigits "{ Class: SmallInteger }" | |
|
85 | 1459 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1460 |
shiftCount isInteger ifFalse:[ |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1461 |
^ shiftCount bitShiftFromInteger:self. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1462 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1463 |
|
85 | 1464 |
shiftCount > 0 ifTrue:[ |
3638 | 1465 |
"left shift" |
85 | 1466 |
|
3638 | 1467 |
digitShift := shiftCount // 8. |
1468 |
bitShift := shiftCount \\ 8. |
|
1469 |
n := self digitLength. |
|
92 | 1470 |
|
3638 | 1471 |
" |
1472 |
modulo 8 shifts can be done faster ... |
|
1473 |
" |
|
1474 |
bitShift == 0 ifTrue:[ |
|
5017
09d6baf12523
dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents:
5003
diff
changeset
|
1475 |
n := n + digitShift. |
09d6baf12523
dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents:
5003
diff
changeset
|
1476 |
result := self class basicNew numberOfDigits:n. |
3638 | 1477 |
result sign:self sign. |
5017
09d6baf12523
dont crash for unnormalized receiver
Claus Gittinger <cg@exept.de>
parents:
5003
diff
changeset
|
1478 |
result digitBytes replaceFrom:(digitShift + 1) to:n with:self digitBytes. |
3638 | 1479 |
" |
1480 |
no normalize needed, since receiver was already normalized |
|
1481 |
" |
|
1482 |
^ result |
|
1483 |
]. |
|
92 | 1484 |
|
3638 | 1485 |
" |
1486 |
less-than-8 shifts can be done faster ... |
|
1487 |
" |
|
1488 |
digitShift == 0 ifTrue:[ |
|
4151 | 1489 |
nn := n+1. |
1490 |
result := self class basicNew numberOfDigits:nn. |
|
3638 | 1491 |
result sign:self sign. |
1492 |
prev := 0. |
|
4151 | 1493 |
1 to:n do:[:index | |
3638 | 1494 |
byte := self digitAt:index. |
1495 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
1496 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
1497 |
prev := byte bitShift:-8. |
|
1498 |
]. |
|
4151 | 1499 |
result digitAt:nn put:prev. |
3638 | 1500 |
" |
1501 |
might have stored a 0-byte ... |
|
1502 |
" |
|
1503 |
prev == 0 ifTrue:[ |
|
1504 |
^ result compressed |
|
1505 |
]. |
|
1506 |
^ result. |
|
1507 |
]. |
|
92 | 1508 |
|
3638 | 1509 |
" |
1510 |
slow case ... |
|
1511 |
" |
|
1512 |
n := n + digitShift + 1. |
|
1513 |
result := self class basicNew numberOfDigits:n. |
|
1514 |
result sign:self sign. |
|
1515 |
byte := self digitAt:1. |
|
1516 |
byte := (byte bitShift:bitShift) bitAnd:16rFF. |
|
1517 |
result digitAt:(digitShift + 1) put:byte. |
|
1518 |
revShift := -8 + bitShift. |
|
1519 |
nDigits := self digitLength. |
|
1520 |
2 to:nDigits do:[:index | |
|
1521 |
byte := self digitAt:index. |
|
1522 |
byte2 := self digitAt:index-1. |
|
1523 |
byte := byte bitShift:bitShift. |
|
1524 |
byte2 := byte2 bitShift:revShift. |
|
1525 |
byte := (byte bitOr:byte2) bitAnd:16rFF. |
|
1526 |
result digitAt:(index + digitShift) put:byte. |
|
1527 |
]. |
|
1528 |
byte2 := self digitAt:nDigits. |
|
1529 |
byte2 := (byte2 bitShift:revShift) bitAnd:16rFF. |
|
1530 |
result digitAt:(nDigits + digitShift + 1) put:byte2. |
|
1531 |
" |
|
1532 |
might have stored a 0-byte ... |
|
1533 |
" |
|
1534 |
byte2 == 0 ifTrue:[ |
|
1535 |
^ result compressed |
|
1536 |
]. |
|
1537 |
^ result |
|
92 | 1538 |
]. |
1539 |
||
1540 |
shiftCount < 0 ifTrue:[ |
|
3638 | 1541 |
"right shift" |
85 | 1542 |
|
3638 | 1543 |
digitShift := shiftCount negated // 8. |
1544 |
bitShift := shiftCount negated \\ 8. |
|
1545 |
n := self digitLength. |
|
92 | 1546 |
|
3638 | 1547 |
digitShift >= n ifTrue:[ |
1548 |
^ 0 |
|
1549 |
]. |
|
92 | 1550 |
|
3638 | 1551 |
" |
1552 |
modulo 8 shifts can be done faster ... |
|
1553 |
" |
|
1554 |
bitShift == 0 ifTrue:[ |
|
1555 |
n := n-digitShift. |
|
1556 |
result := self class basicNew numberOfDigits:n. |
|
1557 |
result sign:self sign. |
|
4830
879f09ec443a
Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents:
4803
diff
changeset
|
1558 |
result digitBytes replaceFrom:1 to:n with:self digitBytes startingAt:(digitShift + 1) . |
3638 | 1559 |
n <= SmallInteger maxBytes ifTrue:[ |
1560 |
^ result compressed |
|
1561 |
]. |
|
1562 |
^ result |
|
1563 |
]. |
|
85 | 1564 |
|
3638 | 1565 |
" |
1566 |
less-than-8 shifts can be done faster ... |
|
1567 |
" |
|
1568 |
digitShift == 0 ifTrue:[ |
|
1569 |
result := self class basicNew numberOfDigits:n. |
|
1570 |
result sign:self sign. |
|
1571 |
prev := 0. |
|
1572 |
bitShift := bitShift negated. |
|
1573 |
revShift := 8 + bitShift. |
|
1574 |
n to:1 by:-1 do:[:index | |
|
1575 |
byte := self digitAt:index. |
|
1576 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
1577 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
1578 |
result digitAt:index put:(byte bitAnd:16rFF). |
|
1579 |
prev := next. |
|
1580 |
]. |
|
1581 |
(n <= 5) ifTrue:[ |
|
1582 |
^ result compressed |
|
1583 |
]. |
|
1584 |
^ result |
|
1585 |
]. |
|
92 | 1586 |
|
3638 | 1587 |
" |
1588 |
slow case ... |
|
1589 |
" |
|
1590 |
nn := n-digitShift. |
|
1591 |
result := self class basicNew numberOfDigits:nn. |
|
1592 |
result sign:self sign. |
|
85 | 1593 |
|
3638 | 1594 |
prev := 0. |
1595 |
bitShift := bitShift negated. |
|
1596 |
revShift := 8 + bitShift. |
|
4151 | 1597 |
nn := digitShift + 1. |
1598 |
n to:nn by:-1 do:[:index | |
|
3638 | 1599 |
byte := self digitAt:index. |
1600 |
next := (byte bitShift:revShift) bitAnd:16rFF. |
|
1601 |
byte := (byte bitShift:bitShift) bitOr:prev. |
|
1602 |
result digitAt:(index - digitShift) put:byte. |
|
1603 |
prev := next. |
|
1604 |
]. |
|
1605 |
"the last stored byte ..." |
|
1606 |
^ result compressed |
|
85 | 1607 |
]. |
92 | 1608 |
|
1609 |
^ self "no shift" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1610 |
|
3638 | 1611 |
"Modified: / 8.7.1998 / 12:45:24 / cg" |
4151 | 1612 |
"Modified: / 5.5.1999 / 16:05:05 / stefan" |
701 | 1613 |
! |
1 | 1614 |
|
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1615 |
bitTest:anInteger |
5782 | 1616 |
"return true, if any bit from aMask is set in the receiver. |
1617 |
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
|
1618 |
is non-0, false otherwise. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1619 |
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
|
1620 |
the receiver and the argument." |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1621 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1622 |
|n "{ Class: SmallInteger }" |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1623 |
byte| |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1624 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1625 |
n := (anInteger digitLength) min:(self digitLength). |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1626 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1627 |
1 to:n do:[:index | |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1628 |
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
|
1629 |
byte ~~ 0 ifTrue:[^ true]. |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1630 |
]. |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1631 |
^ false |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1632 |
|
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1633 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1634 |
16r112233445566778899 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1635 |
16r112233445566778800 bitTest:16rFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1636 |
16r112233445566778899 bitTest:16rFFFFFFFFFFFFFFFF00 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1637 |
16r112233445566778899 bitTest:16rFF0000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1638 |
16r112233445566778899 bitTest:16r00000000000000FFFF |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1639 |
16r1234567800000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1640 |
16r8765432100000000 bitTest:16r8000000000000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1641 |
16r12345678 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1642 |
16r87654321 bitTest:16r80000000 |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1643 |
" |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1644 |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1645 |
"Modified: / 6.6.1999 / 15:10:33 / cg" |
1407
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1646 |
! |
6c278dcd5570
added #bitTest: for general integers
Claus Gittinger <cg@exept.de>
parents:
1337
diff
changeset
|
1647 |
|
701 | 1648 |
bitXor:anInteger |
1649 |
"return the bitwise-or of the receiver and the argument, anInteger. |
|
1650 |
This is a general and slow implementation, walking over the bytes of |
|
1651 |
the receiver and the argument." |
|
1652 |
||
927
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1653 |
|n "{ Class: SmallInteger }" |
8d8edf9df0ae
fixed \\ vs. rem: and // vs. quo: stupidities;
Claus Gittinger <cg@exept.de>
parents:
701
diff
changeset
|
1654 |
result byte| |
701 | 1655 |
|
6358
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1656 |
anInteger isInteger ifFalse:[ |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1657 |
^ anInteger bitXorFromInteger:self. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1658 |
]. |
706234560a0b
allow double dispatch for bit* operations
Claus Gittinger <cg@exept.de>
parents:
6249
diff
changeset
|
1659 |
|
701 | 1660 |
n := (anInteger digitLength) max:(self digitLength). |
1661 |
result := self class basicNew numberOfDigits:n. |
|
1 | 1662 |
|
701 | 1663 |
1 to:n do:[:index | |
3993
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
1664 |
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
|
1665 |
result digitAt:index put:byte. |
701 | 1666 |
]. |
3993
a5a09b2d36cd
oops - bitXor: did not always compress the result.
Claus Gittinger <cg@exept.de>
parents:
3908
diff
changeset
|
1667 |
(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
|
1668 |
^ result compressed |
701 | 1669 |
]. |
1670 |
^ result |
|
1 | 1671 |
|
701 | 1672 |
" |
1673 |
(16r112233445566778899 bitXor:16rFF ) printStringRadix:16 '112233445566778866' |
|
1674 |
(16r112233445566778899 bitXor:16rFFFFFFFFFFFFFFFF00) printStringRadix:16 'EEDDCCBBAA99887799' |
|
1675 |
(16r112233445566778899 bitXor:16rFF0000000000000000) printStringRadix:16 'EE2233445566778899' |
|
1676 |
(16r112233445566778899 bitXor:16r112233445566778800) printStringRadix:16 |
|
1677 |
" |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1678 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
1679 |
"Modified: 5.11.1996 / 14:06:40 / cg" |
1 | 1680 |
! |
1681 |
||
5210 | 1682 |
changeBit:index to:aBooleanOrNumber |
1683 |
"return a new number where the specified bit is on or off, |
|
1684 |
sepending on aBooleanOrNumber. |
|
1685 |
Bits are counted from 1 starting with the least significant. |
|
1686 |
The methods name may be missleading: the receiver is not changed, |
|
1687 |
but a new number is returned. Should be named #withBitChanged:to:" |
|
1688 |
||
1689 |
(aBooleanOrNumber == 0 or:[aBooleanOrNumber == false]) ifTrue:[ |
|
8289 | 1690 |
^ self clearBit:index |
5210 | 1691 |
]. |
1692 |
^ self setBit:index |
|
1693 |
||
1694 |
" |
|
8289 | 1695 |
(16r3fffffff changeBit:31 to:1) hexPrintString |
1696 |
(16r3fffffff asLargeInteger setBit:31) hexPrintString |
|
5210 | 1697 |
" |
1698 |
! |
|
1699 |
||
3683 | 1700 |
clearBit:index |
8937 | 1701 |
"return a new integer where the specified bit is off. |
5209 | 1702 |
Bits are counted from 1 starting with the least significant. |
1703 |
The methods name may be missleading: the receiver is not changed, |
|
1704 |
but a new number is returned. Should be named #withBitCleared:" |
|
3683 | 1705 |
|
1706 |
|n "{ Class: SmallInteger }" |
|
1707 |
byteIndex "{ Class: SmallInteger }" |
|
8937 | 1708 |
bitIndex "{ Class: SmallInteger }" |
3683 | 1709 |
result byte| |
1710 |
||
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1711 |
index <= 0 ifTrue:[ |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1712 |
^ SubscriptOutOfBoundsSignal |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1713 |
raiseRequestWith:index |
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
1714 |
errorString:'bit index out of bounds' |
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1715 |
]. |
3683 | 1716 |
byteIndex := ((index - 1) // 8) + 1. |
1717 |
n := self digitLength. |
|
1718 |
byteIndex > n ifTrue:[ |
|
1719 |
^ self |
|
1720 |
]. |
|
1721 |
||
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
1722 |
result := self simpleDeepCopy. |
3683 | 1723 |
bitIndex := ((index - 1) \\ 8) + 1. |
1724 |
byte := (result digitAt:byteIndex) clearBit:bitIndex. |
|
1725 |
result digitAt:byteIndex put:byte. |
|
1726 |
(byte == 0 or:[n == byteIndex and:[n <= SmallInteger maxBytes]]) ifTrue:[ |
|
1727 |
^ result compressed |
|
1728 |
]. |
|
1729 |
^ result |
|
1730 |
||
1731 |
" |
|
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
1732 |
3111111111 clearBit:1 |
3683 | 1733 |
" |
1734 |
||
1735 |
"Modified: / 28.7.1998 / 18:35:50 / cg" |
|
1736 |
! |
|
1737 |
||
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1738 |
even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1739 |
"return true if the receiver is even" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1740 |
|
9401 | 1741 |
^ (self bitAt:1) == 0 |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1742 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1743 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1744 |
16r112233445566778899 even |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1745 |
16r112233445566778800 even |
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1746 |
1 even |
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1747 |
2 even |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1748 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1749 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1750 |
"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
|
1751 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1752 |
|
701 | 1753 |
highBit |
1754 |
"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
|
1755 |
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
|
1756 |
Returns 0 if no bit is set." |
701 | 1757 |
|
1758 |
|byteNr highByte| |
|
1759 |
||
1760 |
byteNr := self digitLength. |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1761 |
byteNr == 0 ifTrue:[ |
6481
d5c94e93067c
highBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6480
diff
changeset
|
1762 |
^ 0 |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1763 |
]. |
701 | 1764 |
highByte := self digitAt:byteNr. |
1765 |
^ (byteNr - 1) * 8 + highByte highBit |
|
1 | 1766 |
|
701 | 1767 |
" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1768 |
0 highBit |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1769 |
-1 highBit |
701 | 1770 |
(1 bitShift:1) highBit |
1771 |
(1 bitShift:30) highBit |
|
1772 |
(1 bitShift:31) highBit |
|
1773 |
(1 bitShift:32) highBit |
|
1774 |
(1 bitShift:33) highBit |
|
1775 |
(1 bitShift:64) highBit |
|
1776 |
(1 bitShift:1000) highBit |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1777 |
(1 bitShift:1000) negated highBit |
701 | 1778 |
((1 bitShift:64)-1) highBit |
1779 |
" |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1780 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
1781 |
"Modified: / 3.5.1999 / 09:20:57 / stefan" |
2429 | 1782 |
! |
1783 |
||
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1784 |
invertBit:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1785 |
"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
|
1786 |
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
|
1787 |
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
|
1788 |
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
|
1789 |
|
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1790 |
index <= 0 ifTrue:[ |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1791 |
^ SubscriptOutOfBoundsSignal |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1792 |
raiseRequestWith:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1793 |
errorString:'index out of bounds' |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1794 |
]. |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1795 |
^ self bitXor:(1 bitShift:index-1) |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1796 |
|
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1797 |
" |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1798 |
0 invertBit:3 => 4 (2r100) |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1799 |
0 invertBit:48 => 140737488355328 (2r1000.....000) |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1800 |
((0 invertBit:99) invertBit:100) printStringRadix:2 |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1801 |
" |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1802 |
! |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1803 |
|
5209 | 1804 |
isBitSet:index |
1805 |
"return true if the index' bit is set; false otherwise. |
|
1806 |
Bits are counted from 1 starting with the least significant." |
|
1807 |
||
1808 |
^ (self bitAt:index) ~~ 0 |
|
1809 |
||
1810 |
" |
|
1811 |
5 isBitSet:3 => true |
|
1812 |
2r0101 isBitSet:2 => false |
|
1813 |
2r0101 isBitSet:1 => true |
|
1814 |
2r0101 isBitSet:0 index error |
|
1815 |
" |
|
1816 |
! |
|
1817 |
||
2429 | 1818 |
lowBit |
1819 |
"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
|
1820 |
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
|
1821 |
Returns 0 if no bit is set." |
2429 | 1822 |
|
1823 |
|maxBytes "{ Class: SmallInteger }" |
|
1824 |
byte| |
|
1825 |
||
1826 |
maxBytes := self digitLength. |
|
1827 |
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
|
1828 |
byte := self digitAt:byteIndex. |
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1829 |
byte ~~ 0 ifTrue:[ |
7545 | 1830 |
^ (byteIndex-1)*8 + (byte lowBit) |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1831 |
]. |
2429 | 1832 |
]. |
6480
4ff7f2af25fc
lowBit now returns 0 if no bit is set.
Claus Gittinger <cg@exept.de>
parents:
6358
diff
changeset
|
1833 |
^ 0 "/ should not happen |
2429 | 1834 |
|
1835 |
" |
|
1836 |
0 lowBit |
|
1837 |
1 lowBit |
|
1838 |
(1 bitShift:1) lowBit |
|
1839 |
(1 bitShift:1) highBit |
|
1840 |
(1 bitShift:30) lowBit |
|
1841 |
(1 bitShift:30) highBit |
|
1842 |
(1 bitShift:31) lowBit |
|
1843 |
(1 bitShift:31) highBit |
|
1844 |
(1 bitShift:32) lowBit |
|
1845 |
(1 bitShift:32) highBit |
|
1846 |
(1 bitShift:33) lowBit |
|
1847 |
(1 bitShift:33) highBit |
|
1848 |
(1 bitShift:64) lowBit |
|
1849 |
(1 bitShift:64) highBit |
|
1850 |
(1 bitShift:1000) lowBit |
|
1851 |
(1 bitShift:1000) highBit |
|
1852 |
((1 bitShift:64)-1) lowBit |
|
1853 |
((1 bitShift:64)-1) highBit |
|
1854 |
" |
|
1855 |
||
1856 |
"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
|
1857 |
! |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1858 |
|
4643
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1859 |
maskClear:aMask |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1860 |
^ self bitAnd:aMask bitInvert |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1861 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1862 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1863 |
maskSet:aMask |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1864 |
^ self bitOr:aMask |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1865 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1866 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1867 |
noMask:anInteger |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1868 |
"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
|
1869 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1870 |
^ (self bitAnd:anInteger) == 0 |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1871 |
|
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1872 |
"2r00001111 noMask:2r00000001" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1873 |
"2r00001111 noMask:2r11110000" |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1874 |
! |
48ee5b949c81
moved anyMask, allMask ... from SmallInteger.
Claus Gittinger <cg@exept.de>
parents:
4614
diff
changeset
|
1875 |
|
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1876 |
odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1877 |
"return true if the receiver is odd" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1878 |
|
9401 | 1879 |
^ (self bitAt:1) == 1 |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1880 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1881 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1882 |
16r112233445566778899 odd |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1883 |
16r112233445566778800 odd |
5435
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1884 |
1 odd |
ad6612cc6848
oops odd/even fallback was wrong
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
1885 |
2 odd |
4278
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1886 |
" |
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1887 |
|
78c34fcd9952
added #odd and #even for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
4169
diff
changeset
|
1888 |
"Created: / 6.6.1999 / 15:00:55 / cg" |
5209 | 1889 |
! |
1890 |
||
7155 | 1891 |
rightShift:shiftCount |
1892 |
"return the value of the receiver shifted right by shiftCount bits; |
|
1893 |
rightShift if shiftCount > 0; leftShift otherwise. |
|
1894 |
||
1895 |
Notice: the result of bitShift: on negative receivers is not |
|
1896 |
defined in the language standard (since the implementation |
|
1897 |
is free to choose any internal representation for integers) |
|
1898 |
However, ST/X preserves the sign." |
|
1899 |
||
1900 |
^ self bitShift:(shiftCount negated) |
|
1901 |
||
1902 |
" |
|
1903 |
16r100000000 rightShift:1 |
|
1904 |
16r100000000 negated rightShift:1 |
|
1905 |
||
1906 |
16r100000000 rightShift:2 |
|
1907 |
16r100000000 negated rightShift:2 |
|
1908 |
||
1909 |
16r100000000 rightShift:3 |
|
1910 |
16r100000000 negated rightShift:3 |
|
1911 |
||
1912 |
((16r100000000 rightShift:1) rightShift:1) rightShift:1 |
|
1913 |
((16r100000000 negated rightShift:1) rightShift:1) rightShift:1 |
|
1914 |
" |
|
1915 |
! |
|
1916 |
||
5209 | 1917 |
setBit:index |
8937 | 1918 |
"return a new integer, where the specified bit is on. |
5209 | 1919 |
Bits are counted from 1 starting with the least significant. |
1920 |
The methods name may be missleading: the receiver is not changed, |
|
1921 |
but a new number is returned. Should be named #withBitCleared:" |
|
1922 |
||
5496
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1923 |
index <= 0 ifTrue:[ |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1924 |
^ SubscriptOutOfBoundsSignal |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1925 |
raiseRequestWith:index |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1926 |
errorString:'index out of bounds' |
338c81a04468
index bounds in bit operations;
Claus Gittinger <cg@exept.de>
parents:
5469
diff
changeset
|
1927 |
]. |
5209 | 1928 |
^ self bitOr:(1 bitShift:index-1) |
1929 |
||
1930 |
" |
|
1931 |
0 setBit:3 => 4 (2r100) |
|
1932 |
0 setBit:48 => 140737488355328 (2r1000.....000) |
|
1933 |
((0 setBit:99) setBit:100) printStringRadix:2 |
|
1934 |
" |
|
701 | 1935 |
! ! |
1936 |
||
3890 | 1937 |
!Integer methodsFor:'byte access'! |
1938 |
||
1939 |
digitByteLength |
|
1940 |
"return the number bytes required for a 2's complement |
|
1941 |
binary representation of this Integer. |
|
1942 |
For positive receivers, thats the same as the digitLength." |
|
1943 |
||
1944 |
" |
|
1945 |
check if there is a 0-byte ... |
|
1946 |
this allows to ask unnormalized LargeIntegers |
|
1947 |
for their digitLength |
|
1948 |
" |
|
1949 |
|l "{ Class: SmallInteger }" | |
|
1950 |
||
1951 |
self >= 0 ifTrue:[^ self digitLength]. |
|
1952 |
||
1953 |
l := self negated digitLength. |
|
1954 |
(self digitByteAt:l) == 16rFF ifTrue:[ |
|
1955 |
^ (l - 1) max:1 |
|
1956 |
]. |
|
1957 |
^ l |
|
1958 |
||
1959 |
" |
|
1960 |
-129 digitByteLength |
|
1961 |
" |
|
1962 |
||
1963 |
"Created: / 25.10.1998 / 14:58:09 / cg" |
|
1964 |
"Modified: / 25.10.1998 / 19:20:33 / cg" |
|
10488 | 1965 |
! |
1966 |
||
1967 |
digitBytes |
|
1968 |
"return a byteArray filled with the receivers bits |
|
1969 |
(8 bits of the absolute value per element), |
|
1970 |
least significant byte is first" |
|
1971 |
||
1972 |
^ self subclassResponsibility |
|
1973 |
! |
|
1974 |
||
1975 |
digitBytesMSB:msbFlag |
|
1976 |
"return a byteArray filled with the receivers bits |
|
1977 |
(8 bits of the absolute value per element), |
|
1978 |
if msbflag = true, most significant byte is first, |
|
1979 |
otherwise least significant byte is first" |
|
1980 |
||
1981 |
msbFlag ifTrue:[ |
|
1982 |
^ self digitBytes copyReverse. "digitBytes may be shared - reverse a copy!!" |
|
1983 |
]. |
|
1984 |
^ self digitBytes |
|
1985 |
||
1986 |
" |
|
1987 |
16r12 digitBytesMSB:true |
|
1988 |
16r1234 digitBytesMSB:true |
|
1989 |
16r1234 digitBytesMSB:false |
|
1990 |
16r12345678 digitBytesMSB:true |
|
1991 |
16r12345678 digitBytesMSB:false |
|
1992 |
" |
|
3890 | 1993 |
! ! |
1994 |
||
5238 | 1995 |
!Integer methodsFor:'coercing & converting'! |
701 | 1996 |
|
1888 | 1997 |
asFixedPoint |
1998 |
"return the receiver as a fixedPoint number" |
|
1999 |
||
6640
2de8925ad2cd
fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents:
6639
diff
changeset
|
2000 |
^ FixedPoint basicNew |
2de8925ad2cd
fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents:
6639
diff
changeset
|
2001 |
setNumerator:self denominator:1 scale:1 |
1888 | 2002 |
|
2003 |
" |
|
2004 |
100 asFixedPoint |
|
2005 |
100 asFixedPoint + 0.1 asFixedPoint |
|
2006 |
" |
|
2007 |
||
2008 |
"Modified: 5.11.1996 / 15:13:17 / cg" |
|
2009 |
! |
|
2010 |
||
2141 | 2011 |
asFixedPoint:scale |
2012 |
"return the receiver as fixedPoint number, with the given number |
|
2013 |
of post-decimal-point digits." |
|
2014 |
||
6639
d88a51b081ae
fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents:
6635
diff
changeset
|
2015 |
^ FixedPoint basicNew |
d88a51b081ae
fixed asFixedPoint (did return int due to reduce).
Claus Gittinger <cg@exept.de>
parents:
6635
diff
changeset
|
2016 |
setNumerator:self denominator:1 scale:scale |
2141 | 2017 |
|
2018 |
" |
|
2019 |
100 asFixedPoint:2 |
|
2020 |
100 asFixedPoint + (0.1 asFixedPoint:2) |
|
2021 |
" |
|
2022 |
||
2023 |
"Modified: 10.1.1997 / 20:00:08 / cg" |
|
2024 |
! |
|
2025 |
||
7444 | 2026 |
asFloat |
2027 |
"return a Float with same value as myself. |
|
11240 | 2028 |
Since floats have a limited precision, you usually loose bits when doing this." |
7444 | 2029 |
|
2030 |
^ Float fromInteger:self |
|
2031 |
||
2032 |
" |
|
2033 |
1234567890 asFloat |
|
2034 |
1234567890 asFloat asInteger |
|
2035 |
12345678901234567890 asFloat |
|
2036 |
12345678901234567890 asFloat asInteger |
|
2037 |
" |
|
2038 |
! |
|
2039 |
||
701 | 2040 |
asFraction |
2041 |
"return a Fraction with same value as receiver" |
|
2042 |
||
2794
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
2043 |
^ 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
|
2044 |
|
862ee34a4821
when coercing an integer, do not reduce the result.
Claus Gittinger <cg@exept.de>
parents:
2793
diff
changeset
|
2045 |
"Modified: 28.7.1997 / 19:26:06 / cg" |
1 | 2046 |
! |
2047 |
||
701 | 2048 |
asInteger |
1 | 2049 |
"return the receiver truncated towards zero - |
2050 |
for integers this is self" |
|
2051 |
||
2052 |
^ self |
|
1337 | 2053 |
! |
2054 |
||
7444 | 2055 |
asLargeFloat |
2056 |
"return a LargeFloat with same value as myself. |
|
2057 |
Since largeFloats have a limited precision, you usually loose bits when |
|
2058 |
doing this." |
|
2059 |
||
2060 |
^ LargeFloat fromInteger:self |
|
2061 |
||
2062 |
" |
|
2063 |
1234567890 asLargeFloat |
|
2064 |
1234567890 asLargeFloat asInteger |
|
2065 |
12345678901234567890 asLargeFloat |
|
2066 |
12345678901234567890 asLargeFloat asInteger |
|
2067 |
" |
|
2068 |
! |
|
2069 |
||
2070 |
asLongFloat |
|
2071 |
"return a LongFloat with same value as myself. |
|
2072 |
Since longFloats have a limited precision, you usually loose bits when |
|
2073 |
doing this." |
|
2074 |
||
2075 |
^ LongFloat fromInteger:self |
|
2076 |
||
2077 |
" |
|
2078 |
1234567890 asFloat |
|
2079 |
1234567890 asFloat asInteger |
|
2080 |
12345678901234567890 asFloat |
|
2081 |
12345678901234567890 asFloat asInteger |
|
2082 |
" |
|
2083 |
! |
|
2084 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2085 |
asModuloNumber |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2086 |
"return a precomputed modulo number" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2087 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2088 |
^ ModuloNumber modulus:self. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2089 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2090 |
"Created: / 3.5.1999 / 14:48:03 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2091 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2092 |
|
7444 | 2093 |
asShortFloat |
2094 |
"return a ShortFloat with same value as receiver" |
|
2095 |
||
2096 |
^ ShortFloat fromInteger:self |
|
2097 |
! |
|
2098 |
||
1337 | 2099 |
signExtendedByteValue |
2100 |
"return a smallInteger from sign-extending the 8'th bit. |
|
2101 |
May be useful for communication interfaces" |
|
2102 |
||
2103 |
^ (self bitAnd:16rFF) signExtendedByteValue |
|
2104 |
||
2105 |
"Created: 7.5.1996 / 09:31:52 / cg" |
|
2106 |
! |
|
2107 |
||
2108 |
signExtendedShortValue |
|
2109 |
"return a smallInteger from sign-extending the 16'th bit. |
|
2110 |
May be useful for communication interfaces" |
|
2111 |
||
2112 |
^ (self bitAnd:16rFFFF) signExtendedShortValue |
|
2113 |
||
2114 |
"Modified: 7.5.1996 / 09:31:57 / cg" |
|
1 | 2115 |
! ! |
2116 |
||
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2117 |
!Integer methodsFor:'comparing'! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2118 |
|
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2119 |
hash |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2120 |
"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
|
2121 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
2122 |
^ self bitAnd:16r3FFFFFFF. |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2123 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
2124 |
" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
2125 |
-20000000000000 hash |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
2126 |
20000000000000 hash |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
2127 |
" |
1976
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2128 |
|
3298
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
2129 |
"Created: / 14.11.1996 / 12:12:27 / cg" |
bf837fee7a15
Do not return negative hash values.
Stefan Vogel <sv@exept.de>
parents:
3188
diff
changeset
|
2130 |
"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
|
2131 |
! ! |
f6e7094cd3e4
Integer>>hash returns a value clamped to the SmallInteger range
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
2132 |
|
701 | 2133 |
!Integer methodsFor:'double dispatching'! |
2134 |
||
2135 |
differenceFromFraction:aFraction |
|
2136 |
"sent when a fraction does not know how to subtract the receiver, an integer" |
|
1 | 2137 |
|
701 | 2138 |
|d| |
1 | 2139 |
|
701 | 2140 |
d := aFraction denominator. |
7356 | 2141 |
^ aFraction class |
2142 |
numerator:(aFraction numerator - (self * d)) |
|
2143 |
denominator:d |
|
2793 | 2144 |
|
2145 |
"Modified: 28.7.1997 / 19:08:30 / cg" |
|
1 | 2146 |
! |
2147 |
||
7356 | 2148 |
equalFromFraction:aFraction |
2149 |
"that should never be invoked, as fractions are always normalized to integers |
|
2150 |
if resulting from an arithmetic operation. |
|
2151 |
However, this implementation is for subclasses (i.e. fixed point) and also |
|
2152 |
allows comparing unnormalized fractions as might appear within the fraction class" |
|
2153 |
||
2154 |
|denominator numerator| |
|
2155 |
||
2156 |
denominator := aFraction denominator. |
|
2157 |
numerator := aFraction numerator. |
|
2158 |
(denominator == 1) ifFalse:[ |
|
2159 |
^ numerator = (self * denominator) |
|
2160 |
]. |
|
2161 |
^ numerator = self |
|
2162 |
! |
|
2163 |
||
701 | 2164 |
productFromFraction:aFraction |
2165 |
"sent when a fraction does not know how to multiply the receiver, an integer" |
|
1 | 2166 |
|
7356 | 2167 |
^ aFraction class |
2168 |
numerator:(self * aFraction numerator) |
|
2169 |
denominator:aFraction denominator |
|
2793 | 2170 |
|
2171 |
"Modified: 28.7.1997 / 19:08:27 / cg" |
|
1 | 2172 |
! |
2173 |
||
701 | 2174 |
quotientFromFraction:aFraction |
7444 | 2175 |
"Return the quotient of the argument, aFraction and the receiver. |
2176 |
Sent when aFraction does not know how to divide by the receiver." |
|
701 | 2177 |
|
7356 | 2178 |
^ aFraction class |
2179 |
numerator:aFraction numerator |
|
2180 |
denominator:(self * aFraction denominator) |
|
2793 | 2181 |
|
2182 |
"Modified: 28.7.1997 / 19:08:23 / cg" |
|
701 | 2183 |
! |
1 | 2184 |
|
701 | 2185 |
sumFromFraction:aFraction |
2186 |
"sent when a fraction does not know how to add the receiver, an integer" |
|
2187 |
||
2188 |
|d| |
|
2189 |
||
2190 |
d := aFraction denominator. |
|
7356 | 2191 |
^ aFraction class |
2192 |
numerator:(aFraction numerator + (self * d)) |
|
2193 |
denominator:d |
|
2793 | 2194 |
|
2195 |
"Modified: 28.7.1997 / 19:08:11 / cg" |
|
10681 | 2196 |
! |
2197 |
||
2198 |
sumFromTimestamp:aTimestamp |
|
2199 |
"I am to be interpreted as seconds, return the timestamp this number of seconds |
|
2200 |
after aTimestamp" |
|
2201 |
||
2202 |
^ aTimestamp addSeconds:self. |
|
2203 |
||
2204 |
" |
|
2205 |
Timestamp now addSeconds:100 |
|
2206 |
100 sumFromTimestamp:Timestamp now |
|
2207 |
" |
|
1 | 2208 |
! ! |
2209 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2210 |
!Integer methodsFor:'helpers'! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2211 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2212 |
gcd_helper:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2213 |
"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
|
2214 |
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
|
2215 |
larger than the arg." |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2216 |
|
11496 | 2217 |
| a b selfLowBit argLowBit shift t | |
2218 |
||
2219 |
a := self. |
|
2220 |
b := anInteger. |
|
2221 |
||
2222 |
selfLowBit := a lowBit - 1. |
|
2223 |
argLowBit := b lowBit - 1. |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2224 |
shift := selfLowBit min:argLowBit. |
11496 | 2225 |
b := b bitShift:(argLowBit negated). |
2226 |
[a = 0] whileFalse:[ |
|
2227 |
a := a bitShift:(selfLowBit negated). |
|
2228 |
a < b ifTrue:[ |
|
2229 |
t := a. a := b. b := t |
|
2230 |
]. |
|
2231 |
a := a - b. |
|
2232 |
selfLowBit := a lowBit - 1. |
|
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2233 |
]. |
11496 | 2234 |
^ b bitShift:shift |
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2235 |
|
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2236 |
"Created: 1.3.1997 / 16:38:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2237 |
"Modified: 1.3.1997 / 16:45:17 / cg" |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2238 |
! ! |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2239 |
|
10027 | 2240 |
|
1 | 2241 |
!Integer methodsFor:'misc math'! |
2242 |
||
701 | 2243 |
acker:n |
1560 | 2244 |
"return the value of acker(self, n). |
2245 |
;-) Do not try with receivers > 3" |
|
701 | 2246 |
|
2247 |
(self == 0) ifTrue:[^ n + 1]. |
|
2248 |
(n == 0) ifTrue:[^ (self - 1) acker: 1]. |
|
2249 |
^ (self - 1) acker:(self acker:(n - 1)) |
|
2250 |
||
2251 |
" |
|
2252 |
3 acker:2 |
|
7356 | 2253 |
3 acker:7 |
701 | 2254 |
" |
1560 | 2255 |
|
2256 |
"Modified: 18.7.1996 / 13:08:16 / cg" |
|
701 | 2257 |
! |
2258 |
||
11491 | 2259 |
binco:kIn |
2260 |
^ self binomialCoefficient:kIn |
|
2261 |
! |
|
2262 |
||
11476 | 2263 |
binomialCoefficient:kIn |
11486 | 2264 |
"The binomial coefficient |
2265 |
||
11476 | 2266 |
/ n \ with self being n, and 0 <= k <= n. |
2267 |
\ k / |
|
2268 |
||
11486 | 2269 |
is the number of ways of picking k unordered outcomes from n possibilities, |
2270 |
also known as a combination or combinatorial number. |
|
2271 |
Sometimes also called C(n,k) (for choose k from n) |
|
2272 |
||
2273 |
binCo is defined as: |
|
11476 | 2274 |
n!! |
2275 |
---------- |
|
2276 |
k!! (n-k)!! |
|
2277 |
||
2278 |
but there is a faster, recursive formula: |
|
2279 |
||
2280 |
/ n \ = / n - 1 \ + / n - 1 \ |
|
2281 |
\ k / \ k - 1 / \ k / |
|
2282 |
||
2283 |
with: |
|
2284 |
||
2285 |
/ n \ = / n \ = 1 |
|
2286 |
\ 0 / \ n / |
|
2287 |
" |
|
2288 |
||
11486 | 2289 |
|k acc| |
2290 |
||
11476 | 2291 |
kIn > self ifTrue:[^ 0]. |
2292 |
||
2293 |
k := kIn. |
|
2294 |
k > (self / 2) ifTrue:[ |
|
2295 |
"/ symmetry |
|
2296 |
k := self - k. |
|
2297 |
]. |
|
2298 |
||
2299 |
acc := 1. |
|
2300 |
1 to:k do:[:i | |
|
2301 |
acc := acc * (self - k + i) / i. |
|
2302 |
]. |
|
2303 |
^ acc |
|
2304 |
||
2305 |
" |
|
2306 |
(7 binomialCoefficient:3) |
|
11486 | 2307 |
(10 binomialCoefficient:5) |
2308 |
(100 binomialCoefficient:5) |
|
2309 |
(1000 binomialCoefficient:5) |
|
2310 |
||
11476 | 2311 |
TestCase assert: (10 binomialCoefficient:5) = (10 factorial / (5 factorial * 5 factorial)) |
2312 |
TestCase assert: (100 binomialCoefficient:78) = (100 factorial / (78 factorial * (100-78) factorial)) |
|
2313 |
TestCase assert: (1000 binomialCoefficient:5) = (1000 factorial / (5 factorial * (1000-5) factorial)) |
|
2314 |
TestCase assert: (10000 binomialCoefficient:78) = (10000 factorial / (78 factorial * (10000-78) factorial)) |
|
11486 | 2315 |
|
2316 |
Time millisecondsToRun:[ (10000 binomialCoefficient:78) ] -> 0 |
|
2317 |
Time millisecondsToRun:[ (10000 factorial / (78 factorial * (10000-78) factorial)) ] -> 437 |
|
2318 |
||
11476 | 2319 |
" |
2320 |
! |
|
2321 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2322 |
divMod:aNumber |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2323 |
"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
|
2324 |
self \\ aNumber. |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2325 |
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
|
2326 |
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
|
2327 |
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
|
2328 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2329 |
^ 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
|
2330 |
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
|
2331 |
with:(self \\ aNumber) |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2332 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2333 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2334 |
10 divMod:3 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2335 |
100 factorial divMod:103 |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2336 |
" |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2337 |
|
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2338 |
"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
|
2339 |
! |
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2340 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2341 |
extendedEuclid:tb |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2342 |
"return the solution of 'ax + by = gcd(a,b)'. |
7356 | 2343 |
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
|
2344 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2345 |
|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
|
2346 |
|
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
2347 |
self < tb ifTrue:[ |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2348 |
a := self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2349 |
b := tb. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2350 |
swap := false. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2351 |
] ifFalse:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2352 |
a := tb. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2353 |
b := self. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2354 |
swap := true. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2355 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2356 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2357 |
shift := ((a lowBit) min:(b lowBit))-1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2358 |
shift > 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2359 |
tmp := shift negated. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2360 |
a := a bitShift:tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2361 |
b := b bitShift:tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2362 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2363 |
|
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
2364 |
gcd := a. |
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
2365 |
gcd1 := b. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2366 |
u := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2367 |
u1 := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2368 |
v := 0. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2369 |
v1 := 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2370 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2371 |
[ |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
2372 |
"/ The following condition is true: |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
2373 |
"/ (a * u1) + (b * v1) ~= gcd1 ifTrue:[self halt]. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2374 |
t := gcd1 divMod:gcd. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2375 |
gcd1 := gcd. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2376 |
gcd := t at:2. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2377 |
t := t at:1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2378 |
tmp := v. |
5003
304be9a3a226
Add some debug in #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
5001
diff
changeset
|
2379 |
"/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
|
2380 |
v := v1 - (v * t). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2381 |
v1 := tmp. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2382 |
tmp := u. |
5003
304be9a3a226
Add some debug in #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
5001
diff
changeset
|
2383 |
"/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
|
2384 |
u := u1 - (u * t). |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2385 |
u1 := tmp. |
9057
13bd29f579c6
Bug 343: #clearBit: changed the receiver (shouldn't do that).
Stefan Vogel <sv@exept.de>
parents:
8937
diff
changeset
|
2386 |
gcd > 0] whileTrue. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2387 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2388 |
shift > 0 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2389 |
gcd1 := gcd1 bitShift:shift. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2390 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2391 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2392 |
swap ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2393 |
^ Array with:v1 with:u1 with:gcd1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2394 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2395 |
^ Array with:u1 with:v1 with:gcd1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2396 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2397 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2398 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2399 |
14 extendedEuclid:5 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2400 |
14 extendedEuclid:2 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2401 |
25 extendedEuclid:15 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2402 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2403 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2404 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
2405 |
"Modified: / 18.11.1999 / 16:19:24 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2406 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2407 |
|
1 | 2408 |
factorial |
1560 | 2409 |
"return fac(self) (i.e. 1*2*3...*self) using an iterative algorithm. |
2410 |
This is slightly faster than the recursive algorithm, and does not |
|
2411 |
suffer from stack overflow problems (with big receivers)" |
|
2412 |
||
2745 | 2413 |
|p i| |
1560 | 2414 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2415 |
(self < 2) ifTrue:[ |
6195 | 2416 |
self < 0 ifTrue:[ |
2417 |
"/ |
|
2418 |
"/ requested factorial of a negative number |
|
2419 |
"/ |
|
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
2420 |
^ self class |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
2421 |
raise:#domainErrorSignal |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
2422 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
2423 |
selector:#factorial |
6195 | 2424 |
arguments:#() |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3374
diff
changeset
|
2425 |
errorString:'factorial of negative number' |
6195 | 2426 |
]. |
2427 |
^ 1 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2428 |
]. |
1560 | 2429 |
p := 2. |
2745 | 2430 |
i := 3. |
2431 |
[i <= self] whileTrue:[ |
|
6195 | 2432 |
p := p * i. |
2433 |
i := i + 1. |
|
1560 | 2434 |
]. |
2435 |
^ p |
|
2436 |
||
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
2437 |
" |
1560 | 2438 |
10 factorial |
2439 |
1000 factorial |
|
2440 |
10000 factorial |
|
2441 |
10000 factorialR |
|
2442 |
||
2443 |
Time millisecondsToRun:[2000 factorial] |
|
2444 |
Time millisecondsToRun:[2000 factorialR] |
|
2745 | 2445 |
-1 factorial |
1560 | 2446 |
" |
2447 |
||
10027 | 2448 |
"Modified: / 04-10-2006 / 14:31:12 / cg" |
1560 | 2449 |
! |
2450 |
||
2451 |
factorialR |
|
3908 | 2452 |
"return fac(self) (i.e. 1*2*3...*self) using a recursive algorithm. |
11496 | 2453 |
|
3908 | 2454 |
This is included to demonstration purposes - if you really need |
2455 |
factorial numbers, use the iterative #factorial, which is slightly |
|
2456 |
faster and does not suffer from stack overflow problems (with big receivers)." |
|
1 | 2457 |
|
356 | 2458 |
(self >= 2) ifTrue:[ |
3908 | 2459 |
^ self * (self - 1) factorialR |
1 | 2460 |
]. |
356 | 2461 |
^ 1 |
212 | 2462 |
|
2463 |
" |
|
1560 | 2464 |
10 factorialR |
2465 |
1000 factorialR |
|
4158 | 2466 |
Time millisecondsToRun:[10000 factorial] |
212 | 2467 |
" |
1560 | 2468 |
|
3908 | 2469 |
"Created: / 18.7.1996 / 12:48:36 / cg" |
4158 | 2470 |
"Modified: / 8.5.1999 / 18:40:13 / cg" |
1 | 2471 |
! |
2472 |
||
2428
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2473 |
gcd:anInteger |
d54efc515824
better gcd for large integers
Claus Gittinger <cg@exept.de>
parents:
2408
diff
changeset
|
2474 |
"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
|
2475 |
Euclids & Knuths algorithm." |
1 | 2476 |
|
11496 | 2477 |
|a b t| |
2478 |
||
2479 |
a := self abs. |
|
2480 |
b := anInteger abs. |
|
2481 |
||
2482 |
a < b ifTrue:[ |
|
2483 |
t := a. |
|
2484 |
a := b. |
|
2485 |
b := t. |
|
1 | 2486 |
]. |
52 | 2487 |
|
11496 | 2488 |
b = 0 ifTrue: [^ a]. |
2489 |
a := a \\ b. |
|
2490 |
a = 0 ifTrue:[^ b]. |
|
2491 |
^ b gcd_helper:a |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
2492 |
|
3060 | 2493 |
" |
11496 | 2494 |
3141589999999999 gcd:1000000000000000 |
2495 |
||
2496 |
Time millisecondsToRun:[ |
|
2497 |
10000 timesRepeat:[ |
|
2498 |
123456789012345678901234567890 gcd: 9876543210987654321 |
|
2499 |
] |
|
2500 |
] |
|
3060 | 2501 |
" |
2502 |
||
2503 |
"Modified: / 25.10.1997 / 16:08:45 / cg" |
|
1 | 2504 |
! |
2505 |
||
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2506 |
integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2507 |
"return an integer representing 1/self * 2**n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2508 |
Where an integer is one bit longer than self." |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2509 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2510 |
|b rem result| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2511 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2512 |
b := self highBit. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2513 |
rem := 1 bitShift:b. |
10488 | 2514 |
result := LargeInteger basicNew numberOfDigits:(b // 8)+1. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2515 |
b := b+1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2516 |
[b > 0] whileTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2517 |
rem >= self ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2518 |
rem := rem -= self. |
4830
879f09ec443a
Renamed #digits --> #digitBytes.
Claus Gittinger <cg@exept.de>
parents:
4803
diff
changeset
|
2519 |
result digitBytes bitSetAt:b. |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2520 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2521 |
rem := rem mul2. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2522 |
b := b - 1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2523 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2524 |
^ result compressed. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2525 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2526 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2527 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2528 |
333 integerReciprocal (2 raisedTo:18) // 333 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2529 |
393 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2530 |
8 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2531 |
15 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2532 |
15112233445566 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2533 |
10239552311579 integerReciprocal |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2534 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2535 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2536 |
"Modified: / 3.5.1999 / 14:27:18 / stefan" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2537 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2538 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2539 |
inverseMod:n |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2540 |
"find the modular inverse for myself to n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2541 |
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
|
2542 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2543 |
|e ret| |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2544 |
|
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
2545 |
"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
|
2546 |
(self * x) + (n * y) = self gcd:n" |
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
2547 |
|
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2548 |
e := self extendedEuclid:n. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2549 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2550 |
(e at:3) == 1 ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2551 |
ret := e at:1. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2552 |
ret negative ifTrue:[ |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2553 |
^ ret + n |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2554 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2555 |
^ ret. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2556 |
]. |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2557 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2558 |
^ 0 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2559 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2560 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2561 |
14 inverseMod:5 -> 4 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2562 |
5 inverseMod:14 -> 3 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2563 |
14 inverseMod:11 -> 4 (4 * 14) \\ 11 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2564 |
11 inverseMod:14 -> 9 (9 * 11) \\ 14 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2565 |
79 inverseMod:3220 -> 1019 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2566 |
3220 inverseMod:79 -> 54 (54 * 3220) \\ 79 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2567 |
1234567891 inverseMod:1111111111119 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2568 |
-> 148726663534 (148726663534*1234567891) \\ 1111111111119 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2569 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2570 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2571 |
14 extendedEuclid:11 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2572 |
5 extendedEuclid:14 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2573 |
14 extendedEuclid:2 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2574 |
3220 extendedEuclid:79 |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2575 |
" |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2576 |
|
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2577 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
5001
732ea614d36d
Comments for #inverseMod: and #extendedEuclid:
Stefan Vogel <sv@exept.de>
parents:
4996
diff
changeset
|
2578 |
"Modified: / 18.11.1999 / 16:21:37 / stefan" |
4136
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2579 |
! |
8c8bfa5808a3
Optimisations and modulo arithmethic
Stefan Vogel <sv@exept.de>
parents:
4039
diff
changeset
|
2580 |
|
1 | 2581 |
lcm:anInteger |
52 | 2582 |
"return the least common multiple (using gcd:)" |
2583 |
||
2584 |
^ (self * anInteger) abs // (self gcd: anInteger) |
|
2585 |
||
92 | 2586 |
" |
2587 |
65 lcm:15 |
|
2588 |
3 lcm:15 |
|
2589 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2590 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2591 |
|
11496 | 2592 |
primeFactors |
2593 |
"return a collection of prime factors of the receiver. |
|
2594 |
For prime numbers, an empty collection is returned. |
|
2595 |
Can take a long time for big numbers |
|
2596 |
(win a nobel price, if you find something quick)" |
|
2597 |
||
2598 |
|rest n factors limit primes lastPrime checkThisFactor nextTry| |
|
2599 |
||
2600 |
factors := Bag new. |
|
2601 |
rest := self. |
|
2602 |
limit := rest // 2. |
|
2603 |
||
2604 |
"/ try to get the number down fast: |
|
2605 |
n := rest lowBit. |
|
2606 |
n ~~ 1 ifTrue:[ |
|
2607 |
self == 2 ifTrue:[^ #() ]. |
|
2608 |
factors add:2 withOccurrences:(n-1). |
|
2609 |
rest := rest rightShift:(n-1). |
|
2610 |
]. |
|
2611 |
||
2612 |
checkThisFactor := [:prime | |
|
2613 |
prime*prime > rest ifTrue:[ |
|
2614 |
(rest ~~ 1 and:[factors notEmpty]) ifTrue:[ factors add:rest ]. |
|
2615 |
^ factors. |
|
2616 |
]. |
|
2617 |
||
2618 |
[rest \\ prime == 0] whileTrue:[ |
|
2619 |
factors add:prime. |
|
2620 |
rest := rest // prime. |
|
2621 |
rest == 1 ifTrue:[^ factors]. |
|
2622 |
]. |
|
2623 |
lastPrime := prime. |
|
2624 |
]. |
|
2625 |
||
2626 |
limit <= 2000 ifTrue:[ |
|
2627 |
primes := #( |
|
2628 |
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 |
|
2629 |
73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 |
|
2630 |
173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 |
|
2631 |
277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 |
|
2632 |
397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 |
|
2633 |
509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 |
|
2634 |
641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 |
|
2635 |
761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 |
|
2636 |
887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 |
|
2637 |
||
2638 |
1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 |
|
2639 |
1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 |
|
2640 |
1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 |
|
2641 |
1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 |
|
2642 |
1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 |
|
2643 |
1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 |
|
2644 |
1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 |
|
2645 |
1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 |
|
2646 |
1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 |
|
2647 |
). |
|
2648 |
primes do:checkThisFactor. |
|
2649 |
^ factors |
|
2650 |
]. |
|
2651 |
||
2652 |
"/ actually, all of the code is duplicated; once for primes from a table, |
|
2653 |
"/ and then primes as generated on the fly. The prime generation involves a prime-test, |
|
2654 |
"/ which may slow things down quite a bit. |
|
2655 |
"/ (the primesUpTo uses a faster sieve, but can only represent primes to upTo (say) |
|
2656 |
"/ a few millions. |
|
2657 |
||
2658 |
Integer primesUpTo:(limit min:100000) do:checkThisFactor. |
|
2659 |
||
2660 |
nextTry := lastPrime + 2. |
|
2661 |
[ nextTry <= limit ] whileTrue:[ |
|
2662 |
"/ now, we are beyond the list of pre-generated primes. |
|
2663 |
"/ change our strategy to: see if it divides an odd number; |
|
2664 |
"/ if so, add the divisor's prime factors. |
|
2665 |
nextTry*nextTry > rest ifTrue:[ |
|
2666 |
(rest ~~ 1 and:[factors notEmpty]) ifTrue:[ factors add:rest ]. |
|
2667 |
^ factors. |
|
2668 |
]. |
|
2669 |
[(rest \\ nextTry) == 0] whileTrue:[ |
|
2670 |
"/ can only happen relatively late after the last prime, |
|
2671 |
"/ because otherwise, the primeFactors of nextTry would already have |
|
2672 |
"/ been found as divisors. |
|
2673 |
"/ first chance is: (lastPrime + 2) squared |
|
2674 |
nextTry < lastPrime squared ifTrue:[ |
|
2675 |
"/ nextTry is a prime !! |
|
2676 |
factors add:nextTry |
|
2677 |
] ifFalse:[ |
|
2678 |
factors addAll:(nextTry primeFactors). |
|
2679 |
]. |
|
2680 |
rest := rest // nextTry. |
|
2681 |
]. |
|
2682 |
nextTry := nextTry + 2. |
|
2683 |
]. |
|
2684 |
^ factors |
|
2685 |
||
2686 |
" |
|
2687 |
2 to:10000 do:[:n | |
|
2688 |
self assert:((n isPrime and:[ n primeFactors isEmpty]) |
|
2689 |
or:[ n isPrime not and:[n primeFactors product = n]]) |
|
2690 |
] |
|
2691 |
3 to:10000 do:[:n | |
|
2692 |
self assert:(n factorial primeFactors product = n factorial) |
|
2693 |
] |
|
2694 |
||
2695 |
13195 primeFactors |
|
2696 |
12 primeFactors |
|
2697 |
2 primeFactors |
|
2698 |
3 primeFactors |
|
2699 |
5 primeFactors |
|
2700 |
14 primeFactors |
|
2701 |
13423453625634765 primeFactors |
|
2702 |
13423453625634765 isPrime |
|
2703 |
13423453625634765 gcd:(3 * 5 * 19 * 29) |
|
2704 |
13423453625634765 / 8265 |
|
2705 |
1624132320101 isPrime |
|
2706 |
1624132320101 gcd: 8265 |
|
2707 |
||
2708 |
1000000 primeFactors |
|
2709 |
100000000 primeFactors |
|
2710 |
1000000000 primeFactors |
|
2711 |
||
2712 |
Time millisecondsToRun:[ |
|
2713 |
1000 timesRepeat:[ |
|
2714 |
10000000000000000000000000000000000000 primeFactors |
|
2715 |
] |
|
2716 |
] 421 |
|
2717 |
" |
|
2718 |
! |
|
2719 |
||
4973 | 2720 |
raisedTo:exp mod:mod |
2721 |
"return the modulu (remainder) of |
|
2722 |
the receiver raised to exp (an Integer) and mod (another Integer)" |
|
2723 |
||
2724 |
|result m t |
|
5816 | 2725 |
"/ eI "{Class: SmallInteger}" |
2726 |
bits "{Class: SmallInteger}"| |
|
4973 | 2727 |
|
2728 |
"use the addition chaining algorithm" |
|
2729 |
||
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2730 |
exp == 0 ifTrue:[ |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2731 |
^ 1 |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2732 |
]. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2733 |
exp == 1 ifTrue:[ |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2734 |
^ self \\ mod. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2735 |
]. |
4973 | 2736 |
exp negative ifTrue:[ |
2737 |
^ self class |
|
2738 |
raise:#domainErrorSignal |
|
2739 |
receiver:self |
|
2740 |
selector:#raisedTo:mod: |
|
6195 | 2741 |
arguments:(Array with:exp with:mod) |
4973 | 2742 |
errorString:'modulo arithmethic with negative exponent' |
2743 |
]. |
|
2744 |
||
2745 |
m := mod asModuloNumber. |
|
2746 |
||
2747 |
result := 1. |
|
2748 |
t := m modulusOf:self. |
|
2749 |
||
2750 |
"/ to be validated ... |
|
2751 |
"/ (exp class == SmallInteger) ifTrue:[ |
|
2752 |
"/ eI := exp. |
|
2753 |
"/ [eI ~~ 0] whileTrue:[ |
|
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2754 |
"/ (eI bitAnd:1) == 1 ifTrue:[ |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2755 |
"/ result := m modulusOf:(result * t). |
4973 | 2756 |
"/ ]. |
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2757 |
"/ t := m modulusOf:(t * t). |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2758 |
"/ eI := eI bitShift:-1. |
4973 | 2759 |
"/ ]. |
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2760 |
"/ ^ result. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2761 |
"/ ]. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2762 |
|
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2763 |
bits := exp highBit. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2764 |
|
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2765 |
1 to:bits-1 do:[:i| |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2766 |
(exp bitAt:i) == 1 ifTrue:[ |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2767 |
result := m modulusOf:(result * t). |
4973 | 2768 |
]. |
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2769 |
t := m modulusOf:(t * t). |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2770 |
]. |
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2771 |
result := m modulusOf:(result * t). |
4973 | 2772 |
|
2773 |
^ result |
|
2774 |
||
2775 |
" |
|
2776 |
2 raisedTo:2 mod:3 |
|
2777 |
20000000000000 raisedTo:200 mod:190 -> 30 |
|
2778 |
(20000000000000 raisedTo:200) \\ 190 |
|
2779 |
||
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2780 |
Time millisecondsToRun:[10000 timesRepeat:[ |
4973 | 2781 |
20000000000000 raisedTo:200 mod:190 |
2782 |
] |
|
2783 |
] |
|
2784 |
||
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
2785 |
Time millisecondsToRun:[1000 timesRepeat:[ |
4973 | 2786 |
(20000000000000 raisedTo:200) \\ 190 |
2787 |
] |
|
2788 |
] |
|
2789 |
" |
|
2790 |
||
2791 |
"Created: / 27.4.1999 / 15:19:22 / stefan" |
|
2792 |
"Modified: / 5.5.1999 / 11:18:20 / stefan" |
|
6195 | 2793 |
"Modified: / 16.11.2001 / 14:15:21 / cg" |
4973 | 2794 |
! |
2795 |
||
2796 |
raisedToCrtModP:p q:q ep:ep eq:eq u:u |
|
2797 |
"Application of the Chinese Remainder Theorem (CRT). |
|
2798 |
||
2799 |
This is a faster modexp for moduli with a known factorisation into two |
|
2800 |
relatively prime factors p and q, and an input relatively prime to the |
|
2801 |
modulus, the Chinese Remainder Theorem to do the computation mod p and |
|
2802 |
mod q, and then combine the results. This relies on a number of |
|
2803 |
precomputed values, but does not actually require the modulus n or the |
|
2804 |
exponent e. |
|
2805 |
||
2806 |
expout = expin ^ e mod (p*q). |
|
2807 |
We form this by evaluating |
|
2808 |
p2 = (expin ^ e) mod p and |
|
2809 |
q2 = (expin ^ e) mod q |
|
2810 |
and then combining the two by the CRT. |
|
2811 |
||
2812 |
Two optimisations of this are possible. First, we can reduce expin |
|
2813 |
modulo p and q before starting. |
|
2814 |
||
2815 |
Second, since we know the factorisation of p and q (trivially derived |
|
2816 |
from the factorisation of n = p*q), and expin is relatively prime to |
|
2817 |
both p and q, we can use Euler's theorem, expin^phi(m) = 1 (mod m), |
|
2818 |
to throw away multiples of phi(p) or phi(q) in e. |
|
2819 |
Letting ep = e mod phi(p) and |
|
2820 |
eq = e mod phi(q) |
|
2821 |
then combining these two speedups, we only need to evaluate |
|
2822 |
p2 = ((expin mod p) ^ ep) mod p and |
|
2823 |
q2 = ((expin mod q) ^ eq) mod q. |
|
2824 |
||
2825 |
Now we need to apply the CRT. Starting with |
|
2826 |
expout = p2 (mod p) and |
|
2827 |
expout = q2 (mod q) |
|
2828 |
we can say that expout = p2 + p * k, and if we assume that 0 <= p2 < p, |
|
2829 |
then 0 <= expout < p*q for some 0 <= k < q. Since we want expout = q2 |
|
2830 |
(mod q), then p*k = q2-p2 (mod q). Since p and q are relatively prime, |
|
2831 |
p has a multiplicative inverse u mod q. In other words, u = 1/p (mod q). |
|
2832 |
||
2833 |
Multiplying by u on both sides gives k = u*(q2-p2) (mod q). |
|
2834 |
Since we want 0 <= k < q, we can thus find k as |
|
2835 |
k = (u * (q2-p2)) mod q. |
|
2836 |
||
2837 |
Once we have k, evaluating p2 + p * k is easy, and |
|
2838 |
that gives us the result |
|
2839 |
" |
|
2840 |
||
2841 |
|result t mp mq| |
|
2842 |
||
2843 |
||
2844 |
mp := p asModuloNumber. |
|
2845 |
t := mp modulusOf:self. |
|
2846 |
result := t raisedTo:ep mod:mp. |
|
2847 |
||
2848 |
mq := q asModuloNumber. |
|
2849 |
t := mq modulusOf:self. |
|
2850 |
t := t raisedTo:eq mod:mq. |
|
2851 |
||
2852 |
"now p2 is in result, q2 in t" |
|
2853 |
||
2854 |
t := t -= result. |
|
2855 |
t < 0 ifTrue:[ |
|
2856 |
t := t + q. |
|
2857 |
]. |
|
2858 |
t := t *= u. |
|
2859 |
t := mq modulusOf:t. |
|
2860 |
t := t *= p. |
|
2861 |
result := result += t. |
|
2862 |
||
2863 |
^ result. |
|
2864 |
||
2865 |
||
2866 |
||
2867 |
" |
|
2868 |
2 raisedTo:2 mod:3 |
|
2869 |
20000000000000 raisedTo:200 mod:190 |
|
2870 |
(20000000000000 raisedTo:200) \\ 190 |
|
2871 |
" |
|
2872 |
||
2873 |
"Created: / 30.4.1999 / 15:53:15 / stefan" |
|
2874 |
"Modified: / 5.5.1999 / 11:01:15 / stefan" |
|
1 | 2875 |
! ! |
2876 |
||
2877 |
!Integer methodsFor:'printing & storing'! |
|
2878 |
||
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2879 |
asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2880 |
"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
|
2881 |
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
|
2882 |
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
|
2883 |
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
|
2884 |
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
|
2885 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2886 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2887 |
|s rest twoDigits hi lo| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2888 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2889 |
self == 0 ifTrue:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2890 |
^ #[ 16r00 ] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2891 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2892 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2893 |
"/ 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
|
2894 |
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
|
2895 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2896 |
rest := self. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2897 |
[rest > 0] whileTrue:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2898 |
twoDigits := rest \\ 100. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2899 |
rest := rest // 100. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2900 |
hi := twoDigits \\ 10. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2901 |
lo := twoDigits // 10. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2902 |
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
|
2903 |
]. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2904 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2905 |
^ s contents reverse |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2906 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2907 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2908 |
12345678 asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2909 |
12345678901234567890 asBCDBytes |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2910 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2911 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2912 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2913 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
2914 |
|
49 | 2915 |
displayString |
2916 |
"return a string for displaying in a view (as in inspector). |
|
2917 |
The output radix is usually 10, but can be changed by setting |
|
2918 |
DefaultDisplayRadix (see Integer>>displayRadix:)" |
|
2919 |
||
54 | 2920 |
(DefaultDisplayRadix isNil or:[DefaultDisplayRadix == 10]) ifTrue:[ |
159 | 2921 |
^ self printString |
54 | 2922 |
]. |
49 | 2923 |
^ self radixPrintStringRadix:DefaultDisplayRadix |
2924 |
||
92 | 2925 |
" |
2926 |
Integer displayRadix:16. 12345 |
|
2927 |
Integer displayRadix:2. 12345 |
|
2928 |
Integer displayRadix:10. 12345 |
|
2929 |
" |
|
49 | 2930 |
! |
2931 |
||
701 | 2932 |
errorPrintHex |
2933 |
"print the receiver as a hex number on the standard error stream" |
|
2934 |
||
2935 |
(self printStringRadix:16) errorPrint |
|
2936 |
! |
|
1 | 2937 |
|
701 | 2938 |
hexPrintString |
2939 |
"return a hex string representation of the receiver" |
|
2940 |
||
2941 |
^ self printStringRadix:16 |
|
3874 | 2942 |
|
2943 |
" |
|
2944 |
127 hexPrintString |
|
2945 |
-1 hexPrintString |
|
2946 |
" |
|
2947 |
||
2948 |
"Modified: / 11.10.1998 / 01:15:43 / cg" |
|
701 | 2949 |
! |
2950 |
||
2408 | 2951 |
hexPrintString:size |
2952 |
"return a hex string representation of the receiver, |
|
2953 |
padded to size characters" |
|
2954 |
||
5799 | 2955 |
"/ ^ (self printStringRadix:16) leftPaddedTo:size with:$0 |
2956 |
^ self printStringRadix:16 size:size fill:$0 |
|
2957 |
||
2958 |
" |
|
6635 | 2959 |
12345 hexPrintString:4 |
2960 |
123 hexPrintString:4 |
|
5799 | 2961 |
" |
2408 | 2962 |
|
2963 |
"Created: 18.2.1997 / 13:32:33 / cg" |
|
2964 |
! |
|
2965 |
||
701 | 2966 |
printHex |
2967 |
"print the receiver as a hex number on the standard output stream" |
|
1 | 2968 |
|
701 | 2969 |
(self printStringRadix:16) print |
2970 |
! |
|
2971 |
||
2972 |
printOn:aStream |
|
2973 |
"append a printed description of the receiver to aStream" |
|
2974 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2975 |
self printOn:aStream base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2976 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
2977 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
701 | 2978 |
! |
2979 |
||
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
2980 |
printOn:aStream base:b |
1 | 2981 |
"return a string representation of the receiver in the specified |
2982 |
radix (without the initial XXr)" |
|
2983 |
||
5988 | 2984 |
^ self printOn:aStream base:b showRadix:false |
2985 |
||
2986 |
" |
|
2987 |
10 printOn:Transcript base:3 |
|
2988 |
31 printOn:Transcript base:3 |
|
2989 |
-20 printOn:Transcript base:16 |
|
2990 |
-20 printOn:Transcript base:10 |
|
2991 |
3000 factorial printOn:Transcript base:10 |
|
2992 |
" |
|
2993 |
||
2994 |
"Modified: / 20.1.1998 / 18:05:02 / stefan" |
|
2995 |
"Modified: / 7.9.2001 / 13:52:17 / cg" |
|
2996 |
! |
|
2997 |
||
2998 |
printOn:aStream base:b showRadix:showRadix |
|
2999 |
"append a string representation of the receiver in the specified numberBase to aStream |
|
3000 |
(if showRadix is true, with initial XXr) |
|
3001 |
The radix argument should be between 2 and 36." |
|
3002 |
||
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
3003 |
|base num s divMod mod r r2 r4 nD numN| |
1 | 3004 |
|
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
3005 |
base := b. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3006 |
(base between:2 and:36) ifFalse:[ |
4522
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
3007 |
self error:'invalid base' mayProceed:true. |
29a6625dc7b1
error: vs. error:mayProceed:
Claus Gittinger <cg@exept.de>
parents:
4424
diff
changeset
|
3008 |
base := 10. |
54 | 3009 |
]. |
3010 |
||
5988 | 3011 |
showRadix ifTrue:[ |
3012 |
base printOn:aStream. |
|
3013 |
aStream nextPutAll:'r'. |
|
3014 |
]. |
|
3015 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3016 |
(self = 0) ifTrue:[aStream nextPut:$0. ^ self]. |
1 | 3017 |
(self < 0) ifTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3018 |
aStream nextPut:$- . |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3019 |
num := self negated. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3020 |
] ifFalse:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3021 |
num := self. |
1 | 3022 |
]. |
92 | 3023 |
|
3024 |
" |
|
3025 |
claus: changed it from a recursive algorithm; |
|
3026 |
(it used to trigger stack-overflow exceptions when printing |
|
3027 |
3000 factorial ...) |
|
3028 |
" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3029 |
"/ leftPart := num // base. |
92 | 3030 |
"/ (leftPart ~= 0) ifTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3031 |
"/ leftPart printOn:aStream base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3032 |
"/ aStream nextPut:(Character digitValue:(num \\ base). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3033 |
"/ ^ self |
92 | 3034 |
"/ ]. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3035 |
"/ aStream nextPut:(Character digitValue:num). |
2951 | 3036 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
3037 |
"/ 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
|
3038 |
"/ 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
|
3039 |
"/ 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
|
3040 |
"/ 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
|
3041 |
"/ 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
|
3042 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3043 |
r2 := base*base. "/ radix^2 |
3138 | 3044 |
r4 := r2*r2. "/ radix^4 |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3045 |
base <= 10 ifTrue:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3046 |
r := r4*r2. "/ radix^6 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3047 |
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
|
3048 |
] ifFalse:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3049 |
r := r4*base. "/ radix^5 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3050 |
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
|
3051 |
]. |
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
3052 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3053 |
"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
|
3054 |
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
|
3055 |
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
|
3056 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3057 |
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
|
3058 |
|
3121
91de0211651c
even faster largeInt printString, by doing it in bigger junks (8digits a time)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
3059 |
[num >= r] whileTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3060 |
"/ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3061 |
"/ chop off nD digits. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3062 |
"/ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3063 |
divMod := num divMod:r. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3064 |
num := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3065 |
numN := divMod at:2. |
2951 | 3066 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3067 |
"/ process them |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3068 |
nD timesRepeat:[ |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3069 |
divMod := numN divMod:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3070 |
numN := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3071 |
mod := divMod at:2. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3072 |
s nextPut:(Character digitValue:mod). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3073 |
]. |
2951 | 3074 |
]. |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3075 |
|
92 | 3076 |
[num ~= 0] whileTrue:[ |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3077 |
divMod := num divMod:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3078 |
num := divMod at:1. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3079 |
mod := divMod at:2. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3080 |
s nextPut:(Character digitValue:mod). |
1 | 3081 |
]. |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3082 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3083 |
aStream nextPutAll:(s contents reverse). |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3084 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3085 |
" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3086 |
3000 factorial printOn:Transcript base:10 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3087 |
10 printOn:Transcript base:3 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3088 |
31 printOn:Transcript base:3 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3089 |
-20 printOn:Transcript base:16 |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3090 |
-20 printOn:Transcript base:10 |
6515 | 3091 |
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
|
3092 |
" |
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3093 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3094 |
"Modified: / 20.1.1998 / 18:05:02 / stefan" |
5988 | 3095 |
"Created: / 7.9.2001 / 13:51:33 / cg" |
3096 |
"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
|
3097 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3098 |
|
7173 | 3099 |
printOn:aStream base:baseInteger size:sz fill:fillCharacter |
3100 |
"print a string representation of the receiver in the specified |
|
3101 |
base. The string is padded on the left with fillCharacter to make |
|
3102 |
its size as specified in sz." |
|
3103 |
||
3104 |
|stream string actualSize| |
|
3105 |
||
3106 |
stream := '' writeStream. |
|
3107 |
self printOn:stream base:baseInteger showRadix:false. |
|
3108 |
string := stream contents. |
|
3109 |
actualSize := string size. |
|
3110 |
actualSize < sz ifTrue:[ |
|
3111 |
aStream next:sz-actualSize put:fillCharacter. |
|
3112 |
]. |
|
3113 |
aStream nextPutAll:string. |
|
3114 |
||
3115 |
" |
|
3116 |
1024 printOn:Transcript base:16 size:4 fill:$0 |
|
3117 |
1024 printOn:Transcript base:2 size:16 fill:$. |
|
3118 |
1024 printOn:Transcript base:16 size:8 fill:Character space. |
|
3119 |
" |
|
3120 |
! |
|
3121 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3122 |
printOn:aStream radix:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3123 |
"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
|
3124 |
The receiver is printed in radix base (instead of the default, 10). |
5988 | 3125 |
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
|
3126 |
|
8897
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
3127 |
<resource: #obsolete> |
da8d205a93aa
Mark obsolete method as obsolete
Stefan Vogel <sv@exept.de>
parents:
8892
diff
changeset
|
3128 |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3129 |
self printOn:aStream base:base |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3130 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3131 |
"Modified: / 20.1.1998 / 14:10:45 / stefan" |
5988 | 3132 |
"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
|
3133 |
! |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3134 |
|
6635 | 3135 |
printRomanOn:aStream |
6673 | 3136 |
"print the receiver as roman number to the receiver, aStream. |
3137 |
This converts correct (i.e. prefix notation for 4,9,40,90, etc.)." |
|
3138 |
||
3139 |
^ self printRomanOn:aStream naive:false |
|
6635 | 3140 |
|
3141 |
" |
|
6673 | 3142 |
1 to:10 do:[:i | i printRomanOn:Transcript. Transcript cr.]. |
6635 | 3143 |
1999 printRomanOn:Transcript. Transcript cr. |
3144 |
Date today year printRomanOn:Transcript. Transcript cr. |
|
3145 |
" |
|
3146 |
||
3147 |
"test all between 1 and 9999: |
|
3148 |
1 to:9999 do:[:n | |
|
3149 |
|romanString| |
|
3150 |
||
3151 |
romanString := String streamContents:[:stream | n printRomanOn:stream]. |
|
6673 | 3152 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
3153 |
] |
|
3154 |
" |
|
3155 |
! |
|
3156 |
||
3157 |
printRomanOn:aStream naive:naive |
|
3158 |
"print the receiver as roman number to the receiver, aStream. |
|
3159 |
The naive argument controls if the conversion is |
|
6682 | 3160 |
correct (i.e. subtracting prefix notation for 4,9,40,90, etc.), |
3161 |
or naive (i.e. print 4 as IIII and 9 as VIIII); also called simple. |
|
6673 | 3162 |
The naive version is often used for page numbers in documents." |
3163 |
||
3164 |
|restValue spec| |
|
3165 |
||
3166 |
restValue := self. |
|
3167 |
restValue > 0 ifFalse:[self error:'negative roman']. |
|
3168 |
||
3169 |
naive ifTrue:[ |
|
3170 |
spec := #( |
|
3171 |
" value string repeat " |
|
3172 |
1000 'M' true |
|
3173 |
500 'D' false |
|
3174 |
100 'C' true |
|
3175 |
50 'L' false |
|
3176 |
10 'X' true |
|
3177 |
5 'V' false |
|
3178 |
1 'I' true |
|
3179 |
). |
|
3180 |
] ifFalse:[ |
|
3181 |
spec := #( |
|
3182 |
" value string repeat " |
|
3183 |
1000 'M' true |
|
3184 |
900 'CM' false |
|
3185 |
500 'D' false |
|
3186 |
400 'CD' false |
|
3187 |
100 'C' true |
|
3188 |
90 'XC' false |
|
3189 |
50 'L' false |
|
3190 |
40 'XL' false |
|
3191 |
10 'X' true |
|
3192 |
9 'IX' false |
|
3193 |
5 'V' false |
|
3194 |
4 'IV' false |
|
3195 |
1 'I' true |
|
3196 |
). |
|
3197 |
]. |
|
3198 |
||
3199 |
spec |
|
3200 |
inGroupsOf:3 |
|
3201 |
do:[:rValue :rString :repeatFlag | |
|
3202 |
||
3203 |
[ |
|
3204 |
(restValue >= rValue) ifTrue:[ |
|
3205 |
aStream nextPutAll:rString. |
|
3206 |
restValue := restValue - rValue. |
|
3207 |
]. |
|
3208 |
] doWhile:[ repeatFlag and:[ restValue >= rValue] ]. |
|
3209 |
]. |
|
3210 |
||
3211 |
" |
|
3212 |
1 to:10 do:[:i | i printRomanOn:Transcript naive:false. Transcript cr.]. |
|
3213 |
1 to:10 do:[:i | i printRomanOn:Transcript naive:true. Transcript cr.]. |
|
3214 |
||
3215 |
1999 printRomanOn:Transcript. Transcript cr. |
|
3216 |
Date today year printRomanOn:Transcript. Transcript cr. |
|
3217 |
" |
|
3218 |
||
3219 |
"test all between 1 and 9999: |
|
3220 |
1 to:9999 do:[:n | |
|
3221 |
|romanString| |
|
3222 |
||
3223 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:false]. |
|
3224 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
|
3225 |
] |
|
3226 |
" |
|
3227 |
||
3228 |
"test naive all between 1 and 9999: |
|
3229 |
1 to:9999 do:[:n | |
|
3230 |
|romanString| |
|
3231 |
||
3232 |
romanString := String streamContents:[:stream | n printRomanOn:stream naive:true]. |
|
6682 | 3233 |
(Integer readFromRomanString:romanString onError:nil) ~= n ifTrue:[self halt]. |
6635 | 3234 |
] |
3235 |
" |
|
3236 |
! |
|
3237 |
||
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3238 |
printStringRadix:base |
5988 | 3239 |
"return a string representation of the receiver in the specified |
3240 |
base; does NOT prepend XXr to the string. |
|
3241 |
See also: radixPrintStringRadix: |
|
3242 |
printOn:base:showRadix:" |
|
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3243 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3244 |
|s| |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3245 |
|
7685
535a69a7cd69
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7551
diff
changeset
|
3246 |
s := WriteStream on:(String basicNew:20). |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3247 |
self printOn:s base:base. |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3248 |
^ s contents |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3249 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3250 |
"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
|
3251 |
"Modified: / 20.1.1998 / 14:10:54 / stefan" |
5988 | 3252 |
"Modified: / 7.9.2001 / 13:58:13 / cg" |
1 | 3253 |
! |
3254 |
||
3255 |
printStringRadix:aRadix size:sz fill:fillCharacter |
|
3256 |
"return a string representation of the receiver in the specified |
|
3257 |
radix. The string is padded on the left with fillCharacter to make |
|
3258 |
its size as specified in sz." |
|
3259 |
||
2951 | 3260 |
|s actualSize| |
1 | 3261 |
|
3262 |
s := self printStringRadix:aRadix. |
|
2951 | 3263 |
actualSize := s size. |
3264 |
actualSize < sz ifTrue:[ |
|
3265 |
s := ((String new:(sz - actualSize)) atAllPut:fillCharacter) , s |
|
1 | 3266 |
]. |
3267 |
^ s |
|
3268 |
||
92 | 3269 |
" |
3270 |
1024 printStringRadix:16 size:4 fill:$0 |
|
3271 |
1024 printStringRadix:2 size:16 fill:$. |
|
3272 |
1024 printStringRadix:16 size:8 fill:(Character space) |
|
3273 |
" |
|
216 | 3274 |
! |
3275 |
||
5988 | 3276 |
radixPrintStringRadix:radix |
701 | 3277 |
"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
|
3278 |
base; prepend XXr to the string" |
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3279 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3280 |
|s| |
701 | 3281 |
|
7685
535a69a7cd69
String new -> uninitializedNew: / basicNew:
Claus Gittinger <cg@exept.de>
parents:
7551
diff
changeset
|
3282 |
s := WriteStream on:(String basicNew:20). |
5988 | 3283 |
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
|
3284 |
^ s contents |
216 | 3285 |
|
701 | 3286 |
" |
5988 | 3287 |
31 radixPrintStringRadix:2 |
3288 |
31 radixPrintStringRadix:3 |
|
3289 |
31 radixPrintStringRadix:10 |
|
3290 |
31 radixPrintStringRadix:16 |
|
3291 |
31 radixPrintStringRadix:36 |
|
701 | 3292 |
" |
3188
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3293 |
|
301eaf46b9e4
Use #printOn: instead of #printString as base method.
Stefan Vogel <sv@exept.de>
parents:
3162
diff
changeset
|
3294 |
"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
|
3295 |
"Modified: / 20.1.1998 / 14:11:03 / stefan" |
5988 | 3296 |
"Modified: / 7.9.2001 / 13:57:00 / cg" |
6635 | 3297 |
! |
3298 |
||
3299 |
romanPrintString |
|
3300 |
"return a roman number representation of the receiver as a string" |
|
3301 |
||
3302 |
^ String streamContents:[:stream | self printRomanOn:stream]. |
|
3303 |
||
3304 |
" |
|
3305 |
1999 romanPrintString. |
|
3306 |
Date today year romanPrintString. |
|
3307 |
" |
|
1 | 3308 |
! ! |
3309 |
||
701 | 3310 |
!Integer methodsFor:'queries'! |
202 | 3311 |
|
701 | 3312 |
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
|
3313 |
"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
|
3314 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3315 |
self subclassResponsibility |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3316 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3317 |
"This is a very stupid implementation, and should be redefined in |
701 | 3318 |
concrete subclasses." |
202 | 3319 |
|
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3320 |
"/ |num count| |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3321 |
"/ |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3322 |
"/ num := self. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3323 |
"/ count := n. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3324 |
"/ [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
|
3325 |
"/ 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
|
3326 |
"/ 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
|
3327 |
"/ ]. |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3328 |
"/ ^ num \\ 256 |
159 | 3329 |
|
3330 |
" |
|
701 | 3331 |
16r44332211 digitAt:1 |
3332 |
16r44332211 digitAt:2 |
|
3333 |
16r44332211 digitAt:3 |
|
3334 |
16r44332211 digitAt:4 |
|
3335 |
16r44332211 digitAt:5 |
|
3336 |
16r00332211 digitAt:4 |
|
3337 |
16r00332211 digitAt:5 |
|
159 | 3338 |
" |
6055
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3339 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3340 |
"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
|
3341 |
! |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3342 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3343 |
digitByteAt:n |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3344 |
"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
|
3345 |
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
|
3346 |
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
|
3347 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3348 |
self subclassResponsibility |
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3349 |
|
e4d5016bad38
no need for #digitAt: here - it ought to be redefined in ALL
Claus Gittinger <cg@exept.de>
parents:
5988
diff
changeset
|
3350 |
"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
|
3351 |
"Modified: / 26.9.2001 / 21:20:19 / cg" |
1 | 3352 |
! |
3353 |
||
701 | 3354 |
digitLength |
3355 |
"return the number of bytes needed for the binary representation |
|
2816 | 3356 |
of the receiver. |
3357 |
This method is redefined in concrete classes - the fallback here is |
|
3358 |
never really used." |
|
701 | 3359 |
|
3360 |
^ (self log:256) ceiling asInteger |
|
2816 | 3361 |
|
3362 |
"Modified: 31.7.1997 / 13:19:06 / cg" |
|
701 | 3363 |
! |
3364 |
||
3365 |
isInteger |
|
3366 |
"return true, if the receiver is some kind of integer number" |
|
3367 |
||
3368 |
^ true |
|
3036 | 3369 |
! |
3370 |
||
9134 | 3371 |
isPowerOf:p |
3372 |
"return true, if the receiver is a power of p" |
|
3373 |
||
3374 |
p == 2 ifTrue:[ ^ self isPowerOfTwo]. |
|
3375 |
||
3376 |
"/ the following is a q&d hack, using existing code. |
|
3377 |
^ (Integer |
|
9136 | 3378 |
readFromString:(self printStringRadix:p) |
9134 | 3379 |
radix:2 |
3380 |
onError:-1) isPowerOfTwo |
|
3381 |
||
3382 |
" |
|
3383 |
16r0000000000000000 isPowerOf:2 |
|
3384 |
16r0000004000000000 isPowerOf:2 |
|
3385 |
16r0000004000000001 isPowerOf:2 |
|
3386 |
||
3387 |
16r0000000000000001 isPowerOf:2 |
|
3388 |
16r0000000000000002 isPowerOf:2 |
|
3389 |
16r0000000000000004 isPowerOf:2 |
|
3390 |
16r0000000000000008 isPowerOf:2 |
|
3391 |
||
3392 |
16r0000000000000001 isPowerOf:4 |
|
3393 |
16r0000000000000002 isPowerOf:4 |
|
3394 |
16r0000000000000004 isPowerOf:4 |
|
3395 |
16r0000000000000008 isPowerOf:4 |
|
3396 |
16r0000000000000010 isPowerOf:4 |
|
3397 |
16r0000000000000020 isPowerOf:4 |
|
3398 |
||
3399 |
3r0000000000000001 isPowerOf:3 |
|
3400 |
3r0000000000000010 isPowerOf:3 |
|
3401 |
3r0000000000000100 isPowerOf:3 |
|
3402 |
3r0000000000001000 isPowerOf:3 |
|
3403 |
3r0000000000001001 isPowerOf:3 |
|
3404 |
3r0000000000002000 isPowerOf:3 |
|
3405 |
||
3406 |
10 isPowerOf:10 |
|
3407 |
20 isPowerOf:10 |
|
3408 |
100 isPowerOf:10 |
|
3409 |
110 isPowerOf:10 |
|
3410 |
200 isPowerOf:10 |
|
3411 |
1000 isPowerOf:10 |
|
3412 |
10000 isPowerOf:10 |
|
3413 |
100000 isPowerOf:10 |
|
3414 |
100001 isPowerOf:10 |
|
3415 |
" |
|
3416 |
! |
|
3417 |
||
3036 | 3418 |
isPowerOfTwo |
3419 |
"return true, if the receiver is a power of 2" |
|
3420 |
||
9134 | 3421 |
"/ mhmh: how about the following |
3422 |
"/ self == 0 ifTrue:[^ false]. |
|
3423 |
||
3036 | 3424 |
^ (self bitAnd:(self - 1)) == 0 |
3425 |
||
3426 |
" |
|
3427 |
0 isPowerOfTwo |
|
3428 |
1 isPowerOfTwo |
|
3429 |
2 isPowerOfTwo |
|
3430 |
3 isPowerOfTwo |
|
3431 |
4 isPowerOfTwo |
|
3432 |
16r8000000000000000 isPowerOfTwo |
|
3433 |
16r8000000000000001 isPowerOfTwo |
|
3434 |
" |
|
3435 |
||
3436 |
"Modified: 15.10.1997 / 18:43:49 / cg" |
|
701 | 3437 |
! ! |
202 | 3438 |
|
2458 | 3439 |
!Integer methodsFor:'special bit operators'! |
3440 |
||
3441 |
bitAnd_32:anInteger |
|
3442 |
"return a C-semantic 32bit locical-and of the receiver and |
|
3443 |
the argument. Both must be either Small- or LargeIntegers. |
|
3444 |
This (nonstandard) specialized method is provided to allow simulation of |
|
3445 |
and operations with C semantics." |
|
3446 |
||
3447 |
%{ /* NOCONTEXT */ |
|
3448 |
int val1, val2, rslt; |
|
3449 |
||
3450 |
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
|
3451 |
val1 = __intVal(self); |
2458 | 3452 |
} 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
|
3453 |
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
|
3454 |
if (!val1) goto bad; |
2458 | 3455 |
} 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
|
3456 |
goto bad; |
2458 | 3457 |
} |
3458 |
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
|
3459 |
val2 = __intVal(anInteger); |
2458 | 3460 |
} 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
|
3461 |
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
|
3462 |
if (!val2) goto bad; |
2458 | 3463 |
} 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
|
3464 |
goto bad; |
2458 | 3465 |
} |
3466 |
rslt = val1 & val2; |
|
3467 |
RETURN(__MKINT(rslt)); |
|
3468 |
||
3469 |
bad: ; |
|
3470 |
%}. |
|
3471 |
self primitiveFailed. |
|
3472 |
! |
|
3473 |
||
3474 |
bitOr_32:anInteger |
|
3475 |
"return a C-semantic 32bit locical-or of the receiver and |
|
3476 |
the argument. Both must be either Small- or LargeIntegers. |
|
3477 |
This (nonstandard) specialized method is provided to allow simulation of |
|
3478 |
and operations with C semantics." |
|
3479 |
||
3480 |
%{ /* NOCONTEXT */ |
|
3481 |
int val1, val2, rslt; |
|
3482 |
||
3483 |
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
|
3484 |
val1 = __intVal(self); |
2458 | 3485 |
} 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
|
3486 |
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
|
3487 |
if (!val1) goto bad; |
2458 | 3488 |
} 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
|
3489 |
goto bad; |
2458 | 3490 |
} |
3491 |
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
|
3492 |
val2 = __intVal(anInteger); |
2458 | 3493 |
} 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
|
3494 |
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
|
3495 |
if (!val2) goto bad; |
2458 | 3496 |
} 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
|
3497 |
goto bad; |
2458 | 3498 |
} |
3499 |
rslt = val1 | val2; |
|
3500 |
RETURN(__MKINT(rslt)); |
|
3501 |
||
3502 |
bad: ; |
|
3503 |
%}. |
|
3504 |
self primitiveFailed. |
|
3505 |
! ! |
|
3506 |
||
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3507 |
!Integer methodsFor:'testing'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3508 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3509 |
isLiteral |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3510 |
"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
|
3511 |
(i.e. can be used in constant arrays)" |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3512 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3513 |
^ true |
9316 | 3514 |
! |
3515 |
||
3516 |
isPrime |
|
11464 | 3517 |
"return true if I am a prime Number. |
3518 |
This is a q&d hack, which may need optimization if heavily used." |
|
9316 | 3519 |
|
11485 | 3520 |
|limit firstFewPrimes| |
11440 | 3521 |
|
11439 | 3522 |
self even ifTrue:[^ self == 2 ]. |
11485 | 3523 |
self == 1 ifTrue:[^ false ]. |
3524 |
||
11440 | 3525 |
limit := self sqrt. |
11485 | 3526 |
|
3527 |
"/ primes up to 1000 |
|
3528 |
firstFewPrimes := #( |
|
3529 |
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 |
|
3530 |
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 |
|
3531 |
193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 |
|
3532 |
293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 |
|
3533 |
409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 |
|
3534 |
521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 |
|
3535 |
641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 |
|
3536 |
757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 |
|
3537 |
881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997). |
|
3538 |
||
3539 |
firstFewPrimes do:[:p | |
|
3540 |
p > limit ifTrue:[^ true]. |
|
3541 |
(self \\ p) == 0 ifTrue:[ ^ false ]. |
|
3542 |
]. |
|
3543 |
||
3544 |
(firstFewPrimes last+2) to:limit by:2 do:[:i | |
|
11496 | 3545 |
(self \\ i) == 0 ifTrue:[ self halt. ^ false ]. |
9316 | 3546 |
]. |
3547 |
^ true |
|
11485 | 3548 |
|
3549 |
" |
|
3550 |
Integer primesUpTo:1000 |
|
3551 |
(1 to:1000000) count:[:n | n isPrime] 78498 |
|
3552 |
Time millisecondsToRun:[ (1 to:1000000) count:[:n | n isPrime]] 1295 w.o firstFewPrimes |
|
3553 |
Time millisecondsToRun:[ (1 to:1000000) count:[:n | n isPrime]] 936 with firstFewPrimes (less tests) |
|
3554 |
" |
|
9316 | 3555 |
! |
3556 |
||
3557 |
nextPrime |
|
3558 |
"return the next prime after the receiver" |
|
3559 |
||
3560 |
|num| |
|
3561 |
||
3562 |
num := self + 1. |
|
3563 |
num even ifTrue:[ |
|
11494 | 3564 |
num == 2 ifTrue:[^ num]. |
9316 | 3565 |
num := num + 1 |
3566 |
]. |
|
3567 |
[num isPrime] whileFalse:[ |
|
3568 |
num := num + 2 |
|
3569 |
]. |
|
3570 |
^ num |
|
3571 |
||
3572 |
" |
|
11494 | 3573 |
1 nextPrime |
9316 | 3574 |
22 nextPrime |
3575 |
37 nextPrime |
|
3576 |
36 nextPrime |
|
3577 |
3456737 nextPrime |
|
3578 |
" |
|
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3579 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3580 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3581 |
!Integer methodsFor:'tracing'! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3582 |
|
4682 | 3583 |
traceInto:aRequestor level:level from:referrer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3584 |
"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
|
3585 |
|
4682 | 3586 |
^ aRequestor traceInteger:self level:level from:referrer |
4653
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3587 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3588 |
|
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3589 |
! ! |
b3d38149fbde
added #hasSharedInstances & tracing support
Claus Gittinger <cg@exept.de>
parents:
4643
diff
changeset
|
3590 |
|
701 | 3591 |
!Integer methodsFor:'truncation & rounding'! |
3592 |
||
3593 |
ceiling |
|
1558 | 3594 |
"return the smallest integer which is larger or equal to the receiver. |
3595 |
For integers, this is the receiver itself." |
|
345 | 3596 |
|
701 | 3597 |
^ self |
1558 | 3598 |
|
3599 |
"Modified: 18.7.1996 / 12:44:06 / cg" |
|
701 | 3600 |
! |
345 | 3601 |
|
701 | 3602 |
compressed |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3603 |
"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
|
3604 |
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
|
3605 |
zeros removed. This method is redefined in LargeInteger." |
701 | 3606 |
|
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3607 |
^ self |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3608 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3609 |
"Modified: 5.11.1996 / 14:07:41 / cg" |
202 | 3610 |
! |
3611 |
||
701 | 3612 |
floor |
1558 | 3613 |
"return the largest integer which is smaller or equal to the receiver. |
3614 |
For integers, this is the receiver itself." |
|
701 | 3615 |
|
3616 |
^ self |
|
1558 | 3617 |
|
3618 |
"Modified: 18.7.1996 / 12:44:00 / cg" |
|
701 | 3619 |
! |
202 | 3620 |
|
1866
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3621 |
fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3622 |
"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
|
3623 |
(i.e. the receiver minus its truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3624 |
Since integers have no fraction, return 0 here." |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3625 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3626 |
^ 0 |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3627 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3628 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3629 |
1234.56789 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3630 |
1.2345e6 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3631 |
1000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3632 |
10000000000000000 fractionPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3633 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3634 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3635 |
"Modified: 4.11.1996 / 20:27:44 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3636 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3637 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3638 |
integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3639 |
"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
|
3640 |
(i.e. the receivers truncated value) |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3641 |
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
|
3642 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3643 |
^ self |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3644 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3645 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3646 |
1234.56789 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3647 |
1.2345e6 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3648 |
1000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3649 |
10000000000000000 integerPart |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3650 |
" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3651 |
|
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3652 |
"Modified: 4.11.1996 / 20:28:22 / cg" |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3653 |
! |
225afe414934
added fractionPart & integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1842
diff
changeset
|
3654 |
|
701 | 3655 |
normalize |
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3656 |
"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
|
3657 |
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
|
3658 |
zeros removed. |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3659 |
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
|
3660 |
renamed to #compressed for ST-80 compatibility." |
1 | 3661 |
|
9261 | 3662 |
<resource: #obsolete> |
3663 |
||
1883
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3664 |
^ self compressed |
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3665 |
|
d3fbf4b36be7
renamed #normalize to #compressed (ST_80 compat)
Claus Gittinger <cg@exept.de>
parents:
1866
diff
changeset
|
3666 |
"Modified: 5.11.1996 / 14:08:24 / cg" |
701 | 3667 |
! |
1 | 3668 |
|
701 | 3669 |
rounded |
3670 |
"return the receiver rounded toward the next Integer - |
|
1558 | 3671 |
for integers this is the receiver itself." |
701 | 3672 |
|
3673 |
^ self |
|
1558 | 3674 |
|
3675 |
"Modified: 18.7.1996 / 12:44:24 / cg" |
|
701 | 3676 |
! |
69 | 3677 |
|
701 | 3678 |
truncated |
10824 | 3679 |
"return the receiver truncated towards zero as Integer |
1558 | 3680 |
for integers this is the receiver itself." |
159 | 3681 |
|
701 | 3682 |
^ self |
1558 | 3683 |
|
3684 |
"Modified: 18.7.1996 / 12:44:33 / cg" |
|
701 | 3685 |
! ! |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
3686 |
|
8394
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
3687 |
!Integer methodsFor:'visiting'! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
3688 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
3689 |
acceptVisitor:aVisitor with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
3690 |
|
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
3691 |
^ aVisitor visitInteger:self with:aParameter |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
3692 |
! ! |
da194de43766
Generalize visitor pattern and define #visit...:with: -methods instead
Stefan Vogel <sv@exept.de>
parents:
8335
diff
changeset
|
3693 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3694 |
!Integer::ModuloNumber class methodsFor:'documentation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3695 |
|
4803 | 3696 |
copyright |
3697 |
" |
|
3698 |
COPYRIGHT (c) 1999 by eXept Software AG |
|
3699 |
All Rights Reserved |
|
3700 |
||
3701 |
This software is furnished under a license and may be used |
|
3702 |
only in accordance with the terms of that license and with the |
|
3703 |
inclusion of the above copyright notice. This software may not |
|
3704 |
be provided or otherwise made available to, or used by, any |
|
3705 |
other person. No title to or ownership of the software is |
|
3706 |
hereby transferred. |
|
3707 |
" |
|
3708 |
||
3709 |
! |
|
3710 |
||
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3711 |
documentation |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3712 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3713 |
This is a helper class to perform fast computation of the modulus. |
4609 | 3714 |
(with big numbers, this does make a difference) |
4803 | 3715 |
|
3716 |
WARNING: this does only work with numbers which have no common |
|
3717 |
divisor (which is true for cryptographic applications). |
|
3718 |
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
|
3719 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3720 |
[author:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3721 |
Stefan Vogel |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3722 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3723 |
[see also:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3724 |
Integer SmallInteger LargeInsteger |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3725 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3726 |
[instance variables:] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3727 |
modulus the modulus |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3728 |
reciprocal reciprocal of the modulus |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3729 |
shift shift count to cut off some bits |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3730 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3731 |
! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3732 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3733 |
examples |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3734 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3735 |
[exBegin] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3736 |
17 asModuloNumber modulusOf:38 |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3737 |
[exEnd] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3738 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3739 |
[exBegin] |
4609 | 3740 |
38 \\ 17 |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3741 |
[exEnd] |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3742 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3743 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3744 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3745 |
!Integer::ModuloNumber class methodsFor:'instance creation'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3746 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3747 |
modulus:anInteger |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3748 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3749 |
^ self new modulus:anInteger |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3750 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3751 |
"Created: / 3.5.1999 / 11:13:15 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3752 |
"Modified: / 3.5.1999 / 11:18:37 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3753 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3754 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3755 |
!Integer::ModuloNumber methodsFor:'accessing'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3756 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3757 |
modulus |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3758 |
"return the modulus" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3759 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3760 |
^ modulus |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3761 |
! |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3762 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3763 |
modulus:n |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3764 |
"set the modulus" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3765 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3766 |
modulus := n. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3767 |
reciprocal := n integerReciprocal. |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3768 |
shift := n highBit negated. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3769 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3770 |
"Created: / 3.5.1999 / 10:02:39 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3771 |
"Modified: / 3.5.1999 / 14:30:49 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3772 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3773 |
|
5238 | 3774 |
!Integer::ModuloNumber methodsFor:'arithmetic'! |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3775 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3776 |
modulusOf:aNumber |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3777 |
"compute the aNumber modulo myself. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3778 |
The shortcut works only, if aNumber is < modulo * modulo |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3779 |
(When doing arithmethic modulo something). |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3780 |
Otherwise do it the long way" |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3781 |
|
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3782 |
|e t cnt| |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3783 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3784 |
"throw off low nbits(modulus)" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3785 |
|
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3786 |
e := (aNumber bitShift:shift). |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3787 |
e := e * reciprocal. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3788 |
e := (e bitShift:shift). |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3789 |
e := e * modulus. |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3790 |
e := aNumber - e. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3791 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3792 |
"this subtract is done max 2 times" |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3793 |
cnt := 2. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3794 |
[(t := e - modulus) >= 0] whileTrue:[ |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3795 |
e := t. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3796 |
cnt == 0 ifTrue:[ |
8335
e7fa7779b1b9
Fix #raisedTo:mod: for expontents 1 and 0
Stefan Vogel <sv@exept.de>
parents:
8289
diff
changeset
|
3797 |
"shortcut didn't work, do it the long way" |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3798 |
^ e \\ modulus. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3799 |
]. |
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3800 |
cnt := cnt - 1. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3801 |
]. |
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3802 |
^ e. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3803 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3804 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3805 |
|m| |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3806 |
|
4801
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3807 |
m := self new modulus:7. |
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3808 |
m modulusOf:55. |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3809 |
" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3810 |
|
4971
aed19001173f
Fix ModuloNumber for large numbers.
Stefan Vogel <sv@exept.de>
parents:
4952
diff
changeset
|
3811 |
"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
|
3812 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3813 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3814 |
m := self new modulus:7. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3815 |
m modulusOf:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3816 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3817 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3818 |
"SLOW (using standard \\ operation): |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3819 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3820 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3821 |
m := 123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3822 |
Time millisecondsToRun:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3823 |
1000 timesRepeat:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3824 |
874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658 \\ m |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3825 |
] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3826 |
] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3827 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3828 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3829 |
"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
|
3830 |
|m| |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3831 |
|
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3832 |
m := self new modulus:123456789901398721398721931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3833 |
Time millisecondsToRun:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3834 |
1000 timesRepeat:[ |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3835 |
m modulusOf:874928459437598375937451931729371293712943794254034548369328469438562948623498659238469234659823469823658423659823658. |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3836 |
] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3837 |
] |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3838 |
" |
edf02501d5e2
comment about divisor-size vs. dividend-size in ModuloNumber
Claus Gittinger <cg@exept.de>
parents:
4682
diff
changeset
|
3839 |
|
4142
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3840 |
"Modified: / 3.5.1999 / 14:30:32 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3841 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3842 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3843 |
!Integer::ModuloNumber methodsFor:'converting'! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3844 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3845 |
asModuloNumber |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3846 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3847 |
^ self |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3848 |
|
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3849 |
"Created: / 3.5.1999 / 14:48:27 / stefan" |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3850 |
! ! |
a881cc4bfaec
Make ModuloNumber a private class.
Stefan Vogel <sv@exept.de>
parents:
4141
diff
changeset
|
3851 |
|
1842
b28609c73839
added divMod: for faster largeNumber printString generation.
Claus Gittinger <cg@exept.de>
parents:
1700
diff
changeset
|
3852 |
!Integer class methodsFor:'documentation'! |
701 | 3853 |
|
3854 |
version |
|
11496 | 3855 |
^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.210 2009-01-26 08:53:00 cg Exp $' |
1 | 3856 |
! ! |
6858 | 3857 |
|
4996
c3716cebe47b
added a bcdConversionErrorSignal
Claus Gittinger <cg@exept.de>
parents:
4973
diff
changeset
|
3858 |
Integer initialize! |