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