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