author | Claus Gittinger <cg@exept.de> |
Thu, 07 Dec 1995 22:38:49 +0100 | |
changeset 701 | a309e3ef7faf |
parent 569 | 7134eb78cf48 |
child 835 | 8bd6f4aa8130 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
202 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
ArithmeticValue subclass:#Number |
|
701 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
1 | 18 |
! |
19 |
||
213 | 20 |
!Number class methodsFor:'documentation'! |
88 | 21 |
|
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
202 | 25 |
All Rights Reserved |
1 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
1 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
abstract superclass for all kinds of numbers |
|
39 |
" |
|
40 |
! ! |
|
1 | 41 |
|
42 |
!Number class methodsFor:'instance creation'! |
|
43 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
44 |
readFrom:aStringOrStream onError:exceptionBlock |
1 | 45 |
"return the next Number from the (character-)stream aStream; |
202 | 46 |
skipping all whitespace first; return the value of exceptionBlock, |
47 |
if no number can be read." |
|
1 | 48 |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
49 |
|str nextChar radix value negative signExp freakOut| |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
50 |
|
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
51 |
str := aStringOrStream readStream. |
1 | 52 |
|
339 | 53 |
Object errorSignal handle:[:ex | |
54 |
^ exceptionBlock value |
|
55 |
] do:[ |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
56 |
nextChar := str skipSeparators. |
339 | 57 |
nextChar isNil ifTrue:[^ exceptionBlock value]. |
3 | 58 |
|
339 | 59 |
freakOut := [^ exceptionBlock value]. |
60 |
||
61 |
(nextChar == $-) ifTrue:[ |
|
62 |
negative := true. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
63 |
nextChar := str nextPeek |
202 | 64 |
] ifFalse:[ |
339 | 65 |
negative := false. |
66 |
(nextChar == $+) ifTrue:[ |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
67 |
nextChar := str nextPeek |
202 | 68 |
] |
69 |
]. |
|
339 | 70 |
nextChar isDigit ifFalse:[ |
71 |
^ exceptionBlock value. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
72 |
"/ value := super readFrom:str. |
339 | 73 |
"/ negative ifTrue:[value := value negated]. |
74 |
"/ ^ value |
|
75 |
]. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
76 |
value := Integer readFrom:str radix:10 onError:freakOut. |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
77 |
nextChar := str peek. |
339 | 78 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
79 |
str next. |
339 | 80 |
radix := value. |
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
81 |
value := Integer readFrom:str radix:radix onError:freakOut. |
339 | 82 |
] ifFalse:[ |
83 |
radix := 10 |
|
84 |
]. |
|
85 |
(nextChar == $.) ifTrue:[ |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
86 |
nextChar := str nextPeek. |
339 | 87 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[ |
88 |
value := value asFloat |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
89 |
+ (Number readMantissaFrom:str radix:radix). |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
90 |
nextChar := str peek |
339 | 91 |
] |
92 |
]. |
|
93 |
((nextChar == $e) or:[nextChar == $E]) ifTrue:[ |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
94 |
nextChar := str nextPeek. |
339 | 95 |
signExp := 1. |
96 |
(nextChar == $+) ifTrue:[ |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
97 |
nextChar := str nextPeek |
339 | 98 |
] ifFalse:[ |
99 |
(nextChar == $-) ifTrue:[ |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
100 |
nextChar := str nextPeek. |
339 | 101 |
signExp := -1 |
102 |
] |
|
103 |
]. |
|
104 |
(nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[ |
|
105 |
value := value asFloat |
|
106 |
* (10.0 raisedToInteger: |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
107 |
((Integer readFrom:str radix:radix onError:freakOut) * signExp)) |
339 | 108 |
] |
109 |
]. |
|
110 |
negative ifTrue:[ |
|
111 |
^ value negated |
|
112 |
]. |
|
113 |
^ value |
|
114 |
] |
|
1 | 115 |
|
329 | 116 |
" |
117 |
Number readFrom:(ReadStream on:'54.32e-01') |
|
118 |
Number readFrom:(ReadStream on:'12345678901234567890') |
|
119 |
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
|
120 |
Number readFrom:'16rAAAAFFFFAAAAFFFF' |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
121 |
Number readFrom:'0.000001' |
329 | 122 |
'+00000123.45' asNumber |
123 |
" |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
124 |
|
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
125 |
"Modified: 16.11.1995 / 22:49:04 / cg" |
296 | 126 |
! |
127 |
||
128 |
readSmalltalkSyntaxFrom:aStream |
|
129 |
"ST-80 compatibility (thanks to a note from alpha testers) |
|
130 |
read and return the next Number in smalltalk syntax from the |
|
329 | 131 |
(character-)stream aStream." |
132 |
||
133 |
^ Compiler evaluate:aStream compile:false "/ self readFrom:aStream. |
|
296 | 134 |
|
329 | 135 |
" |
136 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'54.32e-01') |
|
137 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'12345678901234567890') |
|
138 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
139 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'(1/10)') |
|
140 |
Number readFrom:(ReadStream on:'(1/10)') |
|
141 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'+00000123.45') |
|
142 |
Number readFrom:(ReadStream on:'+00000123.45') |
|
143 |
" |
|
1 | 144 |
! ! |
701 | 145 |
|
146 |
!Number class methodsFor:'error reporting'! |
|
147 |
||
148 |
raise:aSignalSymbol receiver:someNumber selector:sel arg:arg errorString:text |
|
149 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
|
150 |
||
151 |
|msg| |
|
152 |
||
153 |
msg := MessageSend |
|
154 |
receiver:someNumber |
|
155 |
selector:sel |
|
156 |
arguments:(Array with:arg). |
|
157 |
^ (self perform:aSignalSymbol) |
|
158 |
raiseRequestWith:msg |
|
159 |
errorString:text |
|
160 |
in:thisContext sender |
|
161 |
||
162 |
" |
|
163 |
Number raise:#domainErrorSignal |
|
164 |
receiver:1.0 |
|
165 |
selector:#sin |
|
166 |
arg:nil |
|
167 |
errorString:'foo bar test' |
|
168 |
" |
|
169 |
! |
|
170 |
||
171 |
raise:aSignalSymbol receiver:someNumber selector:sel errorString:text |
|
172 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
|
173 |
||
174 |
|msg| |
|
175 |
||
176 |
msg := MessageSend |
|
177 |
receiver:someNumber |
|
178 |
selector:sel |
|
179 |
arguments:#(). |
|
180 |
^ (self perform:aSignalSymbol) |
|
181 |
raiseRequestWith:msg |
|
182 |
errorString:text |
|
183 |
in:thisContext sender |
|
184 |
||
185 |
" |
|
186 |
Number raise:#domainErrorSignal |
|
187 |
receiver:1.0 |
|
188 |
selector:#foo |
|
189 |
errorString:'foo bar test' |
|
190 |
" |
|
191 |
! ! |
|
192 |
||
193 |
!Number class methodsFor:'private'! |
|
194 |
||
195 |
readMantissaFrom:aStream radix:radix |
|
196 |
"helper for readFrom: - |
|
197 |
return the mantissa from the (character-)stream aStream; |
|
198 |
no whitespace-skipping; error if no number available" |
|
199 |
||
200 |
|nextChar value factor| |
|
201 |
||
202 |
value := 0.0. |
|
203 |
factor := 1.0 / radix. |
|
204 |
nextChar := aStream peek. |
|
205 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
|
206 |
value := value + (nextChar digitValue * factor). |
|
207 |
factor := factor / radix. |
|
208 |
nextChar := aStream nextPeek |
|
209 |
]. |
|
210 |
^ value |
|
211 |
! ! |
|
212 |
||
213 |
!Number methodsFor:'coercing'! |
|
214 |
||
215 |
coerce:aNumber |
|
216 |
"return aNumber converted into receivers type" |
|
217 |
||
218 |
^ self subclassResponsibility |
|
219 |
! |
|
220 |
||
221 |
generality |
|
222 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
223 |
||
224 |
^ 40 |
|
225 |
! |
|
226 |
||
227 |
retry:aSymbol coercing:aNumber |
|
228 |
"Arithmetic represented by the binary operator, aSymbol, |
|
229 |
could not be performed with the receiver and the argument, |
|
230 |
aNumber, because of the differences in representation. Coerce either |
|
231 |
the receiver or the argument, depending on which has higher generality, and |
|
232 |
try again. If the operation is comapre for same value (=), return false if |
|
233 |
the argument is not a Number. |
|
234 |
If the generalities are the same, create an error message." |
|
235 |
||
236 |
|myGenerality otherGenerality| |
|
237 |
||
238 |
(aSymbol == #=) ifTrue:[ |
|
239 |
(aNumber respondsTo:#generality) ifFalse:[^ false] |
|
240 |
] ifFalse:[ |
|
241 |
(aNumber respondsTo:#generality) ifFalse:[ |
|
242 |
self error:'retry:coercing: argument is not a number'. |
|
243 |
^ self |
|
244 |
] |
|
245 |
]. |
|
246 |
myGenerality := self generality. |
|
247 |
otherGenerality := aNumber generality. |
|
248 |
(myGenerality > otherGenerality) ifTrue:[ |
|
249 |
^ self perform:aSymbol with:(self coerce:aNumber) |
|
250 |
]. |
|
251 |
(myGenerality < otherGenerality) ifTrue:[ |
|
252 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber |
|
253 |
]. |
|
254 |
self error:'retry:coercing: oops - same generality' |
|
255 |
! ! |
|
256 |
||
257 |
!Number methodsFor:'converting'! |
|
258 |
||
259 |
@ aNumber |
|
260 |
"return a Point with the receiver as x-coordinate and the argument |
|
261 |
as y-coordinate" |
|
262 |
||
263 |
%{ /* NOCONTEXT */ |
|
264 |
||
265 |
/* |
|
266 |
* I cannot tell if this special code is worth anything |
|
267 |
*/ |
|
268 |
if (_CanDoQuickNew(sizeof(struct __point))) { |
|
269 |
OBJ newPoint; |
|
270 |
int spc; |
|
271 |
||
272 |
_qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
|
273 |
_InstPtr(newPoint)->o_class = Point; |
|
274 |
_PointInstPtr(newPoint)->p_x = self; |
|
275 |
_PointInstPtr(newPoint)->p_y = aNumber; |
|
276 |
if (! __bothSmallInteger(self, aNumber)) { |
|
277 |
spc = __qSpace(newPoint); |
|
278 |
__STORE_SPC(newPoint, aNumber, spc); |
|
279 |
__STORE_SPC(newPoint, self, spc); |
|
280 |
} |
|
281 |
RETURN ( newPoint ); |
|
282 |
} |
|
283 |
%} |
|
284 |
. |
|
285 |
^ Point x:self y:aNumber |
|
286 |
! |
|
287 |
||
288 |
asPoint |
|
289 |
"return a new Point with the receiver as all coordinates; |
|
290 |
often used to supply the same value in two dimensions, as with |
|
291 |
symmetrical gridding or scaling." |
|
292 |
||
293 |
%{ /* NOCONTEXT */ |
|
294 |
||
295 |
if (_CanDoQuickNew(sizeof(struct __point))) { |
|
296 |
OBJ newPoint; |
|
297 |
||
298 |
_qCheckedAlignedNew(newPoint, sizeof(struct __point)); |
|
299 |
_InstPtr(newPoint)->o_class = Point; |
|
300 |
_PointInstPtr(newPoint)->p_x = self; |
|
301 |
_PointInstPtr(newPoint)->p_y = self; |
|
302 |
__STORE(newPoint, self); |
|
303 |
RETURN ( newPoint ); |
|
304 |
} |
|
305 |
%}. |
|
306 |
^ Point x:self y:self |
|
307 |
! |
|
308 |
||
309 |
degreesToRadians |
|
310 |
"interpreting the receiver as radians, return the degrees" |
|
311 |
||
312 |
^ (self * (Float pi)) / 180.0 |
|
313 |
! |
|
314 |
||
315 |
literalArrayEncoding |
|
316 |
"encode myself as an array literal." |
|
317 |
||
318 |
^ self |
|
319 |
||
320 |
"Modified: 1.9.1995 / 02:25:26 / claus" |
|
321 |
! |
|
322 |
||
323 |
radiansToDegrees |
|
324 |
"interpreting the receiver as degrees, return the radians" |
|
325 |
||
326 |
^ (self * 180.0) / (Float pi) |
|
327 |
! ! |
|
328 |
||
329 |
!Number methodsFor:'intervals'! |
|
330 |
||
331 |
to:stop |
|
332 |
"return an interval from receiver up to the argument, incrementing by 1" |
|
333 |
||
334 |
^ Interval from:self to:stop |
|
335 |
! |
|
336 |
||
337 |
to:stop by:step |
|
338 |
"return an interval from receiver up to the argument, incrementing by step" |
|
339 |
||
340 |
^ Interval from:self to:stop by:step |
|
341 |
! ! |
|
342 |
||
343 |
!Number methodsFor:'iteration'! |
|
344 |
||
345 |
timesRepeat:aBlock |
|
346 |
"evaluate the argument, aBlock self times" |
|
347 |
||
348 |
|count| |
|
349 |
||
350 |
count := self. |
|
351 |
[count > 0] whileTrue:[ |
|
352 |
aBlock value. |
|
353 |
count := count - 1 |
|
354 |
] |
|
355 |
! |
|
356 |
||
357 |
to:stop by:incr do:aBlock |
|
358 |
"For each element of the interval from the receiver up to the argument stop, incrementing |
|
359 |
by step, evaluate aBlock passing the element as argument." |
|
360 |
||
361 |
|tmp| |
|
362 |
||
363 |
tmp := self. |
|
364 |
(incr > 0) ifTrue:[ |
|
365 |
[tmp <= stop] whileTrue:[ |
|
366 |
aBlock value:tmp. |
|
367 |
tmp := tmp+incr |
|
368 |
] |
|
369 |
] ifFalse:[ |
|
370 |
[tmp >= stop] whileTrue:[ |
|
371 |
aBlock value:tmp. |
|
372 |
tmp := tmp+incr |
|
373 |
] |
|
374 |
] |
|
375 |
! |
|
376 |
||
377 |
to:stop do:aBlock |
|
378 |
"For each element of the interval from the receiver up to the argument stop, |
|
379 |
evaluate aBlock, passing the number as argument." |
|
380 |
||
381 |
|tmp| |
|
382 |
||
383 |
tmp := self. |
|
384 |
[tmp <= stop] whileTrue:[ |
|
385 |
aBlock value:tmp. |
|
386 |
tmp := tmp+1 |
|
387 |
] |
|
388 |
! ! |
|
389 |
||
390 |
!Number methodsFor:'printing & storing'! |
|
391 |
||
392 |
storeOn:aStream |
|
393 |
"append a string for storing the receiver onto the argument, |
|
394 |
aStream - since numbers are literals,they store as they print." |
|
395 |
||
396 |
^ self printOn:aStream |
|
397 |
! |
|
398 |
||
399 |
storeString |
|
400 |
"return a string for storing |
|
401 |
- since numbers are literals, they store as they print." |
|
402 |
||
403 |
^ self printString |
|
404 |
! ! |
|
405 |
||
406 |
!Number methodsFor:'testing'! |
|
407 |
||
408 |
isLiteral |
|
409 |
"return true, if the receiver can be used as a literal |
|
410 |
(i.e. can be used in constant arrays)" |
|
411 |
||
412 |
^ true |
|
413 |
! |
|
414 |
||
415 |
isNumber |
|
416 |
"return true, if the receiver is a kind of number" |
|
417 |
||
418 |
^ true |
|
419 |
! |
|
420 |
||
421 |
isZero |
|
422 |
^ self = 0 |
|
423 |
! ! |
|
424 |
||
425 |
!Number class methodsFor:'documentation'! |
|
426 |
||
427 |
version |
|
428 |
^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.24 1995-12-07 21:36:01 cg Exp $' |
|
429 |
! ! |