1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1988 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
abstract superclass for all kinds of numbers
|
|
26 |
|
5
|
27 |
$Header: /cvs/stx/stx/libbasic/Number.st,v 1.4 1993-10-13 02:12:54 claus Exp $
|
1
|
28 |
'!
|
|
29 |
|
|
30 |
! Number methodsFor:'converting' !
|
|
31 |
|
|
32 |
@ aNumber
|
|
33 |
"return a Point with the receiver as x-coordinate and the argument
|
|
34 |
as y-coordinate"
|
|
35 |
|
|
36 |
%{ /* NOCONTEXT */
|
|
37 |
|
|
38 |
extern char *newNextPtr, *newEndPtr;
|
|
39 |
|
|
40 |
if (_CanDoQuickNew(sizeof(struct point))) {
|
|
41 |
OBJ newPoint;
|
|
42 |
|
|
43 |
_qCheckedAlignedNew(newPoint, sizeof(struct point), __context);
|
|
44 |
_InstPtr(newPoint)->o_class = Point;
|
|
45 |
_PointInstPtr(newPoint)->p_x = self;
|
|
46 |
_PointInstPtr(newPoint)->p_y = aNumber;
|
2
|
47 |
/*
|
3
|
48 |
* no store check needed for self: self is a number, new object is definitely in newSpace
|
|
49 |
* however, argument may be a context/block
|
|
50 |
*/
|
|
51 |
__STORE(newPoint, aNumber);
|
1
|
52 |
RETURN ( newPoint );
|
|
53 |
}
|
|
54 |
%}
|
|
55 |
.
|
|
56 |
^ Point x:self y:aNumber
|
|
57 |
!
|
|
58 |
|
|
59 |
asPoint
|
|
60 |
"return a new Point with the receiver as all coordinates;
|
|
61 |
often used to supply the same value in two dimensions, as with
|
|
62 |
symmetrical gridding or scaling."
|
|
63 |
|
|
64 |
%{ /* NOCONTEXT */
|
|
65 |
|
|
66 |
extern char *newNextPtr, *newEndPtr;
|
|
67 |
|
|
68 |
if (_CanDoQuickNew(sizeof(struct point))) {
|
|
69 |
OBJ newPoint;
|
|
70 |
|
2
|
71 |
_qCheckedAlignedNew(newPoint, sizeof(struct point), __context);
|
|
72 |
_InstPtr(newPoint)->o_class = Point;
|
|
73 |
_PointInstPtr(newPoint)->p_x = self;
|
|
74 |
_PointInstPtr(newPoint)->p_y = self;
|
|
75 |
/*
|
3
|
76 |
* no store check needed - its definitely in newSpace
|
|
77 |
* and self is a number
|
2
|
78 |
*/
|
|
79 |
RETURN ( newPoint );
|
1
|
80 |
}
|
|
81 |
%}
|
|
82 |
.
|
|
83 |
^ Point x:self y:self
|
|
84 |
!
|
|
85 |
|
|
86 |
degreesToRadians
|
|
87 |
"interpreting the receiver as radians, return the degrees"
|
|
88 |
|
|
89 |
^ (self * (Float pi)) / 180.0
|
|
90 |
!
|
|
91 |
|
|
92 |
radiansToDegrees
|
|
93 |
"interpreting the receiver as degrees, return the radians"
|
|
94 |
|
|
95 |
^ (self * 180.0) / (Float pi)
|
|
96 |
!
|
|
97 |
|
|
98 |
coerce:aNumber
|
|
99 |
^ self subclassResponsibility
|
|
100 |
!
|
|
101 |
|
|
102 |
generality
|
|
103 |
^ 40
|
|
104 |
!
|
|
105 |
|
|
106 |
retry:aSymbol coercing:aNumber
|
|
107 |
"Arithmetic represented by the binary operator, aSymbol,
|
|
108 |
could not be performed with the receiver and the argument,
|
|
109 |
aNumber, because of the differences in representation. Coerce either
|
|
110 |
the receiver or the argument, depending on which has higher generality, and
|
|
111 |
try again. If the operation is comapre for same value (=), return false if
|
|
112 |
the argument is not a Number.
|
|
113 |
If the generalities are the same, create an error message."
|
|
114 |
|
|
115 |
|myGenerality otherGenerality|
|
|
116 |
|
|
117 |
(aSymbol == #=) ifTrue:[
|
|
118 |
(aNumber respondsTo:#generality) ifFalse:[^ false]
|
|
119 |
] ifFalse:[
|
|
120 |
(aNumber respondsTo:#generality) ifFalse:[
|
|
121 |
self error:'retry:coercing: argument is not a number'.
|
|
122 |
^ self
|
|
123 |
]
|
|
124 |
].
|
|
125 |
myGenerality := self generality.
|
|
126 |
otherGenerality := aNumber generality.
|
|
127 |
(myGenerality > otherGenerality) ifTrue:[
|
|
128 |
^ self perform:aSymbol with:(self coerce:aNumber)
|
|
129 |
].
|
|
130 |
(myGenerality < otherGenerality) ifTrue:[
|
|
131 |
^ (aNumber coerce:self) perform:aSymbol with:aNumber
|
|
132 |
].
|
|
133 |
self error:'retry:coercing: oops - same generality'
|
|
134 |
! !
|
|
135 |
|
|
136 |
!Number methodsFor:'printing & storing'!
|
|
137 |
|
|
138 |
storeString
|
|
139 |
^ self printString
|
|
140 |
! !
|
|
141 |
|
|
142 |
!Number methodsFor:'intervals'!
|
|
143 |
|
|
144 |
to:stop
|
|
145 |
"return an interval from receiver up to the argument, incrementing by 1"
|
|
146 |
|
|
147 |
^ Interval from:self to:stop
|
|
148 |
!
|
|
149 |
|
|
150 |
to:stop by:step
|
|
151 |
"return an interval from receiver up to the argument, incrementing by step"
|
|
152 |
|
|
153 |
^ Interval from:self to:stop by:step
|
|
154 |
! !
|
|
155 |
|
|
156 |
!Number methodsFor:'iteration'!
|
|
157 |
|
|
158 |
timesRepeat:aBlock
|
|
159 |
"evaluate the argument, aBlock self times"
|
|
160 |
|
|
161 |
|count|
|
|
162 |
|
|
163 |
count := self.
|
|
164 |
[count > 0] whileTrue:[
|
|
165 |
aBlock value.
|
|
166 |
count := count - 1
|
|
167 |
]
|
|
168 |
!
|
|
169 |
|
|
170 |
to:stop do:aBlock
|
3
|
171 |
"For each element of the interval from the receiver up to the argument stop,
|
|
172 |
evaluate aBlock, passing the number as argument."
|
1
|
173 |
|
|
174 |
|tmp|
|
|
175 |
|
|
176 |
tmp := self.
|
|
177 |
[tmp <= stop] whileTrue:[
|
|
178 |
aBlock value:tmp.
|
|
179 |
tmp := tmp+1
|
|
180 |
]
|
|
181 |
!
|
|
182 |
|
|
183 |
to:stop by:incr do:aBlock
|
3
|
184 |
"For each element of the interval from the receiver up to the argument stop, incrementing
|
|
185 |
by step, evaluate aBlock passing the element as argument."
|
1
|
186 |
|
|
187 |
|tmp|
|
|
188 |
|
|
189 |
tmp := self.
|
|
190 |
(incr > 0) ifTrue:[
|
|
191 |
[tmp <= stop] whileTrue:[
|
|
192 |
aBlock value:tmp.
|
|
193 |
tmp := tmp+incr
|
|
194 |
]
|
|
195 |
] ifFalse:[
|
|
196 |
[tmp >= stop] whileTrue:[
|
|
197 |
aBlock value:tmp.
|
|
198 |
tmp := tmp+incr
|
|
199 |
]
|
|
200 |
]
|
|
201 |
! !
|
|
202 |
|
|
203 |
!Number class methodsFor:'private'!
|
|
204 |
|
|
205 |
readMantissaFrom:aStream radix:radix
|
|
206 |
"helper for readFrom: -
|
|
207 |
return the mantissa from the (character-)stream aStream;
|
|
208 |
no whitespace-skipping; error if no number available"
|
|
209 |
|
|
210 |
|nextChar value factor|
|
|
211 |
|
|
212 |
value := 0.0.
|
|
213 |
factor := 1.0 / radix.
|
|
214 |
nextChar := aStream peek.
|
|
215 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[
|
|
216 |
value := value + (nextChar digitValue * factor).
|
|
217 |
factor := factor / radix.
|
|
218 |
nextChar := aStream nextPeek
|
|
219 |
].
|
|
220 |
^ value
|
|
221 |
! !
|
|
222 |
|
|
223 |
!Number class methodsFor:'instance creation'!
|
|
224 |
|
|
225 |
readFrom:aStream
|
|
226 |
"return the next Number from the (character-)stream aStream;
|
|
227 |
skipping all whitespace first; return nil if no number"
|
|
228 |
|
|
229 |
|nextChar radix value negative signExp|
|
|
230 |
|
|
231 |
nextChar := aStream skipSeparators.
|
3
|
232 |
|
|
233 |
nextChar isNil ifTrue:[
|
|
234 |
"what should be returned at end-of-input ?"
|
|
235 |
^ nil
|
|
236 |
].
|
|
237 |
|
1
|
238 |
(nextChar == $-) ifTrue:[
|
|
239 |
negative := true.
|
|
240 |
nextChar := aStream nextPeek
|
|
241 |
] ifFalse:[
|
|
242 |
negative := false
|
|
243 |
].
|
2
|
244 |
nextChar isDigit ifFalse:[
|
|
245 |
value := super readFrom:aStream.
|
|
246 |
negative ifTrue:[value := value negated].
|
|
247 |
^ value
|
|
248 |
].
|
1
|
249 |
value := Integer readFrom:aStream radix:10.
|
|
250 |
nextChar := aStream peek.
|
|
251 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[
|
|
252 |
aStream next.
|
|
253 |
radix := value.
|
|
254 |
value := Integer readFrom:aStream radix:radix
|
|
255 |
] ifFalse:[
|
|
256 |
radix := 10
|
|
257 |
].
|
|
258 |
(nextChar == $.) ifTrue:[
|
|
259 |
nextChar := aStream nextPeek.
|
|
260 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[
|
|
261 |
value := value asFloat + (Number readMantissaFrom:aStream
|
|
262 |
radix:radix).
|
|
263 |
nextChar := aStream peek
|
|
264 |
]
|
|
265 |
].
|
|
266 |
((nextChar == $e) or:[nextChar == $E]) ifTrue:[
|
|
267 |
nextChar := aStream nextPeek.
|
|
268 |
signExp := 1.
|
|
269 |
(nextChar == $+) ifTrue:[
|
|
270 |
nextChar := aStream nextPeek
|
|
271 |
] ifFalse:[
|
|
272 |
(nextChar == $-) ifTrue:[
|
|
273 |
nextChar := aStream nextPeek.
|
|
274 |
signExp := -1
|
|
275 |
]
|
|
276 |
].
|
|
277 |
(nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[
|
|
278 |
value := value asFloat
|
|
279 |
* (10.0 raisedToInteger:
|
|
280 |
((Integer readFrom:aStream
|
|
281 |
radix:radix) * signExp))
|
|
282 |
]
|
|
283 |
].
|
|
284 |
negative ifTrue:[
|
|
285 |
^ value negated
|
|
286 |
].
|
|
287 |
^ value
|
|
288 |
|
3
|
289 |
"Number readFrom:(ReadStream on:'54.32e-01')"
|
|
290 |
"Number readFrom:(ReadStream on:'12345678901234567890')"
|
|
291 |
"Number readFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF')"
|
|
292 |
"Number readFrom:(ReadStream on:'(1/10)')"
|
1
|
293 |
! !
|