author | Claus Gittinger <cg@exept.de> |
Thu, 30 Jul 1998 18:06:34 +0200 | |
changeset 3695 | 9ebf6de8a764 |
parent 3634 | bbf443dce0cf |
child 3716 | e7904bd50d9d |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
202 | 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 |
|
973 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
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 |
|
202 | 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:] |
|
3394 | 41 |
Claus Gittinger |
1628 | 42 |
|
43 |
[see also:] |
|
3394 | 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 |
||
57 |
"Created: / 25.10.1997 / 15:24:16 / cg" |
|
58 |
! |
|
59 |
||
60 |
fromString:aString onError:exceptionBlock |
|
61 |
"for compatibility with other smalltalks - same as #readFrom:" |
|
62 |
||
63 |
^ self readFrom:aString onError:exceptionBlock |
|
64 |
||
65 |
"Created: / 25.10.1997 / 15:24:33 / cg" |
|
66 |
! |
|
67 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
68 |
readFrom:aStringOrStream onError:exceptionBlock |
1 | 69 |
"return the next Number from the (character-)stream aStream; |
202 | 70 |
skipping all whitespace first; return the value of exceptionBlock, |
71 |
if no number can be read." |
|
1 | 72 |
|
973 | 73 |
ErrorSignal handle:[:ex | |
3394 | 74 |
^ exceptionBlock value |
339 | 75 |
] do:[ |
3394 | 76 |
|str nextChar radix value negative signExp freakOut| |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1635
diff
changeset
|
77 |
|
3394 | 78 |
str := aStringOrStream readStream. |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1635
diff
changeset
|
79 |
|
3394 | 80 |
nextChar := str skipSeparators. |
81 |
nextChar isNil ifTrue:[^ exceptionBlock value]. |
|
3 | 82 |
|
3394 | 83 |
freakOut := [^ exceptionBlock value]. |
339 | 84 |
|
3394 | 85 |
(nextChar == $-) ifTrue:[ |
86 |
negative := true. |
|
87 |
str next. |
|
88 |
nextChar := str peekOrNil |
|
89 |
] ifFalse:[ |
|
90 |
negative := false. |
|
91 |
(nextChar == $+) ifTrue:[ |
|
92 |
str next. |
|
93 |
nextChar := str peekOrNil |
|
94 |
] |
|
95 |
]. |
|
96 |
nextChar isDigit ifFalse:[ |
|
97 |
^ exceptionBlock value. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
98 |
"/ value := super readFrom:str. |
339 | 99 |
"/ negative ifTrue:[value := value negated]. |
100 |
"/ ^ value |
|
3394 | 101 |
]. |
102 |
value := Integer readFrom:str radix:10 onError:freakOut. |
|
103 |
nextChar := str peekOrNil. |
|
104 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
|
105 |
str next. |
|
106 |
radix := value. |
|
107 |
value := Integer readFrom:str radix:radix onError:freakOut. |
|
108 |
] ifFalse:[ |
|
109 |
radix := 10 |
|
110 |
]. |
|
111 |
(nextChar == $.) ifTrue:[ |
|
112 |
str next. |
|
113 |
nextChar := str peekOrNil. |
|
114 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[ |
|
115 |
value := value asFloat |
|
116 |
+ (Number readMantissaFrom:str radix:radix). |
|
117 |
nextChar := str peekOrNil |
|
118 |
] |
|
119 |
]. |
|
120 |
((nextChar == $e) or:[nextChar == $E]) ifTrue:[ |
|
121 |
str next. |
|
122 |
nextChar := str peekOrNil. |
|
123 |
signExp := 1. |
|
124 |
(nextChar == $+) ifTrue:[ |
|
125 |
str next. |
|
126 |
nextChar := str peekOrNil. |
|
127 |
] ifFalse:[ |
|
128 |
(nextChar == $-) ifTrue:[ |
|
129 |
str next. |
|
130 |
nextChar := str peekOrNil. |
|
131 |
signExp := -1 |
|
132 |
] |
|
133 |
]. |
|
134 |
(nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[ |
|
135 |
value := value asFloat |
|
136 |
* (10.0 raisedToInteger: |
|
137 |
((Integer readFrom:str radix:radix onError:freakOut) * signExp)) |
|
138 |
] |
|
139 |
]. |
|
140 |
negative ifTrue:[ |
|
141 |
^ value negated |
|
142 |
]. |
|
143 |
^ value |
|
339 | 144 |
] |
1 | 145 |
|
329 | 146 |
" |
147 |
Number readFrom:(ReadStream on:'54.32e-01') |
|
148 |
Number readFrom:(ReadStream on:'12345678901234567890') |
|
149 |
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
|
150 |
Number readFrom:'16rAAAAFFFFAAAAFFFF' |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
151 |
Number readFrom:'0.000001' |
329 | 152 |
'+00000123.45' asNumber |
153 |
" |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
154 |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
155 |
"Modified: / 14.4.1998 / 19:22:50 / cg" |
296 | 156 |
! |
157 |
||
158 |
readSmalltalkSyntaxFrom:aStream |
|
159 |
"ST-80 compatibility (thanks to a note from alpha testers) |
|
160 |
read and return the next Number in smalltalk syntax from the |
|
329 | 161 |
(character-)stream aStream." |
162 |
||
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
163 |
^ Scanner scanNumberFrom:aStream |
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
164 |
"/ ^ Compiler evaluate:aStream compile:false "/ self readFrom:aStream. |
296 | 165 |
|
329 | 166 |
" |
167 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'54.32e-01') |
|
168 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'12345678901234567890') |
|
169 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
170 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'(1/10)') |
|
171 |
Number readFrom:(ReadStream on:'(1/10)') |
|
172 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'+00000123.45') |
|
173 |
Number readFrom:(ReadStream on:'+00000123.45') |
|
174 |
" |
|
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
175 |
|
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
176 |
"Modified: / 18.6.1998 / 23:02:57 / cg" |
1 | 177 |
! ! |
701 | 178 |
|
1892 | 179 |
!Number class methodsFor:'error reporting'! |
701 | 180 |
|
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
181 |
raise:aSignalSymbol receiver:someNumber selector:sel arg:arg errorString:text |
3394 | 182 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
183 |
||
184 |
|msg| |
|
185 |
||
186 |
msg := MessageSend |
|
187 |
receiver:someNumber |
|
188 |
selector:sel |
|
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
189 |
arguments:(Array with:arg). |
3394 | 190 |
|
191 |
^ (self perform:aSignalSymbol) |
|
192 |
raiseRequestWith:msg |
|
193 |
errorString:text |
|
194 |
in:thisContext sender |
|
195 |
||
196 |
" |
|
197 |
Number |
|
198 |
raise:#domainErrorSignal |
|
199 |
receiver:1.0 |
|
200 |
selector:#sin |
|
201 |
arg:nil |
|
202 |
errorString:'foo bar test' |
|
203 |
" |
|
204 |
! |
|
205 |
||
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
206 |
raise:aSignalSymbol receiver:someNumber selector:sel arguments:argArray errorString:text |
701 | 207 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
208 |
||
209 |
|msg| |
|
210 |
||
211 |
msg := MessageSend |
|
212 |
receiver:someNumber |
|
213 |
selector:sel |
|
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
214 |
arguments:argArray. |
3394 | 215 |
|
701 | 216 |
^ (self perform:aSignalSymbol) |
217 |
raiseRequestWith:msg |
|
218 |
errorString:text |
|
219 |
in:thisContext sender |
|
220 |
||
221 |
" |
|
3394 | 222 |
Number |
223 |
raise:#domainErrorSignal |
|
224 |
receiver:1.0 |
|
225 |
selector:#sin |
|
226 |
arg:nil |
|
227 |
errorString:'foo bar test' |
|
701 | 228 |
" |
229 |
! |
|
230 |
||
231 |
raise:aSignalSymbol receiver:someNumber selector:sel errorString:text |
|
232 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
|
233 |
||
234 |
|msg| |
|
235 |
||
236 |
msg := MessageSend |
|
237 |
receiver:someNumber |
|
238 |
selector:sel |
|
239 |
arguments:#(). |
|
3394 | 240 |
|
701 | 241 |
^ (self perform:aSignalSymbol) |
242 |
raiseRequestWith:msg |
|
243 |
errorString:text |
|
244 |
in:thisContext sender |
|
245 |
||
246 |
" |
|
3394 | 247 |
Number |
248 |
raise:#domainErrorSignal |
|
249 |
receiver:1.0 |
|
250 |
selector:#foo |
|
251 |
errorString:'foo bar test' |
|
701 | 252 |
" |
253 |
! ! |
|
254 |
||
1892 | 255 |
!Number class methodsFor:'private'! |
701 | 256 |
|
257 |
readMantissaFrom:aStream radix:radix |
|
258 |
"helper for readFrom: - |
|
259 |
return the mantissa from the (character-)stream aStream; |
|
260 |
no whitespace-skipping; error if no number available" |
|
261 |
||
262 |
|nextChar value factor| |
|
263 |
||
264 |
value := 0.0. |
|
265 |
factor := 1.0 / radix. |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
266 |
nextChar := aStream peekOrNil. |
701 | 267 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
3394 | 268 |
value := value + (nextChar digitValue * factor). |
269 |
factor := factor / radix. |
|
270 |
aStream next. |
|
271 |
nextChar := aStream peekOrNil |
|
701 | 272 |
]. |
273 |
^ value |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
274 |
|
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
275 |
"Modified: / 14.4.1998 / 18:47:47 / cg" |
701 | 276 |
! ! |
277 |
||
278 |
!Number methodsFor:'coercing'! |
|
279 |
||
280 |
coerce:aNumber |
|
281 |
"return aNumber converted into receivers type" |
|
282 |
||
283 |
^ self subclassResponsibility |
|
284 |
! |
|
285 |
||
286 |
generality |
|
287 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
288 |
||
289 |
^ 40 |
|
290 |
! ! |
|
291 |
||
292 |
!Number methodsFor:'converting'! |
|
293 |
||
3634 | 294 |
% aNumber |
295 |
"Return a complex number with the receiver as the real part and |
|
296 |
aNumber as the imaginary part" |
|
297 |
||
298 |
^ Complex real:self imaginary:aNumber |
|
299 |
||
300 |
"Modified: / 9.7.1998 / 10:18:12 / cg" |
|
301 |
! |
|
302 |
||
701 | 303 |
@ aNumber |
304 |
"return a Point with the receiver as x-coordinate and the argument |
|
305 |
as y-coordinate" |
|
306 |
||
307 |
%{ /* NOCONTEXT */ |
|
308 |
||
309 |
/* |
|
310 |
* I cannot tell if this special code is worth anything |
|
311 |
*/ |
|
3394 | 312 |
if (__CanDoQuickNew(sizeof(struct __point))) { /* OBJECT ALLOCATION */ |
701 | 313 |
OBJ newPoint; |
314 |
int spc; |
|
315 |
||
835 | 316 |
__qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
1133 | 317 |
__InstPtr(newPoint)->o_class = @global(Point); |
318 |
__PointInstPtr(newPoint)->p_x = self; |
|
319 |
__PointInstPtr(newPoint)->p_y = aNumber; |
|
701 | 320 |
if (! __bothSmallInteger(self, aNumber)) { |
321 |
spc = __qSpace(newPoint); |
|
322 |
__STORE_SPC(newPoint, aNumber, spc); |
|
323 |
__STORE_SPC(newPoint, self, spc); |
|
324 |
} |
|
325 |
RETURN ( newPoint ); |
|
326 |
} |
|
327 |
%} |
|
328 |
. |
|
329 |
^ Point x:self y:aNumber |
|
330 |
! |
|
331 |
||
3634 | 332 |
asComplex |
333 |
"Return a complex number with the receiver as the real part and |
|
334 |
zero as the imaginary part" |
|
335 |
||
336 |
^ Complex fromReal:self |
|
337 |
||
338 |
"Modified: / 9.7.1998 / 10:18:16 / cg" |
|
339 |
! |
|
340 |
||
701 | 341 |
asPoint |
342 |
"return a new Point with the receiver as all coordinates; |
|
343 |
often used to supply the same value in two dimensions, as with |
|
344 |
symmetrical gridding or scaling." |
|
345 |
||
346 |
%{ /* NOCONTEXT */ |
|
347 |
||
3394 | 348 |
if (__CanDoQuickNew(sizeof(struct __point))) { /* OBJECT ALLOCATION */ |
701 | 349 |
OBJ newPoint; |
350 |
||
835 | 351 |
__qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
1133 | 352 |
__InstPtr(newPoint)->o_class = @global(Point); |
353 |
__PointInstPtr(newPoint)->p_x = self; |
|
354 |
__PointInstPtr(newPoint)->p_y = self; |
|
701 | 355 |
__STORE(newPoint, self); |
356 |
RETURN ( newPoint ); |
|
357 |
} |
|
358 |
%}. |
|
359 |
^ Point x:self y:self |
|
360 |
! |
|
361 |
||
2416 | 362 |
decodeAsLiteralArray |
363 |
"given a literalEncoding in the receiver, |
|
364 |
create & return the corresponding object. |
|
365 |
The inverse operation to #literalArrayEncoding." |
|
366 |
||
367 |
^ self |
|
368 |
||
369 |
"Created: 25.2.1997 / 19:17:06 / cg" |
|
370 |
"Modified: 25.2.1997 / 19:17:42 / cg" |
|
371 |
! |
|
372 |
||
701 | 373 |
degreesToRadians |
374 |
"interpreting the receiver as radians, return the degrees" |
|
375 |
||
376 |
^ (self * (Float pi)) / 180.0 |
|
377 |
! |
|
378 |
||
379 |
literalArrayEncoding |
|
1245 | 380 |
"encode myself as an array literal, from which a copy of the receiver |
381 |
can be reconstructed with #decodeAsLiteralArray." |
|
701 | 382 |
|
383 |
^ self |
|
384 |
||
385 |
"Modified: 1.9.1995 / 02:25:26 / claus" |
|
1245 | 386 |
"Modified: 22.4.1996 / 13:00:27 / cg" |
701 | 387 |
! |
388 |
||
389 |
radiansToDegrees |
|
390 |
"interpreting the receiver as degrees, return the radians" |
|
391 |
||
392 |
^ (self * 180.0) / (Float pi) |
|
393 |
! ! |
|
394 |
||
395 |
!Number methodsFor:'intervals'! |
|
396 |
||
397 |
to:stop |
|
398 |
"return an interval from receiver up to the argument, incrementing by 1" |
|
399 |
||
400 |
^ Interval from:self to:stop |
|
401 |
! |
|
402 |
||
403 |
to:stop by:step |
|
404 |
"return an interval from receiver up to the argument, incrementing by step" |
|
405 |
||
406 |
^ Interval from:self to:stop by:step |
|
407 |
! ! |
|
408 |
||
409 |
!Number methodsFor:'iteration'! |
|
410 |
||
411 |
timesRepeat:aBlock |
|
412 |
"evaluate the argument, aBlock self times" |
|
413 |
||
414 |
|count| |
|
415 |
||
416 |
count := self. |
|
417 |
[count > 0] whileTrue:[ |
|
418 |
aBlock value. |
|
419 |
count := count - 1 |
|
420 |
] |
|
421 |
! |
|
422 |
||
423 |
to:stop by:incr do:aBlock |
|
424 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
|
425 |
by step, evaluate aBlock passing the element as argument." |
|
426 |
||
427 |
|tmp| |
|
428 |
||
429 |
tmp := self. |
|
430 |
(incr > 0) ifTrue:[ |
|
431 |
[tmp <= stop] whileTrue:[ |
|
432 |
aBlock value:tmp. |
|
433 |
tmp := tmp+incr |
|
434 |
] |
|
435 |
] ifFalse:[ |
|
436 |
[tmp >= stop] whileTrue:[ |
|
437 |
aBlock value:tmp. |
|
438 |
tmp := tmp+incr |
|
439 |
] |
|
440 |
] |
|
441 |
! |
|
442 |
||
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
443 |
to:stop by:incr doWithBreak:aBlock |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
444 |
"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
|
445 |
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
|
446 |
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
|
447 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
448 |
|tmp break| |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
449 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
450 |
break := [^ self]. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
451 |
tmp := self. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
452 |
(incr > 0) ifTrue:[ |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
453 |
[tmp <= stop] whileTrue:[ |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
454 |
aBlock value:tmp value:break. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
455 |
tmp := tmp+incr |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
456 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
457 |
] ifFalse:[ |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
458 |
[tmp >= stop] whileTrue:[ |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
459 |
aBlock value:tmp value:break. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
460 |
tmp := tmp+incr |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
461 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
462 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
463 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
464 |
" |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
465 |
1 to:100 by:5 doWithBreak:[:index :break | |
1422 | 466 |
Transcript showCR:index printString. |
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
467 |
index > 50 ifTrue:[ |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
468 |
break value |
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 |
] |
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 |
! |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
473 |
|
701 | 474 |
to:stop do:aBlock |
475 |
"For each element of the interval from the receiver up to the argument stop, |
|
476 |
evaluate aBlock, passing the number as argument." |
|
477 |
||
478 |
|tmp| |
|
479 |
||
480 |
tmp := self. |
|
481 |
[tmp <= stop] whileTrue:[ |
|
482 |
aBlock value:tmp. |
|
483 |
tmp := tmp+1 |
|
484 |
] |
|
919
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 |
to:stop doWithBreak:aBlock |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
488 |
"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
|
489 |
evaluate aBlock, passing the number as argument. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
490 |
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
|
491 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
492 |
|tmp break| |
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 |
break := [^ self]. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
495 |
tmp := self. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
496 |
[tmp <= stop] whileTrue:[ |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
497 |
aBlock value:tmp value:break. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
498 |
tmp := tmp+1 |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
499 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
500 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
501 |
" |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
502 |
1 to:10 doWithBreak:[:index :break | |
1422 | 503 |
Transcript showCR:index printString. |
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
504 |
index > 5 ifTrue:[ |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
505 |
break value |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
506 |
]. |
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 |
" |
701 | 509 |
! ! |
510 |
||
3634 | 511 |
!Number methodsFor:'mathematical functions'! |
512 |
||
513 |
conjugated |
|
514 |
"Return the complex conjugate of this Number." |
|
515 |
||
516 |
^ self |
|
517 |
||
518 |
"Modified: / 9.7.1998 / 10:17:31 / cg" |
|
519 |
! |
|
520 |
||
521 |
imaginary |
|
522 |
"Return the imaginary part of this Number." |
|
523 |
||
524 |
^ 0 |
|
525 |
||
526 |
"Modified: / 9.7.1998 / 10:17:24 / cg" |
|
527 |
! |
|
528 |
||
529 |
real |
|
530 |
"Return the real part of this Number." |
|
531 |
||
532 |
^ self |
|
533 |
||
534 |
"Modified: / 9.7.1998 / 10:17:17 / cg" |
|
535 |
! ! |
|
536 |
||
701 | 537 |
!Number methodsFor:'printing & storing'! |
538 |
||
539 |
storeOn:aStream |
|
540 |
"append a string for storing the receiver onto the argument, |
|
541 |
aStream - since numbers are literals,they store as they print." |
|
542 |
||
543 |
^ self printOn:aStream |
|
544 |
! |
|
545 |
||
546 |
storeString |
|
547 |
"return a string for storing |
|
548 |
- since numbers are literals, they store as they print." |
|
549 |
||
550 |
^ self printString |
|
551 |
! ! |
|
552 |
||
553 |
!Number methodsFor:'testing'! |
|
554 |
||
555 |
isLiteral |
|
556 |
"return true, if the receiver can be used as a literal |
|
557 |
(i.e. can be used in constant arrays)" |
|
558 |
||
559 |
^ true |
|
560 |
! |
|
561 |
||
562 |
isNumber |
|
563 |
"return true, if the receiver is a kind of number" |
|
564 |
||
565 |
^ true |
|
566 |
! |
|
567 |
||
568 |
isZero |
|
1557 | 569 |
"return true, if the receiver is zero" |
570 |
||
701 | 571 |
^ self = 0 |
1557 | 572 |
|
573 |
"Modified: 18.7.1996 / 12:40:49 / cg" |
|
701 | 574 |
! ! |
575 |
||
1892 | 576 |
!Number class methodsFor:'documentation'! |
701 | 577 |
|
578 |
version |
|
3634 | 579 |
^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.45 1998-07-09 08:19:03 cg Exp $' |
701 | 580 |
! ! |