author | Claus Gittinger <cg@exept.de> |
Fri, 19 Nov 1999 18:26:56 +0100 | |
changeset 5013 | e2aff65e6eba |
parent 4988 | ac426684e852 |
child 5238 | f7a816a660a3 |
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') |
|
5013 | 190 |
|
191 |
|s| |
|
192 |
s := ReadStream on:'2.'. |
|
193 |
Number readSmalltalkSyntaxFrom:s. |
|
194 |
s next |
|
195 |
||
196 |
|s| |
|
197 |
s := ReadStream on:'2.0.'. |
|
198 |
Number readSmalltalkSyntaxFrom:s. |
|
199 |
s next |
|
329 | 200 |
" |
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
201 |
|
5013 | 202 |
"Modified: / 19.11.1999 / 18:26:47 / cg" |
1 | 203 |
! ! |
701 | 204 |
|
1892 | 205 |
!Number class methodsFor:'error reporting'! |
701 | 206 |
|
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
207 |
raise:aSignalSymbol receiver:someNumber selector:sel arg:arg errorString:text |
3394 | 208 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
209 |
||
4558 | 210 |
<context: #return> |
211 |
||
3394 | 212 |
|msg| |
213 |
||
214 |
msg := MessageSend |
|
3770 | 215 |
receiver:someNumber |
216 |
selector:sel |
|
217 |
arguments:(Array with:arg). |
|
3394 | 218 |
|
219 |
^ (self perform:aSignalSymbol) |
|
3770 | 220 |
raiseRequestWith:msg |
221 |
errorString:text |
|
222 |
in:thisContext sender |
|
3394 | 223 |
|
224 |
" |
|
225 |
Number |
|
3770 | 226 |
raise:#domainErrorSignal |
227 |
receiver:1.0 |
|
228 |
selector:#sin |
|
229 |
arg:nil |
|
230 |
errorString:'foo bar test' |
|
3394 | 231 |
" |
232 |
! |
|
233 |
||
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
234 |
raise:aSignalSymbol receiver:someNumber selector:sel arguments:argArray errorString:text |
701 | 235 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
236 |
||
4558 | 237 |
<context: #return> |
238 |
||
701 | 239 |
|msg| |
240 |
||
241 |
msg := MessageSend |
|
3770 | 242 |
receiver:someNumber |
243 |
selector:sel |
|
244 |
arguments:argArray. |
|
3394 | 245 |
|
701 | 246 |
^ (self perform:aSignalSymbol) |
3770 | 247 |
raiseRequestWith:msg |
248 |
errorString:text |
|
249 |
in:thisContext sender |
|
701 | 250 |
|
251 |
" |
|
3394 | 252 |
Number |
3770 | 253 |
raise:#domainErrorSignal |
254 |
receiver:1.0 |
|
255 |
selector:#sin |
|
256 |
arg:nil |
|
257 |
errorString:'foo bar test' |
|
701 | 258 |
" |
259 |
! |
|
260 |
||
261 |
raise:aSignalSymbol receiver:someNumber selector:sel errorString:text |
|
262 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
|
263 |
||
4558 | 264 |
<context: #return> |
265 |
||
701 | 266 |
|msg| |
267 |
||
268 |
msg := MessageSend |
|
3770 | 269 |
receiver:someNumber |
270 |
selector:sel |
|
271 |
arguments:#(). |
|
3394 | 272 |
|
701 | 273 |
^ (self perform:aSignalSymbol) |
3770 | 274 |
raiseRequestWith:msg |
275 |
errorString:text |
|
276 |
in:thisContext sender |
|
701 | 277 |
|
278 |
" |
|
3394 | 279 |
Number |
3770 | 280 |
raise:#domainErrorSignal |
281 |
receiver:1.0 |
|
282 |
selector:#foo |
|
283 |
errorString:'foo bar test' |
|
701 | 284 |
" |
285 |
! ! |
|
286 |
||
1892 | 287 |
!Number class methodsFor:'private'! |
701 | 288 |
|
289 |
readMantissaFrom:aStream radix:radix |
|
290 |
"helper for readFrom: - |
|
291 |
return the mantissa from the (character-)stream aStream; |
|
292 |
no whitespace-skipping; error if no number available" |
|
293 |
||
294 |
|nextChar value factor| |
|
295 |
||
296 |
value := 0.0. |
|
297 |
factor := 1.0 / radix. |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
298 |
nextChar := aStream peekOrNil. |
701 | 299 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
3770 | 300 |
value := value + (nextChar digitValue * factor). |
301 |
factor := factor / radix. |
|
302 |
aStream next. |
|
303 |
nextChar := aStream peekOrNil |
|
701 | 304 |
]. |
305 |
^ value |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
306 |
|
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
307 |
"Modified: / 14.4.1998 / 18:47:47 / cg" |
701 | 308 |
! ! |
309 |
||
4988 | 310 |
!Number methodsFor:'Compatibility - Squeak'! |
311 |
||
312 |
asSmallAngleDegrees |
|
313 |
"Return the receiver normalized to lie within the range (-180, 180)" |
|
314 |
||
315 |
| pos | |
|
316 |
pos _ self \\ 360. |
|
317 |
pos > 180 ifTrue: [pos _ pos - 360]. |
|
318 |
^ pos |
|
319 |
||
320 |
"#(-500 -300 -150 -5 0 5 150 300 500 1200) collect: [:n | n asSmallAngleDegrees]"! |
|
321 |
||
322 |
newTileMorphRepresentative |
|
323 |
^ TileMorph new addArrows; setLiteral: self; addSuffixIfCan |
|
324 |
! |
|
325 |
||
326 |
stringForReadout |
|
327 |
^ self rounded printString |
|
328 |
! ! |
|
329 |
||
701 | 330 |
!Number methodsFor:'coercing'! |
331 |
||
332 |
coerce:aNumber |
|
333 |
"return aNumber converted into receivers type" |
|
334 |
||
335 |
^ self subclassResponsibility |
|
336 |
! |
|
337 |
||
338 |
generality |
|
339 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
340 |
||
341 |
^ 40 |
|
342 |
! ! |
|
343 |
||
344 |
!Number methodsFor:'converting'! |
|
345 |
||
3634 | 346 |
% aNumber |
347 |
"Return a complex number with the receiver as the real part and |
|
348 |
aNumber as the imaginary part" |
|
349 |
||
350 |
^ Complex real:self imaginary:aNumber |
|
351 |
||
352 |
"Modified: / 9.7.1998 / 10:18:12 / cg" |
|
353 |
! |
|
354 |
||
701 | 355 |
@ aNumber |
356 |
"return a Point with the receiver as x-coordinate and the argument |
|
357 |
as y-coordinate" |
|
358 |
||
359 |
%{ /* NOCONTEXT */ |
|
360 |
||
361 |
/* |
|
362 |
* I cannot tell if this special code is worth anything |
|
363 |
*/ |
|
3394 | 364 |
if (__CanDoQuickNew(sizeof(struct __point))) { /* OBJECT ALLOCATION */ |
3770 | 365 |
OBJ newPoint; |
366 |
int spc; |
|
701 | 367 |
|
3770 | 368 |
__qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
369 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
370 |
__PointInstPtr(newPoint)->p_x = self; |
|
371 |
__PointInstPtr(newPoint)->p_y = aNumber; |
|
372 |
if (! __bothSmallInteger(self, aNumber)) { |
|
373 |
spc = __qSpace(newPoint); |
|
374 |
__STORE_SPC(newPoint, aNumber, spc); |
|
375 |
__STORE_SPC(newPoint, self, spc); |
|
376 |
} |
|
377 |
RETURN ( newPoint ); |
|
701 | 378 |
} |
379 |
%} |
|
380 |
. |
|
381 |
^ Point x:self y:aNumber |
|
382 |
! |
|
383 |
||
3634 | 384 |
asComplex |
385 |
"Return a complex number with the receiver as the real part and |
|
386 |
zero as the imaginary part" |
|
387 |
||
388 |
^ Complex fromReal:self |
|
389 |
||
390 |
"Modified: / 9.7.1998 / 10:18:16 / cg" |
|
391 |
! |
|
392 |
||
701 | 393 |
asPoint |
394 |
"return a new Point with the receiver as all coordinates; |
|
395 |
often used to supply the same value in two dimensions, as with |
|
396 |
symmetrical gridding or scaling." |
|
397 |
||
398 |
%{ /* NOCONTEXT */ |
|
399 |
||
3394 | 400 |
if (__CanDoQuickNew(sizeof(struct __point))) { /* OBJECT ALLOCATION */ |
3770 | 401 |
OBJ newPoint; |
701 | 402 |
|
3770 | 403 |
__qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
404 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
405 |
__PointInstPtr(newPoint)->p_x = self; |
|
406 |
__PointInstPtr(newPoint)->p_y = self; |
|
407 |
__STORE(newPoint, self); |
|
408 |
RETURN ( newPoint ); |
|
701 | 409 |
} |
410 |
%}. |
|
411 |
^ Point x:self y:self |
|
412 |
! |
|
413 |
||
2416 | 414 |
decodeAsLiteralArray |
415 |
"given a literalEncoding in the receiver, |
|
416 |
create & return the corresponding object. |
|
417 |
The inverse operation to #literalArrayEncoding." |
|
418 |
||
419 |
^ self |
|
420 |
||
421 |
"Created: 25.2.1997 / 19:17:06 / cg" |
|
422 |
"Modified: 25.2.1997 / 19:17:42 / cg" |
|
423 |
! |
|
424 |
||
701 | 425 |
degreesToRadians |
426 |
"interpreting the receiver as radians, return the degrees" |
|
427 |
||
428 |
^ (self * (Float pi)) / 180.0 |
|
429 |
! |
|
430 |
||
431 |
literalArrayEncoding |
|
1245 | 432 |
"encode myself as an array literal, from which a copy of the receiver |
433 |
can be reconstructed with #decodeAsLiteralArray." |
|
701 | 434 |
|
435 |
^ self |
|
436 |
||
437 |
"Modified: 1.9.1995 / 02:25:26 / claus" |
|
1245 | 438 |
"Modified: 22.4.1996 / 13:00:27 / cg" |
701 | 439 |
! |
440 |
||
441 |
radiansToDegrees |
|
442 |
"interpreting the receiver as degrees, return the radians" |
|
443 |
||
444 |
^ (self * 180.0) / (Float pi) |
|
445 |
! ! |
|
446 |
||
447 |
!Number methodsFor:'intervals'! |
|
448 |
||
449 |
to:stop |
|
450 |
"return an interval from receiver up to the argument, incrementing by 1" |
|
451 |
||
452 |
^ Interval from:self to:stop |
|
453 |
! |
|
454 |
||
455 |
to:stop by:step |
|
456 |
"return an interval from receiver up to the argument, incrementing by step" |
|
457 |
||
458 |
^ Interval from:self to:stop by:step |
|
459 |
! ! |
|
460 |
||
461 |
!Number methodsFor:'iteration'! |
|
462 |
||
463 |
timesRepeat:aBlock |
|
464 |
"evaluate the argument, aBlock self times" |
|
465 |
||
466 |
|count| |
|
467 |
||
468 |
count := self. |
|
469 |
[count > 0] whileTrue:[ |
|
3770 | 470 |
aBlock value. |
471 |
count := count - 1 |
|
701 | 472 |
] |
473 |
! |
|
474 |
||
475 |
to:stop by:incr do:aBlock |
|
476 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
|
477 |
by step, evaluate aBlock passing the element as argument." |
|
478 |
||
479 |
|tmp| |
|
480 |
||
481 |
tmp := self. |
|
482 |
(incr > 0) ifTrue:[ |
|
3770 | 483 |
[tmp <= stop] whileTrue:[ |
484 |
aBlock value:tmp. |
|
485 |
tmp := tmp+incr |
|
486 |
] |
|
701 | 487 |
] ifFalse:[ |
3770 | 488 |
[tmp >= stop] whileTrue:[ |
489 |
aBlock value:tmp. |
|
490 |
tmp := tmp+incr |
|
491 |
] |
|
701 | 492 |
] |
493 |
! |
|
494 |
||
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
495 |
to:stop by:incr doWithBreak:aBlock |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
496 |
"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
|
497 |
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
|
498 |
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
|
499 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
500 |
|tmp break| |
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 |
break := [^ self]. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
503 |
tmp := self. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
504 |
(incr > 0) ifTrue:[ |
3770 | 505 |
[tmp <= stop] whileTrue:[ |
506 |
aBlock value:tmp value:break. |
|
507 |
tmp := tmp+incr |
|
508 |
] |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
509 |
] ifFalse:[ |
3770 | 510 |
[tmp >= stop] whileTrue:[ |
511 |
aBlock value:tmp value:break. |
|
512 |
tmp := tmp+incr |
|
513 |
] |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
514 |
] |
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 |
" |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
517 |
1 to:100 by:5 doWithBreak:[:index :break | |
3770 | 518 |
Transcript showCR:index printString. |
519 |
index > 50 ifTrue:[ |
|
520 |
break value |
|
521 |
]. |
|
919
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 |
! |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
525 |
|
701 | 526 |
to:stop do:aBlock |
527 |
"For each element of the interval from the receiver up to the argument stop, |
|
528 |
evaluate aBlock, passing the number as argument." |
|
529 |
||
530 |
|tmp| |
|
531 |
||
532 |
tmp := self. |
|
533 |
[tmp <= stop] whileTrue:[ |
|
3770 | 534 |
aBlock value:tmp. |
535 |
tmp := tmp+1 |
|
701 | 536 |
] |
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
537 |
! |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
538 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
539 |
to:stop doWithBreak:aBlock |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
540 |
"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
|
541 |
evaluate aBlock, passing the number as argument. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
542 |
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
|
543 |
|
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
544 |
|tmp break| |
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 |
break := [^ self]. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
547 |
tmp := self. |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
548 |
[tmp <= stop] whileTrue:[ |
3770 | 549 |
aBlock value:tmp value:break. |
550 |
tmp := tmp+1 |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
551 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
552 |
|
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 |
1 to:10 doWithBreak:[:index :break | |
3770 | 555 |
Transcript showCR:index printString. |
556 |
index > 5 ifTrue:[ |
|
557 |
break value |
|
558 |
]. |
|
919
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
559 |
] |
d285a0816e54
added to:do: loops with break
Claus Gittinger <cg@exept.de>
parents:
835
diff
changeset
|
560 |
" |
701 | 561 |
! ! |
562 |
||
3634 | 563 |
!Number methodsFor:'mathematical functions'! |
564 |
||
565 |
conjugated |
|
566 |
"Return the complex conjugate of this Number." |
|
567 |
||
568 |
^ self |
|
569 |
||
570 |
"Modified: / 9.7.1998 / 10:17:31 / cg" |
|
571 |
! |
|
572 |
||
573 |
imaginary |
|
574 |
"Return the imaginary part of this Number." |
|
575 |
||
576 |
^ 0 |
|
577 |
||
578 |
"Modified: / 9.7.1998 / 10:17:24 / cg" |
|
579 |
! |
|
580 |
||
581 |
real |
|
582 |
"Return the real part of this Number." |
|
583 |
||
584 |
^ self |
|
585 |
||
586 |
"Modified: / 9.7.1998 / 10:17:17 / cg" |
|
587 |
! ! |
|
588 |
||
701 | 589 |
!Number methodsFor:'printing & storing'! |
590 |
||
591 |
storeOn:aStream |
|
592 |
"append a string for storing the receiver onto the argument, |
|
593 |
aStream - since numbers are literals,they store as they print." |
|
594 |
||
595 |
^ self printOn:aStream |
|
596 |
! |
|
597 |
||
598 |
storeString |
|
599 |
"return a string for storing |
|
600 |
- since numbers are literals, they store as they print." |
|
601 |
||
602 |
^ self printString |
|
603 |
! ! |
|
604 |
||
605 |
!Number methodsFor:'testing'! |
|
606 |
||
607 |
isNumber |
|
608 |
"return true, if the receiver is a kind of number" |
|
609 |
||
610 |
^ true |
|
611 |
! |
|
612 |
||
613 |
isZero |
|
1557 | 614 |
"return true, if the receiver is zero" |
615 |
||
701 | 616 |
^ self = 0 |
1557 | 617 |
|
618 |
"Modified: 18.7.1996 / 12:40:49 / cg" |
|
701 | 619 |
! ! |
620 |
||
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
621 |
!Number methodsFor:'tracing'! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
622 |
|
4682 | 623 |
traceInto:aRequestor level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
624 |
"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
|
625 |
|
4682 | 626 |
^ aRequestor traceNumber:self level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
627 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
628 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
629 |
! ! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
630 |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
631 |
!Number methodsFor:'truncation and rounding'! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
632 |
|
4895 | 633 |
detentBy: detent atMultiplesOf: grid snap: snap |
634 |
"Map all values that are within detent/2 of any multiple of grid |
|
635 |
to that multiple. |
|
636 |
Otherwise, if snap is true, return self, meaning that the values |
|
637 |
in the dead zone will never be returned. |
|
638 |
If snap is false, then expand the range between dead zones |
|
639 |
so that it covers the range between multiples of the grid, |
|
640 |
and scale the value by that factor." |
|
641 |
||
642 |
| r1 r2 | |
|
643 |
||
644 |
r1 := self roundTo: grid. "Nearest multiple of grid" |
|
645 |
(self roundTo: detent) = r1 ifTrue: [^ r1]. "Snap to that multiple..." |
|
646 |
snap ifTrue: [^ self]. "...or return self" |
|
647 |
||
648 |
r2 := self < r1 "Nearest end of dead zone" |
|
649 |
ifTrue: [r1 - (detent asFloat/2)] |
|
650 |
ifFalse: [r1 + (detent asFloat/2)]. |
|
651 |
||
652 |
"Scale values between dead zones to fill range between multiples" |
|
653 |
^ r1 + ((self - r2) * grid asFloat / (grid - detent)) |
|
654 |
||
655 |
" |
|
656 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: true] |
|
657 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: false] |
|
658 |
(3.9 to: 4.1 by: 0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: true] |
|
659 |
(-3.9 to: -4.1 by: -0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: false] |
|
660 |
" |
|
661 |
! |
|
662 |
||
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
663 |
fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
664 |
"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
|
665 |
(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
|
666 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
667 |
^ self - self truncated asFloat |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
668 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
669 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
670 |
1234.56789 fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
671 |
1.2345e6 fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
672 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
673 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
674 |
"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
|
675 |
"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
|
676 |
! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
677 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
678 |
integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
679 |
"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
|
680 |
(i.e. the truncated value)" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
681 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
682 |
^ self truncated asFloat |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
683 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
684 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
685 |
1234.56789 integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
686 |
1.2345e6 integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
687 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
688 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
689 |
"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
|
690 |
"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
|
691 |
! ! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
692 |
|
1892 | 693 |
!Number class methodsFor:'documentation'! |
701 | 694 |
|
695 |
version |
|
5013 | 696 |
^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.56 1999-11-19 17:26:56 cg Exp $' |
701 | 697 |
! ! |