author | Claus Gittinger <cg@exept.de> |
Sat, 17 Nov 2001 11:10:08 +0100 | |
changeset 6211 | 69f320c220d5 |
parent 6195 | 9e60c38d1d61 |
child 6258 | 435600a44e73 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
5322 | 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 |
||
5360 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
1 | 15 |
ArithmeticValue subclass:#Number |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
16 |
instanceVariableNames:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
17 |
classVariableNames:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
18 |
poolDictionaries:'' |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
19 |
category:'Magnitude-Numbers' |
1 | 20 |
! |
21 |
||
1892 | 22 |
!Number class methodsFor:'documentation'! |
88 | 23 |
|
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
5322 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
abstract superclass for all kinds of numbers |
|
1295 | 41 |
|
42 |
[author:] |
|
5322 | 43 |
Claus Gittinger |
1628 | 44 |
|
45 |
[see also:] |
|
5322 | 46 |
Integer LargeInteger SmallInteger |
47 |
LimitedPrecisionReal Float ShortFloat |
|
48 |
Fraction FixedPoint |
|
88 | 49 |
" |
50 |
! ! |
|
1 | 51 |
|
1892 | 52 |
!Number class methodsFor:'instance creation'! |
1 | 53 |
|
3060 | 54 |
fromString:aString |
55 |
"for compatibility with other smalltalks - same as #readFrom:" |
|
56 |
||
57 |
^ self readFrom:aString |
|
58 |
||
3716 | 59 |
" |
60 |
Number fromString:'12345' |
|
61 |
'12345' asNumber |
|
62 |
" |
|
63 |
||
64 |
"Modified: / 3.8.1998 / 20:05:11 / cg" |
|
3060 | 65 |
! |
66 |
||
67 |
fromString:aString onError:exceptionBlock |
|
68 |
"for compatibility with other smalltalks - same as #readFrom:" |
|
69 |
||
70 |
^ self readFrom:aString onError:exceptionBlock |
|
71 |
||
3716 | 72 |
" |
73 |
Number fromString:'12345' onError:0 |
|
74 |
Number fromString:'fooBarBaz' onError:0 |
|
75 |
" |
|
76 |
||
77 |
"Modified: / 3.8.1998 / 20:05:34 / cg" |
|
3060 | 78 |
! |
79 |
||
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
80 |
readFrom:aStringOrStream onError:exceptionBlock |
1 | 81 |
"return the next Number from the (character-)stream aStream; |
202 | 82 |
skipping all whitespace first; return the value of exceptionBlock, |
83 |
if no number can be read." |
|
1 | 84 |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
85 |
|value| |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
86 |
|
973 | 87 |
ErrorSignal handle:[:ex | |
5322 | 88 |
^ exceptionBlock value |
339 | 89 |
] do:[ |
5322 | 90 |
|str nextChar radix negative signExp| |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1635
diff
changeset
|
91 |
|
5322 | 92 |
str := aStringOrStream readStream. |
1701
80d13adb2e77
better errorHandling in readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
1635
diff
changeset
|
93 |
|
5322 | 94 |
nextChar := str skipSeparators. |
95 |
nextChar isNil ifTrue:[^ exceptionBlock value]. |
|
3 | 96 |
|
5322 | 97 |
(nextChar == $-) ifTrue:[ |
98 |
negative := true. |
|
99 |
str next. |
|
100 |
nextChar := str peekOrNil |
|
101 |
] ifFalse:[ |
|
102 |
negative := false. |
|
103 |
(nextChar == $+) ifTrue:[ |
|
104 |
str next. |
|
105 |
nextChar := str peekOrNil |
|
106 |
] |
|
107 |
]. |
|
108 |
(nextChar isDigit or:[nextChar == $.]) ifFalse:[ |
|
109 |
^ exceptionBlock value. |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
110 |
"/ value := super readFrom:str. |
339 | 111 |
"/ negative ifTrue:[value := value negated]. |
112 |
"/ ^ value |
|
5322 | 113 |
]. |
114 |
nextChar == $. ifTrue:[ |
|
115 |
radix := 10. |
|
116 |
value := 0.0. |
|
117 |
] ifFalse:[ |
|
118 |
value := Integer readFrom:str radix:10. |
|
119 |
nextChar := str peekOrNil. |
|
120 |
((nextChar == $r) or:[ nextChar == $R]) ifTrue:[ |
|
121 |
str next. |
|
122 |
radix := value. |
|
123 |
value := Integer readFrom:str radix:radix. |
|
124 |
] ifFalse:[ |
|
125 |
radix := 10 |
|
126 |
]. |
|
127 |
]. |
|
4637
814b3d8e3f72
allow for degenerate float to be read
Claus Gittinger <cg@exept.de>
parents:
4558
diff
changeset
|
128 |
|
5322 | 129 |
(nextChar == $.) ifTrue:[ |
130 |
str next. |
|
131 |
nextChar := str peekOrNil. |
|
132 |
(nextChar notNil and:[nextChar isDigitRadix:radix]) ifTrue:[ |
|
133 |
value := value asFloat |
|
134 |
+ (Number readMantissaFrom:str radix:radix). |
|
135 |
nextChar := str peekOrNil |
|
136 |
] |
|
137 |
]. |
|
138 |
((nextChar == $e) or:[nextChar == $E]) ifTrue:[ |
|
139 |
str next. |
|
140 |
nextChar := str peekOrNil. |
|
141 |
signExp := 1. |
|
142 |
(nextChar == $+) ifTrue:[ |
|
143 |
str next. |
|
144 |
nextChar := str peekOrNil. |
|
145 |
] ifFalse:[ |
|
146 |
(nextChar == $-) ifTrue:[ |
|
147 |
str next. |
|
148 |
nextChar := str peekOrNil. |
|
149 |
signExp := -1 |
|
150 |
] |
|
151 |
]. |
|
152 |
(nextChar notNil and:[(nextChar isDigitRadix:radix)]) ifTrue:[ |
|
153 |
value := value asFloat |
|
154 |
* (10.0 raisedToInteger: |
|
155 |
((Integer readFrom:str radix:radix) * signExp)) |
|
156 |
] |
|
157 |
]. |
|
158 |
negative ifTrue:[ |
|
159 |
value := value negated |
|
160 |
]. |
|
3995
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
161 |
]. |
777470826394
no need for another error-handler blcok in #readFrom:onError:
Claus Gittinger <cg@exept.de>
parents:
3900
diff
changeset
|
162 |
^ value. |
1 | 163 |
|
329 | 164 |
" |
165 |
Number readFrom:(ReadStream on:'54.32e-01') |
|
166 |
Number readFrom:(ReadStream on:'12345678901234567890') |
|
167 |
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
|
168 |
Number readFrom:'16rAAAAFFFFAAAAFFFF' |
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
169 |
Number readFrom:'0.000001' |
329 | 170 |
'+00000123.45' asNumber |
171 |
" |
|
569
7134eb78cf48
readFrom:onError: can now also read from a string
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
172 |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
173 |
"Modified: / 14.4.1998 / 19:22:50 / cg" |
296 | 174 |
! |
175 |
||
176 |
readSmalltalkSyntaxFrom:aStream |
|
177 |
"ST-80 compatibility (thanks to a note from alpha testers) |
|
178 |
read and return the next Number in smalltalk syntax from the |
|
329 | 179 |
(character-)stream aStream." |
180 |
||
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
181 |
^ Scanner scanNumberFrom:aStream |
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
182 |
"/ ^ Compiler evaluate:aStream compile:false "/ self readFrom:aStream. |
296 | 183 |
|
329 | 184 |
" |
185 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'54.32e-01') |
|
186 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'12345678901234567890') |
|
187 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'16rAAAAFFFFAAAAFFFF') |
|
188 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'(1/10)') |
|
189 |
Number readFrom:(ReadStream on:'(1/10)') |
|
190 |
Number readSmalltalkSyntaxFrom:(ReadStream on:'+00000123.45') |
|
191 |
Number readFrom:(ReadStream on:'+00000123.45') |
|
5013 | 192 |
|
193 |
|s| |
|
194 |
s := ReadStream on:'2.'. |
|
195 |
Number readSmalltalkSyntaxFrom:s. |
|
196 |
s next |
|
197 |
||
198 |
|s| |
|
199 |
s := ReadStream on:'2.0.'. |
|
200 |
Number readSmalltalkSyntaxFrom:s. |
|
201 |
s next |
|
329 | 202 |
" |
3613
75629df7f224
fixed readSmalltalkSyntax (dont read behind number)
Claus Gittinger <cg@exept.de>
parents:
3394
diff
changeset
|
203 |
|
5013 | 204 |
"Modified: / 19.11.1999 / 18:26:47 / cg" |
1 | 205 |
! ! |
701 | 206 |
|
1892 | 207 |
!Number class methodsFor:'error reporting'! |
701 | 208 |
|
6195 | 209 |
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arg:arg errorString:text |
3394 | 210 |
"ST-80 compatible signal raising. Provided for PD numeric classes" |
211 |
||
4558 | 212 |
<context: #return> |
213 |
||
6195 | 214 |
^ self |
215 |
raise:aSignalSymbolOrErrorClass |
|
216 |
receiver:someNumber |
|
217 |
selector:sel |
|
218 |
arguments:(Array with:arg) |
|
219 |
errorString:text |
|
3394 | 220 |
|
221 |
" |
|
222 |
Number |
|
6195 | 223 |
raise:#domainErrorSignal |
224 |
receiver:1.0 |
|
225 |
selector:#sin |
|
226 |
arg:nil |
|
227 |
errorString:'foo bar test' |
|
3394 | 228 |
" |
6195 | 229 |
|
230 |
"Modified: / 16.11.2001 / 14:12:50 / cg" |
|
3394 | 231 |
! |
232 |
||
6195 | 233 |
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel arguments:argArray errorString:text |
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
234 |
"ST-80 compatible signal raising. Provided for PD numeric classes. |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
235 |
aSignalSymbolOrErrorClass is either an Error-subclass, or |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
236 |
the selector which is sent to myself, to retrieve the Exception class / Signal." |
701 | 237 |
|
4558 | 238 |
<context: #return> |
239 |
||
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
240 |
|msg signalOrException| |
701 | 241 |
|
242 |
msg := MessageSend |
|
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
243 |
receiver:someNumber |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
244 |
selector:sel |
6195 | 245 |
arguments:argArray. |
3394 | 246 |
|
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
247 |
aSignalSymbolOrErrorClass isSymbol ifTrue:[ |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
248 |
signalOrException := self perform:aSignalSymbolOrErrorClass. |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
249 |
] ifFalse:[ |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
250 |
signalOrException := aSignalSymbolOrErrorClass. "/ assume its an Error-Subclass |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
251 |
]. |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
252 |
|
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
253 |
^ signalOrException |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
254 |
raiseRequestWith:msg |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
255 |
errorString:text |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
256 |
in:thisContext sender |
701 | 257 |
|
258 |
" |
|
3394 | 259 |
Number |
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
260 |
raise:#domainErrorSignal |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
261 |
receiver:1.0 |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
262 |
selector:#foo |
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
263 |
errorString:'foo bar test' |
701 | 264 |
" |
6194
e060dbb4f75c
allow Exception-arg in #raise:receiver:selector...
Claus Gittinger <cg@exept.de>
parents:
6155
diff
changeset
|
265 |
|
6195 | 266 |
"Modified: / 16.11.2001 / 14:12:09 / cg" |
267 |
! |
|
268 |
||
269 |
raise:aSignalSymbolOrErrorClass receiver:someNumber selector:sel errorString:text |
|
270 |
"ST-80 compatible signal raising. Provided for PD numeric classes. |
|
271 |
aSignalSymbolOrErrorClass is either an Error-subclass, or |
|
272 |
the selector which is sent to myself, to retrieve the Exception class / Signal." |
|
273 |
||
274 |
<context: #return> |
|
275 |
||
276 |
^ self |
|
277 |
raise:aSignalSymbolOrErrorClass |
|
278 |
receiver:someNumber |
|
279 |
selector:sel |
|
280 |
arguments:#() |
|
281 |
errorString:text |
|
282 |
||
283 |
" |
|
284 |
Number |
|
285 |
raise:#domainErrorSignal |
|
286 |
receiver:1.0 |
|
287 |
selector:#foo |
|
288 |
errorString:'foo bar test' |
|
289 |
" |
|
290 |
||
291 |
"Modified: / 16.11.2001 / 14:13:16 / cg" |
|
701 | 292 |
! ! |
293 |
||
1892 | 294 |
!Number class methodsFor:'private'! |
701 | 295 |
|
296 |
readMantissaFrom:aStream radix:radix |
|
297 |
"helper for readFrom: - |
|
298 |
return the mantissa from the (character-)stream aStream; |
|
299 |
no whitespace-skipping; error if no number available" |
|
300 |
||
301 |
|nextChar value factor| |
|
302 |
||
303 |
value := 0.0. |
|
304 |
factor := 1.0 / radix. |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
305 |
nextChar := aStream peekOrNil. |
701 | 306 |
[nextChar notNil and:[nextChar isDigitRadix:radix]] whileTrue:[ |
5322 | 307 |
value := value + (nextChar digitValue * factor). |
308 |
factor := factor / radix. |
|
309 |
aStream next. |
|
310 |
nextChar := aStream peekOrNil |
|
701 | 311 |
]. |
312 |
^ value |
|
3375
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
313 |
|
7c64da3964e4
faster conversion from string (due to slow #pastEnd)
Claus Gittinger <cg@exept.de>
parents:
3060
diff
changeset
|
314 |
"Modified: / 14.4.1998 / 18:47:47 / cg" |
701 | 315 |
! ! |
316 |
||
4988 | 317 |
!Number methodsFor:'Compatibility - Squeak'! |
318 |
||
319 |
asSmallAngleDegrees |
|
320 |
"Return the receiver normalized to lie within the range (-180, 180)" |
|
321 |
||
322 |
| pos | |
|
323 |
pos _ self \\ 360. |
|
324 |
pos > 180 ifTrue: [pos _ pos - 360]. |
|
325 |
^ pos |
|
326 |
||
6072 | 327 |
"#(-500 -300 -150 -5 0 5 150 300 500 1200) collect: [:n | n asSmallAngleDegrees]" |
328 |
! |
|
4988 | 329 |
|
6143 | 330 |
closeTo:num |
331 |
"are these two numbers close?" |
|
332 |
||
333 |
| ans | |
|
334 |
||
335 |
self = num ifTrue:[^ true]. |
|
336 |
||
337 |
num isLimitedPrecisionReal "isReal" ifTrue: [^ num closeTo: self asFloat]. |
|
338 |
[ans := (self = num)] ifError: [:aString :aReceiver | ^ false]. |
|
339 |
^ ans |
|
340 |
||
341 |
" |
|
342 |
1 closeTo:1.0000000001 |
|
343 |
1 closeTo:1.001 |
|
344 |
" |
|
6155 | 345 |
|
346 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
6143 | 347 |
! |
348 |
||
4988 | 349 |
newTileMorphRepresentative |
350 |
^ TileMorph new addArrows; setLiteral: self; addSuffixIfCan |
|
351 |
! |
|
352 |
||
353 |
stringForReadout |
|
354 |
^ self rounded printString |
|
355 |
! ! |
|
356 |
||
5238 | 357 |
!Number methodsFor:'coercing & converting'! |
701 | 358 |
|
359 |
coerce:aNumber |
|
360 |
"return aNumber converted into receivers type" |
|
361 |
||
362 |
^ self subclassResponsibility |
|
363 |
! |
|
364 |
||
365 |
generality |
|
366 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
367 |
||
368 |
^ 40 |
|
369 |
! ! |
|
370 |
||
371 |
!Number methodsFor:'converting'! |
|
372 |
||
3634 | 373 |
% aNumber |
374 |
"Return a complex number with the receiver as the real part and |
|
375 |
aNumber as the imaginary part" |
|
376 |
||
377 |
^ Complex real:self imaginary:aNumber |
|
378 |
||
379 |
"Modified: / 9.7.1998 / 10:18:12 / cg" |
|
380 |
! |
|
381 |
||
701 | 382 |
@ aNumber |
383 |
"return a Point with the receiver as x-coordinate and the argument |
|
384 |
as y-coordinate" |
|
385 |
||
386 |
%{ /* NOCONTEXT */ |
|
387 |
||
388 |
/* |
|
389 |
* I cannot tell if this special code is worth anything |
|
390 |
*/ |
|
5322 | 391 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
392 |
OBJ newPoint; |
|
393 |
int spc; |
|
701 | 394 |
|
5322 | 395 |
__qCheckedAlignedNew(newPoint, sizeof(struct __Point)); |
396 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
397 |
__PointInstPtr(newPoint)->p_x = self; |
|
398 |
__PointInstPtr(newPoint)->p_y = aNumber; |
|
399 |
if (! __bothSmallInteger(self, aNumber)) { |
|
400 |
spc = __qSpace(newPoint); |
|
401 |
__STORE_SPC(newPoint, aNumber, spc); |
|
402 |
__STORE_SPC(newPoint, self, spc); |
|
403 |
} |
|
404 |
RETURN ( newPoint ); |
|
701 | 405 |
} |
406 |
%} |
|
407 |
. |
|
408 |
^ Point x:self y:aNumber |
|
409 |
! |
|
410 |
||
3634 | 411 |
asComplex |
412 |
"Return a complex number with the receiver as the real part and |
|
413 |
zero as the imaginary part" |
|
414 |
||
415 |
^ Complex fromReal:self |
|
416 |
||
417 |
"Modified: / 9.7.1998 / 10:18:16 / cg" |
|
418 |
! |
|
419 |
||
701 | 420 |
asPoint |
421 |
"return a new Point with the receiver as all coordinates; |
|
422 |
often used to supply the same value in two dimensions, as with |
|
423 |
symmetrical gridding or scaling." |
|
424 |
||
425 |
%{ /* NOCONTEXT */ |
|
426 |
||
5322 | 427 |
if (__CanDoQuickNew(sizeof(struct __Point))) { /* OBJECT ALLOCATION */ |
428 |
OBJ newPoint; |
|
701 | 429 |
|
5322 | 430 |
__qCheckedAlignedNew(newPoint, sizeof(struct __Point)); |
431 |
__InstPtr(newPoint)->o_class = @global(Point); |
|
432 |
__PointInstPtr(newPoint)->p_x = self; |
|
433 |
__PointInstPtr(newPoint)->p_y = self; |
|
434 |
__STORE(newPoint, self); |
|
435 |
RETURN ( newPoint ); |
|
701 | 436 |
} |
437 |
%}. |
|
438 |
^ Point x:self y:self |
|
439 |
! |
|
440 |
||
2416 | 441 |
decodeAsLiteralArray |
442 |
"given a literalEncoding in the receiver, |
|
443 |
create & return the corresponding object. |
|
444 |
The inverse operation to #literalArrayEncoding." |
|
445 |
||
446 |
^ self |
|
447 |
||
448 |
"Created: 25.2.1997 / 19:17:06 / cg" |
|
449 |
"Modified: 25.2.1997 / 19:17:42 / cg" |
|
450 |
! |
|
451 |
||
701 | 452 |
degreesToRadians |
453 |
"interpreting the receiver as radians, return the degrees" |
|
454 |
||
455 |
^ (self * (Float pi)) / 180.0 |
|
456 |
! |
|
457 |
||
458 |
literalArrayEncoding |
|
1245 | 459 |
"encode myself as an array literal, from which a copy of the receiver |
460 |
can be reconstructed with #decodeAsLiteralArray." |
|
701 | 461 |
|
462 |
^ self |
|
463 |
||
464 |
"Modified: 1.9.1995 / 02:25:26 / claus" |
|
1245 | 465 |
"Modified: 22.4.1996 / 13:00:27 / cg" |
701 | 466 |
! |
467 |
||
468 |
radiansToDegrees |
|
469 |
"interpreting the receiver as degrees, return the radians" |
|
470 |
||
471 |
^ (self * 180.0) / (Float pi) |
|
472 |
! ! |
|
473 |
||
474 |
!Number methodsFor:'intervals'! |
|
475 |
||
6072 | 476 |
downTo:stop |
477 |
"return an interval from receiver down to the argument, incrementing by -1" |
|
478 |
||
479 |
^ self to:stop by:-1 |
|
480 |
||
481 |
" |
|
482 |
(10 downTo:1) do:[:i | Transcript showCR:i]. |
|
483 |
" |
|
484 |
! |
|
485 |
||
701 | 486 |
to:stop |
487 |
"return an interval from receiver up to the argument, incrementing by 1" |
|
488 |
||
489 |
^ Interval from:self to:stop |
|
490 |
! |
|
491 |
||
492 |
to:stop by:step |
|
493 |
"return an interval from receiver up to the argument, incrementing by step" |
|
494 |
||
495 |
^ Interval from:self to:stop by:step |
|
6154 | 496 |
! |
497 |
||
498 |
to:stop byFactor:factor |
|
499 |
"return a geometric series from receiver up to the argument; |
|
500 |
elements have a constant factor in between" |
|
501 |
||
502 |
^ GeometricSeries from:self to:stop byFactor:factor |
|
503 |
||
504 |
" |
|
505 |
(1 to:256 byFactor:2) |
|
506 |
(256 to:1 byFactor:1/2) |
|
507 |
" |
|
701 | 508 |
! ! |
509 |
||
510 |
!Number methodsFor:'iteration'! |
|
511 |
||
512 |
timesRepeat:aBlock |
|
513 |
"evaluate the argument, aBlock self times" |
|
514 |
||
515 |
|count| |
|
516 |
||
517 |
count := self. |
|
518 |
[count > 0] whileTrue:[ |
|
5322 | 519 |
aBlock value. |
520 |
count := count - 1 |
|
701 | 521 |
] |
522 |
! ! |
|
523 |
||
3634 | 524 |
!Number methodsFor:'mathematical functions'! |
525 |
||
526 |
conjugated |
|
527 |
"Return the complex conjugate of this Number." |
|
528 |
||
529 |
^ self |
|
530 |
||
531 |
"Modified: / 9.7.1998 / 10:17:31 / cg" |
|
532 |
! |
|
533 |
||
534 |
imaginary |
|
535 |
"Return the imaginary part of this Number." |
|
536 |
||
537 |
^ 0 |
|
538 |
||
539 |
"Modified: / 9.7.1998 / 10:17:24 / cg" |
|
540 |
! |
|
541 |
||
542 |
real |
|
543 |
"Return the real part of this Number." |
|
544 |
||
545 |
^ self |
|
546 |
||
547 |
"Modified: / 9.7.1998 / 10:17:17 / cg" |
|
548 |
! ! |
|
549 |
||
701 | 550 |
!Number methodsFor:'printing & storing'! |
551 |
||
5299
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
552 |
printOn:aStream paddedWith:padCharacter to:size base:radix |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
553 |
|s| |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
554 |
|
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
555 |
s := self printStringRadix:radix. |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
556 |
s printOn: aStream leftPaddedTo:size with: padCharacter |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
557 |
|
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
558 |
|
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
559 |
! |
92b363dbd833
added #printOn:paddedWith:to:base:
Claus Gittinger <cg@exept.de>
parents:
5238
diff
changeset
|
560 |
|
701 | 561 |
storeOn:aStream |
562 |
"append a string for storing the receiver onto the argument, |
|
563 |
aStream - since numbers are literals,they store as they print." |
|
564 |
||
565 |
^ self printOn:aStream |
|
566 |
! |
|
567 |
||
568 |
storeString |
|
569 |
"return a string for storing |
|
570 |
- since numbers are literals, they store as they print." |
|
571 |
||
572 |
^ self printString |
|
573 |
! ! |
|
574 |
||
575 |
!Number methodsFor:'testing'! |
|
576 |
||
5360 | 577 |
isDivisibleBy:aNumber |
578 |
"return true, if the receiver can be divided by the argument, aNumber without a remainder. |
|
579 |
Notice, that the result is only worth trusting, if the receiver is an integer." |
|
580 |
||
581 |
aNumber = 0 ifTrue: [^ false]. |
|
582 |
aNumber isInteger ifFalse: [^ false]. |
|
583 |
^ (self \\ aNumber) = 0 |
|
584 |
||
585 |
" |
|
586 |
3 isDivisibleBy:2 |
|
587 |
4 isDivisibleBy:2 |
|
588 |
4.0 isDivisibleBy:2 |
|
589 |
4.5 isDivisibleBy:4.5 |
|
590 |
4.5 isDivisibleBy:1.0 |
|
591 |
" |
|
592 |
! |
|
593 |
||
6143 | 594 |
isInfinite |
595 |
||
596 |
^ false |
|
6155 | 597 |
|
598 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
6143 | 599 |
! |
600 |
||
601 |
isNaN |
|
602 |
"return true, if the receiver is an invalid float (NaN - not a number)." |
|
603 |
||
604 |
^ false |
|
6155 | 605 |
|
606 |
"Created: / 5.11.2001 / 18:07:26 / cg" |
|
6143 | 607 |
! |
608 |
||
701 | 609 |
isNumber |
610 |
"return true, if the receiver is a kind of number" |
|
611 |
||
612 |
^ true |
|
613 |
! |
|
614 |
||
6087 | 615 |
isReal |
616 |
"return true, if the receiver is some kind of real number (as opposed to a complex); |
|
617 |
true is returned here - the method is redefined from Object." |
|
618 |
||
619 |
^ true |
|
620 |
! |
|
621 |
||
701 | 622 |
isZero |
1557 | 623 |
"return true, if the receiver is zero" |
624 |
||
701 | 625 |
^ self = 0 |
1557 | 626 |
|
627 |
"Modified: 18.7.1996 / 12:40:49 / cg" |
|
701 | 628 |
! ! |
629 |
||
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
630 |
!Number methodsFor:'tracing'! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
631 |
|
4682 | 632 |
traceInto:aRequestor level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
633 |
"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
|
634 |
|
4682 | 635 |
^ aRequestor traceNumber:self level:level from:referrer |
4659
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
636 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
637 |
|
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
638 |
! ! |
cdf37afc0e26
moved isLiteral - some numbers are not.
Claus Gittinger <cg@exept.de>
parents:
4637
diff
changeset
|
639 |
|
5552 | 640 |
!Number methodsFor:'truncation & rounding'! |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
641 |
|
4895 | 642 |
detentBy: detent atMultiplesOf: grid snap: snap |
643 |
"Map all values that are within detent/2 of any multiple of grid |
|
644 |
to that multiple. |
|
645 |
Otherwise, if snap is true, return self, meaning that the values |
|
646 |
in the dead zone will never be returned. |
|
647 |
If snap is false, then expand the range between dead zones |
|
648 |
so that it covers the range between multiples of the grid, |
|
649 |
and scale the value by that factor." |
|
650 |
||
651 |
| r1 r2 | |
|
652 |
||
653 |
r1 := self roundTo: grid. "Nearest multiple of grid" |
|
654 |
(self roundTo: detent) = r1 ifTrue: [^ r1]. "Snap to that multiple..." |
|
655 |
snap ifTrue: [^ self]. "...or return self" |
|
656 |
||
657 |
r2 := self < r1 "Nearest end of dead zone" |
|
5322 | 658 |
ifTrue: [r1 - (detent asFloat/2)] |
659 |
ifFalse: [r1 + (detent asFloat/2)]. |
|
4895 | 660 |
|
661 |
"Scale values between dead zones to fill range between multiples" |
|
662 |
^ r1 + ((self - r2) * grid asFloat / (grid - detent)) |
|
663 |
||
664 |
" |
|
665 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: true] |
|
666 |
(170 to: 190 by: 2) collect: [:a | a detentBy: 10 atMultiplesOf: 90 snap: false] |
|
667 |
(3.9 to: 4.1 by: 0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: true] |
|
668 |
(-3.9 to: -4.1 by: -0.02) collect: [:a | a detentBy: 0.1 atMultiplesOf: 1.0 snap: false] |
|
669 |
" |
|
670 |
! |
|
671 |
||
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
672 |
fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
673 |
"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
|
674 |
(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
|
675 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
676 |
^ self - self truncated asFloat |
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 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
679 |
1234.56789 fractionPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
680 |
1.2345e6 fractionPart |
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 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
683 |
"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
|
684 |
"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
|
685 |
! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
686 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
687 |
integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
688 |
"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
|
689 |
(i.e. the truncated value)" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
690 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
691 |
^ self truncated asFloat |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
692 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
693 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
694 |
1234.56789 integerPart |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
695 |
1.2345e6 integerPart |
6154 | 696 |
12.5 integerPart |
697 |
-12.5 integerPart |
|
698 |
(5/3) integerPart |
|
699 |
(-5/3) integerPart |
|
700 |
(5/3) truncated |
|
701 |
(-5/3) truncated |
|
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
702 |
" |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
703 |
|
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
704 |
"Created: / 28.10.1998 / 17:14:56 / cg" |
6154 | 705 |
"Modified: / 5.11.2001 / 17:54:22 / cg" |
3900
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
706 |
! ! |
c3bc76767cfc
added generic implementations of #fractionPart and #integerPart
Claus Gittinger <cg@exept.de>
parents:
3770
diff
changeset
|
707 |
|
1892 | 708 |
!Number class methodsFor:'documentation'! |
701 | 709 |
|
710 |
version |
|
6195 | 711 |
^ '$Header: /cvs/stx/stx/libbasic/Number.st,v 1.69 2001-11-16 13:22:25 cg Exp $' |
701 | 712 |
! ! |