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