author | Claus Gittinger <cg@exept.de> |
Sun, 10 Oct 1999 14:46:54 +0200 | |
changeset 4895 | cc546b082506 |
parent 4682 | 4158042a9c8c |
child 4988 | ac426684e852 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
3770 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
ArithmeticValue subclass:#Number |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
14 |
instanceVariableNames:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
15 |
classVariableNames:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
16 |
poolDictionaries:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
17 |
category:'Magnitude-Numbers' |
1 | 18 |
! |
19 |
||
1892 | 20 |
!Number class methodsFor:'documentation'! |
88 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
3770 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
abstract superclass for all kinds of numbers |
|
1295 | 39 |
|
40 |
[author:] |
|
3770 | 41 |
Claus Gittinger |
1628 | 42 |
|
43 |
[see also:] |
|
3770 | 44 |
Integer LargeInteger SmallInteger |
45 |
LimitedPrecisionReal Float ShortFloat |
|
46 |
Fraction FixedPoint |
|
88 | 47 |
" |
48 |
! ! |
|
1 | 49 |
|
1892 | 50 |
!Number class methodsFor:'instance creation'! |
1 | 51 |
|
3060 | 52 |
fromString:aString |
53 |
"for compatibility with other smalltalks - same as #readFrom:" |
|
54 |
||
55 |
^ self readFrom:aString |
|
56 |
||
3716 | 57 |
" |
58 |
Number fromString:'12345' |
|
59 |
'12345' asNumber |
|
60 |
" |
|
61 |
||
62 |
"Modified: / 3.8.1998 / 20:05:11 / cg" |
|
3060 | 63 |
! |
64 |
||
65 |
fromString:aString onError:exceptionBlock |
|
66 |
"for compatibility with other smalltalks - same as #readFrom:" |
|
67 |
||
68 |
^ self readFrom:aString onError:exceptionBlock |
|
69 |
||
3716 | 70 |
" |
71 |
Number fromString:'12345' onError:0 |
|
72 |
Number fromString:'fooBarBaz' onError:0 |
|
73 |
" |
|
74 |
||
75 |
"Modified: / 3.8.1998 / 20:05:34 / cg" |
|
3060 | 76 |
! |
77 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
78 |
readFrom:aStringOrStream onError:exceptionBlock |
1 | 79 |
"return the next Number from the (character-)stream aStream; |
202 | 80 |
skipping all whitespace first; return the value of exceptionBlock, |
81 |
if no number can be read." |
|
1 | 82 |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
83 |
|value| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
84 |
|
973 | 85 |
ErrorSignal handle:[:ex | |
3770 | 86 |
^ exceptionBlock value |
339 | 87 |
] do:[ |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
88 |
|str nextChar radix negative signExp| |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1635
diff
changeset
|
89 |
|
3770 | 90 |
str := aStringOrStream readStream. |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1635
diff
changeset
|
91 |
|
3770 | 92 |
nextChar := str skipSeparators. |
93 |
nextChar isNil ifTrue:[^ exceptionBlock value]. |
|
3 | 94 |
|
3770 | 95 |
(nextChar == $-) ifTrue:[ |
96 |
negative := true. |
|
97 |
str next. |
|
98 |
nextChar := str peekOrNil |
|
99 |
] ifFalse:[ |
|
100 |
negative := false. |
|
101 |
(nextChar == $+) ifTrue:[ |
|
102 |
str next. |
|
103 |
nextChar := str peekOrNil |
|
104 |
] |
|
105 |
]. |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
106 |
(nextChar isDigit or:[nextChar == $.]) ifFalse:[ |
3770 | 107 |
^ exceptionBlock value. |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
108 |
"/ value := super readFrom:str. |
339 | 109 |
"/ negative ifTrue:[value := value negated]. |
110 |
"/ ^ value |
|
3770 | 111 |
]. |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
112 |
nextChar == $. ifTrue:[ |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
113 |
radix := 10. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
114 |
value := 0.0. |
3770 | 115 |
] ifFalse:[ |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
116 |
value := Integer readFrom:str radix:10. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
117 |
nextChar := str peekOrNil. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
118 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
119 |
str next. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
120 |
radix := value. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
121 |
value := Integer readFrom:str radix:radix. |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
122 |
] ifFalse:[ |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
123 |
radix := 10 |
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
124 |
]. |
3770 | 125 |
]. |
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
126 |
|
3770 | 127 |
(nextChar == $.) ifTrue:[ |
128 |
str next. |
|
129 |
nextChar := str peekOrNil. |
|
130 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[ |
|
131 |
value := value asFloat |
|
132 |
+ (Number readMantissaFrom:str radix:radix). |
|
133 |
nextChar := str peekOrNil |
|
134 |
] |
|
135 |
]. |
|
136 |
((nextChar == $e) or:[nextChar == $E]) ifTrue:[ |
|
137 |
str next. |
|
138 |
nextChar := str peekOrNil. |
|
139 |
signExp := 1. |
|
140 |
(nextChar == $+) ifTrue:[ |
|
141 |
str next. |
|
142 |
nextChar := str peekOrNil. |
|
143 |
] ifFalse:[ |
|
144 |
(nextChar == $-) ifTrue:[ |
|
145 |
str next. |
|
146 |
nextChar := str peekOrNil. |
|
147 |
signExp := -1 |
|
148 |
] |
|
149 |
]. |
|
150 |
(nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[ |
|
151 |
value := value asFloat |
|
152 |
* (10.0 raisedToInteger: |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
153 |
((Integer readFrom:str radix:radix) * signExp)) |
3770 | 154 |
] |
155 |
]. |
|
156 |
negative ifTrue:[ |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
157 |
value := value negated |
3770 | 158 |
]. |
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
159 |
]. |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
160 |
^ value. |
1 | 161 |
|
329 | 162 |
" |
163 |
Number readFrom:(ReadStream on:'54.32e-01') |
|
164 |
Number readFrom:(ReadStream on:'12345678901234567890') |
|
165 |
Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
166 |
Number readFrom:'16rAAAAFFFFAAAAFFFF' |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
167 |
Number readFrom:'0.000001' |
329 | 168 |
'+00000123.45' asNumber |
169 |
" |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
170 |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
171 |
"Modified: / 14.4.1998 / 19:22:50 / cg" |
296 | 172 |
! |
173 |
||
174 |
readSmalltalkSyntaxFrom:aStream |
|
175 |
"ST-80 compatibility (thanks to a note from alpha testers) |
|
176 |
read and return the next Number in smalltalk syntax from the |
|
329 | 177 |
(character-)stream aStream." |
178 |
||
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
179 |
^ Scanner scanNumberFrom:aStream |
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
180 |
"/ ^ Compiler evaluate:aStream compile:false "/ self readFrom:aStream. |
296 | 181 |
|
329 | 182 |
" |
183 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'54.32e-01') |
|
184 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'12345678901234567890') |
|
185 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
186 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'(1/10)') |
|
187 |
Number readFrom:(ReadStream on:'(1/10)') |
|
188 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'+00000123.45') |
|
189 |
Number readFrom:(ReadStream on:'+00000123.45') |
|
190 |
" |
|
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
191 |
|
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
192 |
"Modified: / 18.6.1998 / 23:02:57 / cg" |
1 | 193 |
! ! |
701 | 194 |
|
1892 | 195 |
!Number class methodsFor:'error reporting'! |
701 | 196 |
|
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
197 |
raise:aSignalSymbol receiver:someNumber selector:sel arg:arg errorString:text |
3394 | 198 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
199 |
||
4558 | 200 |
<context: #return> |
201 |
||
3394 | 202 |
|msg| |
203 |
||
204 |
msg := MessageSend |
|
3770 | 205 |
receiver:someNumber |
206 |
selector:sel |
|
207 |
arguments:(Array with:arg). |
|
3394 | 208 |
|
209 |
^ (self perform:aSignalSymbol) |
|
3770 | 210 |
raiseRequestWith:msg |
211 |
errorString:text |
|
212 |
in:thisContext sender |
|
3394 | 213 |
|
214 |
" |
|
215 |
Number |
|
3770 | 216 |
raise:#domainErrorSignal |
217 |
receiver:1.0 |
|
218 |
selector:#sin |
|
219 |
arg:nil |
|
220 |
errorString:'foo bar test' |
|
3394 | 221 |
" |
222 |
! |
|
223 |
||
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
224 |
raise:aSignalSymbol receiver:someNumber selector:sel arguments:argArray errorString:text |
701 | 225 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
226 |
||
4558 | 227 |
<context: #return> |
228 |
||
701 | 229 |
|msg| |
230 |
||
231 |
msg := MessageSend |
|
3770 | 232 |
receiver:someNumber |
233 |
selector:sel |
|
234 |
arguments:argArray. |
|
3394 | 235 |
|
701 | 236 |
^ (self perform:aSignalSymbol) |
3770 | 237 |
raiseRequestWith:msg |
238 |
errorString:text |
|
239 |
in:thisContext sender |
|
701 | 240 |
|
241 |
" |
|
3394 | 242 |
Number |
3770 | 243 |
raise:#domainErrorSignal |
244 |
receiver:1.0 |
|
245 |
selector:#sin |
|
246 |
arg:nil |
|
247 |
errorString:'foo bar test' |
|
701 | 248 |
" |
249 |
! |
|
250 |
||
251 |
raise:aSignalSymbol receiver:someNumber selector:sel errorString:text |
|
252 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
|
253 |
||
4558 | 254 |
<context: #return> |
255 |
||
701 | 256 |
|msg| |
257 |
||
258 |
msg := MessageSend |
|
3770 | 259 |
receiver:someNumber |
260 |
selector:sel |
|
261 |
arguments:#(). |
|
3394 | 262 |
|
701 | 263 |
^ (self perform:aSignalSymbol) |
3770 | 264 |
raiseRequestWith:msg |
265 |
errorString:text |
|
266 |
in:thisContext sender |
|
701 | 267 |
|
268 |
" |
|
3394 | 269 |
Number |
3770 | 270 |
raise:#domainErrorSignal |
271 |
receiver:1.0 |
|
272 |
selector:#foo |
|
273 |
errorString:'foo bar test' |
|
701 | 274 |
" |
275 |
! ! |
|
276 |
||
1892 | 277 |
!Number class methodsFor:'private'! |
701 | 278 |
|
279 |
readMantissaFrom:aStream radix:radix |
|
280 |
"helper for readFrom: - |
|
281 |
return the mantissa from the (character-)stream aStream; |
|
282 |
no whitespace-skipping; error if no number available" |
|
283 |
||
284 |
|nextChar value factor| |
|
285 |
||
286 |
value := 0.0. |
|
287 |
factor := 1.0 / radix. |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
288 |
nextChar := aStream peekOrNil. |
701 | 289 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
3770 | 290 |
value := value + (nextChar digitValue * factor). |
291 |
factor := factor / radix. |
|
292 |
aStream next. |
|
293 |
nextChar := aStream peekOrNil |
|
701 | 294 |
]. |
295 |
^ value |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
296 |
|
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
297 |
"Modified: / 14.4.1998 / 18:47:47 / cg" |
701 | 298 |
! ! |
299 |
||
300 |
!Number methodsFor:'coercing'! |
|
301 |
||
302 |
coerce:aNumber |
|
303 |
"return aNumber converted into receivers type" |
|
304 |
||
305 |
^ self subclassResponsibility |
|
306 |
! |
|
307 |
||
308 |
generality |
|
309 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
310 |
||
311 |
^ 40 |
|
312 |
! ! |
|
313 |
||
314 |
!Number methodsFor:'converting'! |
|
315 |
||
3634 | 316 |
% aNumber |
317 |
"Return a complex number with the receiver as the real part and |
|
318 |
aNumber as the imaginary part" |
|
319 |
||
320 |
^ Complex real:self imaginary:aNumber |
|
321 |
||
322 |
"Modified: / 9.7.1998 / 10:18:12 / cg" |
|
323 |
! |
|
324 |
||
701 | 325 |
@ aNumber |
326 |
"return a Point with the receiver as x-coordinate and the argument |
|
327 |
as y-coordinate" |
|
328 |
||
329 |
%{ /* NOCONTEXT */ |
|
330 |
||
331 |
/* |
|
332 |
* I cannot tell if this special code is worth anything |
|
333 |
*/ |
|
3394 | 334 |
if (__CanDoQuickNew(sizeof(struct __point))) { /* OBJECT ALLOCATION */ |
3770 | 335 |
OBJ newPoint; |
336 |
int spc; |
|
701 | 337 |
|
3770 | 338 |
__qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
339 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
340 |
__PointInstPtr(newPoint)->p_x = self; |
|
341 |
__PointInstPtr(newPoint)->p_y = aNumber; |
|
342 |
if (! __bothSmallInteger(self, aNumber)) { |
|
343 |
spc = __qSpace(newPoint); |
|
344 |
__STORE_SPC(newPoint, aNumber, spc); |
|
345 |
__STORE_SPC(newPoint, self, spc); |
|
346 |
} |
|
347 |
RETURN ( newPoint ); |
|
701 | 348 |
} |
349 |
%} |
|
350 |
. |
|
351 |
^ Point x:self y:aNumber |
|
352 |
! |
|
353 |
||
3634 | 354 |
asComplex |
355 |
"Return a complex number with the receiver as the real part and |
|
356 |
zero as the imaginary part" |
|
357 |
||
358 |
^ Complex fromReal:self |
|
359 |
||
360 |
"Modified: / 9.7.1998 / 10:18:16 / cg" |
|
361 |
! |
|
362 |
||
701 | 363 |
asPoint |
364 |
"return a new Point with the receiver as all coordinates; |
|
365 |
often used to supply the same value in two dimensions, as with |
|
366 |
symmetrical gridding or scaling." |
|
367 |
||
368 |
%{ /* NOCONTEXT */ |
|
369 |
||
3394 | 370 |
if (__CanDoQuickNew(sizeof(struct __point))) { /* OBJECT ALLOCATION */ |
3770 | 371 |
OBJ newPoint; |
701 | 372 |
|
3770 | 373 |
__qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
374 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
375 |
__PointInstPtr(newPoint)->p_x = self; |
|
376 |
__PointInstPtr(newPoint)->p_y = self; |
|
377 |
__STORE(newPoint, self); |
|
378 |
RETURN ( newPoint ); |
|
701 | 379 |
} |
380 |
%}. |
|
381 |
^ Point x:self y:self |
|
382 |
! |
|
383 |
||
2416 | 384 |
decodeAsLiteralArray |
385 |
"given a literalEncoding in the receiver, |
|
386 |
create & return the corresponding object. |
|
387 |
The inverse operation to #literalArrayEncoding." |
|
388 |
||
389 |
^ self |
|
390 |
||
391 |
"Created: 25.2.1997 / 19:17:06 / cg" |
|
392 |
"Modified: 25.2.1997 / 19:17:42 / cg" |
|
393 |
! |
|
394 |
||
701 | 395 |
degreesToRadians |
396 |
"interpreting the receiver as radians, return the degrees" |
|
397 |
||
398 |
^ (self * (Float pi)) / 180.0 |
|
399 |
! |
|
400 |
||
401 |
literalArrayEncoding |
|
1245 | 402 |
"encode myself as an array literal, from which a copy of the receiver |
403 |
can be reconstructed with #decodeAsLiteralArray." |
|
701 | 404 |
|
405 |
^ self |
|
406 |
||
407 |
"Modified: 1.9.1995 / 02:25:26 / claus" |
|
1245 | 408 |
"Modified: 22.4.1996 / 13:00:27 / cg" |
701 | 409 |
! |
410 |
||
411 |
radiansToDegrees |
|
412 |
"interpreting the receiver as degrees, return the radians" |
|
413 |
||
414 |
^ (self * 180.0) / (Float pi) |
|
415 |
! ! |
|
416 |
||
417 |
!Number methodsFor:'intervals'! |
|
418 |
||
419 |
to:stop |
|
420 |
"return an interval from receiver up to the argument, incrementing by 1" |
|
421 |
||
422 |
^ Interval from:self to:stop |
|
423 |
! |
|
424 |
||
425 |
to:stop by:step |
|
426 |
"return an interval from receiver up to the argument, incrementing by step" |
|
427 |
||
428 |
^ Interval from:self to:stop by:step |
|
429 |
! ! |
|
430 |
||
431 |
!Number methodsFor:'iteration'! |
|
432 |
||
433 |
timesRepeat:aBlock |
|
434 |
"evaluate the argument, aBlock self times" |
|
435 |
||
436 |
|count| |
|
437 |
||
438 |
count := self. |
|
439 |
[count > 0] whileTrue:[ |
|
3770 | 440 |
aBlock value. |
441 |
count := count - 1 |
|
701 | 442 |
] |
443 |
! |
|
444 |
||
445 |
to:stop by:incr do:aBlock |
|
446 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
|
447 |
by step, evaluate aBlock passing the element as argument." |
|
448 |
||
449 |
|tmp| |
|
450 |
||
451 |
tmp := self. |
|
452 |
(incr > 0) ifTrue:[ |
|
3770 | 453 |
[tmp <= stop] whileTrue:[ |
454 |
aBlock value:tmp. |
|
455 |
tmp := tmp+incr |
|
456 |
] |
|
701 | 457 |
] ifFalse:[ |
3770 | 458 |
[tmp >= stop] whileTrue:[ |
459 |
aBlock value:tmp. |
|
460 |
tmp := tmp+incr |
|
461 |
] |
|
701 | 462 |
] |
463 |
! |
|
464 |
||
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
465 |
to:stop by:incr doWithBreak:aBlock |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
466 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
467 |
by step, evaluate aBlock passing the element as argument. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
468 |
Pass a break argument, to allow for premature exit of the loop." |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
469 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
470 |
|tmp break| |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
471 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
472 |
break := [^ self]. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
473 |
tmp := self. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
474 |
(incr > 0) ifTrue:[ |
3770 | 475 |
[tmp <= stop] whileTrue:[ |
476 |
aBlock value:tmp value:break. |
|
477 |
tmp := tmp+incr |
|
478 |
] |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
479 |
] ifFalse:[ |
3770 | 480 |
[tmp >= stop] whileTrue:[ |
481 |
aBlock value:tmp value:break. |
|
482 |
tmp := tmp+incr |
|
483 |
] |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
484 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
485 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
486 |
" |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
487 |
1 to:100 by:5 doWithBreak:[:index :break | |
3770 | 488 |
Transcript showCR:index printString. |
489 |
index > 50 ifTrue:[ |
|
490 |
break value |
|
491 |
]. |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
492 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
493 |
" |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
494 |
! |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
495 |
|
701 | 496 |
to:stop do:aBlock |
497 |
"For each element of the interval from the receiver up to the argument stop, |
|
498 |
evaluate aBlock, passing the number as argument." |
|
499 |
||
500 |
|tmp| |
|
501 |
||
502 |
tmp := self. |
|
503 |
[tmp <= stop] whileTrue:[ |
|
3770 | 504 |
aBlock value:tmp. |
505 |
tmp := tmp+1 |
|
701 | 506 |
] |
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
507 |
! |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
508 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
509 |
to:stop doWithBreak:aBlock |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
510 |
"For each element of the interval from the receiver up to the argument stop, |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
511 |
evaluate aBlock, passing the number as argument. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
512 |
Pass a break argument, to allow for premature exit of the loop." |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
513 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
514 |
|tmp break| |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
515 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
516 |
break := [^ self]. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
517 |
tmp := self. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
518 |
[tmp <= stop] whileTrue:[ |
3770 | 519 |
aBlock value:tmp value:break. |
520 |
tmp := tmp+1 |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
521 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
522 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
523 |
" |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
524 |
1 to:10 doWithBreak:[:index :break | |
3770 | 525 |
Transcript showCR:index printString. |
526 |
index > 5 ifTrue:[ |
|
527 |
break value |
|
528 |
]. |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
529 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
530 |
" |
701 | 531 |
! ! |
532 |
||
3634 | 533 |
!Number methodsFor:'mathematical functions'! |
534 |
||
535 |
conjugated |
|
536 |
"Return the complex conjugate of this Number." |
|
537 |
||
538 |
^ self |
|
539 |
||
540 |
"Modified: / 9.7.1998 / 10:17:31 / cg" |
|
541 |
! |
|
542 |
||
543 |
imaginary |
|
544 |
"Return the imaginary part of this Number." |
|
545 |
||
546 |
^ 0 |
|
547 |
||
548 |
"Modified: / 9.7.1998 / 10:17:24 / cg" |
|
549 |
! |
|
550 |
||
551 |
real |
|
552 |
"Return the real part of this Number." |
|
553 |
||
554 |
^ self |
|
555 |
||
556 |
"Modified: / 9.7.1998 / 10:17:17 / cg" |
|
557 |
! ! |
|
558 |
||
701 | 559 |
!Number methodsFor:'printing & storing'! |
560 |
||
561 |
storeOn:aStream |
|
562 |
"append a string for storing the receiver onto the argument, |
|
563 |
aStream - since numbers are literals,they store as they print." |
|
564 |
||
565 |
^ self printOn:aStream |
|
566 |
! |
|
567 |
||
568 |
storeString |
|
569 |
"return a string for storing |
|
570 |
- since numbers are literals, they store as they print." |
|
571 |
||
572 |
^ self printString |
|
573 |
! ! |
|
574 |
||
575 |
!Number methodsFor:'testing'! |
|
576 |
||
577 |
isNumber |
|
578 |
"return true, if the receiver is a kind of number" |
|
579 |
||
580 |
^ true |
|
581 |
! |
|
582 |
||
583 |
isZero |
|
1557 | 584 |
"return true, if the receiver is zero" |
585 |
||
701 | 586 |
^ self = 0 |
1557 | 587 |
|
588 |
"Modified: 18.7.1996 / 12:40:49 / cg" |
|
701 | 589 |
! ! |
590 |
||
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
591 |
!Number methodsFor:'tracing'! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
592 |
|
4682 | 593 |
traceInto:aRequestor level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
594 |
"double dispatch into tracer, passing my type implicitely in the selector" |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
595 |
|
4682 | 596 |
^ aRequestor traceNumber:self level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
597 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
598 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
599 |
! ! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
600 |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
601 |
!Number methodsFor:'truncation and rounding'! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
602 |
|
4895 | 603 |
detentBy: detent atMultiplesOf: grid snap: snap |
604 |
"Map all values that are within detent/2 of any multiple of grid |
|
605 |
to that multiple. |
|
606 |
Otherwise, if snap is true, return self, meaning that the values |
|
607 |
in the dead zone will never be returned. |
|
608 |
If snap is false, then expand the range between dead zones |
|
609 |
so that it covers the range between multiples of the grid, |
|
610 |
and scale the value by that factor." |
|
611 |
||
612 |
| r1 r2 | |
|
613 |
||
614 |
r1 := self roundTo: grid. "Nearest multiple of grid" |
|
615 |
(self roundTo: detent) = r1 ifTrue: [^ r1]. "Snap to that multiple..." |
|
616 |
snap ifTrue: [^ self]. "...or return self" |
|
617 |
||
618 |
r2 := self < r1 "Nearest end of dead zone" |
|
619 |
ifTrue: [r1 - (detent asFloat/2)] |
|
620 |
ifFalse: [r1 + (detent asFloat/2)]. |
|
621 |
||
622 |
"Scale values between dead zones to fill range between multiples" |
|
623 |
^ r1 + ((self - r2) * grid asFloat / (grid - detent)) |
|
624 |
||
625 |
" |
|
626 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: true] |
|
627 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: false] |
|
628 |
(3.9 to: 4.1 by: 0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: true] |
|
629 |
(-3.9 to: -4.1 by: -0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: false] |
|
630 |
" |
|
631 |
! |
|
632 |
||
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
633 |
fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
634 |
"return a float with value from digits after the decimal point. |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
635 |
(i.e. the receiver minus its truncated value)" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
636 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
637 |
^ self - self truncated asFloat |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
638 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
639 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
640 |
1234.56789 fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
641 |
1.2345e6 fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
642 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
643 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
644 |
"Modified: / 4.11.1996 / 20:26:54 / cg" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
645 |
"Created: / 28.10.1998 / 17:14:40 / cg" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
646 |
! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
647 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
648 |
integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
649 |
"return a float with value from digits before the decimal point |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
650 |
(i.e. the truncated value)" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
651 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
652 |
^ self truncated asFloat |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
653 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
654 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
655 |
1234.56789 integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
656 |
1.2345e6 integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
657 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
658 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
659 |
"Modified: / 4.11.1996 / 20:26:21 / cg" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
660 |
"Created: / 28.10.1998 / 17:14:56 / cg" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
661 |
! ! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
662 |
|
1892 | 663 |
!Number class methodsFor:'documentation'! |
701 | 664 |
|
665 |
version |
|
4895 | 666 |
^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.54 1999-10-10 12:46:54 cg Exp $' |
701 | 667 |
! ! |