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